mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-23 23:23:50 +03:00
Merge branch 'feature/rename-circuit-field' into feature/mutable-circuit-variables
This commit is contained in:
commit
51b80d1742
@ -16,7 +16,7 @@
|
||||
|
||||
use crate::{
|
||||
ast::Rule,
|
||||
circuits::{CircuitFieldDefinition, CircuitFunction},
|
||||
circuits::{CircuitFunction, CircuitVariableDefinition},
|
||||
};
|
||||
|
||||
use pest_ast::FromPest;
|
||||
@ -25,6 +25,6 @@ use serde::Serialize;
|
||||
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
|
||||
#[pest_ast(rule(Rule::circuit_member))]
|
||||
pub enum CircuitMember<'ast> {
|
||||
CircuitFieldDefinition(CircuitFieldDefinition<'ast>),
|
||||
CircuitVariableDefinition(CircuitVariableDefinition<'ast>),
|
||||
CircuitFunction(CircuitFunction<'ast>),
|
||||
}
|
||||
|
@ -21,8 +21,8 @@ use pest_ast::FromPest;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
|
||||
#[pest_ast(rule(Rule::circuit_field))]
|
||||
pub struct CircuitField<'ast> {
|
||||
#[pest_ast(rule(Rule::circuit_variable))]
|
||||
pub struct CircuitVariable<'ast> {
|
||||
pub identifier: Identifier<'ast>,
|
||||
pub expression: Expression<'ast>,
|
||||
#[pest_ast(outer())]
|
@ -21,8 +21,8 @@ use pest_ast::FromPest;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
|
||||
#[pest_ast(rule(Rule::circuit_field_definition))]
|
||||
pub struct CircuitFieldDefinition<'ast> {
|
||||
#[pest_ast(rule(Rule::circuit_variable_definition))]
|
||||
pub struct CircuitVariableDefinition<'ast> {
|
||||
pub identifier: Identifier<'ast>,
|
||||
pub _type: Type<'ast>,
|
||||
#[pest_ast(outer())]
|
@ -17,11 +17,11 @@
|
||||
pub mod circuit;
|
||||
pub use circuit::*;
|
||||
|
||||
pub mod circuit_field;
|
||||
pub use circuit_field::*;
|
||||
pub mod circuit_variable;
|
||||
pub use circuit_variable::*;
|
||||
|
||||
pub mod circuit_field_definition;
|
||||
pub use circuit_field_definition::*;
|
||||
pub mod circuit_variable_definition;
|
||||
pub use circuit_variable_definition::*;
|
||||
|
||||
pub mod circuit_function;
|
||||
pub use circuit_function::*;
|
||||
|
@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{ast::Rule, circuits::CircuitField, common::Identifier, SpanDef};
|
||||
use crate::{ast::Rule, circuits::CircuitVariable, common::Identifier, SpanDef};
|
||||
|
||||
use pest::Span;
|
||||
use pest_ast::FromPest;
|
||||
@ -24,7 +24,7 @@ use serde::Serialize;
|
||||
#[pest_ast(rule(Rule::expression_circuit_inline))]
|
||||
pub struct CircuitInlineExpression<'ast> {
|
||||
pub identifier: Identifier<'ast>,
|
||||
pub members: Vec<CircuitField<'ast>>,
|
||||
pub members: Vec<CircuitVariable<'ast>>,
|
||||
#[pest_ast(outer())]
|
||||
#[serde(with = "SpanDef")]
|
||||
pub span: Span<'ast>,
|
||||
|
@ -307,17 +307,17 @@ access_static_member = ${ "::" ~ identifier }
|
||||
// Declared in circuits/circuit_definition.rs
|
||||
circuit = { "circuit " ~ identifier ~ "{" ~ NEWLINE* ~ circuit_member* ~ NEWLINE* ~ "}" ~ NEWLINE* }
|
||||
|
||||
// Declared in circuits/circuit_field.rs
|
||||
circuit_field = { identifier ~ ":" ~ expression }
|
||||
// Declared in circuits/circuit_variable.rs
|
||||
circuit_variable = { identifier ~ ":" ~ expression }
|
||||
|
||||
// Declared in circuits/circuit_field_definition.rs
|
||||
circuit_field_definition = { identifier ~ ":" ~ type_ ~ ","?}
|
||||
// Declared in circuits/circuit_variable_definition.rs
|
||||
circuit_variable_definition = { identifier ~ ":" ~ type_ ~ ","?}
|
||||
|
||||
// Declared in circuits/circuit_function.rs
|
||||
circuit_function = { static_? ~ function }
|
||||
|
||||
// Declared in circuits/circuit_member.rs
|
||||
circuit_member = { circuit_function | circuit_field_definition ~ NEWLINE*}
|
||||
circuit_member = { circuit_function | circuit_variable_definition ~ NEWLINE*}
|
||||
|
||||
/// Conditionals
|
||||
|
||||
@ -353,8 +353,8 @@ expression_array_inline = { "[" ~ NEWLINE* ~ inline_array_inner ~ NEWLINE* ~ "]"
|
||||
inline_array_inner = _{(spread_or_expression ~ ("," ~ NEWLINE* ~ spread_or_expression)*)?}
|
||||
|
||||
// Declared in expressions/circuit_inline_expression.rs
|
||||
expression_circuit_inline = { identifier ~ "{" ~ NEWLINE* ~ circuit_field_list ~ NEWLINE* ~ "}" }
|
||||
circuit_field_list = _{ (circuit_field ~ ("," ~ NEWLINE* ~ circuit_field)*)? ~ ","? }
|
||||
expression_circuit_inline = { identifier ~ "{" ~ NEWLINE* ~ circuit_variable_list ~ NEWLINE* ~ "}" }
|
||||
circuit_variable_list = _{ (circuit_variable ~ ("," ~ NEWLINE* ~ circuit_variable)*)? ~ ","? }
|
||||
|
||||
// Declared in expressions/unary_expression.rs
|
||||
expression_unary = { operation_unary ~ expression_term }
|
||||
|
@ -22,7 +22,7 @@ use crate::{
|
||||
value::{ConstrainedCircuitMember, ConstrainedValue},
|
||||
GroupType,
|
||||
};
|
||||
use leo_typed::{CircuitFieldDefinition, CircuitMember, Identifier, Span};
|
||||
use leo_typed::{CircuitMember, CircuitVariableDefinition, Identifier, Span};
|
||||
|
||||
use snarkos_models::{
|
||||
curves::{Field, PrimeField},
|
||||
@ -36,7 +36,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
file_scope: String,
|
||||
function_scope: String,
|
||||
identifier: Identifier,
|
||||
members: Vec<CircuitFieldDefinition>,
|
||||
members: Vec<CircuitVariableDefinition>,
|
||||
span: Span,
|
||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
||||
let mut program_identifier = new_scope(file_scope.clone(), identifier.to_string());
|
||||
@ -55,23 +55,23 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
|
||||
for member in circuit.members.clone().into_iter() {
|
||||
match member {
|
||||
CircuitMember::CircuitField(identifier, _type) => {
|
||||
let matched_field = members
|
||||
CircuitMember::CircuitVariable(identifier, _type) => {
|
||||
let matched_variable = members
|
||||
.clone()
|
||||
.into_iter()
|
||||
.find(|field| field.identifier.eq(&identifier));
|
||||
match matched_field {
|
||||
Some(field) => {
|
||||
.find(|variable| variable.identifier.eq(&identifier));
|
||||
match matched_variable {
|
||||
Some(variable) => {
|
||||
// Resolve and enforce circuit object
|
||||
let field_value = self.enforce_expression(
|
||||
let variable_value = self.enforce_expression(
|
||||
cs,
|
||||
file_scope.clone(),
|
||||
function_scope.clone(),
|
||||
Some(_type.clone()),
|
||||
field.expression,
|
||||
variable.expression,
|
||||
)?;
|
||||
|
||||
resolved_members.push(ConstrainedCircuitMember(identifier, field_value))
|
||||
resolved_members.push(ConstrainedCircuitMember(identifier, variable_value))
|
||||
}
|
||||
None => return Err(ExpressionError::expected_circuit_member(identifier.to_string(), span)),
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
),
|
||||
Assignee::Tuple(_tuple, index) => self.assign_tuple(cs, indicator, variable_name, index, new_value, span),
|
||||
Assignee::CircuitField(_assignee, object_name) => {
|
||||
self.mutute_circuit_field(cs, indicator, variable_name, object_name, new_value, span)
|
||||
self.mutute_circuit_variable(cs, indicator, variable_name, object_name, new_value, span)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! Enforces a circuit field assignment statement in a compiled Leo program.
|
||||
//! Enforces a circuit variable assignment statement in a compiled Leo program.
|
||||
|
||||
use crate::{errors::StatementError, program::ConstrainedProgram, value::ConstrainedValue, GroupType};
|
||||
use leo_typed::{Identifier, Span};
|
||||
@ -28,7 +28,7 @@ use snarkos_models::{
|
||||
};
|
||||
|
||||
impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
pub fn mutute_circuit_field<CS: ConstraintSystem<F>>(
|
||||
pub fn mutute_circuit_variable<CS: ConstraintSystem<F>>(
|
||||
&mut self,
|
||||
cs: &mut CS,
|
||||
indicator: Option<Boolean>,
|
||||
@ -41,10 +41,10 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
|
||||
|
||||
match self.get_mutable_assignee(circuit_name, span.clone())? {
|
||||
ConstrainedValue::CircuitExpression(_variable, members) => {
|
||||
// Modify the circuit field in place
|
||||
let matched_field = members.into_iter().find(|object| object.0 == object_name);
|
||||
// Modify the circuit variable in place
|
||||
let matched_variable = members.into_iter().find(|object| object.0 == object_name);
|
||||
|
||||
match matched_field {
|
||||
match matched_variable {
|
||||
Some(object) => match &object.1 {
|
||||
ConstrainedValue::Function(_circuit_identifier, function) => {
|
||||
return Err(StatementError::immutable_circuit_function(
|
@ -25,8 +25,8 @@ pub use self::assign::*;
|
||||
pub mod assignee;
|
||||
pub use self::assignee::*;
|
||||
|
||||
pub mod circuit_field;
|
||||
pub use self::circuit_field::*;
|
||||
pub mod circuit_variable;
|
||||
pub use self::circuit_variable::*;
|
||||
|
||||
pub mod tuple;
|
||||
pub use self::tuple::*;
|
||||
|
@ -55,24 +55,24 @@ fn test_inline_undefined() {
|
||||
// Members
|
||||
|
||||
#[test]
|
||||
fn test_member_field() {
|
||||
let bytes = include_bytes!("member_field.leo");
|
||||
fn test_member_variable() {
|
||||
let bytes = include_bytes!("member_variable.leo");
|
||||
let program = parse_program(bytes).unwrap();
|
||||
|
||||
assert_satisfied(program);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_member_field_fail() {
|
||||
let bytes = include_bytes!("member_field_fail.leo");
|
||||
fn test_member_variable_fail() {
|
||||
let bytes = include_bytes!("member_variable_fail.leo");
|
||||
let program = parse_program(bytes).unwrap();
|
||||
|
||||
expect_fail(program);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_member_field_and_function() {
|
||||
let bytes = include_bytes!("member_field_and_function.leo");
|
||||
fn test_member_variable_and_function() {
|
||||
let bytes = include_bytes!("member_variable_and_function.leo");
|
||||
let program = parse_program(bytes).unwrap();
|
||||
|
||||
assert_satisfied(program);
|
||||
|
@ -16,9 +16,9 @@
|
||||
|
||||
use crate::{Function, Identifier, Type};
|
||||
use leo_ast::circuits::{
|
||||
CircuitFieldDefinition as AstCircuitFieldDefinition,
|
||||
CircuitFunction as AstCircuitFunction,
|
||||
CircuitMember as AstCircuitMember,
|
||||
CircuitVariableDefinition as AstCircuitVariableDefinition,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -26,13 +26,13 @@ use std::fmt;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum CircuitMember {
|
||||
CircuitField(Identifier, Type),
|
||||
CircuitVariable(Identifier, Type),
|
||||
CircuitFunction(bool, Function),
|
||||
}
|
||||
|
||||
impl<'ast> From<AstCircuitFieldDefinition<'ast>> for CircuitMember {
|
||||
fn from(circuit_value: AstCircuitFieldDefinition<'ast>) -> Self {
|
||||
CircuitMember::CircuitField(
|
||||
impl<'ast> From<AstCircuitVariableDefinition<'ast>> for CircuitMember {
|
||||
fn from(circuit_value: AstCircuitVariableDefinition<'ast>) -> Self {
|
||||
CircuitMember::CircuitVariable(
|
||||
Identifier::from(circuit_value.identifier),
|
||||
Type::from(circuit_value._type),
|
||||
)
|
||||
@ -51,7 +51,7 @@ impl<'ast> From<AstCircuitFunction<'ast>> for CircuitMember {
|
||||
impl<'ast> From<AstCircuitMember<'ast>> for CircuitMember {
|
||||
fn from(object: AstCircuitMember<'ast>) -> Self {
|
||||
match object {
|
||||
AstCircuitMember::CircuitFieldDefinition(circuit_value) => CircuitMember::from(circuit_value),
|
||||
AstCircuitMember::CircuitVariableDefinition(circuit_value) => CircuitMember::from(circuit_value),
|
||||
AstCircuitMember::CircuitFunction(circuit_function) => CircuitMember::from(circuit_function),
|
||||
}
|
||||
}
|
||||
@ -60,7 +60,7 @@ impl<'ast> From<AstCircuitMember<'ast>> for CircuitMember {
|
||||
impl fmt::Display for CircuitMember {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
CircuitMember::CircuitField(ref identifier, ref _type) => write!(f, "{}: {}", identifier, _type),
|
||||
CircuitMember::CircuitVariable(ref identifier, ref _type) => write!(f, "{}: {}", identifier, _type),
|
||||
CircuitMember::CircuitFunction(ref _static, ref function) => {
|
||||
if *_static {
|
||||
write!(f, "static ")?;
|
||||
|
@ -15,19 +15,19 @@
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{Expression, Identifier};
|
||||
use leo_ast::circuits::CircuitField;
|
||||
use leo_ast::circuits::CircuitVariable;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct CircuitFieldDefinition {
|
||||
pub struct CircuitVariableDefinition {
|
||||
pub identifier: Identifier,
|
||||
pub expression: Expression,
|
||||
}
|
||||
|
||||
impl<'ast> From<CircuitField<'ast>> for CircuitFieldDefinition {
|
||||
fn from(member: CircuitField<'ast>) -> Self {
|
||||
CircuitFieldDefinition {
|
||||
impl<'ast> From<CircuitVariable<'ast>> for CircuitVariableDefinition {
|
||||
fn from(member: CircuitVariable<'ast>) -> Self {
|
||||
CircuitVariableDefinition {
|
||||
identifier: Identifier::from(member.identifier),
|
||||
expression: Expression::from(member.expression),
|
||||
}
|
@ -17,8 +17,8 @@
|
||||
pub mod circuit;
|
||||
pub use circuit::*;
|
||||
|
||||
pub mod circuit_field_definition;
|
||||
pub use circuit_field_definition::*;
|
||||
pub mod circuit_variable_definition;
|
||||
pub use circuit_variable_definition::*;
|
||||
|
||||
pub mod circuit_member;
|
||||
pub use circuit_member::*;
|
||||
|
@ -53,8 +53,8 @@ impl<'ast> From<AstAssignee<'ast>> for Assignee {
|
||||
AstAssigneeAccess::Tuple(tuple) => {
|
||||
Assignee::Tuple(Box::new(acc), Expression::get_count_from_ast(tuple.number))
|
||||
}
|
||||
AstAssigneeAccess::Member(circuit_field) => {
|
||||
Assignee::CircuitField(Box::new(acc), Identifier::from(circuit_field.identifier))
|
||||
AstAssigneeAccess::Member(circuit_variable) => {
|
||||
Assignee::CircuitField(Box::new(acc), Identifier::from(circuit_variable.identifier))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -14,7 +14,15 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::{CircuitFieldDefinition, GroupValue, Identifier, IntegerType, RangeOrExpression, Span, SpreadOrExpression};
|
||||
use crate::{
|
||||
CircuitVariableDefinition,
|
||||
GroupValue,
|
||||
Identifier,
|
||||
IntegerType,
|
||||
RangeOrExpression,
|
||||
Span,
|
||||
SpreadOrExpression,
|
||||
};
|
||||
use leo_ast::{
|
||||
access::{Access, AssigneeAccess},
|
||||
common::{Assignee, Identifier as AstIdentifier},
|
||||
@ -90,7 +98,7 @@ pub enum Expression {
|
||||
TupleAccess(Box<Expression>, usize, Span),
|
||||
|
||||
// Circuits
|
||||
Circuit(Identifier, Vec<CircuitFieldDefinition>, Span),
|
||||
Circuit(Identifier, Vec<CircuitVariableDefinition>, Span),
|
||||
CircuitMemberAccess(Box<Expression>, Identifier, Span), // (declared circuit name, circuit member name)
|
||||
CircuitStaticFunctionAccess(Box<Expression>, Identifier, Span), // (defined circuit name, circuit static member name)
|
||||
|
||||
@ -271,8 +279,8 @@ impl<'ast> From<CircuitInlineExpression<'ast>> for Expression {
|
||||
let members = expression
|
||||
.members
|
||||
.into_iter()
|
||||
.map(|member| CircuitFieldDefinition::from(member))
|
||||
.collect::<Vec<CircuitFieldDefinition>>();
|
||||
.map(|member| CircuitVariableDefinition::from(member))
|
||||
.collect::<Vec<CircuitVariableDefinition>>();
|
||||
|
||||
Expression::Circuit(circuit_name, members, Span::from(expression.span))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user