diff --git a/Cargo.lock b/Cargo.lock index 9c9a3006..720f2964 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2669,6 +2669,7 @@ dependencies = [ "itertools", "k256", "num-bigint", + "num-traits", "pallas-addresses", "pallas-codec", "pallas-crypto", diff --git a/crates/uplc/Cargo.toml b/crates/uplc/Cargo.toml index 4b1e9398..4629b925 100644 --- a/crates/uplc/Cargo.toml +++ b/crates/uplc/Cargo.toml @@ -34,6 +34,7 @@ indexmap = "1.9.2" secp256k1 = { version = "0.26.0", optional = true } k256 = { version = "0.12.0", optional = true } num-bigint = "0.4.3" +num-traits = "0.2.15" [dev-dependencies] hex = "0.4.3" diff --git a/crates/uplc/src/machine.rs b/crates/uplc/src/machine.rs index 713d3ae4..1dd72ff4 100644 --- a/crates/uplc/src/machine.rs +++ b/crates/uplc/src/machine.rs @@ -1,3 +1,4 @@ +use num_traits::sign::Signed; use std::{collections::VecDeque, ops::Deref, rc::Rc}; use crate::{ @@ -545,10 +546,10 @@ impl Value { match self { Value::Con(c) => match c.as_ref() { Constant::Integer(i) => { - if *i == 0 { + if *i == 0.into() { 1 } else { - ((i.abs() as f64).log2().floor() as i64 / 64) + 1 + (i.abs().log2().floor() as i64 / 64) + 1 } } Constant::ByteString(b) => { @@ -748,10 +749,10 @@ mod test { term: Term::Apply { function: Term::Apply { function: Term::Builtin(DefaultFunction::AddInteger).into(), - argument: Term::Constant(Constant::Integer(i128::MAX).into()).into(), + argument: Term::Constant(Constant::Integer(i128::MAX.into()).into()).into(), } .into(), - argument: Term::Constant(Constant::Integer(i128::MAX).into()).into(), + argument: Term::Constant(Constant::Integer(i128::MAX.into()).into()).into(), }, }; diff --git a/crates/uplc/src/parser.rs b/crates/uplc/src/parser.rs index 19b6815b..3c70f641 100644 --- a/crates/uplc/src/parser.rs +++ b/crates/uplc/src/parser.rs @@ -1,4 +1,4 @@ -use std::{rc::Rc, str::FromStr}; +use std::{ops::Neg, rc::Rc, str::FromStr}; use crate::{ ast::{Constant, Name, Program, Term, Type}, @@ -6,6 +6,7 @@ use crate::{ }; use interner::Interner; +use num_bigint::BigInt; use pallas_primitives::{alonzo::PlutusData, Fragment}; use peg::{error::ParseError, str::LineCol}; @@ -157,8 +158,8 @@ peg::parser! { rule number() -> isize = n:$("-"* ['0'..='9']+) {? n.parse().or(Err("isize")) } - rule big_number() -> i128 - = n:$("-"* ['0'..='9']+) {? n.parse().or(Err("i128")) } + rule big_number() -> BigInt + = n:$("-"* ['0'..='9']+) {? (if n.starts_with("-") { BigInt::parse_bytes(&n.as_bytes()[1..], 10).map(|i| i.neg()) } else { BigInt::parse_bytes(n.as_bytes(), 10) }).ok_or("BigInt") } rule boolean() -> bool = b:$("True" / "False") { b == "True" } @@ -277,7 +278,7 @@ mod test { parameter_name: x.clone().into(), body: Rc::new(Term::Var(x.into())), }), - argument: Rc::new(Term::Constant(Constant::Integer(0).into())) + argument: Rc::new(Term::Constant(Constant::Integer(0.into()).into())) } } ) @@ -344,7 +345,7 @@ mod test { super::program(uplc).unwrap(), Program:: { version: (11, 22, 33), - term: Term::Constant(Constant::Integer(11).into()), + term: Term::Constant(Constant::Integer(11.into()).into()), } ); } @@ -492,7 +493,7 @@ mod test { term: Term::Apply { function: Rc::new(Term::Apply { function: Rc::new(Term::Builtin(DefaultFunction::ConsByteString)), - argument: Rc::new(Term::Constant(Constant::Integer(256).into())), + argument: Rc::new(Term::Constant(Constant::Integer(256.into()).into())), }), argument: Rc::new(Term::Constant(Constant::ByteString(vec![]).into())) } @@ -511,9 +512,9 @@ mod test { function: Rc::new(Term::Apply { function: Rc::new(Term::Apply { function: Rc::new(Term::Builtin(DefaultFunction::SliceByteString)), - argument: Rc::new(Term::Constant(Constant::Integer(1).into())), + argument: Rc::new(Term::Constant(Constant::Integer(1.into()).into())), }), - argument: Rc::new(Term::Constant(Constant::Integer(2).into())), + argument: Rc::new(Term::Constant(Constant::Integer(2.into()).into())), }), argument: Rc::new(Term::Constant( Constant::ByteString(vec![0x00, 0xFF, 0xAA]).into() @@ -553,7 +554,7 @@ mod test { argument: Rc::new(Term::Constant(Constant::ByteString(vec![0x00]).into())) }), argument: Rc::new(Term::Constant( - Constant::Integer(9223372036854775808).into() + Constant::Integer(9223372036854775808.into()).into() )), } } @@ -704,9 +705,12 @@ mod test { vec![ Constant::ProtoList( Type::Integer, - vec![Constant::Integer(14), Constant::Integer(42)] + vec![Constant::Integer(14.into()), Constant::Integer(42.into())] ), - Constant::ProtoList(Type::Integer, vec![Constant::Integer(1337)]) + Constant::ProtoList( + Type::Integer, + vec![Constant::Integer(1337.into())] + ) ] ) .into() @@ -733,7 +737,7 @@ mod test { term: Term::Constant( Constant::ProtoList( Type::Integer, - vec![Constant::Integer(14), Constant::Integer(42)], + vec![Constant::Integer(14.into()), Constant::Integer(42.into())], ) .into() ) @@ -776,7 +780,7 @@ mod test { Constant::ProtoPair( Type::Integer, Type::ByteString, - Constant::Integer(14).into(), + Constant::Integer(14.into()).into(), Constant::ByteString(vec![0x42]).into(), ) .into() @@ -801,7 +805,7 @@ mod test { Constant::String(String::from("foo")).into(), Constant::ProtoList( Type::Integer, - vec![Constant::Integer(14), Constant::Integer(42)], + vec![Constant::Integer(14.into()), Constant::Integer(42.into())], ) .into() ) @@ -828,8 +832,8 @@ mod test { Constant::ProtoPair( Type::Integer, Type::Integer, - Constant::Integer(14).into(), - Constant::Integer(42).into() + Constant::Integer(14.into()).into(), + Constant::Integer(42.into()).into() ) .into() ) @@ -859,9 +863,9 @@ mod test { term: Term::Apply { function: Rc::new(Term::Apply { function: Rc::new(Term::Builtin(default_function)), - argument: Rc::new(Term::Constant(Constant::Integer(x).into())), + argument: Rc::new(Term::Constant(Constant::Integer(x.into()).into())), }), - argument: Rc::new(Term::Constant(Constant::Integer(y).into())) + argument: Rc::new(Term::Constant(Constant::Integer(y.into()).into())) } } ) diff --git a/crates/uplc/src/program_builder/constant.rs b/crates/uplc/src/program_builder/constant.rs index 27e03d3a..09257da0 100644 --- a/crates/uplc/src/program_builder/constant.rs +++ b/crates/uplc/src/program_builder/constant.rs @@ -3,7 +3,7 @@ use crate::program_builder::WithTerm; pub trait WithConstant: WithTerm { fn with_int(self, int: i128) -> Self::Next { - let term = Term::Constant(Constant::Integer(int).into()); + let term = Term::Constant(Constant::Integer(int.into()).into()); self.next(term) }