3 ms·
Colloquially: You can rewrite source code into a different form as it's being evaluated, then have it be further evaluated as if it were originally written in t
by davidrupp 5y ago
Colloquially: You can rewrite source code into a different form as it's being evaluated, then have it be further evaluated as if it were originally written in the new form.
One fairly simple example is Clojure's `when`, which is an expression that starts off looking like
(when (= 0 1) (+ 1 2))
and gets re-written to
(if (= 0 1) (do (+ 1 2)))
before being further compiled. This rewriting happens as many times as necessary until an expression "bottoms out" and can be evaluated in terms of only built-ins.
It's instructive to see how much of e.g., Clojure is implemented as macros. https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj#L495 https://github.com/clojure/clojure/blob/master/src/clj/cloju... shows pretty clearly the correspondence between the original form and the rewritten form in the definition of `when`. `defn`, which is so foundational to idiomatic Clojure that it's easy to mistake it for a built-in, is a much more complicated example.