Fix print-function stack overflow

This commit is contained in:
Scott Richmond 2022-06-01 17:28:55 -04:00
parent 46581e103d
commit b249817aa9
2 changed files with 10 additions and 5 deletions

View File

@ -1,7 +1,7 @@
(ns ludus.prelude (ns ludus.prelude
(:require (:require
[ludus.data :as data] [ludus.data :as data]
[ludus.show])) [ludus.show :as show]))
;; TODO: make eq, and, or special forms that short-circuit ;; TODO: make eq, and, or special forms that short-circuit
;; Right now, they evaluate all their args ;; Right now, they evaluate all their args
@ -49,12 +49,15 @@
(def panic! {:name "panic!" (def panic! {:name "panic!"
::data/type ::data/clj ::data/type ::data/clj
:body (fn [& args] (throw (ex-info (apply str (interpose " " args)) {})))}) :body (fn [& args] (throw (ex-info (apply show/show (interpose " " args)) {})))})
(defn- print-show [lvalue]
(if (string? lvalue) lvalue (show/show lvalue)))
(def print- {:name "print" (def print- {:name "print"
::data/type ::data/clj ::data/type ::data/clj
:body (fn [& args] :body (fn [& args]
(println (apply str args)) (println (apply str (into [] (map print-show) args)))
:ok)}) :ok)})
(def deref- {:name "deref" (def deref- {:name "deref"
@ -81,7 +84,6 @@
(def prelude {"eq" eq (def prelude {"eq" eq
"add" add "add" add
;;"panic!" panic!
"print" print- "print" print-
"sub" sub "sub" sub
"mult" mult "mult" mult

View File

@ -36,7 +36,8 @@
(defn- show-set [v] (defn- show-set [v]
(str "${" (apply str (into [] show-linear v)) "}")) (str "${" (apply str (into [] show-linear v)) "}"))
(defn show [v] (defn show
([v]
(cond (cond
(string? v) (str "\"" v "\"") (string? v) (str "\"" v "\"")
(number? v) (str v) (number? v) (str v)
@ -47,6 +48,8 @@
(set? v) (show-set v) (set? v) (show-set v)
(map? v) (show-map v) (map? v) (show-map v)
:else (with-out-str (pp/pprint v)))) :else (with-out-str (pp/pprint v))))
([v & vs] (apply str (into [] (comp (map show) (interpose " ")) (concat [v] vs))))
)
(def show-linear (comp (map show) (interpose ", "))) (def show-linear (comp (map show) (interpose ", ")))