Start transitioning design of ast reduction

to method-on-struct based system
This commit is contained in:
greg 2019-09-10 09:27:33 -07:00
parent 6c3a4f907b
commit 25f51a314d
3 changed files with 22 additions and 6 deletions

View File

@ -5,6 +5,7 @@ use std::rc::Rc;
use crate::symbol_table::SymbolTable; use crate::symbol_table::SymbolTable;
use crate::scope_resolution::ScopeResolver; use crate::scope_resolution::ScopeResolver;
use crate::reduced_ast::reduce;
use crate::eval::State; use crate::eval::State;
fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> { fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> {
@ -18,7 +19,7 @@ fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> {
let _ = scope_resolver.resolve(&mut ast); 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); let all_output = state.evaluate(reduced, true);
all_output all_output
} }

View File

@ -107,14 +107,29 @@ pub enum Func {
} }
} }
impl AST { pub fn reduce(ast: &AST, symbol_table: &SymbolTable) -> ReducedAST {
pub fn reduce(&self, 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![]; let mut output = vec![];
for statement in self.0.iter() { for statement in input.0.iter() {
output.push(statement.reduce(symbol_table)); output.push(statement.reduce(self.symbol_table));
} }
ReducedAST(output) ReducedAST(output)
} }
/*
fn meta_statement(&mut self, stmt: &Meta<Statement>) -> Stmt {
}
*/
} }
impl Meta<Statement> { impl Meta<Statement> {

View File

@ -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<reduced_ast::ReducedAST, String> { fn ast_reducing(input: ast::AST, handle: &mut Schala, comp: Option<&mut PassDebugArtifact>) -> Result<reduced_ast::ReducedAST, String> {
let ref symbol_table = handle.symbol_table.borrow(); 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))); comp.map(|comp| comp.add_artifact(format!("{:?}", output)));
Ok(output) Ok(output)
} }