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
// 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)]
pub enum CanonicalizeError {
@ -22,6 +22,8 @@ pub enum CanonicalizeError {
Error(#[from] FormattedError),
}
impl LeoError for CanonicalizeError {}
impl CanonicalizeError {
fn new_from_span(message: String, span: &Span) -> Self {
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
// 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 use error::*;
pub mod reducer;
pub use reducer::*;
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.
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())?;
Ok(())
}

View File

@ -394,11 +394,13 @@ impl ReconstructingReducer for Canonicalizer {
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 {
Type::Array(type_, mut dimensions) => {
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()]));
@ -415,7 +417,9 @@ impl ReconstructingReducer for Canonicalizer {
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()),
}
}
@ -424,9 +428,11 @@ impl ReconstructingReducer for Canonicalizer {
&mut self,
array_init: &ArrayInitExpression,
element: Expression,
) -> Result<ArrayInitExpression, CanonicalizeError> {
) -> Result<ArrayInitExpression, ReducerError> {
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);
@ -473,7 +479,7 @@ impl ReconstructingReducer for Canonicalizer {
assign: &AssignStatement,
assignee: Assignee,
value: Expression,
) -> Result<AssignStatement, CanonicalizeError> {
) -> Result<AssignStatement, ReducerError> {
match value {
Expression::Value(value_expr) if assign.operation != AssignOperation::Assign => {
let mut left = Box::new(Expression::Identifier(assignee.identifier.clone()));
@ -551,7 +557,7 @@ impl ReconstructingReducer for Canonicalizer {
input: Vec<FunctionInput>,
output: Option<Type>,
block: Block,
) -> Result<Function, CanonicalizeError> {
) -> Result<Function, ReducerError> {
let new_output = match output {
None => Some(Type::Tuple(vec![])),
_ => output,
@ -572,7 +578,7 @@ impl ReconstructingReducer for Canonicalizer {
_circuit: &Circuit,
circuit_name: Identifier,
members: Vec<CircuitMember>,
) -> Result<Circuit, CanonicalizeError> {
) -> Result<Circuit, ReducerError> {
self.circuit_name = Some(circuit_name.clone());
let circ = Circuit {
circuit_name,

View File

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

View File

@ -29,7 +29,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
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_ {
Type::Array(type_, dimensions) => Type::Array(Box::new(self.reduce_type(type_, span)?), dimensions.clone()),
Type::Tuple(types) => {
@ -48,7 +48,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
}
// 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 {
Expression::Identifier(identifier) => Expression::Identifier(self.reduce_identifier(&identifier)?),
Expression::Value(value) => Expression::Value(self.reduce_value(&value)?),
@ -83,15 +83,15 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
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)
}
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)
}
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 {
GroupValue::Tuple(group_tuple) => GroupValue::Tuple(self.reduce_group_tuple(&group_tuple)?),
_ => group_value.clone(),
@ -100,7 +100,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
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 {
ValueExpression::Group(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)
}
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 right = self.reduce_expression(&binary.right)?;
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)?;
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 if_true = self.reduce_expression(&ternary.if_true)?;
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)
}
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 target_type = self.reduce_type(&cast.target_type, &cast.span)?;
@ -142,7 +142,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_array_inline(
&mut self,
array_inline: &ArrayInlineExpression,
) -> Result<ArrayInlineExpression, CanonicalizeError> {
) -> Result<ArrayInlineExpression, ReducerError> {
let mut elements = vec![];
for element in array_inline.elements.iter() {
let reduced_element = match element {
@ -160,10 +160,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_array_inline(array_inline, elements)
}
pub fn reduce_array_init(
&mut self,
array_init: &ArrayInitExpression,
) -> Result<ArrayInitExpression, CanonicalizeError> {
pub fn reduce_array_init(&mut self, array_init: &ArrayInitExpression) -> Result<ArrayInitExpression, ReducerError> {
let element = self.reduce_expression(&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(
&mut self,
array_access: &ArrayAccessExpression,
) -> Result<ArrayAccessExpression, CanonicalizeError> {
) -> Result<ArrayAccessExpression, ReducerError> {
let array = self.reduce_expression(&array_access.array)?;
let index = self.reduce_expression(&array_access.index)?;
@ -182,7 +179,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_array_range_access(
&mut self,
array_range_access: &ArrayRangeAccessExpression,
) -> Result<ArrayRangeAccessExpression, CanonicalizeError> {
) -> Result<ArrayRangeAccessExpression, ReducerError> {
let array = self.reduce_expression(&array_range_access.array)?;
let left = array_range_access
.left
@ -199,10 +196,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
.reduce_array_range_access(array_range_access, array, left, right)
}
pub fn reduce_tuple_init(
&mut self,
tuple_init: &TupleInitExpression,
) -> Result<TupleInitExpression, CanonicalizeError> {
pub fn reduce_tuple_init(&mut self, tuple_init: &TupleInitExpression) -> Result<TupleInitExpression, ReducerError> {
let mut elements = vec![];
for element in tuple_init.elements.iter() {
elements.push(self.reduce_expression(element)?);
@ -214,7 +208,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_tuple_access(
&mut self,
tuple_access: &TupleAccessExpression,
) -> Result<TupleAccessExpression, CanonicalizeError> {
) -> Result<TupleAccessExpression, ReducerError> {
let tuple = self.reduce_expression(&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(
&mut self,
variable: &CircuitImpliedVariableDefinition,
) -> Result<CircuitImpliedVariableDefinition, CanonicalizeError> {
) -> Result<CircuitImpliedVariableDefinition, ReducerError> {
let identifier = self.reduce_identifier(&variable.identifier)?;
let expression = variable
.expression
@ -238,7 +232,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_circuit_init(
&mut self,
circuit_init: &CircuitInitExpression,
) -> Result<CircuitInitExpression, CanonicalizeError> {
) -> Result<CircuitInitExpression, ReducerError> {
let name = self.reduce_identifier(&circuit_init.name)?;
let mut members = vec![];
@ -252,7 +246,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_circuit_member_access(
&mut self,
circuit_member_access: &CircuitMemberAccessExpression,
) -> Result<CircuitMemberAccessExpression, CanonicalizeError> {
) -> Result<CircuitMemberAccessExpression, ReducerError> {
let circuit = self.reduce_expression(&circuit_member_access.circuit)?;
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(
&mut self,
circuit_static_fn_access: &CircuitStaticFunctionAccessExpression,
) -> Result<CircuitStaticFunctionAccessExpression, CanonicalizeError> {
) -> Result<CircuitStaticFunctionAccessExpression, ReducerError> {
let circuit = self.reduce_expression(&circuit_static_fn_access.circuit)?;
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)
}
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 mut arguments = vec![];
@ -283,7 +277,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
}
// 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 {
Statement::Return(return_statement) => Statement::Return(self.reduce_return(&return_statement)?),
Statement::Definition(definition) => Statement::Definition(self.reduce_definition(&definition)?),
@ -298,22 +292,19 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
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)?;
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)?;
self.reducer.reduce_variable_name(variable_name, identifier)
}
pub fn reduce_definition(
&mut self,
definition: &DefinitionStatement,
) -> Result<DefinitionStatement, CanonicalizeError> {
pub fn reduce_definition(&mut self, definition: &DefinitionStatement) -> Result<DefinitionStatement, ReducerError> {
let mut variable_names = vec![];
for variable_name in definition.variable_names.iter() {
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)
}
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 {
AssigneeAccess::ArrayRange(left, right) => {
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)
}
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 mut accesses = vec![];
@ -357,7 +348,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
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 value = self.reduce_expression(&assign.value)?;
@ -367,7 +358,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_conditional(
&mut self,
conditional: &ConditionalStatement,
) -> Result<ConditionalStatement, CanonicalizeError> {
) -> Result<ConditionalStatement, ReducerError> {
let condition = self.reduce_expression(&conditional.condition)?;
let block = self.reduce_block(&conditional.block)?;
let next = conditional
@ -379,10 +370,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
self.reducer.reduce_conditional(conditional, condition, block, next)
}
pub fn reduce_iteration(
&mut self,
iteration: &IterationStatement,
) -> Result<IterationStatement, CanonicalizeError> {
pub fn reduce_iteration(&mut self, iteration: &IterationStatement) -> Result<IterationStatement, ReducerError> {
let variable = self.reduce_identifier(&iteration.variable)?;
let start = self.reduce_expression(&iteration.start)?;
let stop = self.reduce_expression(&iteration.stop)?;
@ -394,7 +382,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_console(
&mut self,
console_function_call: &ConsoleStatement,
) -> Result<ConsoleStatement, CanonicalizeError> {
) -> Result<ConsoleStatement, ReducerError> {
let function = match &console_function_call.function {
ConsoleFunction::Assert(expression) => ConsoleFunction::Assert(self.reduce_expression(expression)?),
ConsoleFunction::Debug(format) | ConsoleFunction::Error(format) | ConsoleFunction::Log(format) => {
@ -413,7 +401,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
ConsoleFunction::Debug(_) => ConsoleFunction::Debug(formatted),
ConsoleFunction::Error(_) => ConsoleFunction::Error(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(
&mut self,
expression: &ExpressionStatement,
) -> Result<ExpressionStatement, CanonicalizeError> {
) -> Result<ExpressionStatement, ReducerError> {
let inner_expression = self.reduce_expression(&expression.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![];
for statement in block.statements.iter() {
statements.push(self.reduce_statement(statement)?);
@ -439,7 +427,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
}
// 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![];
for input in program.expected_input.iter() {
inputs.push(self.reduce_function_input(input)?);
@ -469,14 +457,14 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
pub fn reduce_function_input_variable(
&mut self,
variable: &FunctionInputVariable,
) -> Result<FunctionInputVariable, CanonicalizeError> {
) -> Result<FunctionInputVariable, ReducerError> {
let identifier = self.reduce_identifier(&variable.identifier)?;
let type_ = self.reduce_type(&variable.type_, &variable.span)?;
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 {
FunctionInput::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(
&mut self,
package_or_packages: &PackageOrPackages,
) -> Result<PackageOrPackages, CanonicalizeError> {
) -> Result<PackageOrPackages, ReducerError> {
let new = match package_or_packages {
PackageOrPackages::Package(package) => PackageOrPackages::Package(Package {
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)
}
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)?;
self.reducer.reduce_import(import, package_or_packages)
}
pub fn reduce_circuit_member(
&mut self,
circuit_member: &CircuitMember,
) -> Result<CircuitMember, CanonicalizeError> {
pub fn reduce_circuit_member(&mut self, circuit_member: &CircuitMember) -> Result<CircuitMember, ReducerError> {
let new = match circuit_member {
CircuitMember::CircuitVariable(identifier, type_) => CircuitMember::CircuitVariable(
self.reduce_identifier(&identifier)?,
@ -530,7 +515,7 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
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 mut members = vec![];
@ -541,13 +526,13 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
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)?;
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 mut annotations = vec![];

View File

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

View File

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

View File

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