From 6ddea790c05fbec3a0335a8bc3b0a3bdbc76e557 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 31 Jul 2015 00:27:24 -0700 Subject: [PATCH] Beginning of variable lookup everything is null --- src/evaluate.rs | 16 ++++++++++++++++ src/parser.rs | 1 + 2 files changed, 17 insertions(+) diff --git a/src/evaluate.rs b/src/evaluate.rs index c985977..b61b4bb 100644 --- a/src/evaluate.rs +++ b/src/evaluate.rs @@ -16,6 +16,12 @@ impl Environment { Environment(ref mut hash_map) => hash_map.insert(name, binding) }; } + + fn lookup_binding(&mut self, name: &String) -> Option<&Box> { + match *self { + Environment(ref mut hash_map) => hash_map.get(name) + } + } } pub fn evaluate(ast: AST, env: Environment) -> String { @@ -26,6 +32,7 @@ pub fn evaluate(ast: AST, env: Environment) -> String { DoNothing => "".to_string(), Number(n) => return format!("{}", n), LangString(s) => return format!("\"{}\"", s), + Null => "null".to_string(), _ => return "not implemented".to_string() } } @@ -34,6 +41,15 @@ fn reduce(evr: EvalResult) -> EvalResult { let (mut ast, mut env) = evr; match ast { + Name(name) => { + match env.lookup_binding(&name) { + Some(_) => { + (Null, env) + }, + None => (Null, env) + } + }, + Statements(stmts) => { let mut reduced_ast = DoNothing; let mut reduced_env = env; diff --git a/src/parser.rs b/src/parser.rs index 7002281..ccaac0e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -6,6 +6,7 @@ use tokenizer::Token::*; #[derive(Debug)] pub enum AST { + Null, Name(String), LangString(String), Number(f64),