Variable lookup

This commit is contained in:
greg 2018-05-11 17:24:11 -07:00
parent 481afb0f87
commit 78ba4e1ed3
2 changed files with 30 additions and 4 deletions

View File

@ -6,7 +6,7 @@ use builtin::{BinOp, PrefixOp};
#[derive(Debug)]
pub struct ReducedAST(pub Vec<Stmt>);
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum Stmt {
Binding {
name: Rc<String>,
@ -16,10 +16,11 @@ pub enum Stmt {
Expr(Expr),
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum Expr {
Lit(Lit),
Func(Func),
Val(Rc<String>),
Call {
f: Func,
args: Vec<Expr>,
@ -27,7 +28,7 @@ pub enum Expr {
UnimplementedSigilValue
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum Lit {
Nat(u64),
Int(i64),
@ -36,7 +37,7 @@ pub enum Lit {
StringLit(Rc<String>),
}
#[derive(Debug)]
#[derive(Debug, Clone)]
pub enum Func {
BuiltIn(Rc<String>),
UserDefined {
@ -70,6 +71,7 @@ impl Expression {
&BoolLiteral(ref b) => Expr::Lit(Lit::Bool(*b)),
&BinExp(ref binop, ref lhs, ref rhs) => binop.reduce(lhs, rhs),
&PrefixExp(ref op, ref arg) => op.reduce(arg),
&Value(ref name) => Expr::Val(name.clone()),
e => Expr::UnimplementedSigilValue,
}
}

View File

@ -383,6 +383,7 @@ impl<'a> State<'a> {
match expr {
literal @ Lit(_) => Ok(literal),
Call { f, args } => self.apply_function(f, args),
Val(v) => self.value(v),
_ => Err(format!("NOT IMPLEMENTED YET"))
}
}
@ -430,4 +431,27 @@ impl<'a> State<'a> {
_ => return Err(format!("Runtime error: not yet implemented")),
})
}
fn value(&mut self, name: Rc<String>) -> EvalResult<Expr> {
use self::ValueEntry::*;
match self.values.lookup(&name) {
None => return Err(format!("Value {} not found", *name)),
Some(lookup) => match lookup {
&Binding { ref val, .. } => Ok(val.clone()),
_ => Err(format!("Functions not done")),
}
}
/*
fn eval_value(&mut self, name: Rc<String>) -> EvalResult<FullyEvaluatedExpr> {
use self::ValueEntry::*;
match self.lookup(&name) {
None => return Err(format!("Value {} not found", *name)),
Some(lookup) => match lookup {
&Binding { ref val } => Ok(val.clone()),
&Function { .. } => Ok(FullyEvaluatedExpr::FuncLit(name.clone()))
}
}
}
*/
}
}