Hygenic Macro System David Moone
Hygenic Macro System David Moone
David A. Moon
International Lisp Conference
March 23, 2009
Presented at the International Lisp Conference 2009
© Association of Lisp Users
defclass point
x is number
y is number
Types:
literal conditional
quotation definition
name block-expression
invocation assignment etc.
David A. Moon International Lisp Conference, March 23, 2009 11
Genuine, Full-Power, Hygienic, Easy to Use Macros
Macros can:
communicate with each other
operate on already-parsed code
be aware of scopes and definitions
do file I/O
David A. Moon International Lisp Conference, March 23, 2009 12
Genuine, Full-Power, Hygienic, Easy to Use Macros
g(a, a) definition
defmacro if
{ ?test [then] ?then is block & elseif }+
[ else ?else is block ] => ...
print x + y * z
=>
write(stdout, x + y * z)
David A. Moon International Lisp Conference, March 23, 2009 25
Example Macro: if
;; A simplified version of if
defmacro if ?test ?then [ else ?else ] =>
conditional(test, then, else or quotation(false))
defmacro defmacro
?:name { ^ ?:pattern \=> ?:block }+ =>
def msg = sequence-to-string(
collect-pattern-starts(pattern), ", ", " or ")
def err = `wrong-token-error(?=tokens, ?msg)`
def expander = reduce-right(
translate-pattern(`?=tokens`, _, _, _),
err, pattern, block)
continued on next slide
David A. Moon International Lisp Conference, March 23, 2009 29
Example Macro: defmacro (pg 2)
`def ?name = macro([name: ?name,
?=tokens is token-stream,
?=previous-context =>
def ?=macro-context = unique-macro-context()
def ?=source-file, ?=source-line =
source-location(?=tokens)
?expander ])`
parse-expression(token-sequence-stream(
`do
def fcn = ?fcn
{ def ?temps = ?args & ^ }*
def results = values-list(fcn( { ?temps &, }* ))
write(*trace-output*,
“ “ + fcn + “(“ { + ?temps }* +
“) = “ + results + “\n”)
values(results...)`),
false, true)
David A. Moon International Lisp Conference, March 23, 2009 38
For More Information
http://users.rcn.com/david-moon/PLOT/index.html