| 1 |
|
| 2 |
<HTML> |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
<HEAD> |
| 29 |
<TITLE>2.5 Parameterized Macros</TITLE> |
| 30 |
<STYLE TYPE="text/css"> <!-- A {text-decoration: none} // --> </STYLE> |
| 31 |
</HEAD> |
| 32 |
<BODY BACKGROUND="binary/background.gif" |
| 33 |
BGCOLOR="#FFFFFF" |
| 34 |
TEXT="#000000" |
| 35 |
VLINK="#660000" |
| 36 |
LINK="#FF0000" |
| 37 |
ALINK="#CC0000"> |
| 38 |
|
| 39 |
<TABLE WIDTH="490"> |
| 40 |
<TR> |
| 41 |
<TD WIDTH="130" VALIGN="top"> |
| 42 |
<IMG SRC="binary/d_clear.gif" ALT="" WIDTH="130" HEIGHT="1"><BR> |
| 43 |
|
| 44 |
<FONT SIZE="2"> |
| 45 |
<BR> |
| 46 |
|
| 47 |
<A HREF="http://www.ross.net/" |
| 48 |
TARGET="rosshome" |
| 49 |
onClick="window.open('','rosshome','location,status,menubar,scrollbars,resizable',false).focus(); return true;" |
| 50 |
> |
| 51 |
<IMG SRC="binary/rossnet_logo.gif" |
| 52 |
WIDTH="64" HEIGHT="32" |
| 53 |
BORDER="0" ALT="RossNet" |
| 54 |
HSPACE="0" VSPACE="1"></A><BR> |
| 55 |
<BR> |
| 56 |
|
| 57 |
<A HREF="../index.shtml" |
| 58 |
TARGET="funnelweb" |
| 59 |
onClick="window.open('','funnelweb','location,status,menubar,scrollbars,resizable',false).focus(); return true;" |
| 60 |
> |
| 61 |
<IMG SRC="binary/linklogo.gif" |
| 62 |
WIDTH="64" HEIGHT="32" |
| 63 |
BORDER="0" ALT="FunnelWeb" |
| 64 |
HSPACE="0" VSPACE="1"></A><BR> |
| 65 |
<BR> |
| 66 |
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0><TR><TD BGCOLOR="#000000"> |
| 67 |
<A HREF="../reference/index.html" |
| 68 |
TARGET="funnelwebreference" |
| 69 |
onClick="window.open('','funnelwebreference','location,status,menubar,scrollbars,resizable',false).focus(); return true;" |
| 70 |
><FONT COLOR="#FFFFFF"><B>Reference</B></FONT></A><BR> |
| 71 |
<BR> |
| 72 |
<A HREF="../developer/index.html" |
| 73 |
TARGET="funnelwebdeveloper" |
| 74 |
onClick="window.open('','funnelwebdeveloper','location,status,menubar,scrollbars,resizable',false).focus(); return true;" |
| 75 |
><FONT COLOR="#FFFFFF"><B>Developer</B></FONT></A><BR> |
| 76 |
<BR> |
| 77 |
<A HREF="index.html"><FONT COLOR="#FFFFFF"><B>Tutorial</B></FONT></A><BR> |
| 78 |
<A HREF="intro.html"><FONT COLOR="#FFFFFF">1 Introduction</FONT></A><BR> |
| 79 |
<A HREF="macro.html"><FONT COLOR="#FFFFFF">2 Macros</FONT></A><BR> |
| 80 |
<A HREF="type.html"><FONT COLOR="#FFFFFF">3 Typesetting</FONT></A><BR> |
| 81 |
<A HREF="example.html"><FONT COLOR="#FFFFFF">4 Example</FONT></A><BR> |
| 82 |
<A HREF="hints.html"><FONT COLOR="#FFFFFF">5 Hints</FONT></A><BR> |
| 83 |
<A HREF="examples.html"><FONT COLOR="#FFFFFF">6 Examples</FONT></A><BR> |
| 84 |
<A HREF="web.html"><FONT COLOR="#FFFFFF">7 Webmaking</FONT></A><BR> |
| 85 |
|
| 86 |
<BR> |
| 87 |
<A HREF="search.html"><FONT COLOR="#FFFFFF"><B>SEARCH</B></FONT></A><BR> |
| 88 |
</FONT> |
| 89 |
</TD></TR></TABLE> |
| 90 |
|
| 91 |
|
| 92 |
</TD> |
| 93 |
<TD WIDTH="360" VALIGN="top"> |
| 94 |
<FONT SIZE="3"> |
| 95 |
|
| 96 |
|
| 97 |
<A HREF="../reference/index.html"><IMG SRC="binary/title.gif" |
| 98 |
WIDTH="302" HEIGHT="24" |
| 99 |
BORDER="0" ALT="FunnelWeb Tutorial Manual" |
| 100 |
HSPACE="0" VSPACE="0"></A> |
| 101 |
<P><FONT SIZE="5">2.5 Parameterized Macros</FONT><BR> |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
<P>No self-respecting macro preprocessor would be complete |
| 106 |
without some form of macro parameterization, and FunnelWeb |
| 107 |
is no exception. FunnelWeb allows each macro to have from |
| 108 |
zero to nine formal parameters |
| 109 |
named <SAMP>@1</SAMP>, <SAMP>@2</SAMP>, |
| 110 |
<SAMP>@3</SAMP>, <SAMP>@4</SAMP>, <SAMP>@5</SAMP>, <SAMP>@6</SAMP>, |
| 111 |
<SAMP>@7</SAMP>, <SAMP>@8</SAMP>, and <SAMP>@9</SAMP>. |
| 112 |
|
| 113 |
<P>To define a macro with one or more parameters, insert a |
| 114 |
formal parameter list just after |
| 115 |
the macro name in the macro definition. Because macro |
| 116 |
parameters have fixed names |
| 117 |
(<SAMP>@1</SAMP>...<SAMP>@9</SAMP>), there is no need to |
| 118 |
specify the names of formal parameters in the formal |
| 119 |
parameter list. All that need be conveyed is how many |
| 120 |
parameters the macro has. Here is an example of the |
| 121 |
definition of a macro having three parameters: |
| 122 |
|
| 123 |
<P> |
| 124 |
<PRE> |
| 125 |
@$@<While loop@>@(@3@)@M==@{@- |
| 126 |
@1 |
| 127 |
while (@2) |
| 128 |
{ |
| 129 |
@3 |
| 130 |
} |
| 131 |
@} |
| 132 |
</PRE> |
| 133 |
|
| 134 |
<P>To call a parameterized macro, an actual |
| 135 |
parameter list must be supplied |
| 136 |
that contains exactly the same number of actual parameters |
| 137 |
as there are formal parameters in the definition of the |
| 138 |
macro being called. An actual parameter list is delimited by |
| 139 |
<SAMP>@(</SAMP> and <SAMP>@)</SAMP>, and |
| 140 |
parameters are <I>separated</I> by |
| 141 |
<SAMP>@,</SAMP>. The actual parameters themselves |
| 142 |
are general FunnelWeb expressions (see the |
| 143 |
<A HREF="../reference/index.html" |
| 144 |
TARGET="funnelwebreference" |
| 145 |
onClick="window.open('','funnelwebreference','location,status,menubar,scrollbars,resizable',false).focus(); return true;" |
| 146 |
>FunnelWeb Reference Manual</A> for the exact syntax) and can |
| 147 |
be inserted into the list directly or can be delimited by |
| 148 |
<SAMP>@"</SAMP> so as to allow some white space to |
| 149 |
assist in formatting the actual parameters. Here are some |
| 150 |
examples of calls of the <SAMP>While loop</SAMP> macro defined |
| 151 |
above. |
| 152 |
|
| 153 |
<P> |
| 154 |
<PRE> |
| 155 |
@! First form of actual parameters |
| 156 |
@! without whitespace and double quotes. |
| 157 |
@<While loop@>@(@- |
| 158 |
x=1;@,x<=10@,printf("X=%u\n",x);@) |
| 159 |
|
| 160 |
@! Second form of actual parameters. The double |
| 161 |
@! quotes allow non-active whitespace that helps |
| 162 |
@! to lay out the actual parameters neatly. This |
| 163 |
@! call is functionally identical to the one above. |
| 164 |
@<While loop@>@( |
| 165 |
@"x:=1;@" @, |
| 166 |
@"x<=10@" @, |
| 167 |
@"printf("X=%u\n",x);@" @) |
| 168 |
|
| 169 |
@! The two forms can be mixed in a single call. |
| 170 |
@<While loop@>@(x=1;@,x<=10@, |
| 171 |
@"printf("X=%u\n",x);@" @) |
| 172 |
</PRE> |
| 173 |
|
| 174 |
<P>A few rules about parameterized macros are worth |
| 175 |
mentioning. Macros that do not have any parameters must have |
| 176 |
no formal or actual parameter lists. Additive macros can have parameters, but the |
| 177 |
formal parameter list must appear in the first definition |
| 178 |
part only. |
| 179 |
|
| 180 |
<P>Here is another example of the use of parameterized |
| 181 |
macros. This time, parameters and macro calls are used in a |
| 182 |
FunnelWeb input file that constructs an O(n) |
| 183 |
representation of a song whose full size is O(n^2) in the number n of |
| 184 |
unique lines. |
| 185 |
|
| 186 |
<P> |
| 187 |
<PRE> |
| 188 |
@O@<Twelve_bugs.txt@>==@{@- |
| 189 |
The Twelve Bugs of Christmas |
| 190 |
---------------------------- |
| 191 |
@<Verse@>@(@"first@" @,@<1@>@) |
| 192 |
@<Verse@>@(@"second@" @,@<2@>@) |
| 193 |
@<Verse@>@(@"third@" @,@<3@>@) |
| 194 |
@<Verse@>@(@"fourth@" @,@<4@>@) |
| 195 |
@<Verse@>@(@"fifth@" @,@<5@>@) |
| 196 |
@<Verse@>@(@"sixth@" @,@<6@>@) |
| 197 |
@<Verse@>@(@"seventh@" @,@<7@>@) |
| 198 |
@<Verse@>@(@"eighth@" @,@<8@>@) |
| 199 |
@<Verse@>@(@"ninth@" @,@<9@>@) |
| 200 |
@<Verse@>@(@"tenth@" @,@<A@>@) |
| 201 |
@<Verse@>@(@"eleventh@" @,@<B@>@) |
| 202 |
@<Verse@>@(@"twelfth@" @,@<C@>@) |
| 203 |
|
| 204 |
This song appeared in the internet newsgroup |
| 205 |
rec.humor.funny on 24-Dec-1991. It was contributed |
| 206 |
by Pat Scannell (scannell@@darkstar.ma30.bull.com). |
| 207 |
@} |
| 208 |
|
| 209 |
@$@<Verse@>@(@2@)@M==@{@- |
| 210 |
For the @1 bug of Christmas, my manager said to me |
| 211 |
@2 |
| 212 |
@} |
| 213 |
|
| 214 |
@$@<1@>@M==@{@- |
| 215 |
See if they can do it again.@} |
| 216 |
@$@<2@>@M==@{@- |
| 217 |
Ask them how they did it and@+@<1@>@} |
| 218 |
@$@<3@>@M==@{@- |
| 219 |
Try to reproduce it@+@<2@>@} |
| 220 |
@$@<4@>@M==@{@- |
| 221 |
Run with the debugger@+@<3@>@} |
| 222 |
@$@<5@>@M==@{@- |
| 223 |
Ask for a dump@+@<4@>@} |
| 224 |
@$@<6@>@M==@{@- |
| 225 |
Reinstall the software@+@<5@>@} |
| 226 |
@$@<7@>@M==@{@- |
| 227 |
Say they need an upgrade@+@<6@>@} |
| 228 |
@$@<8@>@M==@{@- |
| 229 |
Find a way around it@+@<7@>@} |
| 230 |
@$@<9@>@M==@{@- |
| 231 |
Blame it on the hardware@+@<8@>@} |
| 232 |
@$@<A@>@M==@{@- |
| 233 |
Change the documentation@+@<9@>@} |
| 234 |
@$@<B@>@M==@{@- |
| 235 |
Say it's not supported@+@<A@>@} |
| 236 |
@$@<C@>@M==@{@- |
| 237 |
Tell them it's a feature@+@<B@>@} |
| 238 |
</PRE> |
| 239 |
|
| 240 |
|
| 241 |
<P> |
| 242 |
<TABLE WIDTH="100%"> |
| 243 |
<TR> |
| 244 |
<TD ALIGN="left" VALIGN="bottom"><A HREF="macro_additive.html"><IMG SRC="binary/fw_left.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Prev"></A></TD> |
| 245 |
<TD ALIGN="center" VALIGN="bottom"><A HREF="macro.html"><IMG SRC="binary/fw_up.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Up"></A></TD> |
| 246 |
<TD ALIGN="right" VALIGN="bottom"><A HREF="macro_library.html"><IMG SRC="binary/fw_right.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Next"></A></TD> |
| 247 |
</TR> |
| 248 |
</TABLE> |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
<HR> |
| 253 |
<FONT SIZE="2"> |
| 254 |
<A HREF="mailto:[email protected]">Webmaster</A> |
| 255 |
<A HREF="copyright.html">Copyright © Ross N. Williams 1992,1999. All rights reserved.</A><BR> |
| 256 |
</FONT> |
| 257 |
|
| 258 |
</FONT> |
| 259 |
</TD> |
| 260 |
</TR> |
| 261 |
</TABLE> |
| 262 |
|
| 263 |
|
| 264 |
</BODY> |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
</HTML> |