[nem-en] Mimicing VisualBasic's with statement using macros.

Kamil Skalski kamil.skalski at gmail.com
Tue Feb 14 18:12:48 CET 2006


More complex macro require some usage of compiler internals (for
example to list all methods from given class, etc.), but this With
macro is just a simple syntactic sugar.

It can be achieved by something like:

macro withMacro (obj, body)
syntax ("with", "(", obj, ")", body) {
  match (body) {
     | <[ {.. $bodyElements } ]> =>
          def transformed = bodyElements.Map (fun (x) {
              $obj . $x
          });
          <[ { .. $transformed } ]>

     | _ => Message.FatalError ("expected { } in with macro");
  }
}

This will give you ability to write:

def myObj = MyClass ();
with (myObj) {
    DoFoo ();
    DoBar ();
    AndEvenDoMoreThings ();
}

and get it transformed into:
def myObj = MyClass ();
with (myObj) {
   myObj.DoFoo ();
   myObj.DoBar ();
   myObj.AndEvenDoMoreThings ();
}

Of course you can do some more interesiting processing of { }
elements, like transforming
  x = 5;

into

 myObj.x = 5;

(I don't remember how exactly Visual Basic have it done)

2006/2/13, Kanru Chen <ckanru at gmail.com>:
> Hi,
>
> Is it possible? Like the boo guys has been done in this page:
> http://boo.codehaus.org/Syntactic+Macros
>
> I found that using macros is harder than I first thought because
> it's relating to compiler internals.
> --
>     ~    Kanru Chen <koster at debian.org.tw>
>    'v'   http://stu.csie.ncnu.edu.tw/~kanru.96/
>   // \\  GnuPG-Key ID: 365CC7A2
>  /(   )\ Fingerprint: 3278 DFB4 BB28 6E8C 9E1F  1ECB B1B7 5B5F 365C C7A2
>   ^`~'^
>
> _______________________________________________
> https://nemerle.org/mailman/listinfo/devel-en
>


--
Kamil Skalski
http://nazgul.omega.pl


More information about the devel-en mailing list