2.5. Advanced Procedure Macro Programming

Felix procedure macros also contain rudimentary control structures: you can encode labels, unconditional jumps, conditional jumps, and returns. Note the conditional goto requires the condition resolve to a constant expression. Of course, the value can change with time, since it can depend on macro variables.

This example also illustrates the macro block, which is an anonymous procedure, called after definition. The effect is similar to the statements in the block, except that locally defined symbols are not exported. The construction is regularly required by the syntax extension facility, to prevent name conflicts when the same construction is expanded more than once, particularly cases where a macro expansion has as its argument another expansion of itself.

Start felix section to tut/examples/mac126a.flx[1 /1 ]
     1: #line 485 "./lpsrc/flx_tut_macro.pak"
     2: #import <flx.flxh>
     3: macro proc print_alot ()
     4: {
     5:   macro var count = 10;
     6:   macro start:>
     7:     print count; print " ";
     8:     macro count = count - 1;
     9:     macro if count > 0 goto start;
    10:   endl;
    11: }
    12: 
    13: print_alot();
    14: 
    15: macro {
    16:   macro var count = 10;
    17:   macro start:>
    18:     print count; print " ";
    19:     macro count = count - 1;
    20:     macro if count > 0 goto start;
    21:   endl;
    22: };
    23: 
    24: // note the 'start' macro label is local so there
    25: // is no clash repeating a block.
    26: 
    27: macro {
    28:   macro var count = 10;
    29:   macro start:>
    30:     print count; print " ";
    31:     macro count = count - 1;
    32:     macro if count > 0 goto start;
    33:   endl;
    34: };
    35: 
End felix section to tut/examples/mac126a.flx[1]