Variable lookup
This commit is contained in:
parent
481afb0f87
commit
78ba4e1ed3
@ -6,7 +6,7 @@ use builtin::{BinOp, PrefixOp};
|
|||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ReducedAST(pub Vec<Stmt>);
|
pub struct ReducedAST(pub Vec<Stmt>);
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Stmt {
|
pub enum Stmt {
|
||||||
Binding {
|
Binding {
|
||||||
name: Rc<String>,
|
name: Rc<String>,
|
||||||
@ -16,10 +16,11 @@ pub enum Stmt {
|
|||||||
Expr(Expr),
|
Expr(Expr),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Expr {
|
pub enum Expr {
|
||||||
Lit(Lit),
|
Lit(Lit),
|
||||||
Func(Func),
|
Func(Func),
|
||||||
|
Val(Rc<String>),
|
||||||
Call {
|
Call {
|
||||||
f: Func,
|
f: Func,
|
||||||
args: Vec<Expr>,
|
args: Vec<Expr>,
|
||||||
@ -27,7 +28,7 @@ pub enum Expr {
|
|||||||
UnimplementedSigilValue
|
UnimplementedSigilValue
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Lit {
|
pub enum Lit {
|
||||||
Nat(u64),
|
Nat(u64),
|
||||||
Int(i64),
|
Int(i64),
|
||||||
@ -36,7 +37,7 @@ pub enum Lit {
|
|||||||
StringLit(Rc<String>),
|
StringLit(Rc<String>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Func {
|
pub enum Func {
|
||||||
BuiltIn(Rc<String>),
|
BuiltIn(Rc<String>),
|
||||||
UserDefined {
|
UserDefined {
|
||||||
@ -70,6 +71,7 @@ impl Expression {
|
|||||||
&BoolLiteral(ref b) => Expr::Lit(Lit::Bool(*b)),
|
&BoolLiteral(ref b) => Expr::Lit(Lit::Bool(*b)),
|
||||||
&BinExp(ref binop, ref lhs, ref rhs) => binop.reduce(lhs, rhs),
|
&BinExp(ref binop, ref lhs, ref rhs) => binop.reduce(lhs, rhs),
|
||||||
&PrefixExp(ref op, ref arg) => op.reduce(arg),
|
&PrefixExp(ref op, ref arg) => op.reduce(arg),
|
||||||
|
&Value(ref name) => Expr::Val(name.clone()),
|
||||||
e => Expr::UnimplementedSigilValue,
|
e => Expr::UnimplementedSigilValue,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -383,6 +383,7 @@ impl<'a> State<'a> {
|
|||||||
match expr {
|
match expr {
|
||||||
literal @ Lit(_) => Ok(literal),
|
literal @ Lit(_) => Ok(literal),
|
||||||
Call { f, args } => self.apply_function(f, args),
|
Call { f, args } => self.apply_function(f, args),
|
||||||
|
Val(v) => self.value(v),
|
||||||
_ => Err(format!("NOT IMPLEMENTED YET"))
|
_ => Err(format!("NOT IMPLEMENTED YET"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -430,4 +431,27 @@ impl<'a> State<'a> {
|
|||||||
_ => return Err(format!("Runtime error: not yet implemented")),
|
_ => 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()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user