Deduplicate code
This commit is contained in:
parent
ba76c1d2cf
commit
89e518f878
|
@ -398,9 +398,7 @@ pub fn from_pallas_bigint(n: &pallas::BigInt) -> BigInt {
|
||||||
match n {
|
match n {
|
||||||
pallas::BigInt::Int(i) => i128::from(*i).into(),
|
pallas::BigInt::Int(i) => i128::from(*i).into(),
|
||||||
pallas::BigInt::BigUInt(bytes) => BigInt::from_bytes_be(num_bigint::Sign::Plus, bytes),
|
pallas::BigInt::BigUInt(bytes) => BigInt::from_bytes_be(num_bigint::Sign::Plus, bytes),
|
||||||
pallas::BigInt::BigNInt(bytes) => {
|
pallas::BigInt::BigNInt(bytes) => BigInt::from_bytes_be(num_bigint::Sign::Minus, bytes) - 1,
|
||||||
BigInt::from_bytes_be(num_bigint::Sign::Minus, bytes) - 1
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,12 +4,11 @@ use crate::{
|
||||||
ast::{Constant, Name, Program, Term, Type},
|
ast::{Constant, Name, Program, Term, Type},
|
||||||
builtins::DefaultFunction,
|
builtins::DefaultFunction,
|
||||||
machine::runtime::Compressable,
|
machine::runtime::Compressable,
|
||||||
|
machine::value::to_pallas_bigint,
|
||||||
};
|
};
|
||||||
|
|
||||||
use interner::Interner;
|
use interner::Interner;
|
||||||
use num_bigint::BigInt;
|
use num_bigint::BigInt;
|
||||||
use num_bigint::Sign;
|
|
||||||
use num_traits::ToPrimitive;
|
|
||||||
use pallas_primitives::alonzo::PlutusData;
|
use pallas_primitives::alonzo::PlutusData;
|
||||||
use peg::{error::ParseError, str::LineCol};
|
use peg::{error::ParseError, str::LineCol};
|
||||||
|
|
||||||
|
@ -253,16 +252,7 @@ peg::parser! {
|
||||||
PlutusData::Map(pallas_codec::utils::KeyValuePairs::Def(kvps))
|
PlutusData::Map(pallas_codec::utils::KeyValuePairs::Def(kvps))
|
||||||
}
|
}
|
||||||
/ _* "List" _+ ls:plutus_list() { PlutusData::Array(ls) }
|
/ _* "List" _+ ls:plutus_list() { PlutusData::Array(ls) }
|
||||||
/ _* "I" _+ n:big_number() { match n.to_i64() {
|
/ _* "I" _+ n:big_number() { PlutusData::BigInt(to_pallas_bigint(&n)) }
|
||||||
Some(n) => PlutusData::BigInt(pallas_primitives::alonzo::BigInt::Int(n.into())),
|
|
||||||
None => match n.sign() {
|
|
||||||
Sign::Minus => {
|
|
||||||
let m: BigInt = n+1;
|
|
||||||
PlutusData::BigInt(pallas_primitives::babbage::BigInt::BigNInt(m.to_bytes_be().1.into()))
|
|
||||||
},
|
|
||||||
_ => PlutusData::BigInt(pallas_primitives::babbage::BigInt::BigUInt(n.to_bytes_be().1.into()))
|
|
||||||
}
|
|
||||||
} }
|
|
||||||
/ _* "B" _+ "#" i:ident()* {?
|
/ _* "B" _+ "#" i:ident()* {?
|
||||||
Ok(PlutusData::BoundedBytes(
|
Ok(PlutusData::BoundedBytes(
|
||||||
hex::decode(String::from_iter(i)).or(Err("bytes"))?.into()
|
hex::decode(String::from_iter(i)).or(Err("bytes"))?.into()
|
||||||
|
|
|
@ -2,6 +2,7 @@ use crate::{
|
||||||
ast::{Constant, Program, Term, Type},
|
ast::{Constant, Program, Term, Type},
|
||||||
flat::Binder,
|
flat::Binder,
|
||||||
machine::runtime::Compressable,
|
machine::runtime::Compressable,
|
||||||
|
machine::value::from_pallas_bigint,
|
||||||
};
|
};
|
||||||
use pallas_primitives::babbage::{Constr, PlutusData};
|
use pallas_primitives::babbage::{Constr, PlutusData};
|
||||||
use pretty::RcDoc;
|
use pretty::RcDoc;
|
||||||
|
@ -336,11 +337,9 @@ impl Constant {
|
||||||
RcDoc::text(", "),
|
RcDoc::text(", "),
|
||||||
))
|
))
|
||||||
.append(RcDoc::text("]")),
|
.append(RcDoc::text("]")),
|
||||||
PlutusData::BigInt(bi) => RcDoc::text("I").append(RcDoc::space()).append(match bi {
|
PlutusData::BigInt(bi) => RcDoc::text("I")
|
||||||
pallas_primitives::babbage::BigInt::Int(v) => RcDoc::text(v.to_string()),
|
.append(RcDoc::space())
|
||||||
pallas_primitives::babbage::BigInt::BigUInt(v) => RcDoc::text(v.to_string()),
|
.append(RcDoc::text(from_pallas_bigint(bi).to_string())),
|
||||||
pallas_primitives::babbage::BigInt::BigNInt(v) => RcDoc::text(v.to_string()),
|
|
||||||
}),
|
|
||||||
PlutusData::BoundedBytes(bs) => RcDoc::text("B")
|
PlutusData::BoundedBytes(bs) => RcDoc::text("B")
|
||||||
.append(RcDoc::space())
|
.append(RcDoc::space())
|
||||||
.append(RcDoc::text("#"))
|
.append(RcDoc::text("#"))
|
||||||
|
|
Loading…
Reference in New Issue