Retrieve function from memory when called

This commit is contained in:
greg 2018-05-11 23:34:26 -07:00
parent 1011ff08f3
commit fdbb21990d
2 changed files with 13 additions and 9 deletions

View File

@ -22,7 +22,7 @@ pub enum Expr {
Func(Func),
Val(Rc<String>),
Call {
f: Func,
f: Box<Expr>,
args: Vec<Expr>,
},
UnimplementedSigilValue
@ -79,12 +79,10 @@ impl Expression {
BinExp(binop, lhs, rhs) => binop.reduce(lhs, rhs),
PrefixExp(op, arg) => op.reduce(arg),
Value(name) => Expr::Val(name.clone()),
/*
&Call { ref f, ref arguments } => Expr::Call {
f: Box<Expression>,
arguments: Vec<Expression>,
Call { f, arguments } => Expr::Call {
f: Box::new(f.reduce()),
args: arguments.iter().map(|arg| arg.reduce()).collect(),
},
*/
e => Expr::UnimplementedSigilValue,
}
}
@ -111,14 +109,14 @@ impl Declaration {
impl BinOp {
fn reduce(&self, lhs: &Box<Expression>, rhs: &Box<Expression>) -> Expr {
let f = Func::BuiltIn(self.sigil().clone());
let f = Box::new(Expr::Func(Func::BuiltIn(self.sigil().clone())));
Expr::Call { f, args: vec![lhs.reduce(), rhs.reduce()]}
}
}
impl PrefixOp {
fn reduce(&self, arg: &Box<Expression>) -> Expr {
let f = Func::BuiltIn(self.sigil().clone());
let f = Box::new(Expr::Func(Func::BuiltIn(self.sigil().clone())));
Expr::Call { f, args: vec![arg.reduce()]}
}
}

View File

@ -388,7 +388,13 @@ impl<'a> State<'a> {
use self::Expr::*;
match expr {
literal @ Lit(_) => Ok(literal),
Call { f, args } => self.apply_function(f, args),
Call { box f, args } => {
let f = match self.expression(f)? {
Func(f) => f,
other => return Err(format!("Tried to call {:?} which is not a function", other)),
};
self.apply_function(f, args)
},
Val(v) => self.value(v),
func @ Func(_) => Ok(func),
e => Err(format!("Expr {:?} eval not implemented", e))