added i128 integer support

This commit is contained in:
alessandrokonrad
2022-10-16 20:20:47 -04:00
committed by Lucas
parent e30bd829aa
commit 28b9fed8e5
12 changed files with 117 additions and 16 deletions

View File

@@ -122,7 +122,7 @@ where
#[derive(Debug, Clone, PartialEq)]
pub enum Constant {
// tag: 0
Integer(isize),
Integer(i128),
// tag: 1
ByteString(Vec<u8>),
// tag: 2

View File

@@ -310,7 +310,7 @@ fn encode_type(typ: &Type, bytes: &mut Vec<u8>) {
impl<'b> Decode<'b> for Constant {
fn decode(d: &mut Decoder) -> Result<Self, de::Error> {
match &decode_constant(d)?[..] {
[0] => Ok(Constant::Integer(isize::decode(d)?)),
[0] => Ok(Constant::Integer(i128::decode(d)?)),
[1] => Ok(Constant::ByteString(Vec::<u8>::decode(d)?)),
[2] => Ok(Constant::String(String::decode(d)?)),
[3] => Ok(Constant::Unit),
@@ -354,7 +354,7 @@ impl<'b> Decode<'b> for Constant {
fn decode_constant_value(typ: Type, d: &mut Decoder) -> Result<Constant, de::Error> {
match typ {
Type::Integer => Ok(Constant::Integer(isize::decode(d)?)),
Type::Integer => Ok(Constant::Integer(i128::decode(d)?)),
Type::ByteString => Ok(Constant::ByteString(Vec::<u8>::decode(d)?)),
Type::String => Ok(Constant::String(String::decode(d)?)),
Type::Unit => Ok(Constant::Unit),

View File

@@ -515,7 +515,7 @@ impl Value {
PlutusData::BigInt(i) => {
if let BigInt::Int(g) = i {
let numb: i128 = (*g).try_into().unwrap();
total += Value::Con(Constant::Integer(numb as isize)).to_ex_mem();
total += Value::Con(Constant::Integer(numb)).to_ex_mem();
} else {
unreachable!()
};

View File

@@ -37,9 +37,9 @@ pub enum Error {
#[error("Decoding utf8")]
Utf8(#[from] FromUtf8Error),
#[error("Out of Bounds\n\nindex: {}\nbytestring: {}\npossible: 0 - {}", .0, hex::encode(.1), .1.len() - 1)]
ByteStringOutOfBounds(isize, Vec<u8>),
ByteStringOutOfBounds(i128, Vec<u8>),
#[error("Divide By Zero\n\n{0} / {1}")]
DivideByZero(isize, isize),
DivideByZero(i128, i128),
#[error("Ed25519S PublicKey should be 32 bytes but it was {0}")]
UnexpectedEd25519PublicKeyLength(usize),
#[error("Ed25519S Signature should be 64 bytes but it was {0}")]

View File

@@ -349,7 +349,7 @@ impl DefaultFunction {
if *arg2 != 0 {
let ret = (*arg1 as f64) / (*arg2 as f64);
Ok(Value::Con(Constant::Integer(ret.floor() as isize)))
Ok(Value::Con(Constant::Integer(ret.floor() as i128)))
} else {
Err(Error::DivideByZero(*arg1, *arg2))
}
@@ -363,7 +363,7 @@ impl DefaultFunction {
let ret = if ret < 0. { ret.ceil() } else { ret.floor() };
Ok(Value::Con(Constant::Integer(ret as isize)))
Ok(Value::Con(Constant::Integer(ret as i128)))
} else {
Err(Error::DivideByZero(*arg1, *arg2))
}
@@ -447,7 +447,7 @@ impl DefaultFunction {
},
DefaultFunction::LengthOfByteString => match &args[0] {
Value::Con(Constant::ByteString(arg1)) => {
Ok(Value::Con(Constant::Integer(arg1.len() as isize)))
Ok(Value::Con(Constant::Integer(arg1.len() as i128)))
}
_ => unreachable!(),
},
@@ -456,7 +456,7 @@ impl DefaultFunction {
let index = *arg2 as usize;
if 0 <= *arg2 && index < arg1.len() {
let ret = arg1[index] as isize;
let ret = arg1[index] as i128;
Ok(Value::Con(Constant::Integer(ret)))
} else {
@@ -749,7 +749,7 @@ impl DefaultFunction {
Type::Integer,
Type::List(Box::new(Type::Data)),
// TODO: handle other types of constructor tags
Box::new(Constant::Integer(convert_tag_to_constr(c.tag as isize))),
Box::new(Constant::Integer(convert_tag_to_constr(c.tag as i128))),
Box::new(Constant::ProtoList(
Type::Data,
c.fields
@@ -798,7 +798,7 @@ impl DefaultFunction {
if let BigInt::Int(i) = b {
let x: i128 = (*i).try_into().unwrap();
Ok(Value::Con(Constant::Integer(x as isize)))
Ok(Value::Con(Constant::Integer(x)))
} else {
unreachable!()
}
@@ -844,7 +844,7 @@ impl DefaultFunction {
}
}
fn convert_tag_to_constr(tag: isize) -> isize {
fn convert_tag_to_constr(tag: i128) -> i128 {
if tag < 128 {
tag - 121
} else if (1280..1401).contains(&tag) {

View File

@@ -106,7 +106,7 @@ peg::parser! {
= "(" _* "error" _* ")" { Term::Error }
rule constant_integer() -> Constant
= "integer" _+ i:number() { Constant::Integer(i as isize) }
= "integer" _+ i:big_number() { Constant::Integer(i as i128) }
rule constant_bytestring() -> Constant
= "bytestring" _+ "#" i:ident()* {
@@ -125,6 +125,9 @@ peg::parser! {
rule number() -> isize
= n:$("-"* ['0'..='9']+) {? n.parse().or(Err("isize")) }
rule big_number() -> i128
= n:$("-"* ['0'..='9']+) {? n.parse().or(Err("isize")) }
rule constant_data() -> Constant
= "data" _+ "#" i:ident()* {
Constant::Data(

View File

@@ -2,7 +2,7 @@ use crate::ast::{Constant, Term};
use crate::program_builder::WithTerm;
pub trait WithConstant: WithTerm {
fn with_int(self, int: isize) -> Self::Next {
fn with_int(self, int: i128) -> Self::Next {
let term = Term::Constant(Constant::Integer(int));
self.next(term)
}
@@ -42,7 +42,7 @@ mod tests {
proptest! {
#[test]
fn build_named__with_const(
int: isize
int: i128
) {
let code = format!(r"(program
11.22.33