Reference Developer Tutorial 1 Introduction 2 Macros 3 Typesetting 4 Example 5 Hints 6 Examples 7 Webmaking SEARCH

2.5 Parameterized Macros

No self-respecting macro preprocessor would be complete without some form of macro parameterization, and FunnelWeb is no exception. FunnelWeb allows each macro to have from zero to nine formal parameters named `@1`, `@2`, `@3`, `@4`, `@5`, `@6`, `@7`, `@8`, and `@9`.

To define a macro with one or more parameters, insert a formal parameter list just after the macro name in the macro definition. Because macro parameters have fixed names (`@1`...`@9`), there is no need to specify the names of formal parameters in the formal parameter list. All that need be conveyed is how many parameters the macro has. Here is an example of the definition of a macro having three parameters:

    @$@<While loop@>@(@3@)@M==@{@-
    @1
    while (@2)
      {
       @3
      }
    @}

To call a parameterized macro, an actual parameter list must be supplied that contains exactly the same number of actual parameters as there are formal parameters in the definition of the macro being called. An actual parameter list is delimited by `@(` and `@)`, and parameters are separated by `@,`. The actual parameters themselves are general FunnelWeb expressions (see the FunnelWeb Reference Manual for the exact syntax) and can be inserted into the list directly or can be delimited by `@"` so as to allow some white space to assist in formatting the actual parameters. Here are some examples of calls of the `While loop` macro defined above.

    @! First form of actual parameters
    @! without whitespace and double quotes.
    @<While loop@>@(@-
    x=1;@,x<=10@,printf("X=%u\n",x);@)

    @! Second form of actual parameters. The double
    @! quotes allow non-active whitespace that helps
    @! to lay out the actual parameters neatly. This
    @! call is functionally identical to the one above.
    @<While loop@>@(
       @"x:=1;@" @,
       @"x<=10@" @,
       @"printf("X=%u\n",x);@" @)

    @! The two forms can be mixed in a single call.
    @<While loop@>@(x=1;@,x<=10@,
                  @"printf("X=%u\n",x);@" @)

A few rules about parameterized macros are worth mentioning. Macros that do not have any parameters must have no formal or actual parameter lists. Additive macros can have parameters, but the formal parameter list must appear in the first definition part only.

Here is another example of the use of parameterized macros. This time, parameters and macro calls are used in a FunnelWeb input file that constructs an O(n) representation of a song whose full size is O(n^2) in the number n of unique lines.

    @O@<Twelve_bugs.txt@>==@{@-
    The Twelve Bugs of Christmas
    ----------------------------
    @<Verse@>@(@"first@"    @,@<1@>@)
    @<Verse@>@(@"second@"   @,@<2@>@)
    @<Verse@>@(@"third@"    @,@<3@>@)
    @<Verse@>@(@"fourth@"   @,@<4@>@)
    @<Verse@>@(@"fifth@"    @,@<5@>@)
    @<Verse@>@(@"sixth@"    @,@<6@>@)
    @<Verse@>@(@"seventh@"  @,@<7@>@)
    @<Verse@>@(@"eighth@"   @,@<8@>@)
    @<Verse@>@(@"ninth@"    @,@<9@>@)
    @<Verse@>@(@"tenth@"    @,@<A@>@)
    @<Verse@>@(@"eleventh@" @,@<B@>@)
    @<Verse@>@(@"twelfth@"  @,@<C@>@)

    This song appeared in the internet newsgroup
    rec.humor.funny on 24-Dec-1991. It was contributed
    by Pat Scannell (scannell@@darkstar.ma30.bull.com).
    @}

    @$@<Verse@>@(@2@)@M==@{@-
    For the @1 bug of Christmas, my manager said to me
         @2
    @}

    @$@<1@>@M==@{@-
    See if they can do it again.@}
    @$@<2@>@M==@{@-
    Ask them how they did it and@+@<1@>@}
    @$@<3@>@M==@{@-
    Try to reproduce it@+@<2@>@}
    @$@<4@>@M==@{@-
    Run with the debugger@+@<3@>@}
    @$@<5@>@M==@{@-
    Ask for a dump@+@<4@>@}
    @$@<6@>@M==@{@-
    Reinstall the software@+@<5@>@}
    @$@<7@>@M==@{@-
    Say they need an upgrade@+@<6@>@}
    @$@<8@>@M==@{@-
    Find a way around it@+@<7@>@}
    @$@<9@>@M==@{@-
    Blame it on the hardware@+@<8@>@}
    @$@<A@>@M==@{@-
    Change the documentation@+@<9@>@}
    @$@<B@>@M==@{@-
    Say it's not supported@+@<A@>@}
    @$@<C@>@M==@{@-
    Tell them it's a feature@+@<B@>@}

Webmaster Copyright © Ross N. Williams 1992,1999. All rights reserved.