From 25f51a314d3e7e8bb6f6d8a4246ad0933192ae57 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 10 Sep 2019 09:27:33 -0700 Subject: [PATCH] Start transitioning design of ast reduction to method-on-struct based system --- schala-lang/language/src/eval/test.rs | 3 ++- schala-lang/language/src/reduced_ast.rs | 23 +++++++++++++++++++---- schala-lang/language/src/schala.rs | 2 +- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/schala-lang/language/src/eval/test.rs b/schala-lang/language/src/eval/test.rs index 527f912..e77f2a8 100644 --- a/schala-lang/language/src/eval/test.rs +++ b/schala-lang/language/src/eval/test.rs @@ -5,6 +5,7 @@ use std::rc::Rc; use crate::symbol_table::SymbolTable; use crate::scope_resolution::ScopeResolver; +use crate::reduced_ast::reduce; use crate::eval::State; fn evaluate_all_outputs(input: &str) -> Vec> { @@ -18,7 +19,7 @@ fn evaluate_all_outputs(input: &str) -> Vec> { let _ = scope_resolver.resolve(&mut ast); } - let reduced = ast.reduce(&state.symbol_table_handle.borrow()); + let reduced = reduce(&ast, &state.symbol_table_handle.borrow()); let all_output = state.evaluate(reduced, true); all_output } diff --git a/schala-lang/language/src/reduced_ast.rs b/schala-lang/language/src/reduced_ast.rs index 3669a9d..fbc23d0 100644 --- a/schala-lang/language/src/reduced_ast.rs +++ b/schala-lang/language/src/reduced_ast.rs @@ -107,14 +107,29 @@ pub enum Func { } } -impl AST { - pub fn reduce(&self, symbol_table: &SymbolTable) -> ReducedAST { +pub fn reduce(ast: &AST, symbol_table: &SymbolTable) -> ReducedAST { + let mut reducer = Reducer { symbol_table }; + reducer.ast(ast) +} + +struct Reducer<'a> { + symbol_table: &'a SymbolTable +} + +impl<'a> Reducer<'a> { + fn ast(&mut self, input: &AST) -> ReducedAST { let mut output = vec![]; - for statement in self.0.iter() { - output.push(statement.reduce(symbol_table)); + for statement in input.0.iter() { + output.push(statement.reduce(self.symbol_table)); } ReducedAST(output) } + + /* + fn meta_statement(&mut self, stmt: &Meta) -> Stmt { + + } + */ } impl Meta { diff --git a/schala-lang/language/src/schala.rs b/schala-lang/language/src/schala.rs index a16d6b8..7b02593 100644 --- a/schala-lang/language/src/schala.rs +++ b/schala-lang/language/src/schala.rs @@ -166,7 +166,7 @@ fn typechecking(input: ast::AST, handle: &mut Schala, comp: Option<&mut PassDebu fn ast_reducing(input: ast::AST, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result { let ref symbol_table = handle.symbol_table.borrow(); - let output = input.reduce(symbol_table); + let output = reduced_ast::reduce(&input, symbol_table); comp.map(|comp| comp.add_artifact(format!("{:?}", output))); Ok(output) }