Janet implementation of Lox from the book Crafting Interpreters
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

detect duplicate local vars

+9 -1
+4 -1
lox/interpreter.janet
··· 33 33 left right)) 34 34 35 35 (defn- declare-var [{:token [_ name]}] 36 + (when (table/rawget (curenv) name) 37 + (errorf "Already a variable named '%s' in this scope." name)) 36 38 (setdyn name @[])) 37 39 38 40 (defn- define-var [{:token [_ name]} val] ··· 94 96 (when init (set-var name val))) 95 97 [:fun name params body] 96 98 (let [arity (length params) 97 - env (table/clone (curenv)) 99 + closure (table/clone (curenv)) 100 + env (table/setproto @{} closure) 98 101 call (fn [args] 99 102 (loop [i :range [arity] 100 103 :let [name (params i) arg (args i)]]
+4
readme.md
··· 19 19 - [x] Control Flow 20 20 - [x] Functions 21 21 - [ ] Resolving and Binding 22 + - [x] Lexical scope 23 + - [ ] Prevent self-referential initializer 24 + - [x] Prevent variable name reuse in same scope 25 + - [ ] Prevent top-level return 22 26 - [ ] Classes 23 27 - [ ] Inheritance
+1
test/test_main.janet
··· 118 118 (test-error (process "fun add(x) { return x + 1; } print add();") "Expected 1 arguments but got 0.") 119 119 (test-error (process "fun add(x) { return x + 1; } print add(1, 2);") "Expected 1 arguments but got 2.") 120 120 (test-error (process "var x = 1; x();") "Can only call functions and classes.") 121 + (test-error (process "var x = 1; var x = 2; print x;") "Already a variable named 'x' in this scope.")