Every time I stick a println into some Clojure code to debug it, I
think to myself, "This is Lisp! I should be able to insert a repl
here!"

The problem is of course that Clojure's eval function doesn't know
about the surrounding lexical scope. So I started asking myself, what
is the simplest change I could make to Clojure to support an eval that
understands that scope? Then I tried to implement it.

Basically, here's what I came up with.

1. Modify the Clojure compiler so that when a flag is turned on, it
stores references to the lexical scope in a dynamic var. Thus, each
time the compiler creates a new lexical scope, it also emits the byte
code to push a hash-map with the details onto the var. When that scope
ends, the byte code for popping the hash-map off the var is emitted.

2. Then in Clojure proper, add a special version of eval that uses
that var. It wraps the form being eval'ed in a "let" that emulates the
original lexical scope, something like this:

        `(eval
            (let [~@(make-let-bindings
                     (:lexical-frames (var-get (resolve context))))]
              ~form))

With those two pieces, it's straight-forward creating a "debug-repl"
that understands the surrounding lexical scope.

I'm pretty pleased with the results and wanted to show them off.  More
details here:
http://georgejahad.com/clojure/debug-repl.html

Thanks to the my coworkers, the "Sonian-Clojure Brain Trust", for
their support and encouragement!

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to