From 56c6cfb6ebae4f89750a3d06d1eee1e8fe004054 Mon Sep 17 00:00:00 2001 From: gluax Date: Mon, 29 Mar 2021 14:33:34 -0400 Subject: [PATCH] reducers in --- asg/src/context.rs | 2 +- asg/src/node.rs | 2 +- asg/src/program/mod.rs | 4 +- asg/src/reducer/reconstructing_director.rs | 9 +++- asg/src/reducer/reconstructing_reducer.rs | 9 ++++ asg/src/reducer/visitor.rs | 4 ++ asg/src/reducer/visitor_director.rs | 13 +++++ ast/src/reducer/global_const.rs | 57 ---------------------- ast/src/reducer/reconstructing_director.rs | 15 +++--- 9 files changed, 44 insertions(+), 71 deletions(-) delete mode 100644 ast/src/reducer/global_const.rs diff --git a/asg/src/context.rs b/asg/src/context.rs index 643ea6f803..ea26be19e8 100644 --- a/asg/src/context.rs +++ b/asg/src/context.rs @@ -68,7 +68,7 @@ impl<'a> AsgContextInner<'a> { #[allow(clippy::mut_from_ref)] pub fn alloc_scope(&'a self, scope: Scope<'a>) -> &'a Scope<'a> { - match self.arena.alloc(ArenaNode::Scope(scope)) { + match self.arena.alloc(ArenaNode::Scope(Box::new(scope))) { ArenaNode::Scope(e) => e, _ => unimplemented!(), } diff --git a/asg/src/node.rs b/asg/src/node.rs index 58b4b7709e..4272c5669a 100644 --- a/asg/src/node.rs +++ b/asg/src/node.rs @@ -44,7 +44,7 @@ pub(super) trait FromAst<'a, T: leo_ast::Node + 'static>: Sized { pub enum ArenaNode<'a> { Expression(Expression<'a>), - Scope(Scope<'a>), + Scope(Box>), Statement(Statement<'a>), Variable(Variable<'a>), Circuit(Circuit<'a>), diff --git a/asg/src/program/mod.rs b/asg/src/program/mod.rs index 63ac01f54f..7b4d525676 100644 --- a/asg/src/program/mod.rs +++ b/asg/src/program/mod.rs @@ -224,7 +224,7 @@ impl<'a> Program<'a> { } } - let import_scope = match context.arena.alloc(ArenaNode::Scope(Scope { + let import_scope = match context.arena.alloc(ArenaNode::Scope(Box::new(Scope { context, id: context.get_id(), parent_scope: Cell::new(None), @@ -235,7 +235,7 @@ impl<'a> Program<'a> { circuits: RefCell::new(imported_circuits), function: Cell::new(None), input: Cell::new(None), - })) { + }))) { ArenaNode::Scope(c) => c, _ => unimplemented!(), }; diff --git a/asg/src/reducer/reconstructing_director.rs b/asg/src/reducer/reconstructing_director.rs index c62747bed5..fadbb0b8db 100644 --- a/asg/src/reducer/reconstructing_director.rs +++ b/asg/src/reducer/reconstructing_director.rs @@ -322,6 +322,12 @@ impl<'a, R: ReconstructingReducerProgram<'a>> ReconstructingDirector<'a, R> { self.reducer.reduce_circuit(input, members) } + pub fn reduce_global_const(&mut self, input: &'a DefinitionStatement<'a>) -> &'a DefinitionStatement<'a> { + let value = self.reduce_expression(input.value.get()); + + self.reducer.reduce_global_const(input, value) + } + pub fn reduce_program(&mut self, input: Program<'a>) -> Program<'a> { let imported_modules = input .imported_modules @@ -338,10 +344,11 @@ impl<'a, R: ReconstructingReducerProgram<'a>> ReconstructingDirector<'a, R> { .iter() .map(|(name, c)| (name.clone(), self.reduce_circuit(c))) .collect(); + let global_consts = input .global_consts .iter() - .map(|(name, gc)| (name.clone(), <&DefinitionStatement>::clone(gc))) // TODO REDUCE GC + .map(|(name, gc)| (name.clone(), self.reduce_global_const(gc))) .collect(); self.reducer diff --git a/asg/src/reducer/reconstructing_reducer.rs b/asg/src/reducer/reconstructing_reducer.rs index 5b8df4f438..e034c7c3b8 100644 --- a/asg/src/reducer/reconstructing_reducer.rs +++ b/asg/src/reducer/reconstructing_reducer.rs @@ -382,6 +382,15 @@ pub trait ReconstructingReducerProgram<'a>: ReconstructingReducerStatement<'a> { input } + fn reduce_global_const( + &mut self, + input: &'a DefinitionStatement<'a>, + value: &'a Expression<'a>, + ) -> &'a DefinitionStatement<'a> { + input.value.set(value); + input + } + fn reduce_program( &mut self, input: Program<'a>, diff --git a/asg/src/reducer/visitor.rs b/asg/src/reducer/visitor.rs index b0f4d3ee6a..5e6bb43abf 100644 --- a/asg/src/reducer/visitor.rs +++ b/asg/src/reducer/visitor.rs @@ -159,6 +159,10 @@ pub trait ProgramVisitor<'a>: StatementVisitor<'a> { Default::default() } + fn visit_global_const(&mut self, input: &'a DefinitionStatement<'a>) -> VisitResult { + Default::default() + } + fn visit_program(&mut self, input: &Program<'a>) -> VisitResult { Default::default() } diff --git a/asg/src/reducer/visitor_director.rs b/asg/src/reducer/visitor_director.rs index 9665f31053..e095138ae1 100644 --- a/asg/src/reducer/visitor_director.rs +++ b/asg/src/reducer/visitor_director.rs @@ -424,6 +424,16 @@ impl<'a, R: ProgramVisitor<'a>> VisitorDirector<'a, R> { } } + pub fn visit_global_const(&mut self, input: &'a DefinitionStatement<'a>) -> ConcreteVisitResult { + match self.visitor.visit_global_const(input) { + VisitResult::VisitChildren => { + self.visit_expression(&input.value)?; + Ok(()) + } + x => x.into(), + } + } + pub fn visit_program(&mut self, input: &Program<'a>) -> ConcreteVisitResult { match self.visitor.visit_program(input) { VisitResult::VisitChildren => { @@ -436,6 +446,9 @@ impl<'a, R: ProgramVisitor<'a>> VisitorDirector<'a, R> { for (_, circuit) in input.circuits.iter() { self.visit_circuit(circuit)?; } + for (_, global_const) in input.global_consts.iter() { + self.visit_global_const(global_const)?; + } Ok(()) } x => x.into(), diff --git a/ast/src/reducer/global_const.rs b/ast/src/reducer/global_const.rs deleted file mode 100644 index b7bb856779..0000000000 --- a/ast/src/reducer/global_const.rs +++ /dev/null @@ -1,57 +0,0 @@ -// 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 . - -use leo_grammar::ast::{LanguageParser, Rule}; - -use pest::*; - -#[test] -fn global_const() { - parses_to! { - parser: LanguageParser, - input: "const basic: u32 = 8;", - rule: Rule::global_const, - tokens: [ - global_const(0, 21, [ - const_(0, 6, []), - variables(6, 16, [ - variable_name(6, 11, [ - identifier(6, 11, []) - ]), - type_(13, 16, [ - type_data(13, 16, [ - type_integer(13, 16, [ - type_integer_unsigned(13, 16, [ - type_u32(13, 16, []) - ]) - ]) - ]) - ]), - ]), - expression(19, 20, [ - expression_term(19, 20, [ - value(19, 20, [ - value_number(19, 20, [ - number_positive(19, 20, []) - ]) - ]) - ]) - ]), - LINE_END(20, 21, []) - ]) - ] - } -} diff --git a/ast/src/reducer/reconstructing_director.rs b/ast/src/reducer/reconstructing_director.rs index 101f71266b..6449022721 100644 --- a/ast/src/reducer/reconstructing_director.rs +++ b/ast/src/reducer/reconstructing_director.rs @@ -477,16 +477,13 @@ impl ReconstructingDirector { functions.insert(self.reduce_identifier(identifier)?, self.reduce_function(function)?); } - // TODO reduce gcs + let mut global_consts = IndexMap::new(); + for (name, definition) in program.global_consts.iter() { + global_consts.insert(name.clone(), self.reduce_definition(&definition)?); + } - self.reducer.reduce_program( - program, - inputs, - imports, - circuits, - functions, - program.global_consts.clone(), - ) + self.reducer + .reduce_program(program, inputs, imports, circuits, functions, global_consts) } pub fn reduce_function_input_variable(