reducers in

This commit is contained in:
gluax 2021-03-29 14:33:34 -04:00
parent e1a0838888
commit 56c6cfb6eb
9 changed files with 44 additions and 71 deletions

View File

@ -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!(),
}

View File

@ -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<Scope<'a>>),
Statement(Statement<'a>),
Variable(Variable<'a>),
Circuit(Circuit<'a>),

View File

@ -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!(),
};

View File

@ -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

View File

@ -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>,

View File

@ -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()
}

View File

@ -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(),

View File

@ -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 <https://www.gnu.org/licenses/>.
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, [])
])
]
}
}

View File

@ -477,16 +477,13 @@ impl<R: ReconstructingReducer> ReconstructingDirector<R> {
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(