mirror of
https://github.com/AleoHQ/leo.git
synced 2025-01-01 22:36:52 +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-inputs",
|
||||
"pest",
|
||||
"serde",
|
||||
"snarkos-errors",
|
||||
"snarkos-models",
|
||||
"thiserror",
|
||||
|
@ -900,7 +900,9 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
}
|
||||
|
||||
// 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)?)),
|
||||
|
@ -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" }
|
||||
|
@ -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,
|
||||
|
@ -15,12 +15,12 @@ impl<'ast> From<AstRangeOrExpression<'ast>> 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),
|
||||
});
|
||||
|
@ -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,
|
||||
|
@ -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<NumberImplicitValue<'ast>> for Expression {
|
||||
}
|
||||
|
||||
impl<'ast> From<IntegerValue<'ast>> 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),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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<ImportSymbol>,
|
||||
|
@ -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<Identifier>,
|
||||
|
@ -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::<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"),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
impl Integer {
|
||||
pub fn from_implicit(number: String) -> Self {
|
||||
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> {
|
||||
match integer_type {
|
||||
IntegerType::U8 => {
|
||||
|
@ -150,12 +150,12 @@ impl<'ast> From<MultipleAssignmentStatement<'ast>> for Statement {
|
||||
impl<'ast> From<ForStatement<'ast>> 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),
|
||||
};
|
||||
|
@ -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,
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user