mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-09-11 05:45:30 +03:00
change type struct -> circuit
This commit is contained in:
parent
5046db8e2c
commit
91d241a5d8
@ -2,11 +2,11 @@
|
||||
|
||||
use crate::{
|
||||
constraints::{
|
||||
new_scope_from_variable, new_variable_from_variable, ConstrainedProgram,
|
||||
ConstrainedStructMember, ConstrainedValue,
|
||||
new_scope_from_variable, new_variable_from_variable, ConstrainedCircuitMember,
|
||||
ConstrainedProgram, ConstrainedValue,
|
||||
},
|
||||
errors::ExpressionError,
|
||||
types::{Expression, RangeOrExpression, SpreadOrExpression, StructMember, Variable},
|
||||
types::{CircuitMember, Expression, RangeOrExpression, SpreadOrExpression, Variable},
|
||||
};
|
||||
|
||||
use snarkos_models::{
|
||||
@ -28,7 +28,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
||||
// Reassigning variable to another variable
|
||||
Ok(self.get_mut(&variable_name).unwrap().clone())
|
||||
} else if self.contains_variable(&unresolved_variable) {
|
||||
// Check global scope (function and struct names)
|
||||
// Check global scope (function and circuit names)
|
||||
Ok(self.get_mut_variable(&unresolved_variable).unwrap().clone())
|
||||
} else {
|
||||
Err(ExpressionError::UndefinedVariable(
|
||||
@ -335,33 +335,33 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
||||
}
|
||||
}
|
||||
|
||||
fn enforce_struct_expression(
|
||||
fn enforce_circuit_expression(
|
||||
&mut self,
|
||||
cs: &mut CS,
|
||||
file_scope: String,
|
||||
function_scope: String,
|
||||
variable: Variable<F, G>,
|
||||
members: Vec<StructMember<F, G>>,
|
||||
members: Vec<CircuitMember<F, G>>,
|
||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
||||
let struct_name = new_variable_from_variable(file_scope.clone(), &variable);
|
||||
let circuit_name = new_variable_from_variable(file_scope.clone(), &variable);
|
||||
|
||||
if let Some(ConstrainedValue::StructDefinition(struct_definition)) =
|
||||
self.get_mut_variable(&struct_name)
|
||||
if let Some(ConstrainedValue::CircuitDefinition(circuit_definition)) =
|
||||
self.get_mut_variable(&circuit_name)
|
||||
{
|
||||
let mut resolved_members = vec![];
|
||||
for (field, member) in struct_definition
|
||||
for (field, member) in circuit_definition
|
||||
.fields
|
||||
.clone()
|
||||
.into_iter()
|
||||
.zip(members.clone().into_iter())
|
||||
{
|
||||
if field.variable != member.variable {
|
||||
return Err(ExpressionError::InvalidStructField(
|
||||
return Err(ExpressionError::InvalidCircuitObject(
|
||||
field.variable.name,
|
||||
member.variable.name,
|
||||
));
|
||||
}
|
||||
// Resolve and enforce struct fields
|
||||
// Resolve and enforce circuit fields
|
||||
let member_value = self.enforce_expression(
|
||||
cs,
|
||||
file_scope.clone(),
|
||||
@ -372,37 +372,39 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
||||
// Check member types
|
||||
member_value.expect_type(&field._type)?;
|
||||
|
||||
resolved_members.push(ConstrainedStructMember(member.variable, member_value))
|
||||
resolved_members.push(ConstrainedCircuitMember(member.variable, member_value))
|
||||
}
|
||||
|
||||
Ok(ConstrainedValue::StructExpression(
|
||||
Ok(ConstrainedValue::CircuitExpression(
|
||||
variable,
|
||||
resolved_members,
|
||||
))
|
||||
} else {
|
||||
Err(ExpressionError::UndefinedStruct(variable.to_string()))
|
||||
Err(ExpressionError::UndefinedCircuit(variable.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
fn enforce_struct_access_expression(
|
||||
fn enforce_circuit_access_expression(
|
||||
&mut self,
|
||||
cs: &mut CS,
|
||||
file_scope: String,
|
||||
function_scope: String,
|
||||
struct_variable: Box<Expression<F, G>>,
|
||||
struct_member: Variable<F, G>,
|
||||
circuit_variable: Box<Expression<F, G>>,
|
||||
circuit_member: Variable<F, G>,
|
||||
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
|
||||
match self.enforce_expression(cs, file_scope, function_scope, *struct_variable)? {
|
||||
ConstrainedValue::StructExpression(_name, members) => {
|
||||
let matched_member = members.into_iter().find(|member| member.0 == struct_member);
|
||||
match self.enforce_expression(cs, file_scope, function_scope, *circuit_variable)? {
|
||||
ConstrainedValue::CircuitExpression(_name, members) => {
|
||||
let matched_member = members
|
||||
.into_iter()
|
||||
.find(|member| member.0 == circuit_member);
|
||||
match matched_member {
|
||||
Some(member) => Ok(member.1),
|
||||
None => Err(ExpressionError::UndefinedStructField(
|
||||
struct_member.to_string(),
|
||||
None => Err(ExpressionError::UndefinedCircuitObject(
|
||||
circuit_member.to_string(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
value => Err(ExpressionError::InvalidStructAccess(value.to_string())),
|
||||
value => Err(ExpressionError::InvalidCircuitAccess(value.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
@ -633,17 +635,21 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
||||
self.enforce_array_access_expression(cs, file_scope, function_scope, array, *index)
|
||||
}
|
||||
|
||||
// Structs
|
||||
Expression::Struct(struct_name, members) => {
|
||||
self.enforce_struct_expression(cs, file_scope, function_scope, struct_name, members)
|
||||
}
|
||||
Expression::StructMemberAccess(struct_variable, struct_member) => self
|
||||
.enforce_struct_access_expression(
|
||||
// Circuits
|
||||
Expression::Circuit(circuit_name, members) => self.enforce_circuit_expression(
|
||||
cs,
|
||||
file_scope,
|
||||
function_scope,
|
||||
circuit_name,
|
||||
members,
|
||||
),
|
||||
Expression::CircuitMemberAccess(circuit_variable, circuit_member) => self
|
||||
.enforce_circuit_access_expression(
|
||||
cs,
|
||||
file_scope,
|
||||
function_scope,
|
||||
struct_variable,
|
||||
struct_member,
|
||||
circuit_variable,
|
||||
circuit_member,
|
||||
),
|
||||
|
||||
// Functions
|
||||
|
@ -229,16 +229,16 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
||||
.map(|import| self.enforce_import(cs, program_name.name.clone(), import))
|
||||
.collect::<Result<Vec<_>, ImportError>>()?;
|
||||
|
||||
// evaluate and store all struct definitions
|
||||
// evaluate and store all circuit definitions
|
||||
program
|
||||
.structs
|
||||
.circuits
|
||||
.into_iter()
|
||||
.for_each(|(variable, struct_def)| {
|
||||
let resolved_struct_name =
|
||||
.for_each(|(variable, circuit_def)| {
|
||||
let resolved_circuit_name =
|
||||
new_variable_from_variables(&program_name.clone(), &variable);
|
||||
self.store_variable(
|
||||
resolved_struct_name,
|
||||
ConstrainedValue::StructDefinition(struct_def),
|
||||
resolved_circuit_name,
|
||||
ConstrainedValue::CircuitDefinition(circuit_def),
|
||||
);
|
||||
});
|
||||
|
||||
|
@ -36,7 +36,7 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
||||
// Use same namespace as calling function for imported symbols
|
||||
program = program.name(scope);
|
||||
|
||||
// * -> import all imports, structs, functions in the current scope
|
||||
// * -> import all imports, circuits, functions in the current scope
|
||||
if import.is_star() {
|
||||
// recursively evaluate program statements
|
||||
self.resolve_definitions(cs, program).unwrap();
|
||||
@ -47,24 +47,24 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
||||
|
||||
// match each import symbol to a symbol in the imported file
|
||||
import.symbols.into_iter().for_each(|symbol| {
|
||||
// see if the imported symbol is a struct
|
||||
let matched_struct = program
|
||||
.structs
|
||||
// see if the imported symbol is a circuit
|
||||
let matched_circuit = program
|
||||
.circuits
|
||||
.clone()
|
||||
.into_iter()
|
||||
.find(|(struct_name, _struct_def)| symbol.symbol == *struct_name);
|
||||
.find(|(circuit_name, _circuit_def)| symbol.symbol == *circuit_name);
|
||||
|
||||
match matched_struct {
|
||||
Some((_struct_name, struct_def)) => {
|
||||
match matched_circuit {
|
||||
Some((_circuit_name, circuit_def)) => {
|
||||
// take the alias if it is present
|
||||
let resolved_name = symbol.alias.unwrap_or(symbol.symbol);
|
||||
let resolved_struct_name =
|
||||
let resolved_circuit_name =
|
||||
new_variable_from_variables(&program_name.clone(), &resolved_name);
|
||||
|
||||
// store imported struct under resolved name
|
||||
// store imported circuit under resolved name
|
||||
self.store_variable(
|
||||
resolved_struct_name,
|
||||
ConstrainedValue::StructDefinition(struct_def),
|
||||
resolved_circuit_name,
|
||||
ConstrainedValue::CircuitDefinition(circuit_def),
|
||||
);
|
||||
}
|
||||
None => {
|
||||
|
@ -19,8 +19,8 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
||||
match assignee {
|
||||
Assignee::Variable(name) => new_scope_from_variable(scope, &name),
|
||||
Assignee::Array(array, _index) => self.resolve_assignee(scope, *array),
|
||||
Assignee::StructMember(struct_variable, _member) => {
|
||||
self.resolve_assignee(scope, *struct_variable)
|
||||
Assignee::CircuitMember(circuit_variable, _member) => {
|
||||
self.resolve_assignee(scope, *circuit_variable)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -93,26 +93,27 @@ impl<F: Field + PrimeField, G: Group, CS: ConstraintSystem<F>> ConstrainedProgra
|
||||
}
|
||||
}
|
||||
}
|
||||
Assignee::StructMember(struct_variable, struct_member) => {
|
||||
// Check that struct exists
|
||||
let expected_struct_name =
|
||||
self.resolve_assignee(function_scope.clone(), *struct_variable);
|
||||
Assignee::CircuitMember(circuit_variable, circuit_member) => {
|
||||
// Check that circuit exists
|
||||
let expected_circuit_name =
|
||||
self.resolve_assignee(function_scope.clone(), *circuit_variable);
|
||||
|
||||
match self.get_mut(&expected_struct_name) {
|
||||
Some(ConstrainedValue::StructExpression(_variable, members)) => {
|
||||
// Modify the struct member in place
|
||||
let matched_member =
|
||||
members.into_iter().find(|member| member.0 == struct_member);
|
||||
match self.get_mut(&expected_circuit_name) {
|
||||
Some(ConstrainedValue::CircuitExpression(_variable, members)) => {
|
||||
// Modify the circuit member in place
|
||||
let matched_member = members
|
||||
.into_iter()
|
||||
.find(|member| member.0 == circuit_member);
|
||||
match matched_member {
|
||||
Some(mut member) => member.1 = return_value.to_owned(),
|
||||
None => {
|
||||
return Err(StatementError::UndefinedStructField(
|
||||
struct_member.to_string(),
|
||||
return Err(StatementError::UndefinedCircuitObject(
|
||||
circuit_member.to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => return Err(StatementError::UndefinedStruct(expected_struct_name)),
|
||||
_ => return Err(StatementError::UndefinedCircuit(expected_circuit_name)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
use crate::{
|
||||
errors::ValueError,
|
||||
types::{FieldElement, Function, Struct, Type, Variable},
|
||||
types::{Circuit, FieldElement, Function, Type, Variable},
|
||||
Integer,
|
||||
};
|
||||
|
||||
@ -13,7 +13,7 @@ use snarkos_models::{
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct ConstrainedStructMember<F: Field + PrimeField, G: Group>(
|
||||
pub struct ConstrainedCircuitMember<F: Field + PrimeField, G: Group>(
|
||||
pub Variable<F, G>,
|
||||
pub ConstrainedValue<F, G>,
|
||||
);
|
||||
@ -25,8 +25,8 @@ pub enum ConstrainedValue<F: Field + PrimeField, G: Group> {
|
||||
GroupElement(G),
|
||||
Boolean(Boolean),
|
||||
Array(Vec<ConstrainedValue<F, G>>),
|
||||
StructDefinition(Struct<F, G>),
|
||||
StructExpression(Variable<F, G>, Vec<ConstrainedStructMember<F, G>>),
|
||||
CircuitDefinition(Circuit<F, G>),
|
||||
CircuitExpression(Variable<F, G>, Vec<ConstrainedCircuitMember<F, G>>),
|
||||
Function(Function<F, G>),
|
||||
Return(Vec<ConstrainedValue<F, G>>), // add Null for function returns
|
||||
}
|
||||
@ -58,8 +58,8 @@ impl<F: Field + PrimeField, G: Group> ConstrainedValue<F, G> {
|
||||
}
|
||||
}
|
||||
(
|
||||
ConstrainedValue::StructExpression(ref actual_name, ref _members),
|
||||
Type::Struct(ref expected_name),
|
||||
ConstrainedValue::CircuitExpression(ref actual_name, ref _members),
|
||||
Type::Circuit(ref expected_name),
|
||||
) => {
|
||||
if expected_name != actual_name {
|
||||
return Err(ValueError::StructName(format!(
|
||||
@ -102,7 +102,7 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for ConstrainedValue<F, G> {
|
||||
}
|
||||
write!(f, "]")
|
||||
}
|
||||
ConstrainedValue::StructExpression(ref variable, ref members) => {
|
||||
ConstrainedValue::CircuitExpression(ref variable, ref members) => {
|
||||
write!(f, "{} {{", variable)?;
|
||||
for (i, member) in members.iter().enumerate() {
|
||||
write!(f, "{}: {}", member.0, member.1)?;
|
||||
@ -122,7 +122,7 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for ConstrainedValue<F, G> {
|
||||
}
|
||||
write!(f, "]")
|
||||
}
|
||||
ConstrainedValue::StructDefinition(ref _definition) => {
|
||||
ConstrainedValue::CircuitDefinition(ref _definition) => {
|
||||
unimplemented!("cannot return struct definition in program")
|
||||
}
|
||||
ConstrainedValue::Function(ref function) => write!(f, "{}();", function.function_name),
|
||||
|
@ -41,21 +41,21 @@ pub enum ExpressionError {
|
||||
#[error("Index must resolve to an integer, got {}", _0)]
|
||||
InvalidIndex(String),
|
||||
|
||||
// Structs
|
||||
// Circuits
|
||||
#[error(
|
||||
"Struct {} must be declared before it is used in an inline expression",
|
||||
"Circuit {} must be declared before it is used in an inline expression",
|
||||
_0
|
||||
)]
|
||||
UndefinedStruct(String),
|
||||
UndefinedCircuit(String),
|
||||
|
||||
#[error("Struct field {} does not exist", _0)]
|
||||
UndefinedStructField(String),
|
||||
#[error("Circuit object {} does not exist", _0)]
|
||||
UndefinedCircuitObject(String),
|
||||
|
||||
#[error("Expected struct field {}, got {}", _0, _1)]
|
||||
InvalidStructField(String, String),
|
||||
#[error("Expected circuit object {}, got {}", _0, _1)]
|
||||
InvalidCircuitObject(String, String),
|
||||
|
||||
#[error("Cannot access struct {}", _0)]
|
||||
InvalidStructAccess(String),
|
||||
#[error("Cannot access circuit {}", _0)]
|
||||
InvalidCircuitAccess(String),
|
||||
|
||||
// Functions
|
||||
#[error(
|
||||
|
@ -29,11 +29,11 @@ pub enum StatementError {
|
||||
#[error("Cannot assign to unknown array {}", _0)]
|
||||
UndefinedArray(String),
|
||||
|
||||
#[error("Attempted to assign to unknown struct {}", _0)]
|
||||
UndefinedStruct(String),
|
||||
#[error("Attempted to assign to unknown circuit {}", _0)]
|
||||
UndefinedCircuit(String),
|
||||
|
||||
#[error("Attempted to assign to unknown struct field {}", _0)]
|
||||
UndefinedStructField(String),
|
||||
#[error("Attempted to assign to unknown circuit {}", _0)]
|
||||
UndefinedCircuitObject(String),
|
||||
|
||||
#[error("Function return statement expected {} return values, got {}", _0, _1)]
|
||||
InvalidNumberOfReturns(usize, usize),
|
||||
|
@ -145,9 +145,9 @@ pub enum Expression<F: Field + PrimeField, G: Group> {
|
||||
Array(Vec<Box<SpreadOrExpression<F, G>>>),
|
||||
ArrayAccess(Box<Expression<F, G>>, Box<RangeOrExpression<F, G>>), // (array name, range)
|
||||
|
||||
// Structs
|
||||
Struct(Variable<F, G>, Vec<StructMember<F, G>>),
|
||||
StructMemberAccess(Box<Expression<F, G>>, Variable<F, G>), // (struct name, struct member name)
|
||||
// Circuits
|
||||
Circuit(Variable<F, G>, Vec<CircuitMember<F, G>>),
|
||||
CircuitMemberAccess(Box<Expression<F, G>>, Variable<F, G>), // (circuit name, circuit member name)
|
||||
|
||||
// Functions
|
||||
FunctionCall(Variable<F, G>, Vec<Expression<F, G>>),
|
||||
@ -158,7 +158,7 @@ pub enum Expression<F: Field + PrimeField, G: Group> {
|
||||
pub enum Assignee<F: Field + PrimeField, G: Group> {
|
||||
Variable(Variable<F, G>),
|
||||
Array(Box<Assignee<F, G>>, RangeOrExpression<F, G>),
|
||||
StructMember(Box<Assignee<F, G>>, Variable<F, G>),
|
||||
CircuitMember(Box<Assignee<F, G>>, Variable<F, G>),
|
||||
}
|
||||
|
||||
/// Explicit integer type
|
||||
@ -179,7 +179,7 @@ pub enum Type<F: Field + PrimeField, G: Group> {
|
||||
GroupElement,
|
||||
Boolean,
|
||||
Array(Box<Type<F, G>>, Vec<usize>),
|
||||
Struct(Variable<F, G>),
|
||||
Circuit(Variable<F, G>),
|
||||
}
|
||||
|
||||
impl<F: Field + PrimeField, G: Group> Type<F, G> {
|
||||
@ -225,21 +225,21 @@ pub enum Statement<F: Field + PrimeField, G: Group> {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct StructMember<F: Field + PrimeField, G: Group> {
|
||||
pub struct CircuitMember<F: Field + PrimeField, G: Group> {
|
||||
pub variable: Variable<F, G>,
|
||||
pub expression: Expression<F, G>,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct StructField<F: Field + PrimeField, G: Group> {
|
||||
pub struct CircuitObject<F: Field + PrimeField, G: Group> {
|
||||
pub variable: Variable<F, G>,
|
||||
pub _type: Type<F, G>,
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct Struct<F: Field + PrimeField, G: Group> {
|
||||
pub struct Circuit<F: Field + PrimeField, G: Group> {
|
||||
pub variable: Variable<F, G>,
|
||||
pub fields: Vec<StructField<F, G>>,
|
||||
pub fields: Vec<CircuitObject<F, G>>,
|
||||
}
|
||||
|
||||
/// Function parameters
|
||||
@ -293,7 +293,7 @@ pub struct Program<F: Field + PrimeField, G: Group> {
|
||||
pub name: Variable<F, G>,
|
||||
pub num_parameters: usize,
|
||||
pub imports: Vec<Import<F, G>>,
|
||||
pub structs: HashMap<Variable<F, G>, Struct<F, G>>,
|
||||
pub circuits: HashMap<Variable<F, G>, Circuit<F, G>>,
|
||||
pub functions: HashMap<FunctionName, Function<F, G>>,
|
||||
}
|
||||
|
||||
@ -307,7 +307,7 @@ impl<'ast, F: Field + PrimeField, G: Group> Program<F, G> {
|
||||
},
|
||||
num_parameters: 0,
|
||||
imports: vec![],
|
||||
structs: HashMap::new(),
|
||||
circuits: HashMap::new(),
|
||||
functions: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
//! Format display functions for Leo types.
|
||||
|
||||
use crate::{
|
||||
Assignee, ConditionalNestedOrEnd, ConditionalStatement, Expression, FieldElement, Function,
|
||||
FunctionName, InputModel, InputValue, Integer, IntegerType, RangeOrExpression,
|
||||
SpreadOrExpression, Statement, Struct, StructField, Type, Variable,
|
||||
Assignee, Circuit, CircuitObject, ConditionalNestedOrEnd, ConditionalStatement, Expression,
|
||||
FieldElement, Function, FunctionName, InputModel, InputValue, Integer, IntegerType,
|
||||
RangeOrExpression, SpreadOrExpression, Statement, Type, Variable,
|
||||
};
|
||||
|
||||
use snarkos_models::curves::{Field, Group, PrimeField};
|
||||
@ -127,8 +127,8 @@ impl<'ast, F: Field + PrimeField, G: Group> fmt::Display for Expression<F, G> {
|
||||
}
|
||||
Expression::ArrayAccess(ref array, ref index) => write!(f, "{}[{}]", array, index),
|
||||
|
||||
// Structs
|
||||
Expression::Struct(ref var, ref members) => {
|
||||
// Circuits
|
||||
Expression::Circuit(ref var, ref members) => {
|
||||
write!(f, "{} {{", var)?;
|
||||
for (i, member) in members.iter().enumerate() {
|
||||
write!(f, "{}: {}", member.variable, member.expression)?;
|
||||
@ -138,8 +138,8 @@ impl<'ast, F: Field + PrimeField, G: Group> fmt::Display for Expression<F, G> {
|
||||
}
|
||||
write!(f, "}}")
|
||||
}
|
||||
Expression::StructMemberAccess(ref struct_variable, ref member) => {
|
||||
write!(f, "{}.{}", struct_variable, member)
|
||||
Expression::CircuitMemberAccess(ref circuit_variable, ref member) => {
|
||||
write!(f, "{}.{}", circuit_variable, member)
|
||||
}
|
||||
|
||||
// Function calls
|
||||
@ -162,8 +162,8 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for Assignee<F, G> {
|
||||
match *self {
|
||||
Assignee::Variable(ref variable) => write!(f, "{}", variable),
|
||||
Assignee::Array(ref array, ref index) => write!(f, "{}[{}]", array, index),
|
||||
Assignee::StructMember(ref struct_variable, ref member) => {
|
||||
write!(f, "{}.{}", struct_variable, member)
|
||||
Assignee::CircuitMember(ref circuit_variable, ref member) => {
|
||||
write!(f, "{}.{}", circuit_variable, member)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -262,7 +262,7 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for Type<F, G> {
|
||||
Type::FieldElement => write!(f, "field"),
|
||||
Type::GroupElement => write!(f, "group"),
|
||||
Type::Boolean => write!(f, "bool"),
|
||||
Type::Struct(ref variable) => write!(f, "{}", variable),
|
||||
Type::Circuit(ref variable) => write!(f, "{}", variable),
|
||||
Type::Array(ref array, ref dimensions) => {
|
||||
write!(f, "{}", *array)?;
|
||||
for row in dimensions {
|
||||
@ -274,15 +274,15 @@ impl<F: Field + PrimeField, G: Group> fmt::Display for Type<F, G> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Field + PrimeField, G: Group> fmt::Display for StructField<F, G> {
|
||||
impl<F: Field + PrimeField, G: Group> fmt::Display for CircuitObject<F, G> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}: {}", self.variable, self._type)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: Field + PrimeField, G: Group> Struct<F, G> {
|
||||
impl<F: Field + PrimeField, G: Group> Circuit<F, G> {
|
||||
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "struct {} {{ \n", self.variable)?;
|
||||
write!(f, "circuit {} {{ \n", self.variable)?;
|
||||
for field in self.fields.iter() {
|
||||
write!(f, " {}\n", field)?;
|
||||
}
|
||||
@ -296,7 +296,7 @@ impl<F: Field + PrimeField, G: Group> Struct<F, G> {
|
||||
// }
|
||||
// }
|
||||
|
||||
impl<F: Field + PrimeField, G: Group> fmt::Debug for Struct<F, G> {
|
||||
impl<F: Field + PrimeField, G: Group> fmt::Debug for Circuit<F, G> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.format(f)
|
||||
}
|
||||
|
@ -264,10 +264,10 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::ArrayInitializerExpression
|
||||
}
|
||||
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::InlineCircuitMember<'ast>>
|
||||
for types::StructMember<F, G>
|
||||
for types::CircuitMember<F, G>
|
||||
{
|
||||
fn from(member: ast::InlineCircuitMember<'ast>) -> Self {
|
||||
types::StructMember {
|
||||
types::CircuitMember {
|
||||
variable: types::Variable::from(member.variable),
|
||||
expression: types::Expression::from(member.expression),
|
||||
}
|
||||
@ -282,10 +282,10 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitInlineExpression<'a
|
||||
let members = expression
|
||||
.members
|
||||
.into_iter()
|
||||
.map(|member| types::StructMember::from(member))
|
||||
.collect::<Vec<types::StructMember<F, G>>>();
|
||||
.map(|member| types::CircuitMember::from(member))
|
||||
.collect::<Vec<types::CircuitMember<F, G>>>();
|
||||
|
||||
types::Expression::Struct(variable, members)
|
||||
types::Expression::Circuit(variable, members)
|
||||
}
|
||||
}
|
||||
|
||||
@ -316,7 +316,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::PostfixExpression<'ast>>
|
||||
unimplemented!("only function names are callable, found \"{}\"", expression)
|
||||
}
|
||||
},
|
||||
ast::Access::Member(struct_member) => types::Expression::StructMemberAccess(
|
||||
ast::Access::Member(struct_member) => types::Expression::CircuitMemberAccess(
|
||||
Box::new(acc),
|
||||
types::Variable::from(struct_member.variable),
|
||||
),
|
||||
@ -370,7 +370,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Assignee<'ast>> for types:
|
||||
.into_iter()
|
||||
.fold(variable, |acc, access| match access {
|
||||
ast::AssigneeAccess::Member(struct_member) => {
|
||||
types::Expression::StructMemberAccess(
|
||||
types::Expression::CircuitMemberAccess(
|
||||
Box::new(acc),
|
||||
types::Variable::from(struct_member.variable),
|
||||
)
|
||||
@ -404,7 +404,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Assignee<'ast>> for types:
|
||||
Box::new(acc),
|
||||
types::RangeOrExpression::from(array.expression),
|
||||
),
|
||||
ast::AssigneeAccess::Member(struct_member) => types::Assignee::StructMember(
|
||||
ast::AssigneeAccess::Member(struct_member) => types::Assignee::CircuitMember(
|
||||
Box::new(acc),
|
||||
types::Variable::from(struct_member.variable),
|
||||
),
|
||||
@ -667,7 +667,7 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::ArrayType<'ast>> for types
|
||||
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitType<'ast>> for types::Type<F, G> {
|
||||
fn from(struct_type: ast::CircuitType<'ast>) -> Self {
|
||||
types::Type::Struct(types::Variable::from(struct_type.variable))
|
||||
types::Type::Circuit(types::Variable::from(struct_type.variable))
|
||||
}
|
||||
}
|
||||
|
||||
@ -684,26 +684,26 @@ impl<'ast, F: Field + PrimeField, G: Group> From<ast::Type<'ast>> for types::Typ
|
||||
/// pest ast -> types::Struct
|
||||
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::CircuitObject<'ast>>
|
||||
for types::StructField<F, G>
|
||||
for types::CircuitObject<F, G>
|
||||
{
|
||||
fn from(struct_field: ast::CircuitObject<'ast>) -> Self {
|
||||
types::StructField {
|
||||
types::CircuitObject {
|
||||
variable: types::Variable::from(struct_field.variable),
|
||||
_type: types::Type::from(struct_field._type),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Circuit<'ast>> for types::Struct<F, G> {
|
||||
impl<'ast, F: Field + PrimeField, G: Group> From<ast::Circuit<'ast>> for types::Circuit<F, G> {
|
||||
fn from(struct_definition: ast::Circuit<'ast>) -> Self {
|
||||
let variable = types::Variable::from(struct_definition.variable);
|
||||
let fields = struct_definition
|
||||
.fields
|
||||
.into_iter()
|
||||
.map(|struct_field| types::StructField::from(struct_field))
|
||||
.map(|struct_field| types::CircuitObject::from(struct_field))
|
||||
.collect();
|
||||
|
||||
types::Struct { variable, fields }
|
||||
types::Circuit { variable, fields }
|
||||
}
|
||||
}
|
||||
|
||||
@ -812,7 +812,7 @@ impl<'ast, F: Field + PrimeField, G: Group> types::Program<F, G> {
|
||||
file.structs.into_iter().for_each(|struct_def| {
|
||||
structs.insert(
|
||||
types::Variable::from(struct_def.variable.clone()),
|
||||
types::Struct::from(struct_def),
|
||||
types::Circuit::from(struct_def),
|
||||
);
|
||||
});
|
||||
file.functions.into_iter().for_each(|function_def| {
|
||||
@ -834,7 +834,7 @@ impl<'ast, F: Field + PrimeField, G: Group> types::Program<F, G> {
|
||||
},
|
||||
num_parameters,
|
||||
imports,
|
||||
structs,
|
||||
circuits: structs,
|
||||
functions,
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user