[nem-en] Hygienic macros and lexical scoping
Kamil Skalski
nazgul at omega.pl
Fri Apr 2 13:41:16 CEST 2004
Hello!
This week we have implemented property of hygiene and lexical scoping of
global symbols in macros and quotations. Full description of these features
is (or will be) in http://nemerle.org/metaprogramming.pdf paper and
http://nemerle.org/macros.html tutorial.
In short:
- hygiene
Symbols introduced by macro expansion does not conflict with symbols from
other macro expansions and top level code. For example with macro definition
macro m(ex) <[ def f () { printf ("x") }; $ex; f() ]>
code
def f () {printf ("y") }; m(f());
would be transformed to
def f () { printf ("y") }; def f_34 () { printf ("x") }; f (); f_34 ()
Thus symbol "f" introduced by macro is "renamed" to avoid capture of other
symbols.
- lexical scoping of global symbols
Global symbols used in macros (quotations) are searched in namespaces
imported in place where they are defined, not in place where macro is
expanded. For example
using System.RegularExpressions;
macro m () { <[ Regex ("a+") ]> }
used in code, where System.RegularExpressions are not imported, like
def x = m ();
expands to full name visible at macro definition site
def x = System.RegularExpressions.Regex ("a+");
This stuff is still under testing, but all features seems to work very good.
Kamil Skalski
More information about the devel-en
mailing list