2.3. Procedure Macros

A procedure macro represents a list of statements. Macros defined in macro procedures are exported to the surrounding scope so that macro procedures can be used to package up a set of macro definitions. This represents a special exception to the rule that macros respect scope.
Start C++ section to tut/examples/mac126.flx[1 /1 ]
     1: include "std";
     2: // procedure macro
     3: macro proc k ()
     4: {
     5:   val yy = xx * xx;
     6:   print xx; print " -> "; print yy; endl;
     7: }
     8: 
     9: {
    10:   val xx = 1;
    11:   k();
    12: };
    13: {
    14:   val xx = 2;
    15:   k();
    16: };
    17: 
    18: macro proc pkg (x)
    19: {
    20:   macro proc printxy (y) { print (x+y); endl; }
    21:   macro val doublex = x + x;
    22: }
    23: 
    24: pkg 10;
    25: printxy doublex; // prints 30
    26: 
    27: macro proc dump_s (sts:proc)
    28: {
    29:   print "Start dump_s"; endl;
    30:   sts;
    31:   print "End dump_s"; endl;
    32: }
    33: 
    34: macro proc dump_e (sts:fun)
    35: {
    36:   print "Start dump_e"; endl;
    37:   sts;
    38:   print "End dump_e"; endl;
    39: }
    40: 
    41: dump_s { print 1; endl; print 2; endl; };
    42: dump_e { val x = 1; print x; endl; print 2; endl; };
    43: 
    44: macro proc todump () { print "Dumped"; endl; }
    45: dump_s todump;
    46: dump_e todump;
    47: 
    48: macro proc todump_wa (x) { print x; endl; }
    49: dump_s (todump_wa 66);
    50: dump_e (todump_wa 66);
    51: 
End C++ section to tut/examples/mac126.flx[1]
Note there is a crucial difference between procedure and expressions macros. In the above example, dump requires a proc argument. When the parameter is expanded, the list of statements it represented is produced. Had this been a function macro, the result would have been an the call executable lambda expression applied to unit argument. The difference is in the handling of any variable declarations: they'd be exposed in the first case, but not the second.