diff --git a/Cargo.lock b/Cargo.lock index 3fccd5be74..f0d0e16128 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -550,6 +550,7 @@ dependencies = [ "leo-ast", "leo-inputs", "pest", + "serde", "snarkos-errors", "snarkos-models", "thiserror", diff --git a/compiler/src/constraints/expression.rs b/compiler/src/constraints/expression.rs index 50f428c7e7..69f0d8f728 100644 --- a/compiler/src/constraints/expression.rs +++ b/compiler/src/constraints/expression.rs @@ -900,7 +900,9 @@ impl> ConstrainedProgram { } // Values - Expression::Integer(integer) => Ok(ConstrainedValue::Integer(integer)), + Expression::Integer(type_, integer, span) => { + Ok(ConstrainedValue::Integer(Integer::new_constant(&type_, integer, span)?)) + } Expression::Field(field, span) => Ok(ConstrainedValue::Field(FieldType::constant(field, span)?)), Expression::Group(group_affine, span) => Ok(ConstrainedValue::Group(G::constant(group_affine, span)?)), Expression::Boolean(boolean, span) => Ok(ConstrainedValue::Boolean(new_bool_constant(boolean, span)?)), diff --git a/types/Cargo.toml b/types/Cargo.toml index 03c2901821..9874cd0cba 100644 --- a/types/Cargo.toml +++ b/types/Cargo.toml @@ -12,4 +12,5 @@ snarkos-errors = { path = "../../snarkOS/errors", version = "0.8.0", default-fea snarkos-models = { path = "../../snarkOS/models", version = "0.8.0", default-features = false } pest = { version = "2.0" } +serde = { version = "1.0" } thiserror = { version = "1.0" } diff --git a/types/src/common/identifier.rs b/types/src/common/identifier.rs index 4c7e0dca18..0739dbe3d3 100644 --- a/types/src/common/identifier.rs +++ b/types/src/common/identifier.rs @@ -1,10 +1,11 @@ use crate::Span; use leo_ast::common::Identifier as AstIdentifier; +use serde::{Deserialize, Serialize}; use std::fmt; /// An identifier in the constrained program. -#[derive(Clone, Hash)] +#[derive(Clone, Hash, Serialize, Deserialize)] pub struct Identifier { pub name: String, pub span: Span, diff --git a/types/src/common/range_or_expression.rs b/types/src/common/range_or_expression.rs index a006f28c26..589f306bda 100644 --- a/types/src/common/range_or_expression.rs +++ b/types/src/common/range_or_expression.rs @@ -15,12 +15,12 @@ impl<'ast> From> for RangeOrExpression { match range_or_expression { AstRangeOrExpression::Range(range) => { let from = range.from.map(|from| match Expression::from(from.0) { - Expression::Integer(number) => number, + Expression::Integer(type_, integer, span) => Integer::new_constant(&type_, integer, span).unwrap(), Expression::Implicit(string, _span) => Integer::from_implicit(string), expression => unimplemented!("Range bounds should be integers, found {}", expression), }); let to = range.to.map(|to| match Expression::from(to.0) { - Expression::Integer(number) => number, + Expression::Integer(type_, integer, span) => Integer::new_constant(&type_, integer, span).unwrap(), Expression::Implicit(string, _span) => Integer::from_implicit(string), expression => unimplemented!("Range bounds should be integers, found {}", expression), }); diff --git a/types/src/common/span.rs b/types/src/common/span.rs index b9a0adc144..cc62e32796 100644 --- a/types/src/common/span.rs +++ b/types/src/common/span.rs @@ -1,6 +1,7 @@ use pest::{Position, Span as AstSpan}; +use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug, PartialEq, Eq, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct Span { /// text of input string pub text: String, diff --git a/types/src/expression.rs b/types/src/expression.rs index fc552cab4b..0001de8548 100644 --- a/types/src/expression.rs +++ b/types/src/expression.rs @@ -1,4 +1,4 @@ -use crate::{CircuitFieldDefinition, Identifier, Integer, RangeOrExpression, Span, SpreadOrExpression}; +use crate::{CircuitFieldDefinition, Identifier, IntegerType, RangeOrExpression, Span, SpreadOrExpression}; use leo_ast::{ access::{Access, AssigneeAccess}, common::{Assignee, Identifier as AstIdentifier}, @@ -25,7 +25,7 @@ pub enum Expression { Identifier(Identifier), // Values - Integer(Integer), + Integer(IntegerType, String, Span), Field(String, Span), Group(String, Span), Boolean(String, Span), @@ -120,7 +120,7 @@ impl<'ast> fmt::Display for Expression { Expression::Identifier(ref variable) => write!(f, "{}", variable), // Values - Expression::Integer(ref integer) => write!(f, "{}", integer), + Expression::Integer(ref type_, ref integer, ref _span) => write!(f, "{}{}", integer, type_), Expression::Field(ref field, ref _span) => write!(f, "{}", field), Expression::Group(ref group, ref _span) => write!(f, "{}", group), Expression::Boolean(ref bool, ref _span) => write!(f, "{}", bool), @@ -447,8 +447,12 @@ impl<'ast> From> for Expression { } impl<'ast> From> for Expression { - fn from(field: IntegerValue<'ast>) -> Self { - Expression::Integer(Integer::from(field.number, field._type)) + fn from(integer: IntegerValue<'ast>) -> Self { + Expression::Integer( + IntegerType::from(integer._type), + integer.number.value, + Span::from(integer.span), + ) } } diff --git a/types/src/functions/function_input.rs b/types/src/functions/function_input.rs index e28b12a8e2..98c88071dc 100644 --- a/types/src/functions/function_input.rs +++ b/types/src/functions/function_input.rs @@ -1,9 +1,10 @@ use crate::{Identifier, Span, Type}; use leo_ast::functions::FunctionInput as AstFunctionInput; +use serde::{Deserialize, Serialize}; use std::fmt; -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct FunctionInput { pub identifier: Identifier, pub mutable: bool, diff --git a/types/src/imports/import.rs b/types/src/imports/import.rs index 5f0a1947cc..dc800efd7b 100644 --- a/types/src/imports/import.rs +++ b/types/src/imports/import.rs @@ -3,9 +3,10 @@ use crate::{ImportSymbol, Span}; use leo_ast::imports::Import as AstImport; +use serde::{Deserialize, Serialize}; use std::fmt; -#[derive(Clone)] +#[derive(Clone, Serialize, Deserialize)] pub struct Import { pub path_string: String, pub symbols: Vec, diff --git a/types/src/imports/import_symbol.rs b/types/src/imports/import_symbol.rs index 1c96198764..5865df3324 100644 --- a/types/src/imports/import_symbol.rs +++ b/types/src/imports/import_symbol.rs @@ -1,9 +1,10 @@ use crate::{Identifier, Span}; use leo_ast::imports::ImportSymbol as AstImportSymbol; +use serde::{Deserialize, Serialize}; use std::fmt; -#[derive(Clone)] +#[derive(Clone, Serialize, Deserialize)] pub struct ImportSymbol { pub symbol: Identifier, pub alias: Option, diff --git a/types/src/integer.rs b/types/src/integer.rs index e83c5b8eab..68bb5f0b14 100644 --- a/types/src/integer.rs +++ b/types/src/integer.rs @@ -1,7 +1,6 @@ //! Conversion of integer declarations to constraints in Leo. use crate::{errors::IntegerError, InputValue, IntegerType, Span}; -use leo_ast::{types::IntegerType as AstIntegerType, values::NumberValue}; use snarkos_errors::gadgets::SynthesisError; use snarkos_models::{ @@ -11,17 +10,15 @@ use snarkos_models::{ utilities::{ alloc::AllocGadget, boolean::Boolean, - eq::{ConditionalEqGadget, EqGadget}, + eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget}, select::CondSelectGadget, uint::{UInt, UInt128, UInt16, UInt32, UInt64, UInt8}, }, }, }; - -use snarkos_models::gadgets::utilities::eq::EvaluateEqGadget; use std::fmt; -/// An integer type enum wrapping the integer value. Used only in expressions. +/// An integer type enum wrapping the integer value. #[derive(Debug, Clone, PartialEq, Eq, PartialOrd)] pub enum Integer { U8(UInt8), @@ -31,33 +28,11 @@ pub enum Integer { U128(UInt128), } -impl<'ast> Integer { - pub fn from(number: NumberValue<'ast>, _type: AstIntegerType) -> Self { - match _type { - AstIntegerType::U8Type(_u8) => { - Integer::U8(UInt8::constant(number.value.parse::().expect("unable to parse u8"))) - } - AstIntegerType::U16Type(_u16) => Integer::U16(UInt16::constant( - number.value.parse::().expect("unable to parse u16"), - )), - AstIntegerType::U32Type(_u32) => Integer::U32(UInt32::constant( - number.value.parse::().expect("unable to parse integers.u32"), - )), - AstIntegerType::U64Type(_u64) => Integer::U64(UInt64::constant( - number.value.parse::().expect("unable to parse u64"), - )), - AstIntegerType::U128Type(_u128) => Integer::U128(UInt128::constant( - number.value.parse::().expect("unable to parse u128"), - )), - } - } - +impl Integer { pub fn from_implicit(number: String) -> Self { Integer::U128(UInt128::constant(number.parse::().expect("unable to parse u128"))) } -} -impl Integer { pub fn new_constant(integer_type: &IntegerType, string: String, span: Span) -> Result { match integer_type { IntegerType::U8 => { diff --git a/types/src/statements/statement.rs b/types/src/statements/statement.rs index 2929d548c4..e8d99eaa2d 100644 --- a/types/src/statements/statement.rs +++ b/types/src/statements/statement.rs @@ -150,12 +150,12 @@ impl<'ast> From> for Statement { impl<'ast> From> for Statement { fn from(statement: ForStatement<'ast>) -> Self { let from = match Expression::from(statement.start) { - Expression::Integer(number) => number, + Expression::Integer(type_, integer, span) => Integer::new_constant(&type_, integer, span).unwrap(), Expression::Implicit(string, _span) => Integer::from_implicit(string), expression => unimplemented!("Range bounds should be integers, found {}", expression), }; let to = match Expression::from(statement.stop) { - Expression::Integer(number) => number, + Expression::Integer(type_, integer, span) => Integer::new_constant(&type_, integer, span).unwrap(), Expression::Implicit(string, _span) => Integer::from_implicit(string), expression => unimplemented!("Range bounds should be integers, found {}", expression), }; diff --git a/types/src/types/integer_type.rs b/types/src/types/integer_type.rs index ea1f4c95a8..0b6fdf6b84 100644 --- a/types/src/types/integer_type.rs +++ b/types/src/types/integer_type.rs @@ -1,9 +1,10 @@ use leo_ast::types::IntegerType as AstIntegerType; +use serde::{Deserialize, Serialize}; use std::fmt; /// Explicit integer type -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum IntegerType { U8, U16, diff --git a/types/src/types/type_.rs b/types/src/types/type_.rs index bed59bb394..710d1da7c0 100644 --- a/types/src/types/type_.rs +++ b/types/src/types/type_.rs @@ -1,10 +1,11 @@ use crate::{Expression, Identifier, IntegerType}; use leo_ast::types::{ArrayType, CircuitType, DataType, Type as AstType}; +use serde::{Deserialize, Serialize}; use std::fmt; /// Explicit type used for defining a variable or expression type -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum Type { IntegerType(IntegerType), Field,