From 53b71fe790e89e8d239389adf6d949f4c6d969c4 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Sun, 17 Dec 2023 23:13:50 -0500 Subject: [PATCH] Panic is now a form, not a function. --- src/ludus/base.cljc | 13 ++++++------- src/ludus/grammar.cljc | 5 ++++- src/ludus/interpreter.cljc | 7 +++++++ src/ludus/node.cljc | 11 +++++++++-- src/ludus/prelude.ld | 24 +++++------------------- src/ludus/scanner.cljc | 3 +-- target/repl-port | 2 +- 7 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/ludus/base.cljc b/src/ludus/base.cljc index 0c5fa64..dbe7021 100644 --- a/src/ludus/base.cljc +++ b/src/ludus/base.cljc @@ -73,12 +73,11 @@ (defn- stringify-args [arglist] (apply str (interpose " " (into [] (map print-show) (rest arglist))))) - -(def panic! {:name "panic!" - ::data/type ::data/clj - :body (fn panic-inner - ([] (panic-inner [::data/list])) - ([args] (throw (ex-info (stringify-args args) {}))))}) +; (def panic! {:name "panic!" +; ::data/type ::data/clj +; :body (fn panic-inner +; ([] (panic-inner [::data/list])) +; ([args] (throw (ex-info (stringify-args args) {}))))}) (def print- {:name "print" ::data/type ::data/clj @@ -410,7 +409,7 @@ :to_vec to_vec :fold fold :map map - :panic! panic! + ; :panic! panic! :prn prn- :concat concat- :str str- diff --git a/src/ludus/grammar.cljc b/src/ludus/grammar.cljc index 2290430..47e0978 100644 --- a/src/ludus/grammar.cljc +++ b/src/ludus/grammar.cljc @@ -234,7 +234,9 @@ (defp collection flat choice [;struct-literal 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]) @@ -268,3 +270,4 @@ (defp script order-0 [nls? (one+ script-line) (quiet :eof)]) + diff --git a/src/ludus/interpreter.cljc b/src/ludus/interpreter.cljc index 6b8b470..5f7c069 100644 --- a/src/ludus/interpreter.cljc +++ b/src/ludus/interpreter.cljc @@ -817,11 +817,18 @@ (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] (case (:type ast) (:nil :true :false :number :string :keyword) (interpret-literal ast) + :panic! (interpret-panic ast ctx) + :let-expr (interpret-let ast ctx) :if-expr (interpret-if ast ctx) diff --git a/src/ludus/node.cljc b/src/ludus/node.cljc index a1708b3..33dbb91 100644 --- a/src/ludus/node.cljc +++ b/src/ludus/node.cljc @@ -33,9 +33,7 @@ (defn run [source] (let [user_scanned (s/scan source) user_tokens (:tokens user_scanned) - _ (println "Tokens: " 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 {}) result_str (show/show user_result) post_scanned (s/scan pre/postlude) @@ -60,3 +58,12 @@ ) )) +(do + + (def source "panic! :oops") + + (def tokens (s/scan source)) + + ) + + diff --git a/src/ludus/prelude.ld b/src/ludus/prelude.ld index e3e62f9..ff18830 100644 --- a/src/ludus/prelude.ld +++ b/src/ludus/prelude.ld @@ -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! { "Prints the documentation of a function to the console." (f as :fn) -> do f > base :doc > print! @@ -396,7 +383,7 @@ fn mult { fn div { "Divides numbers. Panics on division by zero." (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, y, ...zs) -> { let divisor = fold (mult, zs, y) @@ -898,8 +885,8 @@ fn err? { 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." ((:ok, value)) -> value - ((:err, msg)) -> panic! ("Unwrapped :err", msg) - (_) -> panic! ("Cannot unwrap something that's not an error tuple.") + ((:err, msg)) -> panic! string ("Unwrapped :err! ", msg) + (_) -> panic! "Cannot unwrap something that's not an error tuple." } fn unwrap_or { @@ -910,10 +897,10 @@ fn unwrap_or { fn assert! { "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 then value - else panic! ("Assert failed:", message, value) + else panic! string ("Assert failed:", message, value) } &&& Turtle & other graphics @@ -1217,7 +1204,6 @@ ns prelude { show prn! report! - panic! doc! concat ref? diff --git a/src/ludus/scanner.cljc b/src/ludus/scanner.cljc index b0a9fde..bc140df 100644 --- a/src/ludus/scanner.cljc +++ b/src/ludus/scanner.cljc @@ -20,7 +20,7 @@ "match" :match ;; impl "nil" :nil ;; impl -> literal word "ns" :ns ;; impl - ;; "panic!" :panic ;; impl (should be a function) + "panic!" :panic ;; impl (should _not_ be a function) "recur" :recur ;; impl "ref" :ref ;; impl "then" :then ;; impl @@ -329,4 +329,3 @@ {:tokens (:tokens scanner) :errors (:errors scanner)}) (recur (-> scanner (scan-token) (next-token)))))) - diff --git a/target/repl-port b/target/repl-port index 8dbb69f..88c8772 100644 --- a/target/repl-port +++ b/target/repl-port @@ -1 +1 @@ -51500 \ No newline at end of file +51828 \ No newline at end of file