diff --git a/ast/src/leo.pest b/ast/src/leo.pest index ad5f5076ef..2c75579668 100644 --- a/ast/src/leo.pest +++ b/ast/src/leo.pest @@ -120,12 +120,15 @@ type_integer = { | type_i64 | type_i128 } + +// Declared in types/integer.rs type_u8 = { "u8" } type_u16 = { "u16" } type_u32 = { "u32" } type_u64 = { "u64" } type_u128 = { "u128" } +// Declared in types/integer.rs type_i8 = { "i8" } type_i16 = { "i16" } type_i32 = { "i32" } diff --git a/ast/src/types/integer_type.rs b/ast/src/types/integer_type.rs index 29c0cda22a..b0895f073b 100644 --- a/ast/src/types/integer_type.rs +++ b/ast/src/types/integer_type.rs @@ -19,6 +19,7 @@ pub enum IntegerType { } // Unsigned + #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::type_u8))] pub struct U8Type {} @@ -39,7 +40,7 @@ pub struct U64Type {} #[pest_ast(rule(Rule::type_u128))] pub struct U128Type {} -// Singed +// Signed #[derive(Clone, Debug, FromPest, PartialEq)] #[pest_ast(rule(Rule::type_i8))] diff --git a/compiler/src/value/integer/integer.rs b/compiler/src/value/integer/integer.rs index 4db3a1c7e7..89cb002117 100644 --- a/compiler/src/value/integer/integer.rs +++ b/compiler/src/value/integer/integer.rs @@ -130,14 +130,17 @@ impl Integer { } } - pub fn get_value(&self) -> Option { + pub fn get_value(&self) -> Option { let integer = self; match_integer!(integer => integer.get_value()) } pub fn to_usize(&self, span: Span) -> Result { - let value = self.get_value().ok_or(IntegerError::invalid_index(span))?; - Ok(value as usize) + let value = self.get_value().ok_or(IntegerError::invalid_index(span.clone()))?; + let value_usize = value + .parse::() + .map_err(|_| IntegerError::invalid_integer(value, span))?; + Ok(value_usize) } pub fn to_bits_le(&self) -> Vec { @@ -165,14 +168,20 @@ impl Integer { cs: &mut CS, integer_type: IntegerType, name: String, - option: Option, + option: Option, span: Span, ) -> Result { Ok(match integer_type { IntegerType::U8 => { let u8_name = format!("{}: u8", name); let u8_name_unique = format!("`{}` {}:{}", u8_name, span.line, span.start); - let u8_option = option.map(|integer| integer as u8); + + let u8_option = option.map(|s| { + s.parse::() + .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .unwrap() + }); + let u8_result = UInt8::alloc(cs.ns(|| u8_name_unique), || { u8_option.ok_or(SynthesisError::AssignmentMissing) }) @@ -183,7 +192,11 @@ impl Integer { IntegerType::U16 => { let u16_name = format!("{}: u16", name); let u16_name_unique = format!("`{}` {}:{}", u16_name, span.line, span.start); - let u16_option = option.map(|integer| integer as u16); + let u16_option = option.map(|s| { + s.parse::() + .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .unwrap() + }); let u16_result = UInt16::alloc(cs.ns(|| u16_name_unique), || { u16_option.ok_or(SynthesisError::AssignmentMissing) }) @@ -194,7 +207,11 @@ impl Integer { IntegerType::U32 => { let u32_name = format!("{}: u32", name); let u32_name_unique = format!("`{}` {}:{}", u32_name, span.line, span.start); - let u32_option = option.map(|integer| integer as u32); + let u32_option = option.map(|s| { + s.parse::() + .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .unwrap() + }); let u32_result = UInt32::alloc(cs.ns(|| u32_name_unique), || { u32_option.ok_or(SynthesisError::AssignmentMissing) }) @@ -205,7 +222,11 @@ impl Integer { IntegerType::U64 => { let u64_name = format!("{}: u64", name); let u64_name_unique = format!("`{}` {}:{}", u64_name, span.line, span.start); - let u64_option = option.map(|integer| integer as u64); + let u64_option = option.map(|s| { + s.parse::() + .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .unwrap() + }); let u64_result = UInt64::alloc(cs.ns(|| u64_name_unique), || { u64_option.ok_or(SynthesisError::AssignmentMissing) }) @@ -216,7 +237,11 @@ impl Integer { IntegerType::U128 => { let u128_name = format!("{}: u128", name); let u128_name_unique = format!("`{}` {}:{}", u128_name, span.line, span.start); - let u128_option = option.map(|integer| integer as u128); + let u128_option = option.map(|s| { + s.parse::() + .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .unwrap() + }); let u128_result = UInt128::alloc(cs.ns(|| u128_name_unique), || { u128_option.ok_or(SynthesisError::AssignmentMissing) }) @@ -228,7 +253,11 @@ impl Integer { IntegerType::I8 => { let i8_name = format!("{}: i8", name); let i8_name_unique = format!("`{}` {}:{}", i8_name, span.line, span.start); - let i8_option = option.map(|integer| integer as i8); + let i8_option = option.map(|s| { + s.parse::() + .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .unwrap() + }); let i8_result = Int8::alloc(cs.ns(|| i8_name_unique), || { i8_option.ok_or(SynthesisError::AssignmentMissing) }) @@ -239,7 +268,11 @@ impl Integer { IntegerType::I16 => { let i16_name = format!("{}: i16", name); let i16_name_unique = format!("`{}` {}:{}", i16_name, span.line, span.start); - let i16_option = option.map(|integer| integer as i16); + let i16_option = option.map(|s| { + s.parse::() + .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .unwrap() + }); let i16_result = Int16::alloc(cs.ns(|| i16_name_unique), || { i16_option.ok_or(SynthesisError::AssignmentMissing) }) @@ -250,7 +283,11 @@ impl Integer { IntegerType::I32 => { let i32_name = format!("{}: i32", name); let i32_name_unique = format!("`{}` {}:{}", i32_name, span.line, span.start); - let i32_option = option.map(|integer| integer as i32); + let i32_option = option.map(|s| { + s.parse::() + .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .unwrap() + }); let i32_result = Int32::alloc(cs.ns(|| i32_name_unique), || { i32_option.ok_or(SynthesisError::AssignmentMissing) }) @@ -261,7 +298,11 @@ impl Integer { IntegerType::I64 => { let i64_name = format!("{}: i64", name); let i64_name_unique = format!("`{}` {}:{}", i64_name, span.line, span.start); - let i64_option = option.map(|integer| integer as i64); + let i64_option = option.map(|s| { + s.parse::() + .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .unwrap() + }); let i64_result = Int64::alloc(cs.ns(|| i64_name_unique), || { i64_option.ok_or(SynthesisError::AssignmentMissing) }) @@ -272,7 +313,11 @@ impl Integer { IntegerType::I128 => { let i128_name = format!("{}: i128", name); let i128_name_unique = format!("`{}` {}:{}", i128_name, span.line, span.start); - let i128_option = option.map(|integer| integer as i128); + let i128_option = option.map(|s| { + s.parse::() + .map_err(|_| IntegerError::invalid_integer(s, span.clone())) + .unwrap() + }); let i128_result = Int128::alloc(cs.ns(|| i128_name_unique), || { i128_option.ok_or(SynthesisError::AssignmentMissing) }) diff --git a/compiler/src/value/integer/macros.rs b/compiler/src/value/integer/macros.rs index 01dfd458b8..7bf3b765b9 100644 --- a/compiler/src/value/integer/macros.rs +++ b/compiler/src/value/integer/macros.rs @@ -7,7 +7,7 @@ use snarkos_models::gadgets::utilities::{ use std::fmt::Debug; pub trait IntegerTrait: Sized + Clone + Debug + Add { - fn get_value(&self) -> Option; + fn get_value(&self) -> Option; fn get_bits(&self) -> Vec; } @@ -15,8 +15,8 @@ pub trait IntegerTrait: Sized + Clone + Debug + Add { macro_rules! integer_trait_impl { ($($gadget: ident)*) => ($( impl IntegerTrait for $gadget { - fn get_value(&self) -> Option { - self.value.map(|num| num as u128) + fn get_value(&self) -> Option { + self.value.map(|num| num.to_string()) } fn get_bits(&self) -> Vec { diff --git a/compiler/src/value/value.rs b/compiler/src/value/value.rs index 9e019284d7..645a008bd8 100644 --- a/compiler/src/value/value.rs +++ b/compiler/src/value/value.rs @@ -191,7 +191,7 @@ impl> ConstrainedValue { ConstrainedValue::Integer(integer) => { let integer_type = integer.get_type(); let option = integer.get_value(); - let name = option.map(|n| n.to_string()).unwrap_or(format!("[allocated]")); + let name = option.clone().unwrap_or(format!("[allocated]")); *integer = Integer::allocate_type(&mut cs, integer_type, name, option, span)?; } diff --git a/leo-inputs/src/leo-inputs.pest b/leo-inputs/src/leo-inputs.pest index ea613aece0..244c05c3f8 100644 --- a/leo-inputs/src/leo-inputs.pest +++ b/leo-inputs/src/leo-inputs.pest @@ -2,7 +2,22 @@ // Declared in common/identifier.rs identifier = @{ ((!protected_name ~ ASCII_ALPHA) | (protected_name ~ (ASCII_ALPHANUMERIC | "_"))) ~ (ASCII_ALPHANUMERIC | "_")* } -protected_name = { "let" | "for"| "if" | "else" | "as" | "return" } +protected_name = { + "address" + | "as" + | "const" + | "else" + | "false" + | "function" + | "for" + | "if" + | "in" + | "let" + | "mut" + | "return" + | "static" + | "true" + } // Declared in common/line_end.rs LINE_END = { ";" ~ NEWLINE* } @@ -20,13 +35,27 @@ type_integer = { | type_u32 | type_u64 | type_u128 + | type_i8 + | type_i16 + | type_i32 + | type_i64 + | type_i128 } + +// Declared in types/integer_type.rs type_u8 = { "u8" } type_u16 = { "u16" } type_u32 = { "u32" } type_u64 = { "u64" } type_u128 = { "u128" } +// Declared in types/integer_type.rs +type_i8 = { "i8" } +type_i16 = { "i16" } +type_i32 = { "i32" } +type_i64 = { "i64" } +type_i128 = { "i128" } + // Declared in types/field_type.rs type_field = { "field" } diff --git a/leo-inputs/src/types/integer_type.rs b/leo-inputs/src/types/integer_type.rs index ed2b520d1c..c49cb11562 100644 --- a/leo-inputs/src/types/integer_type.rs +++ b/leo-inputs/src/types/integer_type.rs @@ -10,8 +10,16 @@ pub enum IntegerType { U32Type(U32Type), U64Type(U64Type), U128Type(U128Type), + + I8Type(I8Type), + I16Type(I16Type), + I32Type(I32Type), + I64Type(I64Type), + I128Type(I128Type), } +// Unsigned + #[derive(Clone, Debug, FromPest, PartialEq, Eq)] #[pest_ast(rule(Rule::type_u8))] pub struct U8Type {} @@ -32,6 +40,28 @@ pub struct U64Type {} #[pest_ast(rule(Rule::type_u128))] pub struct U128Type {} +// Signed + +#[derive(Clone, Debug, FromPest, PartialEq, Eq)] +#[pest_ast(rule(Rule::type_i8))] +pub struct I8Type {} + +#[derive(Clone, Debug, FromPest, PartialEq, Eq)] +#[pest_ast(rule(Rule::type_i16))] +pub struct I16Type {} + +#[derive(Clone, Debug, FromPest, PartialEq, Eq)] +#[pest_ast(rule(Rule::type_i32))] +pub struct I32Type {} + +#[derive(Clone, Debug, FromPest, PartialEq, Eq)] +#[pest_ast(rule(Rule::type_i64))] +pub struct I64Type {} + +#[derive(Clone, Debug, FromPest, PartialEq, Eq)] +#[pest_ast(rule(Rule::type_i128))] +pub struct I128Type {} + impl std::fmt::Display for IntegerType { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { @@ -40,6 +70,12 @@ impl std::fmt::Display for IntegerType { IntegerType::U32Type(_) => write!(f, "u32"), IntegerType::U64Type(_) => write!(f, "u64"), IntegerType::U128Type(_) => write!(f, "u128"), + + IntegerType::I8Type(_) => write!(f, "i8"), + IntegerType::I16Type(_) => write!(f, "i16"), + IntegerType::I32Type(_) => write!(f, "i32"), + IntegerType::I64Type(_) => write!(f, "i64"), + IntegerType::I128Type(_) => write!(f, "i128"), } } } diff --git a/types/src/inputs/input_value.rs b/types/src/inputs/input_value.rs index a731c7d1f1..6faf19efce 100644 --- a/types/src/inputs/input_value.rs +++ b/types/src/inputs/input_value.rs @@ -14,7 +14,7 @@ pub enum InputValue { Boolean(bool), Field(String), Group(String), - Integer(IntegerType, u128), + Integer(IntegerType, String), Array(Vec), } @@ -29,8 +29,7 @@ impl InputValue { } fn from_number(integer_type: IntegerType, number: NumberValue) -> Result { - let integer = number.value.parse::()?; - Ok(InputValue::Integer(integer_type, integer)) + Ok(InputValue::Integer(integer_type, number.value)) } fn from_group(group: GroupValue) -> Self {