Add interpret-repl

This commit is contained in:
Scott Richmond 2022-05-26 19:50:45 -04:00
parent ef97cf16e4
commit 698a07d085

View File

@ -487,8 +487,7 @@
::ast/script ::ast/script
(let [exprs (:exprs ast) (let [exprs (:exprs ast)
inner (pop exprs) inner (pop exprs)
last (peek exprs) last (peek exprs)]
ctx (volatile! (merge prelude/prelude ctx))]
(run! #(interpret-ast % ctx) inner) (run! #(interpret-ast % ctx) inner)
(interpret-ast last ctx)) (interpret-ast last ctx))
@ -516,7 +515,8 @@
(defn interpret [parsed file] (defn interpret [parsed file]
(try (try
(interpret-ast (::parser/ast parsed) {::file file}) (let [base-ctx (volatile! (merge {:file file} prelude/prelude))]
(interpret-ast (::parser/ast parsed) base-ctx))
(catch clojure.lang.ExceptionInfo e (catch clojure.lang.ExceptionInfo e
(println "Ludus panicked in" file) (println "Ludus panicked in" file)
(println "On line" (get-in (ex-data e) [:ast :token ::token/line])) (println "On line" (get-in (ex-data e) [:ast :token ::token/line]))
@ -526,13 +526,34 @@
(defn interpret-safe [parsed] (defn interpret-safe [parsed]
(try (try
(interpret-ast (::parser/ast parsed) {}) (let [base-ctx (volatile! (merge {} prelude/prelude))]
(interpret-ast (::parser/ast parsed) base-ctx))
(catch clojure.lang.ExceptionInfo e (catch clojure.lang.ExceptionInfo e
(println "Ludus panicked!") (println "Ludus panicked!")
(println "On line" (get-in (ex-data e) [:ast :token ::token/line])) (println "On line" (get-in (ex-data e) [:ast :token ::token/line]))
(println (ex-message e)) (println (ex-message e))
(pp/pprint (ex-data e))))) (pp/pprint (ex-data e)))))
(defn interpret-repl
([parsed]
(let [base-ctx (volatile! (merge {} prelude/prelude))]
(try
(let [result (interpret-ast (::parser/ast parsed) base-ctx)]
{:result result :ctx base-ctx})
(catch clojure.lang.ExceptionInfo e
(println "Ludus panicked!")
(println (ex-message e))
{:result ::error :ctx base-ctx}))))
([parsed ctx]
(let [orig-ctx @ctx]
(try
(let [result (interpret-ast (::parser/ast parsed) ctx)]
{:result result :ctx ctx})
(catch clojure.lang.ExceptionInfo e
(println "Ludus panicked!")
(println (ex-message e))
{:result ::error :ctx (volatile! orig-ctx)})))))
(comment (comment
(def source "panic! :oops (def source "panic! :oops