Skip to content

Instantly share code, notes, and snippets.

@fogus
Forked from odyssomay/gist:1209498
Created September 12, 2011 14:54
Show Gist options
  • Save fogus/1211467 to your computer and use it in GitHub Desktop.
Save fogus/1211467 to your computer and use it in GitHub Desktop.
trace forms
(ns trace
(:use clojure.pprint))
(def *ignore*
'#{def quote var try fn* monitor-enter monitor-exit})
(defmulti trace-special-form (fn [form] (first form)))
(defn- trace-bindings [bindings]
(vec (apply concat
(map (fn [[sym value]]
`[~sym (trace-forms ~value)]) (partition 2 bindings)))))
(defmethod trace-special-form 'let* [[_ bindings & body]]
`(let ~(trace-bindings bindings)
(trace-forms ~@body)))
(defmethod trace-special-form 'loop* [[_ bindings & body]]
`(loop ~(trace-bindings bindings)
(trace-forms ~@body)))
(defmethod trace-special-form :default [form]
:default)
(defn trace-form [form]
`(try
~(if (and (or (list? form)
(seq? form))
(> (count form) 0))
(if (*ignore* (first form))
form
(let [sform (trace-special-form form)]
(if (= sform :default)
(let [mform (macroexpand-1 form)]
(if (= form mform)
(map trace-form mform)
(trace-form mform)))
sform)))
form)
(catch Exception e#
(print "Form failed: ")
(pprint '~form)
(throw e#))))
(defmacro trace-forms [& body]
`(do
~@(map trace-form body)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment