mirror of
https://github.com/AleoHQ/leo.git
synced 2024-12-24 01:54:23 +03:00
fix some reducer bugs, still more to implement/make function better
This commit is contained in:
parent
78ae6f1f12
commit
51c20ff07f
36
ast/src/reducer/canonicalization.rs
Normal file
36
ast/src/reducer/canonicalization.rs
Normal file
@ -0,0 +1,36 @@
|
||||
// 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/>.
|
||||
|
||||
//! This module contains the reducer which iterates through ast nodes - converting them into
|
||||
//! asg nodes and saving relevant information.
|
||||
|
||||
use crate::{reducer::ReconstructingReducer, Identifier};
|
||||
|
||||
pub struct Canonicalizer;
|
||||
|
||||
impl Canonicalizer {
|
||||
fn is_self(&self, identifier: &Identifier) -> bool {
|
||||
match identifier.name.as_str() {
|
||||
"Self" | "self" => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ReconstructingReducer for Canonicalizer {
|
||||
// TODO in reduce_circuit -> turn SelfKeyword and SelfType to Circuit Name
|
||||
// and replace self with Circuit Name
|
||||
}
|
@ -17,6 +17,9 @@
|
||||
//! This module contains the reducer which iterates through ast nodes - converting them into
|
||||
//! asg nodes and saving relevant information.
|
||||
|
||||
mod canonicalization;
|
||||
pub use canonicalization::*;
|
||||
|
||||
mod monoid;
|
||||
pub use monoid::*;
|
||||
|
||||
|
@ -397,7 +397,8 @@ impl<T: Monoid, R: MonoidalReducer<T>> MonoidalDirector<T, R> {
|
||||
.iter()
|
||||
.map(|definition| {
|
||||
let definition_identifier = self.reduce_identifier(&definition.identifier);
|
||||
let definition_expression = definition.expression.as_ref().map(|expr| self.reduce_expression(&expr));
|
||||
let definition_expression =
|
||||
definition.expression.as_ref().map(|expr| self.reduce_expression(&expr));
|
||||
(definition_identifier, definition_expression)
|
||||
})
|
||||
.collect();
|
||||
@ -422,10 +423,9 @@ impl<T: Monoid, R: MonoidalReducer<T>> MonoidalDirector<T, R> {
|
||||
let function_arguments = call.arguments.iter().map(|x| self.reduce_expression(x)).collect();
|
||||
|
||||
ExpressionMonoidItems::FunctionCall(function, function_arguments)
|
||||
},
|
||||
}
|
||||
|
||||
// TODO casts?
|
||||
|
||||
_ => ExpressionMonoidItems::Empty,
|
||||
};
|
||||
self.reducer.reduce_expression(expression, items)
|
||||
|
@ -300,12 +300,11 @@ pub trait MonoidalReducer<T: Monoid> {
|
||||
match value {
|
||||
Some(val) => {
|
||||
out = out.append(key).append(val);
|
||||
},
|
||||
}
|
||||
None => {
|
||||
out = out.append(key);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
out
|
||||
}
|
||||
|
@ -149,7 +149,8 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
span: function.block.span.clone(),
|
||||
};
|
||||
|
||||
self.reducer.reduce_function(function, annotations, identifier, input, output, block)
|
||||
self.reducer
|
||||
.reduce_function(function, annotations, identifier, input, output, block)
|
||||
}
|
||||
|
||||
pub fn reduce_identifier(&mut self, identifier: &Identifier) -> Identifier {
|
||||
@ -200,19 +201,17 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
|
||||
expression: self.reduce_expression(&return_statement.expression),
|
||||
span: return_statement.span.clone(),
|
||||
}),
|
||||
Statement::Definition(definition) => {
|
||||
Statement::Definition(DefinitionStatement {
|
||||
declaration_type: definition.declaration_type.clone(),
|
||||
variable_names: definition
|
||||
.variable_names
|
||||
.iter()
|
||||
.map(|variable_name| self.reduce_variable_name(variable_name))
|
||||
.collect(),
|
||||
type_: Some(self.reduce_type(&definition.type_.as_ref().unwrap())), // TODO fix
|
||||
value: self.reduce_expression(&definition.value),
|
||||
span: definition.span.clone(),
|
||||
})
|
||||
}
|
||||
Statement::Definition(definition) => Statement::Definition(DefinitionStatement {
|
||||
declaration_type: definition.declaration_type.clone(),
|
||||
variable_names: definition
|
||||
.variable_names
|
||||
.iter()
|
||||
.map(|variable_name| self.reduce_variable_name(variable_name))
|
||||
.collect(),
|
||||
type_: definition.type_.as_ref().map(|inner| self.reduce_type(&inner)),
|
||||
value: self.reduce_expression(&definition.value),
|
||||
span: definition.span.clone(),
|
||||
}),
|
||||
Statement::Assign(assign) => Statement::Assign(AssignStatement {
|
||||
operation: assign.operation.clone(),
|
||||
assignee: Assignee {
|
||||
|
Loading…
Reference in New Issue
Block a user