From fdbb21990d9f8785760baadcb8fbd0179b498548 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 11 May 2018 23:34:26 -0700 Subject: [PATCH] Retrieve function from memory when called --- schala-lang/src/ast_reducing.rs | 14 ++++++-------- schala-lang/src/eval.rs | 8 +++++++- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/schala-lang/src/ast_reducing.rs b/schala-lang/src/ast_reducing.rs index 8f61c58..4fadc15 100644 --- a/schala-lang/src/ast_reducing.rs +++ b/schala-lang/src/ast_reducing.rs @@ -22,7 +22,7 @@ pub enum Expr { Func(Func), Val(Rc), Call { - f: Func, + f: Box, args: Vec, }, 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, - arguments: Vec, + 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, rhs: &Box) -> 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) -> 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()]} } } diff --git a/schala-lang/src/eval.rs b/schala-lang/src/eval.rs index 66671e0..8d20af5 100644 --- a/schala-lang/src/eval.rs +++ b/schala-lang/src/eval.rs @@ -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))