about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/interpreter
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-01-14T20·16+0300
committertazjin <mail@tazj.in>2021-01-14T20·26+0000
commit740a9a3565b3c30d8b667ff159b15d9f455e94b9 (patch)
treeed144ed7adf28143b121d9cc14cc8df53cb8015b /users/tazjin/rlox/src/interpreter
parent39439d59e8e9ddb1e2b7802f3aff092d77de7acf (diff)
feat(tazjin/rlox): Implement support for closures r/2109
Change-Id: I0ffc810807a1a6ec90455a4f2d2bd977833005bd
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2396
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/interpreter')
-rw-r--r--users/tazjin/rlox/src/interpreter/tests.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/users/tazjin/rlox/src/interpreter/tests.rs b/users/tazjin/rlox/src/interpreter/tests.rs
index 5bc9f0a0a475..875116593e44 100644
--- a/users/tazjin/rlox/src/interpreter/tests.rs
+++ b/users/tazjin/rlox/src/interpreter/tests.rs
@@ -76,3 +76,25 @@ add(1, 2, 3);
 
     assert_eq!(Value::Literal(Literal::Number(6.0)), result);
 }
+
+#[test]
+fn test_closure() {
+    let result = parse_eval(
+        r#"
+fun makeCounter() {
+  var i = 0;
+  fun count() {
+    i = i + 1;
+  }
+
+  return count;
+}
+
+var counter = makeCounter();
+counter(); // "1".
+counter(); // "2".
+"#,
+    );
+
+    assert_eq!(Value::Literal(Literal::Number(2.0)), result);
+}