Beginning of variable lookup
everything is null
This commit is contained in:
parent
6897eb1283
commit
6ddea790c0
@ -16,6 +16,12 @@ impl Environment {
|
|||||||
Environment(ref mut hash_map) => hash_map.insert(name, binding)
|
Environment(ref mut hash_map) => hash_map.insert(name, binding)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn lookup_binding(&mut self, name: &String) -> Option<&Box<AST>> {
|
||||||
|
match *self {
|
||||||
|
Environment(ref mut hash_map) => hash_map.get(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn evaluate(ast: AST, env: Environment) -> String {
|
pub fn evaluate(ast: AST, env: Environment) -> String {
|
||||||
@ -26,6 +32,7 @@ pub fn evaluate(ast: AST, env: Environment) -> String {
|
|||||||
DoNothing => "".to_string(),
|
DoNothing => "".to_string(),
|
||||||
Number(n) => return format!("{}", n),
|
Number(n) => return format!("{}", n),
|
||||||
LangString(s) => return format!("\"{}\"", s),
|
LangString(s) => return format!("\"{}\"", s),
|
||||||
|
Null => "null".to_string(),
|
||||||
_ => return "not implemented".to_string()
|
_ => return "not implemented".to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -34,6 +41,15 @@ fn reduce(evr: EvalResult) -> EvalResult {
|
|||||||
let (mut ast, mut env) = evr;
|
let (mut ast, mut env) = evr;
|
||||||
|
|
||||||
match ast {
|
match ast {
|
||||||
|
Name(name) => {
|
||||||
|
match env.lookup_binding(&name) {
|
||||||
|
Some(_) => {
|
||||||
|
(Null, env)
|
||||||
|
},
|
||||||
|
None => (Null, env)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
Statements(stmts) => {
|
Statements(stmts) => {
|
||||||
let mut reduced_ast = DoNothing;
|
let mut reduced_ast = DoNothing;
|
||||||
let mut reduced_env = env;
|
let mut reduced_env = env;
|
||||||
|
@ -6,6 +6,7 @@ use tokenizer::Token::*;
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum AST {
|
pub enum AST {
|
||||||
|
Null,
|
||||||
Name(String),
|
Name(String),
|
||||||
LangString(String),
|
LangString(String),
|
||||||
Number(f64),
|
Number(f64),
|
||||||
|
Loading…
Reference in New Issue
Block a user