Add constructor reduced ast node; fix test

This commit is contained in:
greg 2018-06-12 03:02:50 -07:00
parent 7809cda240
commit 3b9084810e
2 changed files with 16 additions and 3 deletions

View File

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

View File

@ -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<Expr>),
Func(Func),
Val(Rc<String>),
Constructor {
name: Rc<String>,
args: Vec<Expr>,
},
Call {
f: Box<Expr>,
args: Vec<Expr>,
@ -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(),