improve errors, a bit

This commit is contained in:
Scott Richmond 2024-06-14 17:17:23 -04:00
parent 1841915d92
commit aa5795e168
2 changed files with 39 additions and 8 deletions

View File

@ -78,27 +78,51 @@
(def {:line line-num :source source :input input :start start} (get-in e [:node :token])) (def {:line line-num :source source :input input :start start} (get-in e [:node :token]))
(def source-line (get-line source line-num)) (def source-line (get-line source line-num))
(print " on line " line-num " in " input ", ") (print " on line " line-num " in " input ", ")
(print " binding " (b/show (e :value))) (print " matching: " (b/show (e :value)))
(def pattern (get-in e [:node :data 0])) (def pattern (get-in e [:node :data 0]))
(print " to " (b/show-patt pattern)) (print " with pattern: " (b/show-patt pattern))
(print " >>> " source-line)
(print (caret source line-num start))
e)
(defn- match-no-match [e]
(print "Ludus panicked! no match")
(def {:line line-num :source source :input input :start start} (get-in e [:node :token]))
(print " on line " line-num " in " input ", ")
(def value (e :value))
(print " matching: " (b/show value))
(print " with patterns:")
(def clauses (get-in e [:node :data 1]))
(def patterns (b/pretty-patterns {:body clauses}))
(def fmt-patt (do
(def lines (string/split "\n" patterns))
(def indented (map (fn [x] (string " " x)) lines))
(string/join indented "\n")
))
(print fmt-patt)
(def source-line (get-line source line-num))
(print " >>> " source-line) (print " >>> " source-line)
(print (caret source line-num start)) (print (caret source line-num start))
e) e)
(defn- generic-panic [e] (defn- generic-panic [e]
(def msg (e :msg)) (def msg (e :msg))
(def {:line line-num :source source :input input} (get-in e [:node :token])) (def {:line line-num :source source :input input :start start} (get-in e [:node :token]))
(def source-line (get-line source line-num)) (def source-line (get-line source line-num))
(print "Ludus panicked! " msg) (print "Ludus panicked! " msg)
(print " on line " line-num " in " input ":") (print " on line " line-num " in " input ":")
(print " >>> " source-line)) (print " >>> " source-line)
(print (caret source line-num start))
e)
(defn- unbound-name [e] (defn- unbound-name [e]
(def {:line line-num :source source :lexeme name :input input} (get-in e [:node :token])) (def {:line line-num :source source :lexeme name :input input :start start} (get-in e [:node :token]))
(def source-line (get-line source line-num)) (def source-line (get-line source line-num))
(print "Ludus panicked! unbound name " name) (print "Ludus panicked! unbound name " name)
(print " on line " line-num " in " input ":") (print " on line " line-num " in " input ":")
(print " >>> " source-line)) (print " >>> " source-line)
(print (caret source line-num start))
e)
(defn runtime-error [e] (defn runtime-error [e]
(when (= :string (type e)) (when (= :string (type e))
@ -109,6 +133,8 @@
(case msg (case msg
"no match: function call" (fn-no-match e) "no match: function call" (fn-no-match e)
"no match: let binding" (let-no-match e) "no match: let binding" (let-no-match e)
"no match: match form" (match-no-match e)
"no match: when form" (generic-panic e)
"unbound name" (unbound-name e) "unbound name" (unbound-name e)
(generic-panic e)) (generic-panic e))
e) e)

View File

@ -52,10 +52,15 @@
# (comment # (comment
(do (do
(def source ` (def source `
box foo = nil match :foo with {
:bar -> :baz
:baz -> :quux
"thing {foo}" -> "worste"
42 -> 23
}
`) `)
(def out (-> source (def out (-> source
ludus ludus
j/decode j/decode
)) ))
(setdyn :out stdout) (setdyn :out stdout)