mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-20 02:07:38 +03:00
parse integer later in program. derive serde for basic program types
This commit is contained in:
parent
5f0b3e2b0c
commit
1fafbab5ee
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -550,6 +550,7 @@ dependencies = [
|
|||||||
"leo-ast",
|
"leo-ast",
|
||||||
"leo-inputs",
|
"leo-inputs",
|
||||||
"pest",
|
"pest",
|
||||||
|
"serde",
|
||||||
"snarkos-errors",
|
"snarkos-errors",
|
||||||
"snarkos-models",
|
"snarkos-models",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
|
@ -900,7 +900,9 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Values
|
// 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::Field(field, span) => Ok(ConstrainedValue::Field(FieldType::constant(field, span)?)),
|
||||||
Expression::Group(group_affine, span) => Ok(ConstrainedValue::Group(G::constant(group_affine, 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)?)),
|
Expression::Boolean(boolean, span) => Ok(ConstrainedValue::Boolean(new_bool_constant(boolean, span)?)),
|
||||||
|
@ -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 }
|
snarkos-models = { path = "../../snarkOS/models", version = "0.8.0", default-features = false }
|
||||||
|
|
||||||
pest = { version = "2.0" }
|
pest = { version = "2.0" }
|
||||||
|
serde = { version = "1.0" }
|
||||||
thiserror = { version = "1.0" }
|
thiserror = { version = "1.0" }
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
use crate::Span;
|
use crate::Span;
|
||||||
use leo_ast::common::Identifier as AstIdentifier;
|
use leo_ast::common::Identifier as AstIdentifier;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
/// An identifier in the constrained program.
|
/// An identifier in the constrained program.
|
||||||
#[derive(Clone, Hash)]
|
#[derive(Clone, Hash, Serialize, Deserialize)]
|
||||||
pub struct Identifier {
|
pub struct Identifier {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
|
@ -15,12 +15,12 @@ impl<'ast> From<AstRangeOrExpression<'ast>> for RangeOrExpression {
|
|||||||
match range_or_expression {
|
match range_or_expression {
|
||||||
AstRangeOrExpression::Range(range) => {
|
AstRangeOrExpression::Range(range) => {
|
||||||
let from = range.from.map(|from| match Expression::from(from.0) {
|
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::Implicit(string, _span) => Integer::from_implicit(string),
|
||||||
expression => unimplemented!("Range bounds should be integers, found {}", expression),
|
expression => unimplemented!("Range bounds should be integers, found {}", expression),
|
||||||
});
|
});
|
||||||
let to = range.to.map(|to| match Expression::from(to.0) {
|
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::Implicit(string, _span) => Integer::from_implicit(string),
|
||||||
expression => unimplemented!("Range bounds should be integers, found {}", expression),
|
expression => unimplemented!("Range bounds should be integers, found {}", expression),
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use pest::{Position, Span as AstSpan};
|
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 {
|
pub struct Span {
|
||||||
/// text of input string
|
/// text of input string
|
||||||
pub text: String,
|
pub text: String,
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use crate::{CircuitFieldDefinition, Identifier, Integer, RangeOrExpression, Span, SpreadOrExpression};
|
use crate::{CircuitFieldDefinition, Identifier, IntegerType, RangeOrExpression, Span, SpreadOrExpression};
|
||||||
use leo_ast::{
|
use leo_ast::{
|
||||||
access::{Access, AssigneeAccess},
|
access::{Access, AssigneeAccess},
|
||||||
common::{Assignee, Identifier as AstIdentifier},
|
common::{Assignee, Identifier as AstIdentifier},
|
||||||
@ -25,7 +25,7 @@ pub enum Expression {
|
|||||||
Identifier(Identifier),
|
Identifier(Identifier),
|
||||||
|
|
||||||
// Values
|
// Values
|
||||||
Integer(Integer),
|
Integer(IntegerType, String, Span),
|
||||||
Field(String, Span),
|
Field(String, Span),
|
||||||
Group(String, Span),
|
Group(String, Span),
|
||||||
Boolean(String, Span),
|
Boolean(String, Span),
|
||||||
@ -120,7 +120,7 @@ impl<'ast> fmt::Display for Expression {
|
|||||||
Expression::Identifier(ref variable) => write!(f, "{}", variable),
|
Expression::Identifier(ref variable) => write!(f, "{}", variable),
|
||||||
|
|
||||||
// Values
|
// 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::Field(ref field, ref _span) => write!(f, "{}", field),
|
||||||
Expression::Group(ref group, ref _span) => write!(f, "{}", group),
|
Expression::Group(ref group, ref _span) => write!(f, "{}", group),
|
||||||
Expression::Boolean(ref bool, ref _span) => write!(f, "{}", bool),
|
Expression::Boolean(ref bool, ref _span) => write!(f, "{}", bool),
|
||||||
@ -447,8 +447,12 @@ impl<'ast> From<NumberImplicitValue<'ast>> for Expression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast> From<IntegerValue<'ast>> for Expression {
|
impl<'ast> From<IntegerValue<'ast>> for Expression {
|
||||||
fn from(field: IntegerValue<'ast>) -> Self {
|
fn from(integer: IntegerValue<'ast>) -> Self {
|
||||||
Expression::Integer(Integer::from(field.number, field._type))
|
Expression::Integer(
|
||||||
|
IntegerType::from(integer._type),
|
||||||
|
integer.number.value,
|
||||||
|
Span::from(integer.span),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
use crate::{Identifier, Span, Type};
|
use crate::{Identifier, Span, Type};
|
||||||
use leo_ast::functions::FunctionInput as AstFunctionInput;
|
use leo_ast::functions::FunctionInput as AstFunctionInput;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq)]
|
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub struct FunctionInput {
|
pub struct FunctionInput {
|
||||||
pub identifier: Identifier,
|
pub identifier: Identifier,
|
||||||
pub mutable: bool,
|
pub mutable: bool,
|
||||||
|
@ -3,9 +3,10 @@
|
|||||||
use crate::{ImportSymbol, Span};
|
use crate::{ImportSymbol, Span};
|
||||||
use leo_ast::imports::Import as AstImport;
|
use leo_ast::imports::Import as AstImport;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
pub struct Import {
|
pub struct Import {
|
||||||
pub path_string: String,
|
pub path_string: String,
|
||||||
pub symbols: Vec<ImportSymbol>,
|
pub symbols: Vec<ImportSymbol>,
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
use crate::{Identifier, Span};
|
use crate::{Identifier, Span};
|
||||||
use leo_ast::imports::ImportSymbol as AstImportSymbol;
|
use leo_ast::imports::ImportSymbol as AstImportSymbol;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
pub struct ImportSymbol {
|
pub struct ImportSymbol {
|
||||||
pub symbol: Identifier,
|
pub symbol: Identifier,
|
||||||
pub alias: Option<Identifier>,
|
pub alias: Option<Identifier>,
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
//! Conversion of integer declarations to constraints in Leo.
|
//! Conversion of integer declarations to constraints in Leo.
|
||||||
|
|
||||||
use crate::{errors::IntegerError, InputValue, IntegerType, Span};
|
use crate::{errors::IntegerError, InputValue, IntegerType, Span};
|
||||||
use leo_ast::{types::IntegerType as AstIntegerType, values::NumberValue};
|
|
||||||
|
|
||||||
use snarkos_errors::gadgets::SynthesisError;
|
use snarkos_errors::gadgets::SynthesisError;
|
||||||
use snarkos_models::{
|
use snarkos_models::{
|
||||||
@ -11,17 +10,15 @@ use snarkos_models::{
|
|||||||
utilities::{
|
utilities::{
|
||||||
alloc::AllocGadget,
|
alloc::AllocGadget,
|
||||||
boolean::Boolean,
|
boolean::Boolean,
|
||||||
eq::{ConditionalEqGadget, EqGadget},
|
eq::{ConditionalEqGadget, EqGadget, EvaluateEqGadget},
|
||||||
select::CondSelectGadget,
|
select::CondSelectGadget,
|
||||||
uint::{UInt, UInt128, UInt16, UInt32, UInt64, UInt8},
|
uint::{UInt, UInt128, UInt16, UInt32, UInt64, UInt8},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use snarkos_models::gadgets::utilities::eq::EvaluateEqGadget;
|
|
||||||
use std::fmt;
|
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)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd)]
|
||||||
pub enum Integer {
|
pub enum Integer {
|
||||||
U8(UInt8),
|
U8(UInt8),
|
||||||
@ -31,33 +28,11 @@ pub enum Integer {
|
|||||||
U128(UInt128),
|
U128(UInt128),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast> Integer {
|
impl Integer {
|
||||||
pub fn from(number: NumberValue<'ast>, _type: AstIntegerType) -> Self {
|
|
||||||
match _type {
|
|
||||||
AstIntegerType::U8Type(_u8) => {
|
|
||||||
Integer::U8(UInt8::constant(number.value.parse::<u8>().expect("unable to parse u8")))
|
|
||||||
}
|
|
||||||
AstIntegerType::U16Type(_u16) => Integer::U16(UInt16::constant(
|
|
||||||
number.value.parse::<u16>().expect("unable to parse u16"),
|
|
||||||
)),
|
|
||||||
AstIntegerType::U32Type(_u32) => Integer::U32(UInt32::constant(
|
|
||||||
number.value.parse::<u32>().expect("unable to parse integers.u32"),
|
|
||||||
)),
|
|
||||||
AstIntegerType::U64Type(_u64) => Integer::U64(UInt64::constant(
|
|
||||||
number.value.parse::<u64>().expect("unable to parse u64"),
|
|
||||||
)),
|
|
||||||
AstIntegerType::U128Type(_u128) => Integer::U128(UInt128::constant(
|
|
||||||
number.value.parse::<u128>().expect("unable to parse u128"),
|
|
||||||
)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn from_implicit(number: String) -> Self {
|
pub fn from_implicit(number: String) -> Self {
|
||||||
Integer::U128(UInt128::constant(number.parse::<u128>().expect("unable to parse u128")))
|
Integer::U128(UInt128::constant(number.parse::<u128>().expect("unable to parse u128")))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl Integer {
|
|
||||||
pub fn new_constant(integer_type: &IntegerType, string: String, span: Span) -> Result<Self, IntegerError> {
|
pub fn new_constant(integer_type: &IntegerType, string: String, span: Span) -> Result<Self, IntegerError> {
|
||||||
match integer_type {
|
match integer_type {
|
||||||
IntegerType::U8 => {
|
IntegerType::U8 => {
|
||||||
|
@ -150,12 +150,12 @@ impl<'ast> From<MultipleAssignmentStatement<'ast>> for Statement {
|
|||||||
impl<'ast> From<ForStatement<'ast>> for Statement {
|
impl<'ast> From<ForStatement<'ast>> for Statement {
|
||||||
fn from(statement: ForStatement<'ast>) -> Self {
|
fn from(statement: ForStatement<'ast>) -> Self {
|
||||||
let from = match Expression::from(statement.start) {
|
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::Implicit(string, _span) => Integer::from_implicit(string),
|
||||||
expression => unimplemented!("Range bounds should be integers, found {}", expression),
|
expression => unimplemented!("Range bounds should be integers, found {}", expression),
|
||||||
};
|
};
|
||||||
let to = match Expression::from(statement.stop) {
|
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::Implicit(string, _span) => Integer::from_implicit(string),
|
||||||
expression => unimplemented!("Range bounds should be integers, found {}", expression),
|
expression => unimplemented!("Range bounds should be integers, found {}", expression),
|
||||||
};
|
};
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
use leo_ast::types::IntegerType as AstIntegerType;
|
use leo_ast::types::IntegerType as AstIntegerType;
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
/// Explicit integer type
|
/// Explicit integer type
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub enum IntegerType {
|
pub enum IntegerType {
|
||||||
U8,
|
U8,
|
||||||
U16,
|
U16,
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
use crate::{Expression, Identifier, IntegerType};
|
use crate::{Expression, Identifier, IntegerType};
|
||||||
use leo_ast::types::{ArrayType, CircuitType, DataType, Type as AstType};
|
use leo_ast::types::{ArrayType, CircuitType, DataType, Type as AstType};
|
||||||
|
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
/// Explicit type used for defining a variable or expression type
|
/// 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 {
|
pub enum Type {
|
||||||
IntegerType(IntegerType),
|
IntegerType(IntegerType),
|
||||||
Field,
|
Field,
|
||||||
|
Loading…
Reference in New Issue
Block a user