[OT] What should be in a programming language?

Yigal Chripun yigal100 at gmail.com
Mon Oct 26 14:54:05 PDT 2009


On 26/10/2009 20:30, Jason House wrote:
>
> Your examples in Nemerle or D-ish looked like they are returning
> strings. I'm still not seeing the magic of AST macros.

When we want to decompose some large code (or more precisely, its syntax
tree), we must bind its smaller parts to variables. Then we can process
them recursively or just use them in an arbitrary way to construct the
result.

We can operate on entire subexpressions by writing $( ... ) or $ID
inside the quotation operator <[ ... ]>. This means binding the value of
ID or the interior of parenthesized expression to the part of syntax
tree described by corresponding quotation.

macro for (init, cond, change, body)
{
   <[
     $init;
     def loop () : void {
       if ($cond) { $body; $change; loop() }
       else ()
     };
     loop ()
   ]>
}


he above macro defines function for, which is similar to the loop known
from C. It can be used like this

for (mutable i = 0, i < 10, i++, printf ("%d", i))
/quote

the above is taken from the macros_tutorial page of nemerle.org.
unfortunately the site is down so I'm using Google's cache instead.

there are a few more related topics: Constructs with variable number of
elements, hygiene, ...





More information about the Digitalmars-d mailing list