Janet implementation of Lox from the book Crafting Interpreters

use janet's getline to implement repl

Changed files
+10 -22
lox
+10 -2
lox/main.janet
··· 1 1 (import ./scanner) 2 2 (import ./parser) 3 3 (import ./interpreter) 4 - (import ./repl) 5 4 6 5 (defn process [contents] 7 6 (-> contents ··· 10 9 :parse 11 10 interpreter/interpret)) 12 11 12 + (defn run-repl [process] 13 + (os/sigaction :int nil) 14 + (print "janet-lox interpreter 0.0.1") 15 + (loop [res :iterate (getline "> ") 16 + :let [line (string res)] 17 + :unless (= res :cancel) 18 + :until (= line "")] 19 + (try (process line) ([err] (printf "error: %s" err))))) 20 + 13 21 (defn main [_ &opt path & args] 14 22 (unless (empty? args) (error "expected 0 or 1 args")) 15 23 (if (nil? path) 16 24 17 - (with-dyns [:expr-out stderr] (repl/run process)) 25 + (with-dyns [:expr-out stderr] (run-repl process)) 18 26 (process (slurp path))))
-20
lox/repl.janet
··· 1 - (import spork/stream) 2 - 3 - (defn- inpput-prompt [] 4 - (prin "> ") 5 - (:flush stdout)) 6 - 7 - (defn run [process] 8 - (var buf @"") 9 - (os/sigaction :int |(do (buffer/clear buf) (print) (inpput-prompt)) true) 10 - (with [input (os/open "/dev/stdin" :r)] 11 - (forever 12 - (inpput-prompt) 13 - (prompt :a) 14 - (forever (match (string (:read input 1)) 15 - "" (when (empty? buf) (set buf nil) (break)) 16 - "\n" (break) 17 - c (buffer/push-string buf c))) 18 - (when (nil? buf) (break)) 19 - (try (process buf) ([err] (printf "error: %s" err))) 20 - (buffer/clear buf))))