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 felix section to tut/macros/mac-2.03-0.flx[1 /1 ]
     1: #line 481 "./lpsrc/flx_tut_macro.pak"
     2: #import <flx.flxh>
     3: // procedure macro
     4: macro proc k ()
     5: {
     6:   val yy = xx * xx;
     7:   print xx; print " -> "; print yy; endl;
     8: }
     9: 
    10: {
    11:   val xx = 1;
    12:   k();
    13: };
    14: {
    15:   val xx = 2;
    16:   k();
    17: };
    18: 
    19: macro proc pkg (x)
    20: {
    21:   macro proc printxy (y) { print (x+y); endl; }
    22:   macro val doublex = x + x;
    23: }
    24: 
    25: pkg 10;
    26: printxy doublex; // prints 30
    27: 
    28: macro proc dump_s (sts:proc)
    29: {
    30:   print "Start dump_s"; endl;
    31:   sts;
    32:   print "End dump_s"; endl;
    33: }
    34: 
    35: macro proc dump_e (sts:fun)
    36: {
    37:   print "Start dump_e"; endl;
    38:   sts;
    39:   print "End dump_e"; endl;
    40: }
    41: 
    42: dump_s { print 1; endl; print 2; endl; };
    43: dump_e { val x = 1; print x; endl; print 2; endl; };
    44: 
    45: macro proc todump () { print "Dumped"; endl; }
    46: dump_s todump;
    47: dump_e todump;
    48: 
    49: macro proc todump_wa (x) { print x; endl; }
    50: dump_s (todump_wa 66);
    51: dump_e (todump_wa 66);
End felix section to tut/macros/mac-2.03-0.flx[1]
Start data section to tut/macros/mac-2.03-0.expect[1 /1 ]
     1: 1 -> 1
     2: 2 -> 4
     3: 30
     4: Start dump_s
     5: 1
     6: 2
     7: End dump_s
     8: Start dump_e
     9: 1
    10: 2
    11: End dump_e
    12: Start dump_s
    13: Dumped
    14: End dump_s
    15: Start dump_e
    16: Dumped
    17: End dump_e
    18: Start dump_s
    19: 66
    20: End dump_s
    21: Start dump_e
    22: 66
    23: End dump_e
End data section to tut/macros/mac-2.03-0.expect[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.