Macros

spork-lang 0.6.0

Define macros with quasiquoting, unquoting, and hygienic generated symbols.

Macros run during compilation, receive unevaluated Spork forms, and return forms for the compiler to continue processing. This page covers declarations, template construction, hygiene helpers, and expansion inspection. The fixed syntax and expansion rules for quote, quasiquote, ~, and ~@ are defined in the reader macro reference.

Defining Macros#

(defmacro unless [test & body]
  `(if ~test nil (do ~@body)))

Using quasiquote in macros#

Reader macros provide the template operations; macro code combines them to return a complete form:

; ` creates a template
; ~ inserts one evaluated value
; ~@ inserts each value from a sequence into the surrounding form

(defmacro debug [expr]
  `(let [val# ~expr]
     (print '~expr "=" val#)
     val#))

Auto-gensym#

Inside a quasiquoted template, appending # to a symbol creates a unique generated symbol. Repeated uses of the same suffixed name within that template resolve to the same generated symbol, preventing accidental capture of a caller's bindings:

(defmacro swap! [a b]
  `(let [tmp# ~a]
     (set! ~a ~b)
     (set! ~b tmp#)))