ready to return real errors, fixed Self outside circuit bugs

This commit is contained in:
gluax 2021-03-12 20:41:49 -05:00
parent 88166a64f9
commit 3a1c29e3b2
6 changed files with 586 additions and 381 deletions

1
Cargo.lock generated
View File

@ -1209,6 +1209,7 @@ dependencies = [
name = "leo-ast" name = "leo-ast"
version = "1.2.3" version = "1.2.3"
dependencies = [ dependencies = [
"anyhow",
"criterion", "criterion",
"indexmap", "indexmap",
"leo-input", "leo-input",

View File

@ -35,6 +35,9 @@ features = ["derive", "rc"]
[dependencies.serde_json] [dependencies.serde_json]
version = "1.0" version = "1.0"
[dependencies.anyhow]
version = "1.0"
[dependencies.thiserror] [dependencies.thiserror]
version = "1.0" version = "1.0"

View File

@ -61,6 +61,8 @@ pub use self::types::*;
mod node; mod node;
pub use node::*; pub use node::*;
use anyhow::Result;
/// The abstract syntax tree (AST) for a Leo program. /// The abstract syntax tree (AST) for a Leo program.
/// ///
/// The [`Ast`] type represents a Leo program as a series of recursive data types. /// The [`Ast`] type represents a Leo program as a series of recursive data types.
@ -78,6 +80,11 @@ impl Ast {
Self { ast: program } Self { ast: program }
} }
pub fn canonicalize(&mut self) -> Result<()> {
self.ast = ReconstructingDirector::new(Canonicalizer).reduce_program(self.as_repr())?;
Ok(())
}
/// Returns a reference to the inner program AST representation. /// Returns a reference to the inner program AST representation.
pub fn as_repr(&self) -> &Program { pub fn as_repr(&self) -> &Program {
&self.ast &self.ast

View File

@ -15,7 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::*; use crate::*;
// use indexmap::IndexMap; use anyhow::Result;
pub struct Canonicalizer; pub struct Canonicalizer;
@ -143,7 +143,7 @@ impl Canonicalizer {
} }
impl ReconstructingReducer for Canonicalizer { impl ReconstructingReducer for Canonicalizer {
fn reduce_type(&mut self, _type_: &Type, new: Type, in_circuit: bool) -> Type { fn reduce_type(&mut self, _type_: &Type, new: Type, in_circuit: bool) -> Result<Type> {
match new { match new {
Type::Array(_, mut dimensions) => { Type::Array(_, mut dimensions) => {
// TODO need to throw errors in all these. // TODO need to throw errors in all these.
@ -166,17 +166,22 @@ impl ReconstructingReducer for Canonicalizer {
next = array.clone(); next = array.clone();
} }
array Ok(array)
} }
Type::SelfType if !in_circuit => { Type::SelfType if !in_circuit => {
println!("Brrr bad Self"); println!("Brrr bad Self");
new.clone() Ok(new.clone())
} }
_ => new.clone(), _ => Ok(new.clone()),
} }
} }
fn reduce_array_init(&mut self, array_init: &ArrayInitExpression, element: Expression) -> ArrayInitExpression { fn reduce_array_init(
&mut self,
array_init: &ArrayInitExpression,
element: Expression,
_in_circuit: bool,
) -> Result<ArrayInitExpression> {
// TODO ERROR HERE if len is 0 // TODO ERROR HERE if len is 0
// if array_init.dimensions.0.len() == 0 { // if array_init.dimensions.0.len() == 0 {
@ -185,11 +190,11 @@ impl ReconstructingReducer for Canonicalizer {
let element = Box::new(element); let element = Box::new(element);
if array_init.dimensions.0.len() == 1 { if array_init.dimensions.0.len() == 1 {
return ArrayInitExpression { return Ok(ArrayInitExpression {
element, element,
dimensions: array_init.dimensions.clone(), dimensions: array_init.dimensions.clone(),
span: array_init.span.clone(), span: array_init.span.clone(),
}; });
} }
let mut dimensions = array_init.dimensions.clone(); let mut dimensions = array_init.dimensions.clone();
@ -214,14 +219,20 @@ impl ReconstructingReducer for Canonicalizer {
outer_element = Box::new(next.clone()); outer_element = Box::new(next.clone());
} }
ArrayInitExpression { Ok(ArrayInitExpression {
element: outer_element, element: outer_element,
dimensions: ArrayDimensions(vec![dimensions.remove_first().unwrap()]), dimensions: ArrayDimensions(vec![dimensions.remove_first().unwrap()]),
span: array_init.span.clone(), span: array_init.span.clone(),
} })
} }
fn reduce_assign(&mut self, assign: &AssignStatement, assignee: Assignee, value: Expression) -> AssignStatement { fn reduce_assign(
&mut self,
assign: &AssignStatement,
assignee: Assignee,
value: Expression,
_in_circuit: bool,
) -> Result<AssignStatement> {
match value { match value {
Expression::Value(value) => { Expression::Value(value) => {
let left = Box::new(Expression::Identifier(assignee.identifier.clone())); let left = Box::new(Expression::Identifier(assignee.identifier.clone()));
@ -251,14 +262,14 @@ impl ReconstructingReducer for Canonicalizer {
span: assign.span.clone(), span: assign.span.clone(),
}); });
AssignStatement { Ok(AssignStatement {
operation: assign.operation.clone(), operation: assign.operation.clone(),
assignee, assignee,
value, value,
span: assign.span.clone(), span: assign.span.clone(),
})
} }
} _ => Ok(assign.clone()),
_ => assign.clone(),
} }
} }
@ -271,29 +282,34 @@ impl ReconstructingReducer for Canonicalizer {
output: Option<Type>, output: Option<Type>,
block: Block, block: Block,
_in_circuit: bool, _in_circuit: bool,
) -> Function { ) -> Result<Function> {
let new_output = match output { let new_output = match output {
None => Some(Type::Tuple(vec![])), None => Some(Type::Tuple(vec![])),
_ => output, _ => output,
}; };
Function { Ok(Function {
identifier, identifier,
annotations, annotations,
input, input,
output: new_output, output: new_output,
block, block,
span: function.span.clone(), span: function.span.clone(),
} })
} }
fn reduce_circuit(&mut self, _circuit: &Circuit, circuit_name: Identifier, members: Vec<CircuitMember>) -> Circuit { fn reduce_circuit(
Circuit { &mut self,
_circuit: &Circuit,
circuit_name: Identifier,
members: Vec<CircuitMember>,
) -> Result<Circuit> {
Ok(Circuit {
circuit_name: circuit_name.clone(), circuit_name: circuit_name.clone(),
members: members members: members
.iter() .iter()
.map(|member| self.canonicalize_circuit_member(member, &circuit_name)) .map(|member| self.canonicalize_circuit_member(member, &circuit_name))
.collect(), .collect(),
} })
} }
} }

View File

@ -18,6 +18,8 @@
//! asg nodes and saving relevant information. //! asg nodes and saving relevant information.
use crate::*; use crate::*;
use anyhow::Result;
use indexmap::IndexMap;
pub struct ReconstructingDirector<R: ReconstructingReducer> { pub struct ReconstructingDirector<R: ReconstructingReducer> {
reducer: R, reducer: R,
@ -28,14 +30,20 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
Self { reducer } Self { reducer }
} }
pub fn reduce_type(&mut self, type_: &Type, in_circuit: bool) -> Type { pub fn reduce_type(&mut self, type_: &Type, in_circuit: bool) -> Result<Type> {
let new = match type_ { let new = match type_ {
// Data type wrappers
Type::Array(type_, dimensions) => { Type::Array(type_, dimensions) => {
Type::Array(Box::new(self.reduce_type(type_, in_circuit)), dimensions.clone()) Type::Array(Box::new(self.reduce_type(type_, in_circuit)?), dimensions.clone())
} }
Type::Tuple(types) => Type::Tuple(types.iter().map(|type_| self.reduce_type(type_, in_circuit)).collect()), Type::Tuple(types) => {
Type::Circuit(identifier) => Type::Circuit(self.reduce_identifier(identifier)), let mut reduced_types = vec![];
for type_ in types.iter() {
reduced_types.push(self.reduce_type(type_, in_circuit)?);
}
Type::Tuple(reduced_types)
}
Type::Circuit(identifier) => Type::Circuit(self.reduce_identifier(identifier)?),
_ => type_.clone(), _ => type_.clone(),
}; };
@ -43,60 +51,74 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
} }
// Expressions // Expressions
pub fn reduce_expression(&mut self, expression: &Expression) -> Expression { pub fn reduce_expression(&mut self, expression: &Expression, in_circuit: bool) -> Result<Expression> {
let new = match expression { let new = match expression {
Expression::Identifier(identifier) => Expression::Identifier(self.reduce_identifier(&identifier)), Expression::Identifier(identifier) => Expression::Identifier(self.reduce_identifier(&identifier)?),
Expression::Value(value) => Expression::Value(self.reduce_value(&value)), Expression::Value(value) => Expression::Value(self.reduce_value(&value)?),
Expression::Binary(binary) => Expression::Binary(self.reduce_binary(&binary)), Expression::Binary(binary) => Expression::Binary(self.reduce_binary(&binary, in_circuit)?),
Expression::Unary(unary) => Expression::Unary(self.reduce_unary(&unary)), Expression::Unary(unary) => Expression::Unary(self.reduce_unary(&unary, in_circuit)?),
Expression::Ternary(ternary) => Expression::Ternary(self.reduce_ternary(&ternary)), Expression::Ternary(ternary) => Expression::Ternary(self.reduce_ternary(&ternary, in_circuit)?),
Expression::Cast(cast) => Expression::Cast(self.reduce_cast(&cast)), Expression::Cast(cast) => Expression::Cast(self.reduce_cast(&cast, in_circuit)?),
Expression::ArrayInline(array_inline) => Expression::ArrayInline(self.reduce_array_inline(&array_inline)), Expression::ArrayInline(array_inline) => {
Expression::ArrayInit(array_init) => Expression::ArrayInit(self.reduce_array_init(&array_init)), Expression::ArrayInline(self.reduce_array_inline(&array_inline, in_circuit)?)
Expression::ArrayAccess(array_access) => Expression::ArrayAccess(self.reduce_array_access(&array_access)), }
Expression::ArrayInit(array_init) => {
Expression::ArrayInit(self.reduce_array_init(&array_init, in_circuit)?)
}
Expression::ArrayAccess(array_access) => {
Expression::ArrayAccess(self.reduce_array_access(&array_access, in_circuit)?)
}
Expression::ArrayRangeAccess(array_range_access) => { Expression::ArrayRangeAccess(array_range_access) => {
Expression::ArrayRangeAccess(self.reduce_array_range_access(&array_range_access)) Expression::ArrayRangeAccess(self.reduce_array_range_access(&array_range_access, in_circuit)?)
} }
Expression::TupleInit(tuple_init) => Expression::TupleInit(self.reduce_tuple_init(&tuple_init)), Expression::TupleInit(tuple_init) => {
Expression::TupleAccess(tuple_access) => Expression::TupleAccess(self.reduce_tuple_access(&tuple_access)), Expression::TupleInit(self.reduce_tuple_init(&tuple_init, in_circuit)?)
}
Expression::TupleAccess(tuple_access) => {
Expression::TupleAccess(self.reduce_tuple_access(&tuple_access, in_circuit)?)
}
Expression::CircuitInit(circuit_init) => Expression::CircuitInit(self.reduce_circuit_init(&circuit_init)), Expression::CircuitInit(circuit_init) => {
Expression::CircuitInit(self.reduce_circuit_init(&circuit_init, in_circuit)?)
}
Expression::CircuitMemberAccess(circuit_member_access) => { Expression::CircuitMemberAccess(circuit_member_access) => {
Expression::CircuitMemberAccess(self.reduce_circuit_member_access(&circuit_member_access)) Expression::CircuitMemberAccess(self.reduce_circuit_member_access(&circuit_member_access, in_circuit)?)
} }
Expression::CircuitStaticFunctionAccess(circuit_static_fn_access) => { Expression::CircuitStaticFunctionAccess(circuit_static_fn_access) => {
Expression::CircuitStaticFunctionAccess(self.reduce_circuit_static_fn_access(&circuit_static_fn_access)) Expression::CircuitStaticFunctionAccess(
self.reduce_circuit_static_fn_access(&circuit_static_fn_access, in_circuit)?,
)
} }
Expression::Call(call) => Expression::Call(self.reduce_call(&call)), Expression::Call(call) => Expression::Call(self.reduce_call(&call, in_circuit)?),
}; };
self.reducer.reduce_expression(expression, new) self.reducer.reduce_expression(expression, new, in_circuit)
} }
pub fn reduce_identifier(&mut self, identifier: &Identifier) -> Identifier { pub fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier> {
self.reducer.reduce_identifier(identifier) self.reducer.reduce_identifier(identifier)
} }
pub fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> GroupTuple { pub fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> Result<GroupTuple> {
self.reducer.reduce_group_tuple(group_tuple) self.reducer.reduce_group_tuple(group_tuple)
} }
pub fn reduce_group_value(&mut self, group_value: &GroupValue) -> GroupValue { pub fn reduce_group_value(&mut self, group_value: &GroupValue) -> Result<GroupValue> {
let new = match group_value { let new = match group_value {
GroupValue::Tuple(group_tuple) => GroupValue::Tuple(self.reduce_group_tuple(&group_tuple)), GroupValue::Tuple(group_tuple) => GroupValue::Tuple(self.reduce_group_tuple(&group_tuple)?),
_ => group_value.clone(), _ => group_value.clone(),
}; };
self.reducer.reduce_group_value(group_value, new) self.reducer.reduce_group_value(group_value, new)
} }
pub fn reduce_value(&mut self, value: &ValueExpression) -> ValueExpression { pub fn reduce_value(&mut self, value: &ValueExpression) -> Result<ValueExpression> {
let new = match value { let new = match value {
ValueExpression::Group(group_value) => { ValueExpression::Group(group_value) => {
ValueExpression::Group(Box::new(self.reduce_group_value(&group_value))) ValueExpression::Group(Box::new(self.reduce_group_value(&group_value)?))
} }
_ => value.clone(), _ => value.clone(),
}; };
@ -104,256 +126,333 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_value(value, new) self.reducer.reduce_value(value, new)
} }
pub fn reduce_binary(&mut self, binary: &BinaryExpression) -> BinaryExpression { pub fn reduce_binary(&mut self, binary: &BinaryExpression, in_circuit: bool) -> Result<BinaryExpression> {
let left = self.reduce_expression(&binary.left); let left = self.reduce_expression(&binary.left, in_circuit)?;
let right = self.reduce_expression(&binary.right); let right = self.reduce_expression(&binary.right, in_circuit)?;
self.reducer.reduce_binary(binary, left, right, binary.op.clone()) self.reducer
.reduce_binary(binary, left, right, binary.op.clone(), in_circuit)
} }
pub fn reduce_unary(&mut self, unary: &UnaryExpression) -> UnaryExpression { pub fn reduce_unary(&mut self, unary: &UnaryExpression, in_circuit: bool) -> Result<UnaryExpression> {
let inner = self.reduce_expression(&unary.inner); let inner = self.reduce_expression(&unary.inner, in_circuit)?;
self.reducer.reduce_unary(unary, inner, unary.op.clone()) self.reducer.reduce_unary(unary, inner, unary.op.clone(), in_circuit)
} }
pub fn reduce_ternary(&mut self, ternary: &TernaryExpression) -> TernaryExpression { pub fn reduce_ternary(&mut self, ternary: &TernaryExpression, in_circuit: bool) -> Result<TernaryExpression> {
let condition = self.reduce_expression(&ternary.condition); let condition = self.reduce_expression(&ternary.condition, in_circuit)?;
let if_true = self.reduce_expression(&ternary.if_true); let if_true = self.reduce_expression(&ternary.if_true, in_circuit)?;
let if_false = self.reduce_expression(&ternary.if_false); let if_false = self.reduce_expression(&ternary.if_false, in_circuit)?;
self.reducer.reduce_ternary(ternary, condition, if_true, if_false) self.reducer
.reduce_ternary(ternary, condition, if_true, if_false, in_circuit)
} }
pub fn reduce_cast(&mut self, cast: &CastExpression) -> CastExpression { pub fn reduce_cast(&mut self, cast: &CastExpression, in_circuit: bool) -> Result<CastExpression> {
let inner = self.reduce_expression(&cast.inner); let inner = self.reduce_expression(&cast.inner, in_circuit)?;
let target_type = cast.target_type.clone(); // TODO reduce let target_type = self.reduce_type(&cast.target_type, in_circuit)?;
self.reducer.reduce_cast(cast, inner, target_type) self.reducer.reduce_cast(cast, inner, target_type, in_circuit)
} }
pub fn reduce_array_inline(&mut self, array_inline: &ArrayInlineExpression) -> ArrayInlineExpression { pub fn reduce_array_inline(
let elements = array_inline &mut self,
.elements array_inline: &ArrayInlineExpression,
.iter() in_circuit: bool,
.map(|element| match element { ) -> Result<ArrayInlineExpression> {
let mut elements = vec![];
for element in array_inline.elements.iter() {
let reduced_element = match element {
SpreadOrExpression::Expression(expression) => { SpreadOrExpression::Expression(expression) => {
SpreadOrExpression::Expression(self.reduce_expression(expression)) SpreadOrExpression::Expression(self.reduce_expression(expression, in_circuit)?)
} }
SpreadOrExpression::Spread(expression) => { SpreadOrExpression::Spread(expression) => {
SpreadOrExpression::Spread(self.reduce_expression(expression)) SpreadOrExpression::Spread(self.reduce_expression(expression, in_circuit)?)
} }
}) };
.collect();
self.reducer.reduce_array_inline(array_inline, elements) elements.push(reduced_element);
} }
pub fn reduce_array_init(&mut self, array_init: &ArrayInitExpression) -> ArrayInitExpression { self.reducer.reduce_array_inline(array_inline, elements, in_circuit)
let element = self.reduce_expression(&array_init.element);
self.reducer.reduce_array_init(array_init, element)
} }
pub fn reduce_array_access(&mut self, array_access: &ArrayAccessExpression) -> ArrayAccessExpression { pub fn reduce_array_init(
let array = self.reduce_expression(&array_access.array); &mut self,
let index = self.reduce_expression(&array_access.index); array_init: &ArrayInitExpression,
in_circuit: bool,
) -> Result<ArrayInitExpression> {
let element = self.reduce_expression(&array_init.element, in_circuit)?;
self.reducer.reduce_array_access(array_access, array, index) self.reducer.reduce_array_init(array_init, element, in_circuit)
}
pub fn reduce_array_access(
&mut self,
array_access: &ArrayAccessExpression,
in_circuit: bool,
) -> Result<ArrayAccessExpression> {
let array = self.reduce_expression(&array_access.array, in_circuit)?;
let index = self.reduce_expression(&array_access.index, in_circuit)?;
self.reducer.reduce_array_access(array_access, array, index, in_circuit)
} }
pub fn reduce_array_range_access( pub fn reduce_array_range_access(
&mut self, &mut self,
array_range_access: &ArrayRangeAccessExpression, array_range_access: &ArrayRangeAccessExpression,
) -> ArrayRangeAccessExpression { in_circuit: bool,
let array = self.reduce_expression(&array_range_access.array); ) -> Result<ArrayRangeAccessExpression> {
let left = array_range_access let array = self.reduce_expression(&array_range_access.array, in_circuit)?;
.left let left = match array_range_access.left.as_ref() {
.as_ref() Some(left) => Some(self.reduce_expression(left, in_circuit)?),
.map(|left| self.reduce_expression(left)); None => None,
let right = array_range_access };
.right let right = match array_range_access.right.as_ref() {
.as_ref() Some(right) => Some(self.reduce_expression(right, in_circuit)?),
.map(|right| self.reduce_expression(right)); None => None,
};
self.reducer self.reducer
.reduce_array_range_access(array_range_access, array, left, right) .reduce_array_range_access(array_range_access, array, left, right, in_circuit)
} }
pub fn reduce_tuple_init(&mut self, tuple_init: &TupleInitExpression) -> TupleInitExpression { pub fn reduce_tuple_init(
let elements = tuple_init &mut self,
.elements tuple_init: &TupleInitExpression,
.iter() in_circuit: bool,
.map(|expr| self.reduce_expression(expr)) ) -> Result<TupleInitExpression> {
.collect(); let mut elements = vec![];
for element in tuple_init.elements.iter() {
self.reducer.reduce_tuple_init(tuple_init, elements) elements.push(self.reduce_expression(element, in_circuit)?);
} }
pub fn reduce_tuple_access(&mut self, tuple_access: &TupleAccessExpression) -> TupleAccessExpression { self.reducer.reduce_tuple_init(tuple_init, elements, in_circuit)
let tuple = self.reduce_expression(&tuple_access.tuple);
self.reducer.reduce_tuple_access(tuple_access, tuple)
} }
pub fn reduce_circuit_init(&mut self, circuit_init: &CircuitInitExpression) -> CircuitInitExpression { pub fn reduce_tuple_access(
let name = self.reduce_identifier(&circuit_init.name); &mut self,
let members = circuit_init tuple_access: &TupleAccessExpression,
.members in_circuit: bool,
.iter() ) -> Result<TupleAccessExpression> {
.map(|definition| { let tuple = self.reduce_expression(&tuple_access.tuple, in_circuit)?;
let identifier = self.reduce_identifier(&definition.identifier);
let expression = definition.expression.as_ref().map(|expr| self.reduce_expression(expr));
CircuitImpliedVariableDefinition { identifier, expression } self.reducer.reduce_tuple_access(tuple_access, tuple, in_circuit)
}) }
.collect();
self.reducer.reduce_circuit_init(circuit_init, name, members) pub fn reduce_circuit_implied_variable_definition(
&mut self,
variable: &CircuitImpliedVariableDefinition,
in_circuit: bool,
) -> Result<CircuitImpliedVariableDefinition> {
let identifier = self.reduce_identifier(&variable.identifier)?;
let expression = match variable.expression.as_ref() {
Some(expr) => Some(self.reduce_expression(expr, in_circuit)?),
None => None,
};
self.reducer
.reduce_circuit_implied_variable_definition(variable, identifier, expression, in_circuit)
}
pub fn reduce_circuit_init(
&mut self,
circuit_init: &CircuitInitExpression,
in_circuit: bool,
) -> Result<CircuitInitExpression> {
let name = self.reduce_identifier(&circuit_init.name)?;
let mut members = vec![];
for member in circuit_init.members.iter() {
members.push(self.reduce_circuit_implied_variable_definition(member, in_circuit)?);
}
self.reducer
.reduce_circuit_init(circuit_init, name, members, in_circuit)
} }
pub fn reduce_circuit_member_access( pub fn reduce_circuit_member_access(
&mut self, &mut self,
circuit_member_access: &CircuitMemberAccessExpression, circuit_member_access: &CircuitMemberAccessExpression,
) -> CircuitMemberAccessExpression { in_circuit: bool,
let circuit = self.reduce_expression(&circuit_member_access.circuit); ) -> Result<CircuitMemberAccessExpression> {
let name = self.reduce_identifier(&circuit_member_access.name); let circuit = self.reduce_expression(&circuit_member_access.circuit, in_circuit)?;
let name = self.reduce_identifier(&circuit_member_access.name)?;
self.reducer self.reducer
.reduce_circuit_member_access(circuit_member_access, circuit, name) .reduce_circuit_member_access(circuit_member_access, circuit, name, in_circuit)
} }
pub fn reduce_circuit_static_fn_access( pub fn reduce_circuit_static_fn_access(
&mut self, &mut self,
circuit_static_fn_access: &CircuitStaticFunctionAccessExpression, circuit_static_fn_access: &CircuitStaticFunctionAccessExpression,
) -> CircuitStaticFunctionAccessExpression { in_circuit: bool,
let circuit = self.reduce_expression(&circuit_static_fn_access.circuit); ) -> Result<CircuitStaticFunctionAccessExpression> {
let name = self.reduce_identifier(&circuit_static_fn_access.name); let circuit = self.reduce_expression(&circuit_static_fn_access.circuit, in_circuit)?;
let name = self.reduce_identifier(&circuit_static_fn_access.name)?;
self.reducer self.reducer
.reduce_circuit_static_fn_access(circuit_static_fn_access, circuit, name) .reduce_circuit_static_fn_access(circuit_static_fn_access, circuit, name, in_circuit)
} }
pub fn reduce_call(&mut self, call: &CallExpression) -> CallExpression { pub fn reduce_call(&mut self, call: &CallExpression, in_circuit: bool) -> Result<CallExpression> {
let function = self.reduce_expression(&call.function); let function = self.reduce_expression(&call.function, in_circuit)?;
let arguments = call.arguments.iter().map(|expr| self.reduce_expression(expr)).collect();
self.reducer.reduce_call(call, function, arguments) let mut arguments = vec![];
for argument in call.arguments.iter() {
arguments.push(self.reduce_expression(argument, in_circuit)?);
}
self.reducer.reduce_call(call, function, arguments, in_circuit)
} }
// Statements // Statements
pub fn reduce_statement(&mut self, statement: &Statement, in_circuit: bool) -> Statement { pub fn reduce_statement(&mut self, statement: &Statement, in_circuit: bool) -> Result<Statement> {
let new = match statement { let new = match statement {
Statement::Return(return_statement) => Statement::Return(self.reduce_return(&return_statement)), Statement::Return(return_statement) => {
Statement::Definition(definition) => Statement::Definition(self.reduce_definition(&definition, in_circuit)), Statement::Return(self.reduce_return(&return_statement, in_circuit)?)
Statement::Assign(assign) => Statement::Assign(self.reduce_assign(&assign)),
Statement::Conditional(conditional) => {
Statement::Conditional(self.reduce_conditional(&conditional, in_circuit))
} }
Statement::Iteration(iteration) => Statement::Iteration(self.reduce_iteration(&iteration, in_circuit)), Statement::Definition(definition) => {
Statement::Console(console) => Statement::Console(self.reduce_console(&console)), Statement::Definition(self.reduce_definition(&definition, in_circuit)?)
Statement::Expression(expression) => Statement::Expression(self.reduce_expression_statement(&expression)), }
Statement::Block(block) => Statement::Block(self.reduce_block(&block, in_circuit)), Statement::Assign(assign) => Statement::Assign(self.reduce_assign(&assign, in_circuit)?),
Statement::Conditional(conditional) => {
Statement::Conditional(self.reduce_conditional(&conditional, in_circuit)?)
}
Statement::Iteration(iteration) => Statement::Iteration(self.reduce_iteration(&iteration, in_circuit)?),
Statement::Console(console) => Statement::Console(self.reduce_console(&console, in_circuit)?),
Statement::Expression(expression) => {
Statement::Expression(self.reduce_expression_statement(&expression, in_circuit)?)
}
Statement::Block(block) => Statement::Block(self.reduce_block(&block, in_circuit)?),
}; };
self.reducer.reduce_statement(statement, new, in_circuit) self.reducer.reduce_statement(statement, new, in_circuit)
} }
pub fn reduce_return(&mut self, return_statement: &ReturnStatement) -> ReturnStatement { pub fn reduce_return(&mut self, return_statement: &ReturnStatement, in_circuit: bool) -> Result<ReturnStatement> {
let expression = self.reduce_expression(&return_statement.expression); let expression = self.reduce_expression(&return_statement.expression, in_circuit)?;
self.reducer.reduce_return(return_statement, expression) self.reducer.reduce_return(return_statement, expression, in_circuit)
} }
pub fn reduce_variable_name(&mut self, variable_name: &VariableName) -> VariableName { pub fn reduce_variable_name(&mut self, variable_name: &VariableName) -> Result<VariableName> {
let identifier = self.reduce_identifier(&variable_name.identifier); let identifier = self.reduce_identifier(&variable_name.identifier)?;
self.reducer.reduce_variable_name(variable_name, identifier) self.reducer.reduce_variable_name(variable_name, identifier)
} }
pub fn reduce_definition(&mut self, definition: &DefinitionStatement, in_circuit: bool) -> DefinitionStatement { pub fn reduce_definition(
let variable_names = definition &mut self,
.variable_names definition: &DefinitionStatement,
.iter() in_circuit: bool,
.map(|variable_name| self.reduce_variable_name(variable_name)) ) -> Result<DefinitionStatement> {
.collect(); let mut variable_names = vec![];
let type_ = definition for variable_name in definition.variable_names.iter() {
.type_ variable_names.push(self.reduce_variable_name(variable_name)?);
.as_ref() }
.map(|inner| self.reduce_type(inner, in_circuit));
let value = self.reduce_expression(&definition.value); let type_ = match definition.type_.as_ref() {
Some(inner) => Some(self.reduce_type(inner, in_circuit)?),
None => None,
};
let value = self.reduce_expression(&definition.value, in_circuit)?;
self.reducer self.reducer
.reduce_definition(definition, variable_names, type_, value, in_circuit) .reduce_definition(definition, variable_names, type_, value, in_circuit)
} }
pub fn reduce_assignee_access(&mut self, access: &AssigneeAccess) -> AssigneeAccess { pub fn reduce_assignee_access(&mut self, access: &AssigneeAccess, in_circuit: bool) -> Result<AssigneeAccess> {
let new = match access { let new = match access {
AssigneeAccess::ArrayRange(left, right) => AssigneeAccess::ArrayRange( AssigneeAccess::ArrayRange(left, right) => {
left.as_ref().map(|expr| self.reduce_expression(expr)), let left = match left.as_ref() {
right.as_ref().map(|expr| self.reduce_expression(expr)), Some(left) => Some(self.reduce_expression(left, in_circuit)?),
), None => None,
AssigneeAccess::ArrayIndex(index) => AssigneeAccess::ArrayIndex(self.reduce_expression(&index)), };
AssigneeAccess::Member(identifier) => AssigneeAccess::Member(self.reduce_identifier(&identifier)), let right = match right.as_ref() {
Some(right) => Some(self.reduce_expression(right, in_circuit)?),
None => None,
};
AssigneeAccess::ArrayRange(left, right)
}
AssigneeAccess::ArrayIndex(index) => {
AssigneeAccess::ArrayIndex(self.reduce_expression(&index, in_circuit)?)
}
AssigneeAccess::Member(identifier) => AssigneeAccess::Member(self.reduce_identifier(&identifier)?),
_ => access.clone(), _ => access.clone(),
}; };
self.reducer.reduce_assignee_access(access, new) self.reducer.reduce_assignee_access(access, new, in_circuit)
} }
pub fn reduce_assignee(&mut self, assignee: &Assignee) -> Assignee { pub fn reduce_assignee(&mut self, assignee: &Assignee, in_circuit: bool) -> Result<Assignee> {
let identifier = self.reduce_identifier(&assignee.identifier); let identifier = self.reduce_identifier(&assignee.identifier)?;
let accesses = assignee
.accesses
.iter()
.map(|access| self.reduce_assignee_access(access))
.collect();
self.reducer.reduce_assignee(assignee, identifier, accesses) let mut accesses = vec![];
for access in assignee.accesses.iter() {
accesses.push(self.reduce_assignee_access(access, in_circuit)?);
} }
pub fn reduce_assign(&mut self, assign: &AssignStatement) -> AssignStatement { self.reducer.reduce_assignee(assignee, identifier, accesses, in_circuit)
let assignee = self.reduce_assignee(&assign.assignee);
let value = self.reduce_expression(&assign.value);
self.reducer.reduce_assign(assign, assignee, value)
} }
pub fn reduce_conditional(&mut self, conditional: &ConditionalStatement, in_circuit: bool) -> ConditionalStatement { pub fn reduce_assign(&mut self, assign: &AssignStatement, in_circuit: bool) -> Result<AssignStatement> {
let condition = self.reduce_expression(&conditional.condition); let assignee = self.reduce_assignee(&assign.assignee, in_circuit)?;
let block = self.reduce_block(&conditional.block, in_circuit); let value = self.reduce_expression(&assign.value, in_circuit)?;
let next = conditional
.next self.reducer.reduce_assign(assign, assignee, value, in_circuit)
.as_ref() }
.map(|condition| self.reduce_statement(condition, in_circuit));
pub fn reduce_conditional(
&mut self,
conditional: &ConditionalStatement,
in_circuit: bool,
) -> Result<ConditionalStatement> {
let condition = self.reduce_expression(&conditional.condition, in_circuit)?;
let block = self.reduce_block(&conditional.block, in_circuit)?;
let next = match conditional.next.as_ref() {
Some(condition) => Some(self.reduce_statement(condition, in_circuit)?),
None => None,
};
self.reducer self.reducer
.reduce_conditional(conditional, condition, block, next, in_circuit) .reduce_conditional(conditional, condition, block, next, in_circuit)
} }
pub fn reduce_iteration(&mut self, iteration: &IterationStatement, in_circuit: bool) -> IterationStatement { pub fn reduce_iteration(&mut self, iteration: &IterationStatement, in_circuit: bool) -> Result<IterationStatement> {
let variable = self.reduce_identifier(&iteration.variable); let variable = self.reduce_identifier(&iteration.variable)?;
let start = self.reduce_expression(&iteration.start); let start = self.reduce_expression(&iteration.start, in_circuit)?;
let stop = self.reduce_expression(&iteration.stop); let stop = self.reduce_expression(&iteration.stop, in_circuit)?;
let block = self.reduce_block(&iteration.block, in_circuit); let block = self.reduce_block(&iteration.block, in_circuit)?;
self.reducer self.reducer
.reduce_iteration(iteration, variable, start, stop, block, in_circuit) .reduce_iteration(iteration, variable, start, stop, block, in_circuit)
} }
pub fn reduce_console(&mut self, console_function_call: &ConsoleStatement) -> ConsoleStatement { pub fn reduce_console(
&mut self,
console_function_call: &ConsoleStatement,
in_circuit: bool,
) -> Result<ConsoleStatement> {
let function = match &console_function_call.function { let function = match &console_function_call.function {
ConsoleFunction::Assert(expression) => ConsoleFunction::Assert(self.reduce_expression(expression)), ConsoleFunction::Assert(expression) => {
ConsoleFunction::Assert(self.reduce_expression(expression, in_circuit)?)
}
ConsoleFunction::Debug(format) | ConsoleFunction::Error(format) | ConsoleFunction::Log(format) => { ConsoleFunction::Debug(format) | ConsoleFunction::Error(format) | ConsoleFunction::Log(format) => {
let mut parameters = vec![];
for parameter in format.parameters.iter() {
parameters.push(self.reduce_expression(parameter, in_circuit)?);
}
let formatted = FormattedString { let formatted = FormattedString {
parts: format.parts.clone(), parts: format.parts.clone(),
parameters: format parameters,
.parameters
.iter()
.map(|parameter| self.reduce_expression(parameter))
.collect(),
span: format.span.clone(), span: format.span.clone(),
}; };
match &console_function_call.function { match &console_function_call.function {
ConsoleFunction::Debug(_) => ConsoleFunction::Debug(formatted), ConsoleFunction::Debug(_) => ConsoleFunction::Debug(formatted),
ConsoleFunction::Error(_) => ConsoleFunction::Error(formatted), ConsoleFunction::Error(_) => ConsoleFunction::Error(formatted),
@ -363,51 +462,52 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
} }
}; };
self.reducer.reduce_console(console_function_call, function) self.reducer.reduce_console(console_function_call, function, in_circuit)
} }
pub fn reduce_expression_statement(&mut self, expression: &ExpressionStatement) -> ExpressionStatement { pub fn reduce_expression_statement(
let inner_expression = self.reduce_expression(&expression.expression); &mut self,
self.reducer.reduce_expression_statement(expression, inner_expression) expression: &ExpressionStatement,
in_circuit: bool,
) -> Result<ExpressionStatement> {
let inner_expression = self.reduce_expression(&expression.expression, in_circuit)?;
self.reducer
.reduce_expression_statement(expression, inner_expression, in_circuit)
} }
pub fn reduce_block(&mut self, block: &Block, in_circuit: bool) -> Block { pub fn reduce_block(&mut self, block: &Block, in_circuit: bool) -> Result<Block> {
let statements = block let mut statements = vec![];
.statements for statement in block.statements.iter() {
.iter() statements.push(self.reduce_statement(statement, in_circuit)?);
.map(|statement| self.reduce_statement(statement, in_circuit)) }
.collect();
self.reducer.reduce_block(block, statements, in_circuit) self.reducer.reduce_block(block, statements, in_circuit)
} }
// Program // Program
pub fn reduce_program(&mut self, program: &Program) -> Program { pub fn reduce_program(&mut self, program: &Program) -> Result<Program> {
let inputs = program let mut inputs = vec![];
.expected_input for input in program.expected_input.iter() {
.iter() inputs.push(self.reduce_function_input(input, false)?);
.map(|input| self.reduce_function_input(input, false)) }
.collect();
let imports = program let mut imports = vec![];
.imports for import in program.imports.iter() {
.iter() imports.push(self.reduce_import(import)?);
.map(|import| self.reduce_import(import)) }
.collect();
let circuits = program let mut circuits = IndexMap::new();
.circuits for (identifier, circuit) in program.circuits.iter() {
.iter() circuits.insert(self.reduce_identifier(identifier)?, self.reduce_circuit(circuit)?);
.map(|(identifier, circuit)| (self.reduce_identifier(identifier), self.reduce_circuit(circuit))) }
.collect();
let functions = program let mut functions = IndexMap::new();
.functions for (identifier, function) in program.functions.iter() {
.iter() functions.insert(
.map(|(identifier, function)| { self.reduce_identifier(identifier)?,
( self.reduce_function(function, false)?,
self.reduce_identifier(identifier), );
self.reduce_function(function, false), }
)
})
.collect();
self.reducer self.reducer
.reduce_program(program, inputs, imports, circuits, functions) .reduce_program(program, inputs, imports, circuits, functions)
@ -417,18 +517,18 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
&mut self, &mut self,
variable: &FunctionInputVariable, variable: &FunctionInputVariable,
in_circuit: bool, in_circuit: bool,
) -> FunctionInputVariable { ) -> Result<FunctionInputVariable> {
let identifier = self.reduce_identifier(&variable.identifier); let identifier = self.reduce_identifier(&variable.identifier)?;
let type_ = self.reduce_type(&variable.type_, in_circuit); let type_ = self.reduce_type(&variable.type_, in_circuit)?;
self.reducer self.reducer
.reduce_function_input_variable(variable, identifier, type_, in_circuit) .reduce_function_input_variable(variable, identifier, type_, in_circuit)
} }
pub fn reduce_function_input(&mut self, input: &FunctionInput, in_circuit: bool) -> FunctionInput { pub fn reduce_function_input(&mut self, input: &FunctionInput, in_circuit: bool) -> Result<FunctionInput> {
let new = match input { let new = match input {
FunctionInput::Variable(function_input_variable) => { FunctionInput::Variable(function_input_variable) => {
FunctionInput::Variable(self.reduce_function_input_variable(function_input_variable, in_circuit)) FunctionInput::Variable(self.reduce_function_input_variable(function_input_variable, in_circuit)?)
} }
_ => input.clone(), _ => input.clone(),
}; };
@ -436,15 +536,15 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_function_input(input, new, in_circuit) self.reducer.reduce_function_input(input, new, in_circuit)
} }
pub fn reduce_package_or_packages(&mut self, package_or_packages: &PackageOrPackages) -> PackageOrPackages { pub fn reduce_package_or_packages(&mut self, package_or_packages: &PackageOrPackages) -> Result<PackageOrPackages> {
let new = match package_or_packages { let new = match package_or_packages {
PackageOrPackages::Package(package) => PackageOrPackages::Package(Package { PackageOrPackages::Package(package) => PackageOrPackages::Package(Package {
name: self.reduce_identifier(&package.name), name: self.reduce_identifier(&package.name)?,
access: package.access.clone(), access: package.access.clone(),
span: package.span.clone(), span: package.span.clone(),
}), }),
PackageOrPackages::Packages(packages) => PackageOrPackages::Packages(Packages { PackageOrPackages::Packages(packages) => PackageOrPackages::Packages(Packages {
name: self.reduce_identifier(&packages.name), name: self.reduce_identifier(&packages.name)?,
accesses: packages.accesses.clone(), accesses: packages.accesses.clone(),
span: packages.span.clone(), span: packages.span.clone(),
}), }),
@ -453,61 +553,63 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_package_or_packages(package_or_packages, new) self.reducer.reduce_package_or_packages(package_or_packages, new)
} }
pub fn reduce_import(&mut self, import: &ImportStatement) -> ImportStatement { pub fn reduce_import(&mut self, import: &ImportStatement) -> Result<ImportStatement> {
let package_or_packages = self.reduce_package_or_packages(&import.package_or_packages); let package_or_packages = self.reduce_package_or_packages(&import.package_or_packages)?;
self.reducer.reduce_import(import, package_or_packages) self.reducer.reduce_import(import, package_or_packages)
} }
pub fn reduce_circuit_member(&mut self, circuit_member: &CircuitMember) -> CircuitMember { pub fn reduce_circuit_member(&mut self, circuit_member: &CircuitMember) -> Result<CircuitMember> {
let new = match circuit_member { let new = match circuit_member {
CircuitMember::CircuitVariable(identifier, type_) => { CircuitMember::CircuitVariable(identifier, type_) => {
CircuitMember::CircuitVariable(self.reduce_identifier(&identifier), self.reduce_type(&type_, true)) CircuitMember::CircuitVariable(self.reduce_identifier(&identifier)?, self.reduce_type(&type_, true)?)
} }
CircuitMember::CircuitFunction(function) => { CircuitMember::CircuitFunction(function) => {
CircuitMember::CircuitFunction(self.reduce_function(&function, true)) CircuitMember::CircuitFunction(self.reduce_function(&function, true)?)
} }
}; };
self.reducer.reduce_circuit_member(circuit_member, new) self.reducer.reduce_circuit_member(circuit_member, new)
} }
pub fn reduce_circuit(&mut self, circuit: &Circuit) -> Circuit { pub fn reduce_circuit(&mut self, circuit: &Circuit) -> Result<Circuit> {
let circuit_name = self.reduce_identifier(&circuit.circuit_name); let circuit_name = self.reduce_identifier(&circuit.circuit_name)?;
let members = circuit
.members let mut members = vec![];
.iter() for member in circuit.members.iter() {
.map(|member| self.reduce_circuit_member(member)) members.push(self.reduce_circuit_member(member)?);
.collect(); }
self.reducer.reduce_circuit(circuit, circuit_name, members) self.reducer.reduce_circuit(circuit, circuit_name, members)
} }
fn reduce_annotation(&mut self, annotation: &Annotation) -> Annotation { fn reduce_annotation(&mut self, annotation: &Annotation) -> Result<Annotation> {
let name = self.reduce_identifier(&annotation.name); let name = self.reduce_identifier(&annotation.name)?;
self.reducer.reduce_annotation(annotation, name) self.reducer.reduce_annotation(annotation, name)
} }
pub fn reduce_function(&mut self, function: &Function, in_circuit: bool) -> Function { pub fn reduce_function(&mut self, function: &Function, in_circuit: bool) -> Result<Function> {
let identifier = self.reduce_identifier(&function.identifier); let identifier = self.reduce_identifier(&function.identifier)?;
let annotations = function
.annotations let mut annotations = vec![];
.iter() for annotation in function.annotations.iter() {
.map(|annotation| self.reduce_annotation(annotation)) annotations.push(self.reduce_annotation(annotation)?);
.collect(); }
let input = function
.input let mut inputs = vec![];
.iter() for input in function.input.iter() {
.map(|input| self.reduce_function_input(input, false)) inputs.push(self.reduce_function_input(input, false)?);
.collect(); }
let output = function
.output let output = match function.output.as_ref() {
.as_ref() Some(type_) => Some(self.reduce_type(type_, in_circuit)?),
.map(|output| self.reduce_type(output, in_circuit)); None => None,
let block = self.reduce_block(&function.block, false); };
let block = self.reduce_block(&function.block, false)?;
self.reducer self.reducer
.reduce_function(function, identifier, annotations, input, output, block, in_circuit) .reduce_function(function, identifier, annotations, inputs, output, block, in_circuit)
} }
} }

View File

@ -15,41 +15,47 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::*; use crate::*;
use anyhow::Result;
use indexmap::IndexMap; use indexmap::IndexMap;
// Needed to fix clippy bug. // Needed to fix clippy bug.
#[allow(clippy::redundant_closure)] #[allow(clippy::redundant_closure)]
pub trait ReconstructingReducer { pub trait ReconstructingReducer {
fn reduce_type(&mut self, _type_: &Type, new: Type, _in_circuit: bool) -> Type { fn reduce_type(&mut self, _type_: &Type, new: Type, _in_circuit: bool) -> Result<Type> {
new Ok(new)
} }
// Expressions // Expressions
fn reduce_expression(&mut self, _expression: &Expression, new: Expression) -> Expression { fn reduce_expression(
new &mut self,
_expression: &Expression,
new: Expression,
_in_circuit: bool,
) -> Result<Expression> {
Ok(new)
} }
fn reduce_identifier(&mut self, identifier: &Identifier) -> Identifier { fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier> {
Identifier { Ok(Identifier {
name: identifier.name.clone(), name: identifier.name.clone(),
span: identifier.span.clone(), span: identifier.span.clone(),
} })
} }
fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> GroupTuple { fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> Result<GroupTuple> {
GroupTuple { Ok(GroupTuple {
x: group_tuple.x.clone(), x: group_tuple.x.clone(),
y: group_tuple.y.clone(), y: group_tuple.y.clone(),
span: group_tuple.span.clone(), span: group_tuple.span.clone(),
} })
} }
fn reduce_group_value(&mut self, _group_value: &GroupValue, new: GroupValue) -> GroupValue { fn reduce_group_value(&mut self, _group_value: &GroupValue, new: GroupValue) -> Result<GroupValue> {
new Ok(new)
} }
fn reduce_value(&mut self, _value: &ValueExpression, new: ValueExpression) -> ValueExpression { fn reduce_value(&mut self, _value: &ValueExpression, new: ValueExpression) -> Result<ValueExpression> {
new Ok(new)
} }
fn reduce_binary( fn reduce_binary(
@ -58,21 +64,28 @@ pub trait ReconstructingReducer {
left: Expression, left: Expression,
right: Expression, right: Expression,
op: BinaryOperation, op: BinaryOperation,
) -> BinaryExpression { _in_circuit: bool,
BinaryExpression { ) -> Result<BinaryExpression> {
Ok(BinaryExpression {
left: Box::new(left), left: Box::new(left),
right: Box::new(right), right: Box::new(right),
op, op,
span: binary.span.clone(), span: binary.span.clone(),
} })
} }
fn reduce_unary(&mut self, unary: &UnaryExpression, inner: Expression, op: UnaryOperation) -> UnaryExpression { fn reduce_unary(
UnaryExpression { &mut self,
unary: &UnaryExpression,
inner: Expression,
op: UnaryOperation,
_in_circuit: bool,
) -> Result<UnaryExpression> {
Ok(UnaryExpression {
inner: Box::new(inner), inner: Box::new(inner),
op, op,
span: unary.span.clone(), span: unary.span.clone(),
} })
} }
fn reduce_ternary( fn reduce_ternary(
@ -81,40 +94,53 @@ pub trait ReconstructingReducer {
condition: Expression, condition: Expression,
if_true: Expression, if_true: Expression,
if_false: Expression, if_false: Expression,
) -> TernaryExpression { _in_circuit: bool,
TernaryExpression { ) -> Result<TernaryExpression> {
Ok(TernaryExpression {
condition: Box::new(condition), condition: Box::new(condition),
if_true: Box::new(if_true), if_true: Box::new(if_true),
if_false: Box::new(if_false), if_false: Box::new(if_false),
span: ternary.span.clone(), span: ternary.span.clone(),
} })
} }
fn reduce_cast(&mut self, cast: &CastExpression, inner: Expression, target_type: Type) -> CastExpression { fn reduce_cast(
CastExpression { &mut self,
cast: &CastExpression,
inner: Expression,
target_type: Type,
_in_circuit: bool,
) -> Result<CastExpression> {
Ok(CastExpression {
inner: Box::new(inner), inner: Box::new(inner),
target_type, target_type,
span: cast.span.clone(), span: cast.span.clone(),
} })
} }
fn reduce_array_inline( fn reduce_array_inline(
&mut self, &mut self,
array_inline: &ArrayInlineExpression, array_inline: &ArrayInlineExpression,
elements: Vec<SpreadOrExpression>, elements: Vec<SpreadOrExpression>,
) -> ArrayInlineExpression { _in_circuit: bool,
ArrayInlineExpression { ) -> Result<ArrayInlineExpression> {
Ok(ArrayInlineExpression {
elements, elements,
span: array_inline.span.clone(), span: array_inline.span.clone(),
} })
} }
fn reduce_array_init(&mut self, array_init: &ArrayInitExpression, element: Expression) -> ArrayInitExpression { fn reduce_array_init(
ArrayInitExpression { &mut self,
array_init: &ArrayInitExpression,
element: Expression,
_in_circuit: bool,
) -> Result<ArrayInitExpression> {
Ok(ArrayInitExpression {
element: Box::new(element), element: Box::new(element),
dimensions: array_init.dimensions.clone(), dimensions: array_init.dimensions.clone(),
span: array_init.span.clone(), span: array_init.span.clone(),
} })
} }
fn reduce_array_access( fn reduce_array_access(
@ -122,12 +148,13 @@ pub trait ReconstructingReducer {
array_access: &ArrayAccessExpression, array_access: &ArrayAccessExpression,
array: Expression, array: Expression,
index: Expression, index: Expression,
) -> ArrayAccessExpression { _in_circuit: bool,
ArrayAccessExpression { ) -> Result<ArrayAccessExpression> {
Ok(ArrayAccessExpression {
array: Box::new(array), array: Box::new(array),
index: Box::new(index), index: Box::new(index),
span: array_access.span.clone(), span: array_access.span.clone(),
} })
} }
fn reduce_array_range_access( fn reduce_array_range_access(
@ -136,36 +163,49 @@ pub trait ReconstructingReducer {
array: Expression, array: Expression,
left: Option<Expression>, left: Option<Expression>,
right: Option<Expression>, right: Option<Expression>,
) -> ArrayRangeAccessExpression { _in_circuit: bool,
ArrayRangeAccessExpression { ) -> Result<ArrayRangeAccessExpression> {
Ok(ArrayRangeAccessExpression {
array: Box::new(array), array: Box::new(array),
left: left.map(|expr| Box::new(expr)), left: left.map(|expr| Box::new(expr)),
right: right.map(|expr| Box::new(expr)), right: right.map(|expr| Box::new(expr)),
span: array_rage_access.span.clone(), span: array_rage_access.span.clone(),
} })
} }
fn reduce_tuple_init( fn reduce_tuple_init(
&mut self, &mut self,
tuple_init: &TupleInitExpression, tuple_init: &TupleInitExpression,
elements: Vec<Expression>, elements: Vec<Expression>,
) -> TupleInitExpression { _in_circuit: bool,
TupleInitExpression { ) -> Result<TupleInitExpression> {
Ok(TupleInitExpression {
elements, elements,
span: tuple_init.span.clone(), span: tuple_init.span.clone(),
} })
} }
fn reduce_tuple_access( fn reduce_tuple_access(
&mut self, &mut self,
tuple_access: &TupleAccessExpression, tuple_access: &TupleAccessExpression,
tuple: Expression, tuple: Expression,
) -> TupleAccessExpression { _in_circuit: bool,
TupleAccessExpression { ) -> Result<TupleAccessExpression> {
Ok(TupleAccessExpression {
tuple: Box::new(tuple), tuple: Box::new(tuple),
index: tuple_access.index.clone(), index: tuple_access.index.clone(),
span: tuple_access.span.clone(), span: tuple_access.span.clone(),
})
} }
fn reduce_circuit_implied_variable_definition(
&mut self,
_variable: &CircuitImpliedVariableDefinition,
identifier: Identifier,
expression: Option<Expression>,
_in_circuit: bool,
) -> Result<CircuitImpliedVariableDefinition> {
Ok(CircuitImpliedVariableDefinition { identifier, expression })
} }
fn reduce_circuit_init( fn reduce_circuit_init(
@ -173,12 +213,13 @@ pub trait ReconstructingReducer {
circuit_init: &CircuitInitExpression, circuit_init: &CircuitInitExpression,
name: Identifier, name: Identifier,
members: Vec<CircuitImpliedVariableDefinition>, members: Vec<CircuitImpliedVariableDefinition>,
) -> CircuitInitExpression { _in_circuit: bool,
CircuitInitExpression { ) -> Result<CircuitInitExpression> {
Ok(CircuitInitExpression {
name, name,
members, members,
span: circuit_init.span.clone(), span: circuit_init.span.clone(),
} })
} }
fn reduce_circuit_member_access( fn reduce_circuit_member_access(
@ -186,12 +227,13 @@ pub trait ReconstructingReducer {
circuit_member_access: &CircuitMemberAccessExpression, circuit_member_access: &CircuitMemberAccessExpression,
circuit: Expression, circuit: Expression,
name: Identifier, name: Identifier,
) -> CircuitMemberAccessExpression { _in_circuit: bool,
CircuitMemberAccessExpression { ) -> Result<CircuitMemberAccessExpression> {
Ok(CircuitMemberAccessExpression {
circuit: Box::new(circuit), circuit: Box::new(circuit),
name, name,
span: circuit_member_access.span.clone(), span: circuit_member_access.span.clone(),
} })
} }
fn reduce_circuit_static_fn_access( fn reduce_circuit_static_fn_access(
@ -199,12 +241,13 @@ pub trait ReconstructingReducer {
circuit_static_fn_access: &CircuitStaticFunctionAccessExpression, circuit_static_fn_access: &CircuitStaticFunctionAccessExpression,
circuit: Expression, circuit: Expression,
name: Identifier, name: Identifier,
) -> CircuitStaticFunctionAccessExpression { _in_circuit: bool,
CircuitStaticFunctionAccessExpression { ) -> Result<CircuitStaticFunctionAccessExpression> {
Ok(CircuitStaticFunctionAccessExpression {
circuit: Box::new(circuit), circuit: Box::new(circuit),
name, name,
span: circuit_static_fn_access.span.clone(), span: circuit_static_fn_access.span.clone(),
} })
} }
fn reduce_call( fn reduce_call(
@ -212,32 +255,38 @@ pub trait ReconstructingReducer {
call: &CallExpression, call: &CallExpression,
function: Expression, function: Expression,
arguments: Vec<Expression>, arguments: Vec<Expression>,
) -> CallExpression { _in_circuit: bool,
CallExpression { ) -> Result<CallExpression> {
Ok(CallExpression {
function: Box::new(function), function: Box::new(function),
arguments, arguments,
span: call.span.clone(), span: call.span.clone(),
} })
} }
// Statements // Statements
fn reduce_statement(&mut self, _statement: &Statement, new: Statement, _in_circuit: bool) -> Statement { fn reduce_statement(&mut self, _statement: &Statement, new: Statement, _in_circuit: bool) -> Result<Statement> {
new Ok(new)
} }
fn reduce_return(&mut self, return_statement: &ReturnStatement, expression: Expression) -> ReturnStatement { fn reduce_return(
ReturnStatement { &mut self,
return_statement: &ReturnStatement,
expression: Expression,
_in_circuit: bool,
) -> Result<ReturnStatement> {
Ok(ReturnStatement {
expression, expression,
span: return_statement.span.clone(), span: return_statement.span.clone(),
} })
} }
fn reduce_variable_name(&mut self, variable_name: &VariableName, identifier: Identifier) -> VariableName { fn reduce_variable_name(&mut self, variable_name: &VariableName, identifier: Identifier) -> Result<VariableName> {
VariableName { Ok(VariableName {
mutable: variable_name.mutable, mutable: variable_name.mutable,
identifier, identifier,
span: variable_name.span.clone(), span: variable_name.span.clone(),
} })
} }
fn reduce_definition( fn reduce_definition(
@ -247,18 +296,23 @@ pub trait ReconstructingReducer {
type_: Option<Type>, type_: Option<Type>,
value: Expression, value: Expression,
_in_circuit: bool, _in_circuit: bool,
) -> DefinitionStatement { ) -> Result<DefinitionStatement> {
DefinitionStatement { Ok(DefinitionStatement {
declaration_type: definition.declaration_type.clone(), declaration_type: definition.declaration_type.clone(),
variable_names, variable_names,
type_, type_,
value, value,
span: definition.span.clone(), span: definition.span.clone(),
} })
} }
fn reduce_assignee_access(&mut self, _access: &AssigneeAccess, new: AssigneeAccess) -> AssigneeAccess { fn reduce_assignee_access(
new &mut self,
_access: &AssigneeAccess,
new: AssigneeAccess,
_in_circuit: bool,
) -> Result<AssigneeAccess> {
Ok(new)
} }
fn reduce_assignee( fn reduce_assignee(
@ -266,21 +320,28 @@ pub trait ReconstructingReducer {
assignee: &Assignee, assignee: &Assignee,
identifier: Identifier, identifier: Identifier,
accesses: Vec<AssigneeAccess>, accesses: Vec<AssigneeAccess>,
) -> Assignee { _in_circuit: bool,
Assignee { ) -> Result<Assignee> {
Ok(Assignee {
identifier, identifier,
accesses, accesses,
span: assignee.span.clone(), span: assignee.span.clone(),
} })
} }
fn reduce_assign(&mut self, assign: &AssignStatement, assignee: Assignee, value: Expression) -> AssignStatement { fn reduce_assign(
AssignStatement { &mut self,
assign: &AssignStatement,
assignee: Assignee,
value: Expression,
_in_circuit: bool,
) -> Result<AssignStatement> {
Ok(AssignStatement {
operation: assign.operation.clone(), operation: assign.operation.clone(),
assignee, assignee,
value, value,
span: assign.span.clone(), span: assign.span.clone(),
} })
} }
fn reduce_conditional( fn reduce_conditional(
@ -290,13 +351,13 @@ pub trait ReconstructingReducer {
block: Block, block: Block,
statement: Option<Statement>, statement: Option<Statement>,
_in_circuit: bool, _in_circuit: bool,
) -> ConditionalStatement { ) -> Result<ConditionalStatement> {
ConditionalStatement { Ok(ConditionalStatement {
condition, condition,
block, block,
next: statement.map(|statement| Box::new(statement)), next: statement.map(|statement| Box::new(statement)),
span: conditional.span.clone(), span: conditional.span.clone(),
} })
} }
fn reduce_iteration( fn reduce_iteration(
@ -307,39 +368,45 @@ pub trait ReconstructingReducer {
stop: Expression, stop: Expression,
block: Block, block: Block,
_in_circuit: bool, _in_circuit: bool,
) -> IterationStatement { ) -> Result<IterationStatement> {
IterationStatement { Ok(IterationStatement {
variable, variable,
start, start,
stop, stop,
block, block,
span: iteration.span.clone(), span: iteration.span.clone(),
} })
} }
fn reduce_console(&mut self, console: &ConsoleStatement, function: ConsoleFunction) -> ConsoleStatement { fn reduce_console(
ConsoleStatement { &mut self,
console: &ConsoleStatement,
function: ConsoleFunction,
_in_circuit: bool,
) -> Result<ConsoleStatement> {
Ok(ConsoleStatement {
function, function,
span: console.span.clone(), span: console.span.clone(),
} })
} }
fn reduce_expression_statement( fn reduce_expression_statement(
&mut self, &mut self,
expression_statement: &ExpressionStatement, expression_statement: &ExpressionStatement,
expression: Expression, expression: Expression,
) -> ExpressionStatement { _in_circuit: bool,
ExpressionStatement { ) -> Result<ExpressionStatement> {
Ok(ExpressionStatement {
expression, expression,
span: expression_statement.span.clone(), span: expression_statement.span.clone(),
} })
} }
fn reduce_block(&mut self, block: &Block, statements: Vec<Statement>, _in_circuit: bool) -> Block { fn reduce_block(&mut self, block: &Block, statements: Vec<Statement>, _in_circuit: bool) -> Result<Block> {
Block { Ok(Block {
statements, statements,
span: block.span.clone(), span: block.span.clone(),
} })
} }
// Program // Program
@ -350,14 +417,14 @@ pub trait ReconstructingReducer {
imports: Vec<ImportStatement>, imports: Vec<ImportStatement>,
circuits: IndexMap<Identifier, Circuit>, circuits: IndexMap<Identifier, Circuit>,
functions: IndexMap<Identifier, Function>, functions: IndexMap<Identifier, Function>,
) -> Program { ) -> Result<Program> {
Program { Ok(Program {
name: program.name.clone(), name: program.name.clone(),
expected_input, expected_input,
imports, imports,
circuits, circuits,
functions, functions,
} })
} }
fn reduce_function_input_variable( fn reduce_function_input_variable(
@ -366,14 +433,14 @@ pub trait ReconstructingReducer {
identifier: Identifier, identifier: Identifier,
type_: Type, type_: Type,
_in_circuit: bool, _in_circuit: bool,
) -> FunctionInputVariable { ) -> Result<FunctionInputVariable> {
FunctionInputVariable { Ok(FunctionInputVariable {
identifier, identifier,
const_: variable.const_, const_: variable.const_,
mutable: variable.mutable, mutable: variable.mutable,
type_, type_,
span: variable.span.clone(), span: variable.span.clone(),
} })
} }
fn reduce_function_input( fn reduce_function_input(
@ -381,39 +448,48 @@ pub trait ReconstructingReducer {
_input: &FunctionInput, _input: &FunctionInput,
new: FunctionInput, new: FunctionInput,
_in_circuit: bool, _in_circuit: bool,
) -> FunctionInput { ) -> Result<FunctionInput> {
new Ok(new)
} }
fn reduce_package_or_packages( fn reduce_package_or_packages(
&mut self, &mut self,
_package_or_packages: &PackageOrPackages, _package_or_packages: &PackageOrPackages,
new: PackageOrPackages, new: PackageOrPackages,
) -> PackageOrPackages { ) -> Result<PackageOrPackages> {
new Ok(new)
} }
fn reduce_import(&mut self, import: &ImportStatement, package_or_packages: PackageOrPackages) -> ImportStatement { fn reduce_import(
ImportStatement { &mut self,
import: &ImportStatement,
package_or_packages: PackageOrPackages,
) -> Result<ImportStatement> {
Ok(ImportStatement {
package_or_packages, package_or_packages,
span: import.span.clone(), span: import.span.clone(),
} })
} }
fn reduce_circuit_member(&mut self, _circuit_member: &CircuitMember, new: CircuitMember) -> CircuitMember { fn reduce_circuit_member(&mut self, _circuit_member: &CircuitMember, new: CircuitMember) -> Result<CircuitMember> {
new Ok(new)
} }
fn reduce_circuit(&mut self, _circuit: &Circuit, circuit_name: Identifier, members: Vec<CircuitMember>) -> Circuit { fn reduce_circuit(
Circuit { circuit_name, members } &mut self,
_circuit: &Circuit,
circuit_name: Identifier,
members: Vec<CircuitMember>,
) -> Result<Circuit> {
Ok(Circuit { circuit_name, members })
} }
fn reduce_annotation(&mut self, annotation: &Annotation, name: Identifier) -> Annotation { fn reduce_annotation(&mut self, annotation: &Annotation, name: Identifier) -> Result<Annotation> {
Annotation { Ok(Annotation {
span: annotation.span.clone(), span: annotation.span.clone(),
name, name,
arguments: annotation.arguments.clone(), arguments: annotation.arguments.clone(),
} })
} }
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
@ -426,14 +502,14 @@ pub trait ReconstructingReducer {
output: Option<Type>, output: Option<Type>,
block: Block, block: Block,
_in_circuit: bool, _in_circuit: bool,
) -> Function { ) -> Result<Function> {
Function { Ok(Function {
identifier, identifier,
annotations, annotations,
input, input,
output, output,
block, block,
span: function.span.clone(), span: function.span.clone(),
} })
} }
} }