(no title)
Pauan | 10 years ago
Nulan's macro system is very similar to Common Lisp or Arc's macro system, in the sense that a macro is simply a compile-time function that accepts code and returns code.
That means you can use all of the standard stuff within a macro: loops, filters, maps, etc.
Nulan achieves hygiene with two different techniques:
1) Gensyms. This is a standard technique in Common Lisp and Arc. But it only works for locally-defined variables.
2) All global variables are replaced with gensyms, and when a macro expands to a global variable, Nulan will automatically replace it with the appropriate gensym.
This is similar to Common Lisp's package system.
----
Here is an example of a Nulan macro:
(REWRITE-RULE
| (AND ~a ~b)
&(IF ~a
| ~b
| false)
| (AND ~a ~@b)
&(AND ~a (AND ~@b)))
This says that:(AND 1 2) should be replaced with (IF 1 2 false)
(AND 1 2 3) should be replaced with (IF 1 (IF 2 3 false) false)
(AND 1 2 3 4) should be replaced with (IF 1 (IF 2 (IF 3 4 false) false) false)
----
The & syntax is similar to ` in other Lisps except that it replaces global variables with gensyms.
The ~ syntax is similar to , in other Lisps.
The ~@ syntax is similar to ,@ in other Lisps.
----
Here is an alternate way of writing the AND macro:
(REWRITE-RULE
| (AND ~a ~b)
&(IF ~a
| ~b
| false)
| (AND ~@a ~b)
(reduce-right b a -> b a
&(AND ~a ~b)))
The reduce-right function is the same as foldr in Haskell, or fold-right in Scheme.This demonstrates that we can run arbitrary code at compile-time.
----
Although Nulan macros are hygienic by default, you can intentionally break hygiene when you need to, by inserting symbols:
(REWRITE-RULE
| (AIF ~a
| ~b
| ~c)
&(LOCAL
| ~(SYMBOL "it") <= ~a
\ (IF ~(SYMBOL "it")
| ~b
| ~c)))
The above macro is the same as Arc's aif macro. You use it like this: (AIF foo
| (bar it)
| (qux it))
Essentially, it is identical to IF, except that it binds "foo" to the local variable "it", which can then be used inside the body of the AIF.In general, Nulan macros are just as convenient and powerful as Common Lisp or Arc macros.
----
For a list of some of the changes, please see this post:
No comments yet.