From 7809cda240b5084c814166676c6fe28d1bc2b9df Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 12 Jun 2018 02:56:28 -0700 Subject: [PATCH] Pass symbol_table to ast reduce To distinguish between values and data constructors --- schala-lang/src/lib.rs | 5 ++-- schala-lang/src/reduced_ast.rs | 47 +++++++++++++++++----------------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/schala-lang/src/lib.rs b/schala-lang/src/lib.rs index 23f47fd..c478b05 100644 --- a/schala-lang/src/lib.rs +++ b/schala-lang/src/lib.rs @@ -117,8 +117,9 @@ fn typechecking(handle: &mut Schala, input: ast::AST, comp: Option<&mut Unfinish } } -fn ast_reducing(_handle: &mut Schala, input: ast::AST, comp: Option<&mut UnfinishedComputation>) -> Result { - let output = input.reduce(); +fn ast_reducing(handle: &mut Schala, input: ast::AST, comp: Option<&mut UnfinishedComputation>) -> Result { + let ref symbol_table = handle.symbol_table.borrow(); + let output = input.reduce(symbol_table); comp.map(|comp| comp.add_artifact(TraceArtifact::new("ast_reducing", format!("{:?}", output)))); Ok(output) } diff --git a/schala-lang/src/reduced_ast.rs b/schala-lang/src/reduced_ast.rs index 2b5aca3..98d57ea 100644 --- a/schala-lang/src/reduced_ast.rs +++ b/schala-lang/src/reduced_ast.rs @@ -1,6 +1,7 @@ use std::rc::Rc; use ast::{AST, Statement, Expression, Declaration}; +use symbol_table::SymbolTable; use builtin::{BinOp, PrefixOp}; #[derive(Debug)] @@ -65,27 +66,27 @@ pub enum Func { } impl AST { - pub fn reduce(&self) -> ReducedAST { + pub fn reduce(&self, symbol_table: &SymbolTable) -> ReducedAST { let mut output = vec![]; for statement in self.0.iter() { - output.push(statement.reduce()); + output.push(statement.reduce(symbol_table)); } ReducedAST(output) } } impl Statement { - fn reduce(&self) -> Stmt { + fn reduce(&self, symbol_table: &SymbolTable) -> Stmt { use ast::Statement::*; match self { - ExpressionStatement(expr) => Stmt::Expr(expr.reduce()), - Declaration(decl) => decl.reduce(), + ExpressionStatement(expr) => Stmt::Expr(expr.reduce(symbol_table)), + Declaration(decl) => decl.reduce(symbol_table), } } } impl Expression { - fn reduce(&self) -> Expr { + fn reduce(&self, symbol_table: &SymbolTable) -> Expr { use ast::ExpressionType::*; let ref input = self.0; match input { @@ -93,20 +94,20 @@ impl Expression { FloatLiteral(f) => Expr::Lit(Lit::Float(*f)), StringLiteral(s) => Expr::Lit(Lit::StringLit(s.clone())), BoolLiteral(b) => Expr::Lit(Lit::Bool(*b)), - BinExp(binop, lhs, rhs) => binop.reduce(lhs, rhs), - PrefixExp(op, arg) => op.reduce(arg), + BinExp(binop, lhs, rhs) => binop.reduce(symbol_table, lhs, rhs), + PrefixExp(op, arg) => op.reduce(symbol_table, arg), Value(name) => Expr::Val(name.clone()), Call { f, arguments } => Expr::Call { - f: Box::new(f.reduce()), - args: arguments.iter().map(|arg| arg.reduce()).collect(), + f: Box::new(f.reduce(symbol_table)), + args: arguments.iter().map(|arg| arg.reduce(symbol_table)).collect(), }, - TupleLiteral(exprs) => Expr::Tuple(exprs.iter().map(|e| e.reduce()).collect()), + TupleLiteral(exprs) => Expr::Tuple(exprs.iter().map(|e| e.reduce(symbol_table)).collect()), IfExpression(cond, then_clause, else_clause) => Expr::Conditional { - cond: Box::new((**cond).reduce()), - then_clause: then_clause.iter().map(|expr| expr.reduce()).collect(), + cond: Box::new((**cond).reduce(symbol_table)), + then_clause: then_clause.iter().map(|expr| expr.reduce(symbol_table)).collect(), else_clause: match else_clause { None => vec![], - Some(stmts) => stmts.iter().map(|expr| expr.reduce()).collect(), + Some(stmts) => stmts.iter().map(|expr| expr.reduce(symbol_table)).collect(), } }, _ => Expr::UnimplementedSigilValue, @@ -115,17 +116,17 @@ impl Expression { } impl Declaration { - fn reduce(&self) -> Stmt { + fn reduce(&self, symbol_table: &SymbolTable) -> Stmt { use self::Declaration::*; use ::ast::Signature; match self { - Binding {name, constant, expr } => Stmt::Binding { name: name.clone(), constant: *constant, expr: expr.reduce() }, + Binding {name, constant, expr } => Stmt::Binding { name: name.clone(), constant: *constant, expr: expr.reduce(symbol_table) }, FuncDecl(Signature { name, params, .. }, statements) => Stmt::PreBinding { name: name.clone(), func: Func::UserDefined { name: Some(name.clone()), params: params.iter().map(|param| param.0.clone()).collect(), - body: statements.iter().map(|stmt| stmt.reduce()).collect(), + body: statements.iter().map(|stmt| stmt.reduce(symbol_table)).collect(), } }, TypeDecl(_,_) => Stmt::Noop, @@ -135,22 +136,22 @@ impl Declaration { } impl BinOp { - fn reduce(&self, lhs: &Box, rhs: &Box) -> Expr { + fn reduce(&self, symbol_table: &SymbolTable, lhs: &Box, rhs: &Box) -> Expr { if **self.sigil() == "=" { Expr::Assign { - val: Box::new(lhs.reduce()), - expr: Box::new(rhs.reduce()), + val: Box::new(lhs.reduce(symbol_table)), + expr: Box::new(rhs.reduce(symbol_table)), } } else { let f = Box::new(Expr::Func(Func::BuiltIn(self.sigil().clone()))); - Expr::Call { f, args: vec![lhs.reduce(), rhs.reduce()]} + Expr::Call { f, args: vec![lhs.reduce(symbol_table), rhs.reduce(symbol_table)]} } } } impl PrefixOp { - fn reduce(&self, arg: &Box) -> Expr { + fn reduce(&self, symbol_table: &SymbolTable, arg: &Box) -> Expr { let f = Box::new(Expr::Func(Func::BuiltIn(self.sigil().clone()))); - Expr::Call { f, args: vec![arg.reduce()]} + Expr::Call { f, args: vec![arg.reduce(symbol_table)]} } }