From 7ab385d398b4c3185c27bb9520f844675d1ad2ad Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 14 May 2018 22:55:53 -0700 Subject: [PATCH] Bring custom ADTs to the repl --- schala-lang/src/ast_reducing.rs | 1 + schala-lang/src/eval.rs | 49 +++++++++++++++++++++++---------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/schala-lang/src/ast_reducing.rs b/schala-lang/src/ast_reducing.rs index 21f1a00..24fd796 100644 --- a/schala-lang/src/ast_reducing.rs +++ b/schala-lang/src/ast_reducing.rs @@ -51,6 +51,7 @@ pub enum Lit { Float(f64), Bool(bool), StringLit(Rc), + Custom(Rc), } #[derive(Debug, Clone)] diff --git a/schala-lang/src/eval.rs b/schala-lang/src/eval.rs index da86358..e7d8106 100644 --- a/schala-lang/src/eval.rs +++ b/schala-lang/src/eval.rs @@ -7,7 +7,7 @@ use itertools::Itertools; use util::StateStack; use ast_reducing::{ReducedAST, Stmt, Expr, Lit, Func}; -use typechecking::TypeContext; +use typechecking::{TypeContext, Symbol, Type, TConst}; pub struct State<'a> { values: StateStack<'a, Rc, ValueEntry>, @@ -55,6 +55,7 @@ impl Expr { Float(f) => format!("{}", f), Bool(b) => format!("{}", b), StringLit(s) => format!("\"{}\"", s), + Custom(s) => format!("{}", s), }, Expr::Func(f) => match f { BuiltIn(name) => format!("", name), @@ -259,24 +260,44 @@ impl<'a> State<'a> { fn value(&mut self, name: Rc) -> EvalResult { use self::ValueEntry::*; + use self::Func::*; //TODO add a layer of indirection here to talk to the symbol table first, and only then look up //in the values table let type_context = self.type_context_handle.borrow(); - //type_context.symbol_table - - match self.values.lookup(&name) { - None => return Err(format!("Value {} not found", *name)), - Some(lookup) => match lookup { - Binding { val, .. } => Ok( - if let Expr::Func(Func::UserDefined { name: None, params, body }) = val { - Expr::Func(Func::UserDefined { name: Some(name.clone()), params: params.clone(), body: body.clone() }) //TODO here is unnecessary cloning - } else { - val.clone() - } - ) + Ok(match type_context.symbol_table.values.get(&name) { + Some(Symbol { name, ty }) => match ty { + Type::Const(TConst::Custom(typename)) => { + Expr::Lit(Lit::Custom(name.clone())) + }, + Type::Func(_,_) => match self.values.lookup(&name) { + Some(Binding { val: Expr::Func(UserDefined { name, params, body }), .. }) => { + Expr::Func(UserDefined { name: name.clone(), params: params.clone(), body: body.clone() }) + }, + _ => unreachable!(), + }, + e => return Err(format!("Bad type in symbol table {:?}", e)) + }, + /* see if it's an ordinary variable TODO make variables go in symbol table */ + None => match self.values.lookup(&name) { + Some(Binding { val, .. }) => val.clone(), + None => return Err(format!("Couldn't find value {}", name)), } - } + }) + + /* + None => match self.values.lookup(&name) { + None => return Err(format!("Value {} not found", *name)), + Some(lookup) => match lookup { + Binding { val, .. } => Ok( + if let Expr::Func(Func::UserDefined { name: None, params, body }) = val { + Expr::Func(Func::UserDefined { name: Some(name.clone()), params: params.clone(), body: body.clone() }) //TODO here is unnecessary cloning + } else { + val.clone() + }) + } + } + */ } }