More generic errors for reducing

This commit is contained in:
gluax 2021-04-12 12:33:06 -04:00
parent bddcef732f
commit 214d9e0f8c
11 changed files with 237 additions and 166 deletions

View File

@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License // 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/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{FormattedError, Span}; use crate::{FormattedError, LeoError, Span};
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum CanonicalizeError { pub enum CanonicalizeError {
@ -22,6 +22,8 @@ pub enum CanonicalizeError {
Error(#[from] FormattedError), Error(#[from] FormattedError),
} }
impl LeoError for CanonicalizeError {}
impl CanonicalizeError { impl CanonicalizeError {
fn new_from_span(message: String, span: &Span) -> Self { fn new_from_span(message: String, span: &Span) -> Self {
CanonicalizeError::Error(FormattedError::new_from_span(message, span)) CanonicalizeError::Error(FormattedError::new_from_span(message, span))

View File

@ -0,0 +1,37 @@
// Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 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::{FormattedError, LeoError, Span};
#[derive(Debug, Error)]
pub enum CombinerError {
#[error("{}", _0)]
Error(#[from] FormattedError),
}
impl LeoError for CombinerError {}
impl CombinerError {
fn new_from_span(message: String, span: &Span) -> Self {
CombinerError::Error(FormattedError::new_from_span(message, span))
}
pub fn asg_statement_not_block(span: &Span) -> Self {
let message = "AstStatement should be be a block".to_string();
Self::new_from_span(message, span)
}
}

View File

@ -14,7 +14,16 @@
// You should have received a copy of the GNU General Public License // 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/>. // along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
pub mod canonicalization;
pub use canonicalization::*;
pub mod combiner;
pub use combiner::*;
pub mod error; pub mod error;
pub use error::*; pub use error::*;
pub mod reducer;
pub use reducer::*;
pub trait LeoError {} pub trait LeoError {}

43
ast/src/errors/reducer.rs Normal file
View File

@ -0,0 +1,43 @@
// Copyright (C) 2019-2021 Aleo Systems Inc.
// This file is part of the Leo library.
// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 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::{CanonicalizeError, CombinerError, FormattedError, LeoError, Span};
#[derive(Debug, Error)]
pub enum ReducerError {
#[error("{}", _0)]
Error(#[from] FormattedError),
#[error("{}", _0)]
CanonicalizeError(#[from] CanonicalizeError),
#[error("{}", _0)]
CombinerError(#[from] CombinerError),
}
impl LeoError for ReducerError {}
impl ReducerError {
fn new_from_span(message: String, span: &Span) -> Self {
ReducerError::Error(FormattedError::new_from_span(message, span))
}
pub fn impossible_console_assert_call(span: &Span) -> Self {
let message = "Console::Assert cannot be matched here, its handled in another case.".to_string();
Self::new_from_span(message, span)
}
}

View File

@ -83,7 +83,7 @@ impl Ast {
} }
/// Mutates the program ast by preforming canonicalization on it. /// Mutates the program ast by preforming canonicalization on it.
pub fn canonicalize(&mut self) -> Result<(), CanonicalizeError> { pub fn canonicalize(&mut self) -> Result<(), ReducerError> {
self.ast = ReconstructingDirector::new(Canonicalizer::default()).reduce_program(self.as_repr())?; self.ast = ReconstructingDirector::new(Canonicalizer::default()).reduce_program(self.as_repr())?;
Ok(()) Ok(())
} }

View File

@ -394,11 +394,13 @@ impl ReconstructingReducer for Canonicalizer {
self.in_circuit = !self.in_circuit; self.in_circuit = !self.in_circuit;
} }
fn reduce_type(&mut self, _type_: &Type, new: Type, span: &Span) -> Result<Type, CanonicalizeError> { fn reduce_type(&mut self, _type_: &Type, new: Type, span: &Span) -> Result<Type, ReducerError> {
match new { match new {
Type::Array(type_, mut dimensions) => { Type::Array(type_, mut dimensions) => {
if dimensions.is_zero() { if dimensions.is_zero() {
return Err(CanonicalizeError::invalid_array_dimension_size(span)); return Err(ReducerError::from(CanonicalizeError::invalid_array_dimension_size(
span,
)));
} }
let mut next = Type::Array(type_, ArrayDimensions(vec![dimensions.remove_last().unwrap()])); let mut next = Type::Array(type_, ArrayDimensions(vec![dimensions.remove_last().unwrap()]));
@ -415,7 +417,9 @@ impl ReconstructingReducer for Canonicalizer {
Ok(array) Ok(array)
} }
Type::SelfType if !self.in_circuit => Err(CanonicalizeError::big_self_outside_of_circuit(span)), Type::SelfType if !self.in_circuit => {
Err(ReducerError::from(CanonicalizeError::big_self_outside_of_circuit(span)))
}
_ => Ok(new.clone()), _ => Ok(new.clone()),
} }
} }
@ -424,9 +428,11 @@ impl ReconstructingReducer for Canonicalizer {
&mut self, &mut self,
array_init: &ArrayInitExpression, array_init: &ArrayInitExpression,
element: Expression, element: Expression,
) -> Result<ArrayInitExpression, CanonicalizeError> { ) -> Result<ArrayInitExpression, ReducerError> {
if array_init.dimensions.is_zero() { if array_init.dimensions.is_zero() {
return Err(CanonicalizeError::invalid_array_dimension_size(&array_init.span)); return Err(ReducerError::from(CanonicalizeError::invalid_array_dimension_size(
&array_init.span,
)));
} }
let element = Box::new(element); let element = Box::new(element);
@ -473,7 +479,7 @@ impl ReconstructingReducer for Canonicalizer {
assign: &AssignStatement, assign: &AssignStatement,
assignee: Assignee, assignee: Assignee,
value: Expression, value: Expression,
) -> Result<AssignStatement, CanonicalizeError> { ) -> Result<AssignStatement, ReducerError> {
match value { match value {
Expression::Value(value_expr) if assign.operation != AssignOperation::Assign => { Expression::Value(value_expr) if assign.operation != AssignOperation::Assign => {
let mut left = Box::new(Expression::Identifier(assignee.identifier.clone())); let mut left = Box::new(Expression::Identifier(assignee.identifier.clone()));
@ -551,7 +557,7 @@ impl ReconstructingReducer for Canonicalizer {
input: Vec<FunctionInput>, input: Vec<FunctionInput>,
output: Option<Type>, output: Option<Type>,
block: Block, block: Block,
) -> Result<Function, CanonicalizeError> { ) -> Result<Function, ReducerError> {
let new_output = match output { let new_output = match output {
None => Some(Type::Tuple(vec![])), None => Some(Type::Tuple(vec![])),
_ => output, _ => output,
@ -572,7 +578,7 @@ impl ReconstructingReducer for Canonicalizer {
_circuit: &Circuit, _circuit: &Circuit,
circuit_name: Identifier, circuit_name: Identifier,
members: Vec<CircuitMember>, members: Vec<CircuitMember>,
) -> Result<Circuit, CanonicalizeError> { ) -> Result<Circuit, ReducerError> {
self.circuit_name = Some(circuit_name.clone()); self.circuit_name = Some(circuit_name.clone());
let circ = Circuit { let circ = Circuit {
circuit_name, circuit_name,

View File

@ -17,9 +17,6 @@
mod canonicalization; mod canonicalization;
pub use canonicalization::*; pub use canonicalization::*;
mod errors;
pub use errors::*;
mod reconstructing_reducer; mod reconstructing_reducer;
pub use reconstructing_reducer::*; pub use reconstructing_reducer::*;

View File

@ -29,7 +29,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
Self { reducer } Self { reducer }
} }
pub fn reduce_type(&mut self, type_: &Type, span: &Span) -> Result<Type, CanonicalizeError> { pub fn reduce_type(&mut self, type_: &Type, span: &Span) -> Result<Type, ReducerError> {
let new = match type_ { let new = match type_ {
Type::Array(type_, dimensions) => Type::Array(Box::new(self.reduce_type(type_, span)?), dimensions.clone()), Type::Array(type_, dimensions) => Type::Array(Box::new(self.reduce_type(type_, span)?), dimensions.clone()),
Type::Tuple(types) => { Type::Tuple(types) => {
@ -48,7 +48,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
} }
// Expressions // Expressions
pub fn reduce_expression(&mut self, expression: &Expression) -> Result<Expression, CanonicalizeError> { pub fn reduce_expression(&mut self, expression: &Expression) -> Result<Expression, ReducerError> {
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)?),
@ -83,15 +83,15 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_expression(expression, new) self.reducer.reduce_expression(expression, new)
} }
pub fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier, CanonicalizeError> { pub fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier, ReducerError> {
self.reducer.reduce_identifier(identifier) self.reducer.reduce_identifier(identifier)
} }
pub fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> Result<GroupTuple, CanonicalizeError> { pub fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> Result<GroupTuple, ReducerError> {
self.reducer.reduce_group_tuple(group_tuple) self.reducer.reduce_group_tuple(group_tuple)
} }
pub fn reduce_group_value(&mut self, group_value: &GroupValue) -> Result<GroupValue, CanonicalizeError> { pub fn reduce_group_value(&mut self, group_value: &GroupValue) -> Result<GroupValue, ReducerError> {
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(),
@ -100,7 +100,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_group_value(group_value, new) self.reducer.reduce_group_value(group_value, new)
} }
pub fn reduce_value(&mut self, value: &ValueExpression) -> Result<ValueExpression, CanonicalizeError> { pub fn reduce_value(&mut self, value: &ValueExpression) -> Result<ValueExpression, ReducerError> {
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)?))
@ -111,20 +111,20 @@ 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) -> Result<BinaryExpression, CanonicalizeError> { pub fn reduce_binary(&mut self, binary: &BinaryExpression) -> Result<BinaryExpression, ReducerError> {
let left = self.reduce_expression(&binary.left)?; let left = self.reduce_expression(&binary.left)?;
let right = self.reduce_expression(&binary.right)?; let right = self.reduce_expression(&binary.right)?;
self.reducer.reduce_binary(binary, left, right, binary.op.clone()) self.reducer.reduce_binary(binary, left, right, binary.op.clone())
} }
pub fn reduce_unary(&mut self, unary: &UnaryExpression) -> Result<UnaryExpression, CanonicalizeError> { pub fn reduce_unary(&mut self, unary: &UnaryExpression) -> Result<UnaryExpression, ReducerError> {
let inner = self.reduce_expression(&unary.inner)?; let inner = self.reduce_expression(&unary.inner)?;
self.reducer.reduce_unary(unary, inner, unary.op.clone()) self.reducer.reduce_unary(unary, inner, unary.op.clone())
} }
pub fn reduce_ternary(&mut self, ternary: &TernaryExpression) -> Result<TernaryExpression, CanonicalizeError> { pub fn reduce_ternary(&mut self, ternary: &TernaryExpression) -> Result<TernaryExpression, ReducerError> {
let condition = self.reduce_expression(&ternary.condition)?; let condition = self.reduce_expression(&ternary.condition)?;
let if_true = self.reduce_expression(&ternary.if_true)?; let if_true = self.reduce_expression(&ternary.if_true)?;
let if_false = self.reduce_expression(&ternary.if_false)?; let if_false = self.reduce_expression(&ternary.if_false)?;
@ -132,7 +132,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_ternary(ternary, condition, if_true, if_false) self.reducer.reduce_ternary(ternary, condition, if_true, if_false)
} }
pub fn reduce_cast(&mut self, cast: &CastExpression) -> Result<CastExpression, CanonicalizeError> { pub fn reduce_cast(&mut self, cast: &CastExpression) -> Result<CastExpression, ReducerError> {
let inner = self.reduce_expression(&cast.inner)?; let inner = self.reduce_expression(&cast.inner)?;
let target_type = self.reduce_type(&cast.target_type, &cast.span)?; let target_type = self.reduce_type(&cast.target_type, &cast.span)?;
@ -142,7 +142,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_array_inline( pub fn reduce_array_inline(
&mut self, &mut self,
array_inline: &ArrayInlineExpression, array_inline: &ArrayInlineExpression,
) -> Result<ArrayInlineExpression, CanonicalizeError> { ) -> Result<ArrayInlineExpression, ReducerError> {
let mut elements = vec![]; let mut elements = vec![];
for element in array_inline.elements.iter() { for element in array_inline.elements.iter() {
let reduced_element = match element { let reduced_element = match element {
@ -160,10 +160,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_array_inline(array_inline, elements) self.reducer.reduce_array_inline(array_inline, elements)
} }
pub fn reduce_array_init( pub fn reduce_array_init(&mut self, array_init: &ArrayInitExpression) -> Result<ArrayInitExpression, ReducerError> {
&mut self,
array_init: &ArrayInitExpression,
) -> Result<ArrayInitExpression, CanonicalizeError> {
let element = self.reduce_expression(&array_init.element)?; let element = self.reduce_expression(&array_init.element)?;
self.reducer.reduce_array_init(array_init, element) self.reducer.reduce_array_init(array_init, element)
@ -172,7 +169,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_array_access( pub fn reduce_array_access(
&mut self, &mut self,
array_access: &ArrayAccessExpression, array_access: &ArrayAccessExpression,
) -> Result<ArrayAccessExpression, CanonicalizeError> { ) -> Result<ArrayAccessExpression, ReducerError> {
let array = self.reduce_expression(&array_access.array)?; let array = self.reduce_expression(&array_access.array)?;
let index = self.reduce_expression(&array_access.index)?; let index = self.reduce_expression(&array_access.index)?;
@ -182,7 +179,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_array_range_access( pub fn reduce_array_range_access(
&mut self, &mut self,
array_range_access: &ArrayRangeAccessExpression, array_range_access: &ArrayRangeAccessExpression,
) -> Result<ArrayRangeAccessExpression, CanonicalizeError> { ) -> Result<ArrayRangeAccessExpression, ReducerError> {
let array = self.reduce_expression(&array_range_access.array)?; let array = self.reduce_expression(&array_range_access.array)?;
let left = array_range_access let left = array_range_access
.left .left
@ -199,10 +196,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
.reduce_array_range_access(array_range_access, array, left, right) .reduce_array_range_access(array_range_access, array, left, right)
} }
pub fn reduce_tuple_init( pub fn reduce_tuple_init(&mut self, tuple_init: &TupleInitExpression) -> Result<TupleInitExpression, ReducerError> {
&mut self,
tuple_init: &TupleInitExpression,
) -> Result<TupleInitExpression, CanonicalizeError> {
let mut elements = vec![]; let mut elements = vec![];
for element in tuple_init.elements.iter() { for element in tuple_init.elements.iter() {
elements.push(self.reduce_expression(element)?); elements.push(self.reduce_expression(element)?);
@ -214,7 +208,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_tuple_access( pub fn reduce_tuple_access(
&mut self, &mut self,
tuple_access: &TupleAccessExpression, tuple_access: &TupleAccessExpression,
) -> Result<TupleAccessExpression, CanonicalizeError> { ) -> Result<TupleAccessExpression, ReducerError> {
let tuple = self.reduce_expression(&tuple_access.tuple)?; let tuple = self.reduce_expression(&tuple_access.tuple)?;
self.reducer.reduce_tuple_access(tuple_access, tuple) self.reducer.reduce_tuple_access(tuple_access, tuple)
@ -223,7 +217,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_circuit_implied_variable_definition( pub fn reduce_circuit_implied_variable_definition(
&mut self, &mut self,
variable: &CircuitImpliedVariableDefinition, variable: &CircuitImpliedVariableDefinition,
) -> Result<CircuitImpliedVariableDefinition, CanonicalizeError> { ) -> Result<CircuitImpliedVariableDefinition, ReducerError> {
let identifier = self.reduce_identifier(&variable.identifier)?; let identifier = self.reduce_identifier(&variable.identifier)?;
let expression = variable let expression = variable
.expression .expression
@ -238,7 +232,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_circuit_init( pub fn reduce_circuit_init(
&mut self, &mut self,
circuit_init: &CircuitInitExpression, circuit_init: &CircuitInitExpression,
) -> Result<CircuitInitExpression, CanonicalizeError> { ) -> Result<CircuitInitExpression, ReducerError> {
let name = self.reduce_identifier(&circuit_init.name)?; let name = self.reduce_identifier(&circuit_init.name)?;
let mut members = vec![]; let mut members = vec![];
@ -252,7 +246,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_circuit_member_access( pub fn reduce_circuit_member_access(
&mut self, &mut self,
circuit_member_access: &CircuitMemberAccessExpression, circuit_member_access: &CircuitMemberAccessExpression,
) -> Result<CircuitMemberAccessExpression, CanonicalizeError> { ) -> Result<CircuitMemberAccessExpression, ReducerError> {
let circuit = self.reduce_expression(&circuit_member_access.circuit)?; let circuit = self.reduce_expression(&circuit_member_access.circuit)?;
let name = self.reduce_identifier(&circuit_member_access.name)?; let name = self.reduce_identifier(&circuit_member_access.name)?;
@ -263,7 +257,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
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,
) -> Result<CircuitStaticFunctionAccessExpression, CanonicalizeError> { ) -> Result<CircuitStaticFunctionAccessExpression, ReducerError> {
let circuit = self.reduce_expression(&circuit_static_fn_access.circuit)?; let circuit = self.reduce_expression(&circuit_static_fn_access.circuit)?;
let name = self.reduce_identifier(&circuit_static_fn_access.name)?; let name = self.reduce_identifier(&circuit_static_fn_access.name)?;
@ -271,7 +265,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
.reduce_circuit_static_fn_access(circuit_static_fn_access, circuit, name) .reduce_circuit_static_fn_access(circuit_static_fn_access, circuit, name)
} }
pub fn reduce_call(&mut self, call: &CallExpression) -> Result<CallExpression, CanonicalizeError> { pub fn reduce_call(&mut self, call: &CallExpression) -> Result<CallExpression, ReducerError> {
let function = self.reduce_expression(&call.function)?; let function = self.reduce_expression(&call.function)?;
let mut arguments = vec![]; let mut arguments = vec![];
@ -283,7 +277,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
} }
// Statements // Statements
pub fn reduce_statement(&mut self, statement: &Statement) -> Result<Statement, CanonicalizeError> { pub fn reduce_statement(&mut self, statement: &Statement) -> Result<Statement, ReducerError> {
let new = match statement { let new = match statement {
Statement::Return(return_statement) => Statement::Return(self.reduce_return(&return_statement)?), Statement::Return(return_statement) => Statement::Return(self.reduce_return(&return_statement)?),
Statement::Definition(definition) => Statement::Definition(self.reduce_definition(&definition)?), Statement::Definition(definition) => Statement::Definition(self.reduce_definition(&definition)?),
@ -298,22 +292,19 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_statement(statement, new) self.reducer.reduce_statement(statement, new)
} }
pub fn reduce_return(&mut self, return_statement: &ReturnStatement) -> Result<ReturnStatement, CanonicalizeError> { pub fn reduce_return(&mut self, return_statement: &ReturnStatement) -> Result<ReturnStatement, ReducerError> {
let expression = self.reduce_expression(&return_statement.expression)?; let expression = self.reduce_expression(&return_statement.expression)?;
self.reducer.reduce_return(return_statement, expression) self.reducer.reduce_return(return_statement, expression)
} }
pub fn reduce_variable_name(&mut self, variable_name: &VariableName) -> Result<VariableName, CanonicalizeError> { pub fn reduce_variable_name(&mut self, variable_name: &VariableName) -> Result<VariableName, ReducerError> {
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( pub fn reduce_definition(&mut self, definition: &DefinitionStatement) -> Result<DefinitionStatement, ReducerError> {
&mut self,
definition: &DefinitionStatement,
) -> Result<DefinitionStatement, CanonicalizeError> {
let mut variable_names = vec![]; let mut variable_names = vec![];
for variable_name in definition.variable_names.iter() { for variable_name in definition.variable_names.iter() {
variable_names.push(self.reduce_variable_name(variable_name)?); variable_names.push(self.reduce_variable_name(variable_name)?);
@ -330,7 +321,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_definition(definition, variable_names, type_, value) self.reducer.reduce_definition(definition, variable_names, type_, value)
} }
pub fn reduce_assignee_access(&mut self, access: &AssigneeAccess) -> Result<AssigneeAccess, CanonicalizeError> { pub fn reduce_assignee_access(&mut self, access: &AssigneeAccess) -> Result<AssigneeAccess, ReducerError> {
let new = match access { let new = match access {
AssigneeAccess::ArrayRange(left, right) => { AssigneeAccess::ArrayRange(left, right) => {
let left = left.as_ref().map(|left| self.reduce_expression(left)).transpose()?; let left = left.as_ref().map(|left| self.reduce_expression(left)).transpose()?;
@ -346,7 +337,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_assignee_access(access, new) self.reducer.reduce_assignee_access(access, new)
} }
pub fn reduce_assignee(&mut self, assignee: &Assignee) -> Result<Assignee, CanonicalizeError> { pub fn reduce_assignee(&mut self, assignee: &Assignee) -> Result<Assignee, ReducerError> {
let identifier = self.reduce_identifier(&assignee.identifier)?; let identifier = self.reduce_identifier(&assignee.identifier)?;
let mut accesses = vec![]; let mut accesses = vec![];
@ -357,7 +348,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_assignee(assignee, identifier, accesses) self.reducer.reduce_assignee(assignee, identifier, accesses)
} }
pub fn reduce_assign(&mut self, assign: &AssignStatement) -> Result<AssignStatement, CanonicalizeError> { pub fn reduce_assign(&mut self, assign: &AssignStatement) -> Result<AssignStatement, ReducerError> {
let assignee = self.reduce_assignee(&assign.assignee)?; let assignee = self.reduce_assignee(&assign.assignee)?;
let value = self.reduce_expression(&assign.value)?; let value = self.reduce_expression(&assign.value)?;
@ -367,7 +358,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_conditional( pub fn reduce_conditional(
&mut self, &mut self,
conditional: &ConditionalStatement, conditional: &ConditionalStatement,
) -> Result<ConditionalStatement, CanonicalizeError> { ) -> Result<ConditionalStatement, ReducerError> {
let condition = self.reduce_expression(&conditional.condition)?; let condition = self.reduce_expression(&conditional.condition)?;
let block = self.reduce_block(&conditional.block)?; let block = self.reduce_block(&conditional.block)?;
let next = conditional let next = conditional
@ -379,10 +370,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_conditional(conditional, condition, block, next) self.reducer.reduce_conditional(conditional, condition, block, next)
} }
pub fn reduce_iteration( pub fn reduce_iteration(&mut self, iteration: &IterationStatement) -> Result<IterationStatement, ReducerError> {
&mut self,
iteration: &IterationStatement,
) -> Result<IterationStatement, CanonicalizeError> {
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)?;
let stop = self.reduce_expression(&iteration.stop)?; let stop = self.reduce_expression(&iteration.stop)?;
@ -394,7 +382,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_console( pub fn reduce_console(
&mut self, &mut self,
console_function_call: &ConsoleStatement, console_function_call: &ConsoleStatement,
) -> Result<ConsoleStatement, CanonicalizeError> { ) -> Result<ConsoleStatement, ReducerError> {
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)?),
ConsoleFunction::Debug(format) | ConsoleFunction::Error(format) | ConsoleFunction::Log(format) => { ConsoleFunction::Debug(format) | ConsoleFunction::Error(format) | ConsoleFunction::Log(format) => {
@ -413,7 +401,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
ConsoleFunction::Debug(_) => ConsoleFunction::Debug(formatted), ConsoleFunction::Debug(_) => ConsoleFunction::Debug(formatted),
ConsoleFunction::Error(_) => ConsoleFunction::Error(formatted), ConsoleFunction::Error(_) => ConsoleFunction::Error(formatted),
ConsoleFunction::Log(_) => ConsoleFunction::Log(formatted), ConsoleFunction::Log(_) => ConsoleFunction::Log(formatted),
_ => unimplemented!(), // impossible _ => return Err(ReducerError::impossible_console_assert_call(&format.span)),
} }
} }
}; };
@ -424,12 +412,12 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_expression_statement( pub fn reduce_expression_statement(
&mut self, &mut self,
expression: &ExpressionStatement, expression: &ExpressionStatement,
) -> Result<ExpressionStatement, CanonicalizeError> { ) -> Result<ExpressionStatement, ReducerError> {
let inner_expression = self.reduce_expression(&expression.expression)?; let inner_expression = self.reduce_expression(&expression.expression)?;
self.reducer.reduce_expression_statement(expression, inner_expression) self.reducer.reduce_expression_statement(expression, inner_expression)
} }
pub fn reduce_block(&mut self, block: &Block) -> Result<Block, CanonicalizeError> { pub fn reduce_block(&mut self, block: &Block) -> Result<Block, ReducerError> {
let mut statements = vec![]; let mut statements = vec![];
for statement in block.statements.iter() { for statement in block.statements.iter() {
statements.push(self.reduce_statement(statement)?); statements.push(self.reduce_statement(statement)?);
@ -439,7 +427,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
} }
// Program // Program
pub fn reduce_program(&mut self, program: &Program) -> Result<Program, CanonicalizeError> { pub fn reduce_program(&mut self, program: &Program) -> Result<Program, ReducerError> {
let mut inputs = vec![]; let mut inputs = vec![];
for input in program.expected_input.iter() { for input in program.expected_input.iter() {
inputs.push(self.reduce_function_input(input)?); inputs.push(self.reduce_function_input(input)?);
@ -469,14 +457,14 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_function_input_variable( pub fn reduce_function_input_variable(
&mut self, &mut self,
variable: &FunctionInputVariable, variable: &FunctionInputVariable,
) -> Result<FunctionInputVariable, CanonicalizeError> { ) -> Result<FunctionInputVariable, ReducerError> {
let identifier = self.reduce_identifier(&variable.identifier)?; let identifier = self.reduce_identifier(&variable.identifier)?;
let type_ = self.reduce_type(&variable.type_, &variable.span)?; let type_ = self.reduce_type(&variable.type_, &variable.span)?;
self.reducer.reduce_function_input_variable(variable, identifier, type_) self.reducer.reduce_function_input_variable(variable, identifier, type_)
} }
pub fn reduce_function_input(&mut self, input: &FunctionInput) -> Result<FunctionInput, CanonicalizeError> { pub fn reduce_function_input(&mut self, input: &FunctionInput) -> Result<FunctionInput, ReducerError> {
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)?) FunctionInput::Variable(self.reduce_function_input_variable(function_input_variable)?)
@ -490,7 +478,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_package_or_packages( pub fn reduce_package_or_packages(
&mut self, &mut self,
package_or_packages: &PackageOrPackages, package_or_packages: &PackageOrPackages,
) -> Result<PackageOrPackages, CanonicalizeError> { ) -> Result<PackageOrPackages, ReducerError> {
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)?,
@ -507,16 +495,13 @@ 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) -> Result<ImportStatement, CanonicalizeError> { pub fn reduce_import(&mut self, import: &ImportStatement) -> Result<ImportStatement, ReducerError> {
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( pub fn reduce_circuit_member(&mut self, circuit_member: &CircuitMember) -> Result<CircuitMember, ReducerError> {
&mut self,
circuit_member: &CircuitMember,
) -> Result<CircuitMember, CanonicalizeError> {
let new = match circuit_member { let new = match circuit_member {
CircuitMember::CircuitVariable(identifier, type_) => CircuitMember::CircuitVariable( CircuitMember::CircuitVariable(identifier, type_) => CircuitMember::CircuitVariable(
self.reduce_identifier(&identifier)?, self.reduce_identifier(&identifier)?,
@ -530,7 +515,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_circuit_member(circuit_member, new) self.reducer.reduce_circuit_member(circuit_member, new)
} }
pub fn reduce_circuit(&mut self, circuit: &Circuit) -> Result<Circuit, CanonicalizeError> { pub fn reduce_circuit(&mut self, circuit: &Circuit) -> Result<Circuit, ReducerError> {
let circuit_name = self.reduce_identifier(&circuit.circuit_name)?; let circuit_name = self.reduce_identifier(&circuit.circuit_name)?;
let mut members = vec![]; let mut members = vec![];
@ -541,13 +526,13 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_circuit(circuit, circuit_name, members) self.reducer.reduce_circuit(circuit, circuit_name, members)
} }
fn reduce_annotation(&mut self, annotation: &Annotation) -> Result<Annotation, CanonicalizeError> { fn reduce_annotation(&mut self, annotation: &Annotation) -> Result<Annotation, ReducerError> {
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) -> Result<Function, CanonicalizeError> { pub fn reduce_function(&mut self, function: &Function) -> Result<Function, ReducerError> {
let identifier = self.reduce_identifier(&function.identifier)?; let identifier = self.reduce_identifier(&function.identifier)?;
let mut annotations = vec![]; let mut annotations = vec![];

View File

@ -23,27 +23,23 @@ pub trait ReconstructingReducer {
fn in_circuit(&self) -> bool; fn in_circuit(&self) -> bool;
fn swap_in_circuit(&mut self); fn swap_in_circuit(&mut self);
fn reduce_type(&mut self, _type_: &Type, new: Type, _span: &Span) -> Result<Type, CanonicalizeError> { fn reduce_type(&mut self, _type_: &Type, new: Type, _span: &Span) -> Result<Type, ReducerError> {
Ok(new) Ok(new)
} }
// Expressions // Expressions
fn reduce_expression( fn reduce_expression(&mut self, _expression: &Expression, new: Expression) -> Result<Expression, ReducerError> {
&mut self,
_expression: &Expression,
new: Expression,
) -> Result<Expression, CanonicalizeError> {
Ok(new) Ok(new)
} }
fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier, CanonicalizeError> { fn reduce_identifier(&mut self, identifier: &Identifier) -> Result<Identifier, ReducerError> {
Ok(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) -> Result<GroupTuple, CanonicalizeError> { fn reduce_group_tuple(&mut self, group_tuple: &GroupTuple) -> Result<GroupTuple, ReducerError> {
Ok(GroupTuple { Ok(GroupTuple {
x: group_tuple.x.clone(), x: group_tuple.x.clone(),
y: group_tuple.y.clone(), y: group_tuple.y.clone(),
@ -51,11 +47,7 @@ pub trait ReconstructingReducer {
}) })
} }
fn reduce_group_value( fn reduce_group_value(&mut self, _group_value: &GroupValue, new: GroupValue) -> Result<GroupValue, ReducerError> {
&mut self,
_group_value: &GroupValue,
new: GroupValue,
) -> Result<GroupValue, CanonicalizeError> {
Ok(new) Ok(new)
} }
@ -63,7 +55,7 @@ pub trait ReconstructingReducer {
&mut self, &mut self,
_value: &ValueExpression, _value: &ValueExpression,
new: ValueExpression, new: ValueExpression,
) -> Result<ValueExpression, CanonicalizeError> { ) -> Result<ValueExpression, ReducerError> {
Ok(new) Ok(new)
} }
@ -73,7 +65,7 @@ pub trait ReconstructingReducer {
left: Expression, left: Expression,
right: Expression, right: Expression,
op: BinaryOperation, op: BinaryOperation,
) -> Result<BinaryExpression, CanonicalizeError> { ) -> Result<BinaryExpression, ReducerError> {
Ok(BinaryExpression { Ok(BinaryExpression {
left: Box::new(left), left: Box::new(left),
right: Box::new(right), right: Box::new(right),
@ -87,7 +79,7 @@ pub trait ReconstructingReducer {
unary: &UnaryExpression, unary: &UnaryExpression,
inner: Expression, inner: Expression,
op: UnaryOperation, op: UnaryOperation,
) -> Result<UnaryExpression, CanonicalizeError> { ) -> Result<UnaryExpression, ReducerError> {
Ok(UnaryExpression { Ok(UnaryExpression {
inner: Box::new(inner), inner: Box::new(inner),
op, op,
@ -101,7 +93,7 @@ pub trait ReconstructingReducer {
condition: Expression, condition: Expression,
if_true: Expression, if_true: Expression,
if_false: Expression, if_false: Expression,
) -> Result<TernaryExpression, CanonicalizeError> { ) -> Result<TernaryExpression, ReducerError> {
Ok(TernaryExpression { Ok(TernaryExpression {
condition: Box::new(condition), condition: Box::new(condition),
if_true: Box::new(if_true), if_true: Box::new(if_true),
@ -115,7 +107,7 @@ pub trait ReconstructingReducer {
cast: &CastExpression, cast: &CastExpression,
inner: Expression, inner: Expression,
target_type: Type, target_type: Type,
) -> Result<CastExpression, CanonicalizeError> { ) -> Result<CastExpression, ReducerError> {
Ok(CastExpression { Ok(CastExpression {
inner: Box::new(inner), inner: Box::new(inner),
target_type, target_type,
@ -127,7 +119,7 @@ pub trait ReconstructingReducer {
&mut self, &mut self,
array_inline: &ArrayInlineExpression, array_inline: &ArrayInlineExpression,
elements: Vec<SpreadOrExpression>, elements: Vec<SpreadOrExpression>,
) -> Result<ArrayInlineExpression, CanonicalizeError> { ) -> Result<ArrayInlineExpression, ReducerError> {
Ok(ArrayInlineExpression { Ok(ArrayInlineExpression {
elements, elements,
span: array_inline.span.clone(), span: array_inline.span.clone(),
@ -138,7 +130,7 @@ pub trait ReconstructingReducer {
&mut self, &mut self,
array_init: &ArrayInitExpression, array_init: &ArrayInitExpression,
element: Expression, element: Expression,
) -> Result<ArrayInitExpression, CanonicalizeError> { ) -> Result<ArrayInitExpression, ReducerError> {
Ok(ArrayInitExpression { Ok(ArrayInitExpression {
element: Box::new(element), element: Box::new(element),
dimensions: array_init.dimensions.clone(), dimensions: array_init.dimensions.clone(),
@ -151,7 +143,7 @@ pub trait ReconstructingReducer {
array_access: &ArrayAccessExpression, array_access: &ArrayAccessExpression,
array: Expression, array: Expression,
index: Expression, index: Expression,
) -> Result<ArrayAccessExpression, CanonicalizeError> { ) -> Result<ArrayAccessExpression, ReducerError> {
Ok(ArrayAccessExpression { Ok(ArrayAccessExpression {
array: Box::new(array), array: Box::new(array),
index: Box::new(index), index: Box::new(index),
@ -165,7 +157,7 @@ pub trait ReconstructingReducer {
array: Expression, array: Expression,
left: Option<Expression>, left: Option<Expression>,
right: Option<Expression>, right: Option<Expression>,
) -> Result<ArrayRangeAccessExpression, CanonicalizeError> { ) -> Result<ArrayRangeAccessExpression, ReducerError> {
Ok(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)),
@ -178,7 +170,7 @@ pub trait ReconstructingReducer {
&mut self, &mut self,
tuple_init: &TupleInitExpression, tuple_init: &TupleInitExpression,
elements: Vec<Expression>, elements: Vec<Expression>,
) -> Result<TupleInitExpression, CanonicalizeError> { ) -> Result<TupleInitExpression, ReducerError> {
Ok(TupleInitExpression { Ok(TupleInitExpression {
elements, elements,
span: tuple_init.span.clone(), span: tuple_init.span.clone(),
@ -189,7 +181,7 @@ pub trait ReconstructingReducer {
&mut self, &mut self,
tuple_access: &TupleAccessExpression, tuple_access: &TupleAccessExpression,
tuple: Expression, tuple: Expression,
) -> Result<TupleAccessExpression, CanonicalizeError> { ) -> Result<TupleAccessExpression, ReducerError> {
Ok(TupleAccessExpression { Ok(TupleAccessExpression {
tuple: Box::new(tuple), tuple: Box::new(tuple),
index: tuple_access.index.clone(), index: tuple_access.index.clone(),
@ -202,7 +194,7 @@ pub trait ReconstructingReducer {
_variable: &CircuitImpliedVariableDefinition, _variable: &CircuitImpliedVariableDefinition,
identifier: Identifier, identifier: Identifier,
expression: Option<Expression>, expression: Option<Expression>,
) -> Result<CircuitImpliedVariableDefinition, CanonicalizeError> { ) -> Result<CircuitImpliedVariableDefinition, ReducerError> {
Ok(CircuitImpliedVariableDefinition { identifier, expression }) Ok(CircuitImpliedVariableDefinition { identifier, expression })
} }
@ -211,7 +203,7 @@ pub trait ReconstructingReducer {
circuit_init: &CircuitInitExpression, circuit_init: &CircuitInitExpression,
name: Identifier, name: Identifier,
members: Vec<CircuitImpliedVariableDefinition>, members: Vec<CircuitImpliedVariableDefinition>,
) -> Result<CircuitInitExpression, CanonicalizeError> { ) -> Result<CircuitInitExpression, ReducerError> {
Ok(CircuitInitExpression { Ok(CircuitInitExpression {
name, name,
members, members,
@ -224,7 +216,7 @@ pub trait ReconstructingReducer {
circuit_member_access: &CircuitMemberAccessExpression, circuit_member_access: &CircuitMemberAccessExpression,
circuit: Expression, circuit: Expression,
name: Identifier, name: Identifier,
) -> Result<CircuitMemberAccessExpression, CanonicalizeError> { ) -> Result<CircuitMemberAccessExpression, ReducerError> {
Ok(CircuitMemberAccessExpression { Ok(CircuitMemberAccessExpression {
circuit: Box::new(circuit), circuit: Box::new(circuit),
name, name,
@ -237,7 +229,7 @@ pub trait ReconstructingReducer {
circuit_static_fn_access: &CircuitStaticFunctionAccessExpression, circuit_static_fn_access: &CircuitStaticFunctionAccessExpression,
circuit: Expression, circuit: Expression,
name: Identifier, name: Identifier,
) -> Result<CircuitStaticFunctionAccessExpression, CanonicalizeError> { ) -> Result<CircuitStaticFunctionAccessExpression, ReducerError> {
Ok(CircuitStaticFunctionAccessExpression { Ok(CircuitStaticFunctionAccessExpression {
circuit: Box::new(circuit), circuit: Box::new(circuit),
name, name,
@ -250,7 +242,7 @@ pub trait ReconstructingReducer {
call: &CallExpression, call: &CallExpression,
function: Expression, function: Expression,
arguments: Vec<Expression>, arguments: Vec<Expression>,
) -> Result<CallExpression, CanonicalizeError> { ) -> Result<CallExpression, ReducerError> {
Ok(CallExpression { Ok(CallExpression {
function: Box::new(function), function: Box::new(function),
arguments, arguments,
@ -259,7 +251,7 @@ pub trait ReconstructingReducer {
} }
// Statements // Statements
fn reduce_statement(&mut self, _statement: &Statement, new: Statement) -> Result<Statement, CanonicalizeError> { fn reduce_statement(&mut self, _statement: &Statement, new: Statement) -> Result<Statement, ReducerError> {
Ok(new) Ok(new)
} }
@ -267,7 +259,7 @@ pub trait ReconstructingReducer {
&mut self, &mut self,
return_statement: &ReturnStatement, return_statement: &ReturnStatement,
expression: Expression, expression: Expression,
) -> Result<ReturnStatement, CanonicalizeError> { ) -> Result<ReturnStatement, ReducerError> {
Ok(ReturnStatement { Ok(ReturnStatement {
expression, expression,
span: return_statement.span.clone(), span: return_statement.span.clone(),
@ -278,7 +270,7 @@ pub trait ReconstructingReducer {
&mut self, &mut self,
variable_name: &VariableName, variable_name: &VariableName,
identifier: Identifier, identifier: Identifier,
) -> Result<VariableName, CanonicalizeError> { ) -> Result<VariableName, ReducerError> {
Ok(VariableName { Ok(VariableName {
mutable: variable_name.mutable, mutable: variable_name.mutable,
identifier, identifier,
@ -292,7 +284,7 @@ pub trait ReconstructingReducer {
variable_names: Vec<VariableName>, variable_names: Vec<VariableName>,
type_: Option<Type>, type_: Option<Type>,
value: Expression, value: Expression,
) -> Result<DefinitionStatement, CanonicalizeError> { ) -> Result<DefinitionStatement, ReducerError> {
Ok(DefinitionStatement { Ok(DefinitionStatement {
declaration_type: definition.declaration_type.clone(), declaration_type: definition.declaration_type.clone(),
variable_names, variable_names,
@ -306,7 +298,7 @@ pub trait ReconstructingReducer {
&mut self, &mut self,
_access: &AssigneeAccess, _access: &AssigneeAccess,
new: AssigneeAccess, new: AssigneeAccess,
) -> Result<AssigneeAccess, CanonicalizeError> { ) -> Result<AssigneeAccess, ReducerError> {
Ok(new) Ok(new)
} }
@ -315,7 +307,7 @@ pub trait ReconstructingReducer {
assignee: &Assignee, assignee: &Assignee,
identifier: Identifier, identifier: Identifier,
accesses: Vec<AssigneeAccess>, accesses: Vec<AssigneeAccess>,
) -> Result<Assignee, CanonicalizeError> { ) -> Result<Assignee, ReducerError> {
Ok(Assignee { Ok(Assignee {
identifier, identifier,
accesses, accesses,
@ -328,7 +320,7 @@ pub trait ReconstructingReducer {
assign: &AssignStatement, assign: &AssignStatement,
assignee: Assignee, assignee: Assignee,
value: Expression, value: Expression,
) -> Result<AssignStatement, CanonicalizeError> { ) -> Result<AssignStatement, ReducerError> {
Ok(AssignStatement { Ok(AssignStatement {
operation: assign.operation.clone(), operation: assign.operation.clone(),
assignee, assignee,
@ -343,7 +335,7 @@ pub trait ReconstructingReducer {
condition: Expression, condition: Expression,
block: Block, block: Block,
statement: Option<Statement>, statement: Option<Statement>,
) -> Result<ConditionalStatement, CanonicalizeError> { ) -> Result<ConditionalStatement, ReducerError> {
Ok(ConditionalStatement { Ok(ConditionalStatement {
condition, condition,
block, block,
@ -359,7 +351,7 @@ pub trait ReconstructingReducer {
start: Expression, start: Expression,
stop: Expression, stop: Expression,
block: Block, block: Block,
) -> Result<IterationStatement, CanonicalizeError> { ) -> Result<IterationStatement, ReducerError> {
Ok(IterationStatement { Ok(IterationStatement {
variable, variable,
start, start,
@ -373,7 +365,7 @@ pub trait ReconstructingReducer {
&mut self, &mut self,
console: &ConsoleStatement, console: &ConsoleStatement,
function: ConsoleFunction, function: ConsoleFunction,
) -> Result<ConsoleStatement, CanonicalizeError> { ) -> Result<ConsoleStatement, ReducerError> {
Ok(ConsoleStatement { Ok(ConsoleStatement {
function, function,
span: console.span.clone(), span: console.span.clone(),
@ -384,14 +376,14 @@ pub trait ReconstructingReducer {
&mut self, &mut self,
expression_statement: &ExpressionStatement, expression_statement: &ExpressionStatement,
expression: Expression, expression: Expression,
) -> Result<ExpressionStatement, CanonicalizeError> { ) -> Result<ExpressionStatement, ReducerError> {
Ok(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>) -> Result<Block, CanonicalizeError> { fn reduce_block(&mut self, block: &Block, statements: Vec<Statement>) -> Result<Block, ReducerError> {
Ok(Block { Ok(Block {
statements, statements,
span: block.span.clone(), span: block.span.clone(),
@ -406,7 +398,7 @@ 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>,
) -> Result<Program, CanonicalizeError> { ) -> Result<Program, ReducerError> {
Ok(Program { Ok(Program {
name: program.name.clone(), name: program.name.clone(),
expected_input, expected_input,
@ -421,7 +413,7 @@ pub trait ReconstructingReducer {
variable: &FunctionInputVariable, variable: &FunctionInputVariable,
identifier: Identifier, identifier: Identifier,
type_: Type, type_: Type,
) -> Result<FunctionInputVariable, CanonicalizeError> { ) -> Result<FunctionInputVariable, ReducerError> {
Ok(FunctionInputVariable { Ok(FunctionInputVariable {
identifier, identifier,
const_: variable.const_, const_: variable.const_,
@ -435,7 +427,7 @@ pub trait ReconstructingReducer {
&mut self, &mut self,
_input: &FunctionInput, _input: &FunctionInput,
new: FunctionInput, new: FunctionInput,
) -> Result<FunctionInput, CanonicalizeError> { ) -> Result<FunctionInput, ReducerError> {
Ok(new) Ok(new)
} }
@ -443,7 +435,7 @@ pub trait ReconstructingReducer {
&mut self, &mut self,
_package_or_packages: &PackageOrPackages, _package_or_packages: &PackageOrPackages,
new: PackageOrPackages, new: PackageOrPackages,
) -> Result<PackageOrPackages, CanonicalizeError> { ) -> Result<PackageOrPackages, ReducerError> {
Ok(new) Ok(new)
} }
@ -451,7 +443,7 @@ pub trait ReconstructingReducer {
&mut self, &mut self,
import: &ImportStatement, import: &ImportStatement,
package_or_packages: PackageOrPackages, package_or_packages: PackageOrPackages,
) -> Result<ImportStatement, CanonicalizeError> { ) -> Result<ImportStatement, ReducerError> {
Ok(ImportStatement { Ok(ImportStatement {
package_or_packages, package_or_packages,
span: import.span.clone(), span: import.span.clone(),
@ -462,7 +454,7 @@ pub trait ReconstructingReducer {
&mut self, &mut self,
_circuit_member: &CircuitMember, _circuit_member: &CircuitMember,
new: CircuitMember, new: CircuitMember,
) -> Result<CircuitMember, CanonicalizeError> { ) -> Result<CircuitMember, ReducerError> {
Ok(new) Ok(new)
} }
@ -471,15 +463,11 @@ pub trait ReconstructingReducer {
_circuit: &Circuit, _circuit: &Circuit,
circuit_name: Identifier, circuit_name: Identifier,
members: Vec<CircuitMember>, members: Vec<CircuitMember>,
) -> Result<Circuit, CanonicalizeError> { ) -> Result<Circuit, ReducerError> {
Ok(Circuit { circuit_name, members }) Ok(Circuit { circuit_name, members })
} }
fn reduce_annotation( fn reduce_annotation(&mut self, annotation: &Annotation, name: Identifier) -> Result<Annotation, ReducerError> {
&mut self,
annotation: &Annotation,
name: Identifier,
) -> Result<Annotation, CanonicalizeError> {
Ok(Annotation { Ok(Annotation {
span: annotation.span.clone(), span: annotation.span.clone(),
name, name,
@ -496,7 +484,7 @@ pub trait ReconstructingReducer {
input: Vec<FunctionInput>, input: Vec<FunctionInput>,
output: Option<Type>, output: Option<Type>,
block: Block, block: Block,
) -> Result<Function, CanonicalizeError> { ) -> Result<Function, ReducerError> {
Ok(Function { Ok(Function {
identifier, identifier,
annotations, annotations,

View File

@ -16,7 +16,7 @@
use crate::errors::FunctionError; use crate::errors::FunctionError;
use leo_asg::{AsgConvertError, FormattedError}; use leo_asg::{AsgConvertError, FormattedError};
use leo_ast::{CanonicalizeError, LeoError}; use leo_ast::{LeoError, ReducerError};
use leo_input::InputParserError; use leo_input::InputParserError;
use leo_parser::SyntaxError; use leo_parser::SyntaxError;
use leo_state::LocalDataVerificationError; use leo_state::LocalDataVerificationError;
@ -56,7 +56,7 @@ pub enum CompilerError {
AsgConvertError(#[from] AsgConvertError), AsgConvertError(#[from] AsgConvertError),
#[error("{}", _0)] #[error("{}", _0)]
CanonicalizeError(#[from] CanonicalizeError), ReducerError(#[from] ReducerError),
} }
impl LeoError for CompilerError {} impl LeoError for CompilerError {}

View File

@ -63,7 +63,6 @@ use leo_ast::{
BinaryExpression as AstBinaryExpression, BinaryExpression as AstBinaryExpression,
Block as AstBlockStatement, Block as AstBlockStatement,
CallExpression as AstCallExpression, CallExpression as AstCallExpression,
CanonicalizeError,
CastExpression as AstCastExpression, CastExpression as AstCastExpression,
Circuit as AstCircuit, Circuit as AstCircuit,
CircuitImpliedVariableDefinition, CircuitImpliedVariableDefinition,
@ -71,6 +70,7 @@ use leo_ast::{
CircuitMember as AstCircuitMember, CircuitMember as AstCircuitMember,
CircuitMemberAccessExpression, CircuitMemberAccessExpression,
CircuitStaticFunctionAccessExpression, CircuitStaticFunctionAccessExpression,
CombinerError,
ConditionalStatement as AstConditionalStatement, ConditionalStatement as AstConditionalStatement,
ConsoleFunction as AstConsoleFunction, ConsoleFunction as AstConsoleFunction,
ConsoleStatement as AstConsoleStatement, ConsoleStatement as AstConsoleStatement,
@ -82,6 +82,7 @@ use leo_ast::{
IterationStatement as AstIterationStatement, IterationStatement as AstIterationStatement,
PositiveNumber, PositiveNumber,
ReconstructingReducer, ReconstructingReducer,
ReducerError,
ReturnStatement as AstReturnStatement, ReturnStatement as AstReturnStatement,
Span, Span,
SpreadOrExpression, SpreadOrExpression,
@ -105,7 +106,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
Self { ast_reducer, options } Self { ast_reducer, options }
} }
pub fn reduce_type(&mut self, ast: &AstType, asg: &AsgType, span: &Span) -> Result<AstType, CanonicalizeError> { pub fn reduce_type(&mut self, ast: &AstType, asg: &AsgType, span: &Span) -> Result<AstType, ReducerError> {
let new = match (ast, asg) { let new = match (ast, asg) {
(AstType::Array(ast_type, ast_dimensions), AsgType::Array(asg_type, asg_dimensions)) => { (AstType::Array(ast_type, ast_dimensions), AsgType::Array(asg_type, asg_dimensions)) => {
if self.options.type_inference_enabled { if self.options.type_inference_enabled {
@ -140,7 +141,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstExpression, ast: &AstExpression,
asg: &AsgExpression, asg: &AsgExpression,
) -> Result<AstExpression, CanonicalizeError> { ) -> Result<AstExpression, ReducerError> {
let new = match (ast, asg) { let new = match (ast, asg) {
// TODO what to do for the following: // TODO what to do for the following:
// Ast::Identifier, Ast::Value, Asg::Constant, Asg::ValueRef // Ast::Identifier, Ast::Value, Asg::Constant, Asg::ValueRef
@ -199,7 +200,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstArrayAccessExpression, ast: &AstArrayAccessExpression,
asg: &AsgArrayAccessExpression, asg: &AsgArrayAccessExpression,
) -> Result<AstArrayAccessExpression, CanonicalizeError> { ) -> Result<AstArrayAccessExpression, ReducerError> {
let array = self.reduce_expression(&ast.array, asg.array.get())?; let array = self.reduce_expression(&ast.array, asg.array.get())?;
let index = self.reduce_expression(&ast.index, asg.index.get())?; let index = self.reduce_expression(&ast.index, asg.index.get())?;
@ -210,7 +211,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstArrayInitExpression, ast: &AstArrayInitExpression,
asg: &AsgArrayInitExpression, asg: &AsgArrayInitExpression,
) -> Result<AstArrayInitExpression, CanonicalizeError> { ) -> Result<AstArrayInitExpression, ReducerError> {
let element = self.reduce_expression(&ast.element, asg.element.get())?; let element = self.reduce_expression(&ast.element, asg.element.get())?;
self.ast_reducer.reduce_array_init(ast, element) self.ast_reducer.reduce_array_init(ast, element)
@ -220,7 +221,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstArrayInlineExpression, ast: &AstArrayInlineExpression,
asg: &AsgArrayInlineExpression, asg: &AsgArrayInlineExpression,
) -> Result<AstArrayInlineExpression, CanonicalizeError> { ) -> Result<AstArrayInlineExpression, ReducerError> {
let mut elements = vec![]; let mut elements = vec![];
for (ast_element, asg_element) in ast.elements.iter().zip(asg.elements.iter()) { for (ast_element, asg_element) in ast.elements.iter().zip(asg.elements.iter()) {
let reduced_element = match ast_element { let reduced_element = match ast_element {
@ -242,7 +243,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstArrayRangeAccessExpression, ast: &AstArrayRangeAccessExpression,
asg: &AsgArrayRangeAccessExpression, asg: &AsgArrayRangeAccessExpression,
) -> Result<AstArrayRangeAccessExpression, CanonicalizeError> { ) -> Result<AstArrayRangeAccessExpression, ReducerError> {
let array = self.reduce_expression(&ast.array, asg.array.get())?; let array = self.reduce_expression(&ast.array, asg.array.get())?;
let left = match (ast.left.as_ref(), asg.left.get()) { let left = match (ast.left.as_ref(), asg.left.get()) {
(Some(ast_left), Some(asg_left)) => Some(self.reduce_expression(ast_left, asg_left)?), (Some(ast_left), Some(asg_left)) => Some(self.reduce_expression(ast_left, asg_left)?),
@ -260,7 +261,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstBinaryExpression, ast: &AstBinaryExpression,
asg: &AsgBinaryExpression, asg: &AsgBinaryExpression,
) -> Result<AstBinaryExpression, CanonicalizeError> { ) -> Result<AstBinaryExpression, ReducerError> {
let left = self.reduce_expression(&ast.left, asg.left.get())?; let left = self.reduce_expression(&ast.left, asg.left.get())?;
let right = self.reduce_expression(&ast.right, asg.right.get())?; let right = self.reduce_expression(&ast.right, asg.right.get())?;
@ -271,7 +272,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstCallExpression, ast: &AstCallExpression,
asg: &AsgCallExpression, asg: &AsgCallExpression,
) -> Result<AstCallExpression, CanonicalizeError> { ) -> Result<AstCallExpression, ReducerError> {
// TODO FIGURE IT OUT // TODO FIGURE IT OUT
// let function = self.reduce_expression(&ast.function, asg.function.get())?; // let function = self.reduce_expression(&ast.function, asg.function.get())?;
// let target = asg.target.get().map(|exp| self.reduce_expression()) // let target = asg.target.get().map(|exp| self.reduce_expression())
@ -289,7 +290,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstCastExpression, ast: &AstCastExpression,
asg: &AsgCastExpression, asg: &AsgCastExpression,
) -> Result<AstCastExpression, CanonicalizeError> { ) -> Result<AstCastExpression, ReducerError> {
let inner = self.reduce_expression(&ast.inner, &asg.inner.get())?; let inner = self.reduce_expression(&ast.inner, &asg.inner.get())?;
let target_type = self.reduce_type(&ast.target_type, &asg.target_type, &ast.span)?; let target_type = self.reduce_type(&ast.target_type, &asg.target_type, &ast.span)?;
@ -300,7 +301,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &ValueExpression, ast: &ValueExpression,
_asg: &AsgConstant, _asg: &AsgConstant,
) -> Result<ValueExpression, CanonicalizeError> { ) -> Result<ValueExpression, ReducerError> {
// TODO REDUCE GV // TODO REDUCE GV
// Is this needed? // Is this needed?
let new = match ast { let new = match ast {
@ -317,7 +318,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &CircuitMemberAccessExpression, ast: &CircuitMemberAccessExpression,
_asg: &AsgCircuitAccessExpression, _asg: &AsgCircuitAccessExpression,
) -> Result<CircuitMemberAccessExpression, CanonicalizeError> { ) -> Result<CircuitMemberAccessExpression, ReducerError> {
// TODO FIGURE IT OUT // TODO FIGURE IT OUT
// let circuit = self.reduce_expression(&circuit_member_access.circuit)?; // let circuit = self.reduce_expression(&circuit_member_access.circuit)?;
// let name = self.reduce_identifier(&circuit_member_access.name)?; // let name = self.reduce_identifier(&circuit_member_access.name)?;
@ -332,7 +333,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &CircuitStaticFunctionAccessExpression, ast: &CircuitStaticFunctionAccessExpression,
_asg: &AsgCircuitAccessExpression, _asg: &AsgCircuitAccessExpression,
) -> Result<CircuitStaticFunctionAccessExpression, CanonicalizeError> { ) -> Result<CircuitStaticFunctionAccessExpression, ReducerError> {
// TODO FIGURE IT OUT // TODO FIGURE IT OUT
// let circuit = self.reduce_expression(&circuit_member_access.circuit)?; // let circuit = self.reduce_expression(&circuit_member_access.circuit)?;
// let name = self.reduce_identifier(&circuit_member_access.name)?; // let name = self.reduce_identifier(&circuit_member_access.name)?;
@ -347,7 +348,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &CircuitImpliedVariableDefinition, ast: &CircuitImpliedVariableDefinition,
asg: &AsgExpression, asg: &AsgExpression,
) -> Result<CircuitImpliedVariableDefinition, CanonicalizeError> { ) -> Result<CircuitImpliedVariableDefinition, ReducerError> {
let expression = ast let expression = ast
.expression .expression
.as_ref() .as_ref()
@ -362,7 +363,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstCircuitInitExpression, ast: &AstCircuitInitExpression,
asg: &AsgCircuitInitExpression, asg: &AsgCircuitInitExpression,
) -> Result<AstCircuitInitExpression, CanonicalizeError> { ) -> Result<AstCircuitInitExpression, ReducerError> {
let mut members = vec![]; let mut members = vec![];
for (ast_member, asg_member) in ast.members.iter().zip(asg.values.iter()) { for (ast_member, asg_member) in ast.members.iter().zip(asg.values.iter()) {
members.push(self.reduce_circuit_implied_variable_definition(ast_member, asg_member.1.get())?); members.push(self.reduce_circuit_implied_variable_definition(ast_member, asg_member.1.get())?);
@ -375,7 +376,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstTernaryExpression, ast: &AstTernaryExpression,
asg: &AsgTernaryExpression, asg: &AsgTernaryExpression,
) -> Result<AstTernaryExpression, CanonicalizeError> { ) -> Result<AstTernaryExpression, ReducerError> {
let condition = self.reduce_expression(&ast.condition, asg.condition.get())?; let condition = self.reduce_expression(&ast.condition, asg.condition.get())?;
let if_true = self.reduce_expression(&ast.if_true, asg.if_true.get())?; let if_true = self.reduce_expression(&ast.if_true, asg.if_true.get())?;
let if_false = self.reduce_expression(&ast.if_false, asg.if_false.get())?; let if_false = self.reduce_expression(&ast.if_false, asg.if_false.get())?;
@ -387,7 +388,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstTupleAccessExpression, ast: &AstTupleAccessExpression,
asg: &AsgTupleAccessExpression, asg: &AsgTupleAccessExpression,
) -> Result<AstTupleAccessExpression, CanonicalizeError> { ) -> Result<AstTupleAccessExpression, ReducerError> {
let tuple = self.reduce_expression(&ast.tuple, asg.tuple_ref.get())?; let tuple = self.reduce_expression(&ast.tuple, asg.tuple_ref.get())?;
self.ast_reducer.reduce_tuple_access(ast, tuple) self.ast_reducer.reduce_tuple_access(ast, tuple)
@ -397,7 +398,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstTupleInitExpression, ast: &AstTupleInitExpression,
asg: &AsgTupleInitExpression, asg: &AsgTupleInitExpression,
) -> Result<AstTupleInitExpression, CanonicalizeError> { ) -> Result<AstTupleInitExpression, ReducerError> {
let mut elements = vec![]; let mut elements = vec![];
for (ast_element, asg_element) in ast.elements.iter().zip(asg.elements.iter()) { for (ast_element, asg_element) in ast.elements.iter().zip(asg.elements.iter()) {
let element = self.reduce_expression(ast_element, asg_element.get())?; let element = self.reduce_expression(ast_element, asg_element.get())?;
@ -411,7 +412,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstUnaryExpression, ast: &AstUnaryExpression,
asg: &AsgUnaryExpression, asg: &AsgUnaryExpression,
) -> Result<AstUnaryExpression, CanonicalizeError> { ) -> Result<AstUnaryExpression, ReducerError> {
let inner = self.reduce_expression(&ast.inner, asg.inner.get())?; let inner = self.reduce_expression(&ast.inner, asg.inner.get())?;
self.ast_reducer.reduce_unary(ast, inner, ast.op.clone()) self.ast_reducer.reduce_unary(ast, inner, ast.op.clone())
@ -421,7 +422,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &ValueExpression, ast: &ValueExpression,
_asg: &AsgVariableRef, _asg: &AsgVariableRef,
) -> Result<ValueExpression, CanonicalizeError> { ) -> Result<ValueExpression, ReducerError> {
// TODO FIGURE IT OUT // TODO FIGURE IT OUT
let new = match ast { let new = match ast {
// ValueExpression::Group(group_value) => { // ValueExpression::Group(group_value) => {
@ -438,7 +439,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast_statement: &AstStatement, ast_statement: &AstStatement,
asg_statement: &AsgStatement, asg_statement: &AsgStatement,
) -> Result<AstStatement, CanonicalizeError> { ) -> Result<AstStatement, ReducerError> {
let new = match (ast_statement, asg_statement) { let new = match (ast_statement, asg_statement) {
(AstStatement::Assign(ast), AsgStatement::Assign(asg)) => { (AstStatement::Assign(ast), AsgStatement::Assign(asg)) => {
AstStatement::Assign(self.reduce_assign(ast, asg)?) AstStatement::Assign(self.reduce_assign(ast, asg)?)
@ -472,7 +473,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstAssignAccess, ast: &AstAssignAccess,
asg: &AsgAssignAccess, asg: &AsgAssignAccess,
) -> Result<AstAssignAccess, CanonicalizeError> { ) -> Result<AstAssignAccess, ReducerError> {
let new = match (ast, asg) { let new = match (ast, asg) {
(AstAssignAccess::ArrayRange(ast_left, ast_right), AsgAssignAccess::ArrayRange(asg_left, asg_right)) => { (AstAssignAccess::ArrayRange(ast_left, ast_right), AsgAssignAccess::ArrayRange(asg_left, asg_right)) => {
let left = match (ast_left.as_ref(), asg_left.get()) { let left = match (ast_left.as_ref(), asg_left.get()) {
@ -496,7 +497,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
self.ast_reducer.reduce_assignee_access(ast, new) self.ast_reducer.reduce_assignee_access(ast, new)
} }
pub fn reduce_assignee(&mut self, ast: &Assignee, asg: &[AsgAssignAccess]) -> Result<Assignee, CanonicalizeError> { pub fn reduce_assignee(&mut self, ast: &Assignee, asg: &[AsgAssignAccess]) -> Result<Assignee, ReducerError> {
let mut accesses = vec![]; let mut accesses = vec![];
for (ast_access, asg_access) in ast.accesses.iter().zip(asg) { for (ast_access, asg_access) in ast.accesses.iter().zip(asg) {
accesses.push(self.reduce_assign_access(ast_access, asg_access)?); accesses.push(self.reduce_assign_access(ast_access, asg_access)?);
@ -509,7 +510,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstAssignStatement, ast: &AstAssignStatement,
asg: &AsgAssignStatement, asg: &AsgAssignStatement,
) -> Result<AstAssignStatement, CanonicalizeError> { ) -> Result<AstAssignStatement, ReducerError> {
let assignee = self.reduce_assignee(&ast.assignee, &asg.target_accesses)?; let assignee = self.reduce_assignee(&ast.assignee, &asg.target_accesses)?;
let value = self.reduce_expression(&ast.value, asg.value.get())?; let value = self.reduce_expression(&ast.value, asg.value.get())?;
@ -520,7 +521,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstBlockStatement, ast: &AstBlockStatement,
asg: &AsgBlockStatement, asg: &AsgBlockStatement,
) -> Result<AstBlockStatement, CanonicalizeError> { ) -> Result<AstBlockStatement, ReducerError> {
let mut statements = vec![]; let mut statements = vec![];
for (ast_statement, asg_statement) in ast.statements.iter().zip(asg.statements.iter()) { for (ast_statement, asg_statement) in ast.statements.iter().zip(asg.statements.iter()) {
statements.push(self.reduce_statement(ast_statement, asg_statement.get())?); statements.push(self.reduce_statement(ast_statement, asg_statement.get())?);
@ -533,14 +534,15 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstConditionalStatement, ast: &AstConditionalStatement,
asg: &AsgConditionalStatement, asg: &AsgConditionalStatement,
) -> Result<AstConditionalStatement, CanonicalizeError> { ) -> Result<AstConditionalStatement, ReducerError> {
let condition = self.reduce_expression(&ast.condition, asg.condition.get())?; let condition = self.reduce_expression(&ast.condition, asg.condition.get())?;
let block; let block;
if let AsgStatement::Block(asg_block) = asg.result.get() { if let AsgStatement::Block(asg_block) = asg.result.get() {
block = self.reduce_block(&ast.block, asg_block)?; block = self.reduce_block(&ast.block, asg_block)?;
} else { } else {
// TODO throw error? return Err(ReducerError::from(CombinerError::asg_statement_not_block(
block = ast.block.clone(); &asg.span.as_ref().unwrap(),
)));
} }
let next = match (ast.next.as_ref(), asg.next.get()) { let next = match (ast.next.as_ref(), asg.next.get()) {
(Some(ast_next), Some(asg_next)) => Some(self.reduce_statement(ast_next, asg_next)?), (Some(ast_next), Some(asg_next)) => Some(self.reduce_statement(ast_next, asg_next)?),
@ -554,7 +556,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstConsoleStatement, ast: &AstConsoleStatement,
asg: &AsgConsoleStatement, asg: &AsgConsoleStatement,
) -> Result<AstConsoleStatement, CanonicalizeError> { ) -> Result<AstConsoleStatement, ReducerError> {
let function = match (&ast.function, &asg.function) { let function = match (&ast.function, &asg.function) {
(AstConsoleFunction::Assert(ast_expression), AsgConsoleFunction::Assert(asg_expression)) => { (AstConsoleFunction::Assert(ast_expression), AsgConsoleFunction::Assert(asg_expression)) => {
AstConsoleFunction::Assert(self.reduce_expression(&ast_expression, asg_expression.get())?) AstConsoleFunction::Assert(self.reduce_expression(&ast_expression, asg_expression.get())?)
@ -577,7 +579,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
AstConsoleFunction::Debug(_) => AstConsoleFunction::Debug(formatted), AstConsoleFunction::Debug(_) => AstConsoleFunction::Debug(formatted),
AstConsoleFunction::Error(_) => AstConsoleFunction::Error(formatted), AstConsoleFunction::Error(_) => AstConsoleFunction::Error(formatted),
AstConsoleFunction::Log(_) => AstConsoleFunction::Log(formatted), AstConsoleFunction::Log(_) => AstConsoleFunction::Log(formatted),
_ => unimplemented!(), // impossible _ => return Err(ReducerError::impossible_console_assert_call(&ast_format.span)),
} }
} }
_ => ast.function.clone(), _ => ast.function.clone(),
@ -590,7 +592,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstDefinitionStatement, ast: &AstDefinitionStatement,
asg: &AsgDefinitionStatement, asg: &AsgDefinitionStatement,
) -> Result<AstDefinitionStatement, CanonicalizeError> { ) -> Result<AstDefinitionStatement, ReducerError> {
let type_; let type_;
if asg.variables.len() > 1 { if asg.variables.len() > 1 {
@ -628,7 +630,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstExpressionStatement, ast: &AstExpressionStatement,
asg: &AsgExpressionStatement, asg: &AsgExpressionStatement,
) -> Result<AstExpressionStatement, CanonicalizeError> { ) -> Result<AstExpressionStatement, ReducerError> {
let inner_expression = self.reduce_expression(&ast.expression, asg.expression.get())?; let inner_expression = self.reduce_expression(&ast.expression, asg.expression.get())?;
self.ast_reducer.reduce_expression_statement(ast, inner_expression) self.ast_reducer.reduce_expression_statement(ast, inner_expression)
} }
@ -637,14 +639,16 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstIterationStatement, ast: &AstIterationStatement,
asg: &AsgIterationStatement, asg: &AsgIterationStatement,
) -> Result<AstIterationStatement, CanonicalizeError> { ) -> Result<AstIterationStatement, ReducerError> {
let start = self.reduce_expression(&ast.start, asg.start.get())?; let start = self.reduce_expression(&ast.start, asg.start.get())?;
let stop = self.reduce_expression(&ast.stop, asg.stop.get())?; let stop = self.reduce_expression(&ast.stop, asg.stop.get())?;
let block; let block;
if let AsgStatement::Block(asg_block) = asg.body.get() { if let AsgStatement::Block(asg_block) = asg.body.get() {
block = self.reduce_block(&ast.block, asg_block)?; block = self.reduce_block(&ast.block, asg_block)?;
} else { } else {
block = ast.block.clone(); return Err(ReducerError::from(CombinerError::asg_statement_not_block(
&asg.span.as_ref().unwrap(),
)));
} }
self.ast_reducer self.ast_reducer
@ -655,7 +659,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstReturnStatement, ast: &AstReturnStatement,
asg: &AsgReturnStatement, asg: &AsgReturnStatement,
) -> Result<AstReturnStatement, CanonicalizeError> { ) -> Result<AstReturnStatement, ReducerError> {
let expression = self.reduce_expression(&ast.expression, asg.expression.get())?; let expression = self.reduce_expression(&ast.expression, asg.expression.get())?;
self.ast_reducer.reduce_return(ast, expression) self.ast_reducer.reduce_return(ast, expression)
@ -665,7 +669,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &leo_ast::Program, ast: &leo_ast::Program,
asg: &leo_asg::Program, asg: &leo_asg::Program,
) -> Result<leo_ast::Program, leo_ast::CanonicalizeError> { ) -> Result<leo_ast::Program, leo_ast::ReducerError> {
self.ast_reducer.swap_in_circuit(); self.ast_reducer.swap_in_circuit();
let mut circuits = IndexMap::new(); let mut circuits = IndexMap::new();
for ((ast_ident, ast_circuit), (_asg_ident, asg_circuit)) in ast.circuits.iter().zip(&asg.circuits) { for ((ast_ident, ast_circuit), (_asg_ident, asg_circuit)) in ast.circuits.iter().zip(&asg.circuits) {
@ -687,7 +691,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
) )
} }
pub fn reduce_function(&mut self, ast: &AstFunction, asg: &AsgFunction) -> Result<AstFunction, CanonicalizeError> { pub fn reduce_function(&mut self, ast: &AstFunction, asg: &AsgFunction) -> Result<AstFunction, ReducerError> {
let output = ast let output = ast
.output .output
.as_ref() .as_ref()
@ -720,7 +724,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
&mut self, &mut self,
ast: &AstCircuitMember, ast: &AstCircuitMember,
asg: &AsgCircuitMember, asg: &AsgCircuitMember,
) -> Result<AstCircuitMember, CanonicalizeError> { ) -> Result<AstCircuitMember, ReducerError> {
let new = match (ast, asg) { let new = match (ast, asg) {
(AstCircuitMember::CircuitVariable(identifier, ast_type), AsgCircuitMember::Variable(asg_type)) => { (AstCircuitMember::CircuitVariable(identifier, ast_type), AsgCircuitMember::Variable(asg_type)) => {
AstCircuitMember::CircuitVariable( AstCircuitMember::CircuitVariable(
@ -737,7 +741,7 @@ impl<R: ReconstructingReducer> CombineAstAsgDirector<R> {
self.ast_reducer.reduce_circuit_member(ast, new) self.ast_reducer.reduce_circuit_member(ast, new)
} }
pub fn reduce_circuit(&mut self, ast: &AstCircuit, asg: &AsgCircuit) -> Result<AstCircuit, CanonicalizeError> { pub fn reduce_circuit(&mut self, ast: &AstCircuit, asg: &AsgCircuit) -> Result<AstCircuit, ReducerError> {
let mut members = vec![]; let mut members = vec![];
for (ast_member, asg_member) in ast.members.iter().zip(asg.members.borrow().iter()) { for (ast_member, asg_member) in ast.members.iter().zip(asg.members.borrow().iter()) {
members.push(self.reduce_circuit_member(ast_member, asg_member.1)?); members.push(self.reduce_circuit_member(ast_member, asg_member.1)?);