Skip to content

Instantly share code, notes, and snippets.

@AdamClements
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdamClements/e89cfe659be08abf2710 to your computer and use it in GitHub Desktop.
Save AdamClements/e89cfe659be08abf2710 to your computer and use it in GitHub Desktop.
Currently gets any top level forms which have metadata and wraps them with something which prints the locals, the form and the result of evaluation every time it's called. Issues include not coping with recur/throw where the tail of the function is no longer correct, and it doesn't show you for example the results of evaluating inside vectors or…
(defmacro locals [] (zipmap (map #(list 'quote %) (keys &env)) (keys &env)))
(defn remove-autogenerated-locals
"Remove any locals generated by macros (heuristic - named something
with underscores in it). When we're doing something other than
println with the data, these could be left in but it's a bit noisy
at the moment"
[coll]
(into {} (remove (fn [[k v]] (re-find #"[_]" (name k))) coll)))
(defn eval-printing-meta [location form subform]
(let [local-things `(locals)]
`(let [result# ~subform]
(binding [*print-length* 10]
(clojure.pprint/pprint {:result result# :location ~location :form (quote ~form) :locals (remove-autogenerated-locals ~local-things)}))
result#)))
(defn wrap-things-with-meta [form]
(if (meta form)
(eval-printing-meta (meta form) form (map wrap-things-with-meta form))
form))
(defmacro lexically-trace [body]
(list 'do
(list 'println "Entering lexically-trace at " (meta &form))
(wrap-things-with-meta body)))
;;; Currently doesn't work due to the recur no longer being in tail position:
(comment
(defn test-recursion [x y]
(lexically-trace (if (= y 0) [x y] (recur (inc x) (dec y))))))
(defn test-nested [a]
(lexically-trace
(+ a (- (let [x 10] (/ 4 x a)) (let [a 2] (* a 7))))))
(defn test-ranges [max]
(lexically-trace (+ max (first (range)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment