better_panics #15

Merged
scott merged 5 commits from better_panics into main 2023-12-18 05:26:58 +00:00
Showing only changes of commit 4e646101e2 - Show all commits

View File

@ -818,16 +818,16 @@
(defn- interpret-literal [ast] (-> ast :data first)) (defn- interpret-literal [ast] (-> ast :data first))
(defn- interpret-panic [ast ctx] (defn- interpret-panic [ast ctx]
(let [msg-value (interpret-ast (:data ast) ctx) (let [msg-value (interpret-ast (-> ast :data first) ctx)
msg-string (show/show msg-value)] msg-string (show/show msg-value)]
(throw (ex-info (str "Ludus panicked: " msg-string) {:ast ast})))) (throw (ex-info msg-string {:ast ast}))))
(defn interpret-ast [ast ctx] (defn interpret-ast [ast ctx]
(case (:type ast) (case (:type ast)
(:nil :true :false :number :string :keyword) (interpret-literal ast) (:nil :true :false :number :string :keyword) (interpret-literal ast)
:panic! (interpret-panic ast ctx) :panic (interpret-panic ast ctx)
:let-expr (interpret-let ast ctx) :let-expr (interpret-let ast ctx)
@ -893,7 +893,7 @@
; :struct-literal ; :struct-literal
; (interpret-struct ast ctx) ; (interpret-struct ast ctx)
(throw (ex-info (str "Unknown AST node type " (get ast :type :err) " on line " (get-in ast [:token :line])) {:ast ast})))) (throw (ex-info (str "Unknown AST node type " (get ast :type :none) " on line " (get-in ast [:token :line])) {:ast ast}))))
(defn get-line [source line] (defn get-line [source line]
(if line (if line
@ -984,24 +984,35 @@
:message (ex-message e)} :message (ex-message e)}
)))) ))))
; No prelude; helps when there are errors in the prelude
(defn interpret-bare [source parsed ctx]
(let [base-ctx (volatile! {::parent (volatile! ctx)})]
(try
(interpret-ast parsed base-ctx)
(catch Throwable e
{::data/error true
:line (get-in (ex-data e) [:ast :token :line])
:message (ex-message e)}))))
;; repl ;; repl
(comment (do
(println "***********") (println "***********")
(def source " (def source "
let times = 1.3 panic! (:oh, :no)
repeat times { print! (:foo) }
") ")
(def tokens (-> source scanner/scan :tokens)) (def tokens (-> source scanner/scan :tokens))
(def ast (p/apply-parser g/script tokens)) (def ast (p/apply-parser g/script tokens))
(def result (interpret-safe source ast {})) (def result (interpret-bare source ast {}))
;(-> ast prettify-ast println) (println tokens)
(-> ast prettify-ast println)
(println result) (println result)