Panic is now a form, not a function.

This commit is contained in:
Scott Richmond 2023-12-17 23:13:50 -05:00
parent 6cf3fdd5f2
commit 53b71fe790
7 changed files with 33 additions and 32 deletions

View File

@ -73,12 +73,11 @@
(defn- stringify-args [arglist] (defn- stringify-args [arglist]
(apply str (interpose " " (into [] (map print-show) (rest arglist))))) (apply str (interpose " " (into [] (map print-show) (rest arglist)))))
; (def panic! {:name "panic!"
(def panic! {:name "panic!" ; ::data/type ::data/clj
::data/type ::data/clj ; :body (fn panic-inner
:body (fn panic-inner ; ([] (panic-inner [::data/list]))
([] (panic-inner [::data/list])) ; ([args] (throw (ex-info (stringify-args args) {}))))})
([args] (throw (ex-info (stringify-args args) {}))))})
(def print- {:name "print" (def print- {:name "print"
::data/type ::data/clj ::data/type ::data/clj
@ -410,7 +409,7 @@
:to_vec to_vec :to_vec to_vec
:fold fold :fold fold
:map map :map map
:panic! panic! ; :panic! panic!
:prn prn- :prn prn-
:concat concat- :concat concat-
:str str- :str str-

View File

@ -234,7 +234,9 @@
(defp collection flat choice [;struct-literal (defp collection flat choice [;struct-literal
dict list-literal set-literal tuple]) dict list-literal set-literal tuple])
(defp simple flat choice [literal collection synthetic recur-call lambda]) (defp panic group order-1 [(quiet :panic) expression])
(defp simple flat choice [literal collection synthetic recur-call lambda panic])
(defp compound flat choice [match loop-expr if-expr when-expr do-expr block repeat-expr]) (defp compound flat choice [match loop-expr if-expr when-expr do-expr block repeat-expr])
@ -268,3 +270,4 @@
(defp script order-0 [nls? (defp script order-0 [nls?
(one+ script-line) (one+ script-line)
(quiet :eof)]) (quiet :eof)])

View File

@ -817,11 +817,18 @@
(defn- interpret-literal [ast] (-> ast :data first)) (defn- interpret-literal [ast] (-> ast :data first))
(defn- interpret-panic [ast ctx]
(let [msg-value (interpret-ast (:data ast) ctx)
msg-string (show/show msg-value)]
(throw (ex-info (str "Ludus panicked: " 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)
:let-expr (interpret-let ast ctx) :let-expr (interpret-let ast ctx)
:if-expr (interpret-if ast ctx) :if-expr (interpret-if ast ctx)

View File

@ -33,9 +33,7 @@
(defn run [source] (defn run [source]
(let [user_scanned (s/scan source) (let [user_scanned (s/scan source)
user_tokens (:tokens user_scanned) user_tokens (:tokens user_scanned)
_ (println "Tokens: " user_tokens)
user_parsed (p/apply-parser g/script user_tokens) user_parsed (p/apply-parser g/script user_tokens)
_ (println "Ast: " (i/prettify-ast user_parsed))
user_result (i/interpret-safe source user_parsed {}) user_result (i/interpret-safe source user_parsed {})
result_str (show/show user_result) result_str (show/show user_result)
post_scanned (s/scan pre/postlude) post_scanned (s/scan pre/postlude)
@ -60,3 +58,12 @@
) )
)) ))
(do
(def source "panic! :oops")
(def tokens (s/scan source))
)

View File

@ -276,19 +276,6 @@ fn report! {
} }
} }
fn panic! {
"Causes Ludus to panic, outputting any arguments as messages."
() -> {
add_msg! ("Ludus panicked!")
base :panic! ()
}
(...args) -> {
add_msg! ("Ludus panicked!")
add_msg! (args)
base :panic! (args)
}
}
fn doc! { fn doc! {
"Prints the documentation of a function to the console." "Prints the documentation of a function to the console."
(f as :fn) -> do f > base :doc > print! (f as :fn) -> do f > base :doc > print!
@ -396,7 +383,7 @@ fn mult {
fn div { fn div {
"Divides numbers. Panics on division by zero." "Divides numbers. Panics on division by zero."
(x as :number) -> x (x as :number) -> x
(_, 0) -> panic! ("Division by zero.") (_, 0) -> panic! "Division by zero."
(x as :number, y as :number) -> base :div (x, y) (x as :number, y as :number) -> base :div (x, y)
(x, y, ...zs) -> { (x, y, ...zs) -> {
let divisor = fold (mult, zs, y) let divisor = fold (mult, zs, y)
@ -898,8 +885,8 @@ fn err? {
fn unwrap! { fn unwrap! {
"Takes a result tuple. If it's :ok, then returns the value. If it's not :ok, then it panics. If it's not a result tuple, it also panics." "Takes a result tuple. If it's :ok, then returns the value. If it's not :ok, then it panics. If it's not a result tuple, it also panics."
((:ok, value)) -> value ((:ok, value)) -> value
((:err, msg)) -> panic! ("Unwrapped :err", msg) ((:err, msg)) -> panic! string ("Unwrapped :err! ", msg)
(_) -> panic! ("Cannot unwrap something that's not an error tuple.") (_) -> panic! "Cannot unwrap something that's not an error tuple."
} }
fn unwrap_or { fn unwrap_or {
@ -910,10 +897,10 @@ fn unwrap_or {
fn assert! { fn assert! {
"Asserts a condition: returns the value if the value is truthy, panics if the value is falsy. Takes an optional message." "Asserts a condition: returns the value if the value is truthy, panics if the value is falsy. Takes an optional message."
(value) -> if value then value else panic! ("Assert failed", value) (value) -> if value then value else panic! string ("Assert failed", value)
(value, message) -> if value (value, message) -> if value
then value then value
else panic! ("Assert failed:", message, value) else panic! string ("Assert failed:", message, value)
} }
&&& Turtle & other graphics &&& Turtle & other graphics
@ -1217,7 +1204,6 @@ ns prelude {
show show
prn! prn!
report! report!
panic!
doc! doc!
concat concat
ref? ref?

View File

@ -20,7 +20,7 @@
"match" :match ;; impl "match" :match ;; impl
"nil" :nil ;; impl -> literal word "nil" :nil ;; impl -> literal word
"ns" :ns ;; impl "ns" :ns ;; impl
;; "panic!" :panic ;; impl (should be a function) "panic!" :panic ;; impl (should _not_ be a function)
"recur" :recur ;; impl "recur" :recur ;; impl
"ref" :ref ;; impl "ref" :ref ;; impl
"then" :then ;; impl "then" :then ;; impl
@ -329,4 +329,3 @@
{:tokens (:tokens scanner) {:tokens (:tokens scanner)
:errors (:errors scanner)}) :errors (:errors scanner)})
(recur (-> scanner (scan-token) (next-token)))))) (recur (-> scanner (scan-token) (next-token))))))

View File

@ -1 +1 @@
51500 51828