From b249817aa917e5fe2541c2dc9b0d77b0bdc910e9 Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Wed, 1 Jun 2022 17:28:55 -0400 Subject: [PATCH] Fix print-function stack overflow --- src/ludus/prelude.clj | 10 ++++++---- src/ludus/show.clj | 5 ++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/ludus/prelude.clj b/src/ludus/prelude.clj index 65ed14a..94a632d 100644 --- a/src/ludus/prelude.clj +++ b/src/ludus/prelude.clj @@ -1,7 +1,7 @@ (ns ludus.prelude (:require [ludus.data :as data] - [ludus.show])) + [ludus.show :as show])) ;; TODO: make eq, and, or special forms that short-circuit ;; Right now, they evaluate all their args @@ -49,12 +49,15 @@ (def panic! {:name "panic!" ::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" ::data/type ::data/clj :body (fn [& args] - (println (apply str args)) + (println (apply str (into [] (map print-show) args))) :ok)}) (def deref- {:name "deref" @@ -81,7 +84,6 @@ (def prelude {"eq" eq "add" add - ;;"panic!" panic! "print" print- "sub" sub "mult" mult diff --git a/src/ludus/show.clj b/src/ludus/show.clj index 6e4ac01..aa0ee4f 100644 --- a/src/ludus/show.clj +++ b/src/ludus/show.clj @@ -36,7 +36,8 @@ (defn- show-set [v] (str "${" (apply str (into [] show-linear v)) "}")) -(defn show [v] +(defn show + ([v] (cond (string? v) (str "\"" v "\"") (number? v) (str v) @@ -47,6 +48,8 @@ (set? v) (show-set v) (map? v) (show-map 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 ", ")))