-
-
Save fogus/1211467 to your computer and use it in GitHub Desktop.
trace forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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