about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/interpreter.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-01-14T15·16+0300
committertazjin <mail@tazj.in>2021-01-14T15·19+0000
commit1d8e3f4f8b4479a47e744b3d153fe0e624958817 (patch)
tree06d2747cf725fe6e2606c90355c8c1af25305e9c /users/tazjin/rlox/src/interpreter.rs
parent56d8fa97ed936aacb66b13bb4a8c849a9797db93 (diff)
feat(tazjin/rlox): Implement function definitions r/2106
... with this, functions now work.

Note that this bubbled up another weird code structure nit: The
parser::Function type should probably not carry its name directly.

However this doesn't matter much and I don't care right now.

Change-Id: If8e3b23f07033260433b9acd45f37c0e61fd2ff8
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2393
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/interpreter.rs')
-rw-r--r--users/tazjin/rlox/src/interpreter.rs11
1 files changed, 9 insertions, 2 deletions
diff --git a/users/tazjin/rlox/src/interpreter.rs b/users/tazjin/rlox/src/interpreter.rs
index ef188b797fa8..f04d767dc5a9 100644
--- a/users/tazjin/rlox/src/interpreter.rs
+++ b/users/tazjin/rlox/src/interpreter.rs
@@ -41,7 +41,7 @@ impl<'a> Callable<'a> {
                 }
 
                 lox.interpret_block(Rc::new(RwLock::new(fn_env)), &func.body)
-            },
+            }
         }
     }
 }
@@ -220,7 +220,7 @@ impl<'a> Interpreter<'a> {
             Statement::Block(block) => return self.interpret_block(Default::default(), block),
             Statement::If(if_stmt) => return self.interpret_if(if_stmt),
             Statement::While(while_stmt) => return self.interpret_while(while_stmt),
-            Statement::Function(_) => unimplemented!(),
+            Statement::Function(func) => return self.interpret_function(func.clone()),
         };
 
         Ok(value)
@@ -278,6 +278,13 @@ impl<'a> Interpreter<'a> {
         Ok(value)
     }
 
+    fn interpret_function(&mut self, stmt: Rc<parser::Function<'a>>) -> Result<Value<'a>, Error> {
+        let name = stmt.name.clone();
+        let value = Value::Callable(Callable::Function(stmt));
+        self.define_var(&name, value.clone())?;
+        Ok(value)
+    }
+
     fn eval(&mut self, expr: &Expr<'a>) -> Result<Value<'a>, Error> {
         match expr {
             Expr::Assign(assign) => self.eval_assign(assign),