Message-ID: <1752185000.300202.1369036550170.JavaMail.email@example.com> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_300201_886594498.1369036550170" ------=_Part_300201_886594498.1369036550170 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
by Toby Miller
Syntactic macros allow new language constructs to be created.=C2=A0 See = Macros for a list of existing ones in b= oo.
We could, for example, mimic VisualBasic's with statement.
Given the following code:
If we define a 'with' macro we could rewrite it like this:
In boo, macros are CLI objects that implement the Boo.Lang.Compiler.IAst= Macro interface. It is interesting to note that there is nothing magic<= /em> about these objects. They must simply implement the interface the comp= iler expects. This implies that boo macros can be written in any CLI langua= ge!
When an unknown syntactic structure is encountered at compile time, like=
with statement above, the compiler will look for the corr=
ect IAstMacro class, create an instance, and ask that instance to expand th=
e macro. The compiler identifies the class to use via a simple naming conve=
ntion. The class name must start with the name of the macro and end with 'M=
acro'. Additionally the Pascal case naming convention must be used. So in this case the name must be 'WithMac=
Here's the code to implement our macro:
Some explanation is in order. The parsing stage of the compiler pipeline= parses a source stream into an abstract syntax tree (AST). A subtree, corr= esponding to the macro, will be passed to the Expand() method. Expand() is = responsible for building an AST that will replace the provided subtree.
The subtree corresponding to a macro statement is embodied by the MacroS= tatement parameter.
A MacroStatement has a collection of arguments and a block.
In this case we expect a single argument: a reference to an object. We t= hen traverse the block looking for references whose name begins with the '_= ' character. Whenever we encounter one, we replace it with a reference to a= member.
There are two classes related specifically to AST traversal: DepthFirstV= isitor and DepthFirstTransformer. Both classes walk an AST invoking appropr= iate methods for each type of element in the tree. In this case, we subclas= sed DepthFirstTransformer as a convenient way to find and replace Reference= Expression nodes in the macro's block.
You can find this plus other macro examples in the examples/macros directory.
A custom macro syntax is also planned.
Some other examples of macros already implemented in boo:------=_Part_300201_886594498.1369036550170--