From 3b9084810ee58607e697cf812b0f744a64f4c642 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 12 Jun 2018 03:02:50 -0700 Subject: [PATCH] Add constructor reduced ast node; fix test --- schala-lang/src/eval.rs | 3 ++- schala-lang/src/reduced_ast.rs | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/schala-lang/src/eval.rs b/schala-lang/src/eval.rs index d22dc5c..c1fe7ed 100644 --- a/schala-lang/src/eval.rs +++ b/schala-lang/src/eval.rs @@ -325,7 +325,8 @@ mod eval_tests { let mut state = State::new(symbol_table); let ast = parse(tokenize($string)).0.unwrap(); state.symbol_table_handle.borrow_mut().add_top_level_symbols(&ast); - let all_output = state.evaluate(ast.reduce(), true); + let reduced = ast.reduce(&state.symbol_table_handle.borrow()); + let all_output = state.evaluate(reduced, true); let ref output = all_output.last().unwrap(); assert_eq!(**output, Ok($correct.to_string())); } diff --git a/schala-lang/src/reduced_ast.rs b/schala-lang/src/reduced_ast.rs index 98d57ea..6e8243c 100644 --- a/schala-lang/src/reduced_ast.rs +++ b/schala-lang/src/reduced_ast.rs @@ -1,7 +1,7 @@ use std::rc::Rc; use ast::{AST, Statement, Expression, Declaration}; -use symbol_table::SymbolTable; +use symbol_table::{Symbol, SymbolSpec, SymbolTable}; use builtin::{BinOp, PrefixOp}; #[derive(Debug)] @@ -29,6 +29,10 @@ pub enum Expr { Tuple(Vec), Func(Func), Val(Rc), + Constructor { + name: Rc, + args: Vec, + }, Call { f: Box, args: Vec, @@ -96,7 +100,15 @@ impl Expression { BoolLiteral(b) => Expr::Lit(Lit::Bool(*b)), BinExp(binop, lhs, rhs) => binop.reduce(symbol_table, lhs, rhs), PrefixExp(op, arg) => op.reduce(symbol_table, arg), - Value(name) => Expr::Val(name.clone()), + Value(name) => { + match symbol_table.values.get(name) { + Some(Symbol { name, spec: SymbolSpec::DataConstructor { type_name, type_args } }) => { + //TODO finish + Expr::Val(name.clone()) + }, + _ => Expr::Val(name.clone()), + } + }, Call { f, arguments } => Expr::Call { f: Box::new(f.reduce(symbol_table)), args: arguments.iter().map(|arg| arg.reduce(symbol_table)).collect(),