| 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>5.5 Fudging Conditionals</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">5.5 Fudging Conditionals</FONT><BR> |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
<P>As a macro preprocessor, one facility that FunnelWeb |
| 106 |
lacks is a conditional facility (such as C's |
| 107 |
<SAMP>#ifdef</SAMP>). It might, therefore, come as a surprise |
| 108 |
to know that the FunnelWeb V1 actually had a |
| 109 |
built in conditional facility. The facility allowed the |
| 110 |
programmer to specify a construct that would select from one |
| 111 |
of a number of macro expressions depending on the value of a |
| 112 |
controlling macro expression. |
| 113 |
|
| 114 |
<P>In three years the construct was never used. |
| 115 |
|
| 116 |
<P>The reason was that conditional constructs could be |
| 117 |
fudged nearly as easily as they could be used. Because of |
| 118 |
this, the inbuilt conditional feature was removed in the |
| 119 |
FunnelWeb V2. Not only did this simplify the |
| 120 |
program, but is also allowed recursive macros to be detected |
| 121 |
through static analysis rather than during macro expansion. |
| 122 |
|
| 123 |
<P>There are two basic ways to fudge a conditional. First, |
| 124 |
the comment facility of the target programming language may |
| 125 |
be employed. For example, in Ada, comments |
| 126 |
commence with "<TT>--</TT>" and terminate at the end of the |
| 127 |
line. Using this fact, it is easy to construct macros that |
| 128 |
can be called at the start of each target line and which |
| 129 |
turn on and off the lines so marked by defining the macro to |
| 130 |
be the empty string (ON) or the comment symbol |
| 131 |
(<SAMP>--</SAMP>) (OFF). For example: |
| 132 |
|
| 133 |
<P><PRE> |
| 134 |
@A@<Debug Macro@> |
| 135 |
|
| 136 |
The following macro determines whether debug code |
| 137 |
will be included in the program. All lines of |
| 138 |
debug code commence with a call to this macro and |
| 139 |
so we can turn all that code on or off here by |
| 140 |
defining this macro to be either empty or the |
| 141 |
single-line comment symbol (<SAMP>--</SAMP>). Note |
| 142 |
the use of a quick macro name. |
| 143 |
|
| 144 |
@$@#D@M==@{@} @! Turns the debug code ON. |
| 145 |
@! Use this definition to turn |
| 146 |
@! the debug code OFF: @$@#D==@{--@} |
| 147 |
|
| 148 |
... then later in the file... |
| 149 |
|
| 150 |
@$@<Sloth incrementing loop@>==@{@- |
| 151 |
while sloth<walrus loop |
| 152 |
@#D assert(sloth<walrus,"Sloth bomb."); |
| 153 |
@#D assert(timer<timermax,"Timer bomb."); |
| 154 |
inc(sloth); |
| 155 |
end loop@} |
| 156 |
</PRE> |
| 157 |
|
| 158 |
<P>The other way to fudge a conditional is to |
| 159 |
define a macro with a single parameter. A call to |
| 160 |
the macro is then wrapped around all the |
| 161 |
conditional code in the program. The macro can |
| 162 |
then be defined to present or ignore the code of |
| 163 |
its argument. For example: |
| 164 |
|
| 165 |
<P> |
| 166 |
<PRE> |
| 167 |
@A@<Debug Macro@> |
| 168 |
|
| 169 |
The following macro determines whether debug code |
| 170 |
will be included in the program. All debug code is |
| 171 |
wrapped by a call to this macro and so we can turn |
| 172 |
all the debug code on or off here by defining this |
| 173 |
macro to be either empty or its parameter. |
| 174 |
|
| 175 |
@$@#D@(@1@)@M==@{@1@} @! Debug code ON. |
| 176 |
@! Use this definition to turn the |
| 177 |
@! debug code OFF: @$@#D@(@1@)==@{@} |
| 178 |
|
| 179 |
... then later in the file... |
| 180 |
|
| 181 |
@$@<Sloth incrementing loop@>==@{@- |
| 182 |
while sloth<walrus loop |
| 183 |
@#D@(assert(sloth<walrus,"Sloth bomb."); |
| 184 |
assert(timer<timermax,"Timer bomb");@) |
| 185 |
inc(sloth); |
| 186 |
end loop@} |
| 187 |
</PRE> |
| 188 |
|
| 189 |
<P>In languages that allow multi-line comments (e.g. C |
| 190 |
with <SAMP>/*</SAMP> and <SAMP>*/</SAMP>), comments can be used to |
| 191 |
eliminate the conditioned code rather than absence. For |
| 192 |
example: |
| 193 |
|
| 194 |
<P> |
| 195 |
<PRE> |
| 196 |
@! Comments out the debug code |
| 197 |
@$@#D@(@1@)@M==@{/* @1 */@} |
| 198 |
</PRE> |
| 199 |
|
| 200 |
<P>(Note: If this example were ever actually used, the |
| 201 |
programmer would have to be careful not to place comments in |
| 202 |
the argument code. Nested comments in C are non-portable.) |
| 203 |
|
| 204 |
<P>The parameterized macro idea can be generalized to |
| 205 |
support the choice of more than one mutually exclusive |
| 206 |
alternative. For example: |
| 207 |
|
| 208 |
<P> |
| 209 |
<PRE> |
| 210 |
@A This module contains non-portable code that |
| 211 |
must execute on Hewlett Packard, Sun, and DEC |
| 212 |
workstations. The following FunnelWeb macro is |
| 213 |
defined to choose between these three. The first |
| 214 |
parameter is the HP code, the second is the Sun |
| 215 |
code, and the third is the DEC code. Whichever |
| 216 |
parameter constitutes the body of this macro |
| 217 |
determines which machine the code is being |
| 218 |
targeted for. |
| 219 |
|
| 220 |
@$@<Machine specific code@>@(@3@)@M==@{@- |
| 221 |
@1@} @! Configure for HP. |
| 222 |
|
| 223 |
...then later in the file... |
| 224 |
|
| 225 |
@<Machine specific code@>@( |
| 226 |
@"get_command_line(comline)@" @, @! HP. |
| 227 |
@"scan_command_line(128,comline);@" @, @! Sun. |
| 228 |
@"dcl_get_command_line(comline,256);@" @) @! DEC. |
| 229 |
</PRE> |
| 230 |
|
| 231 |
<P>Of course, this could also be performed using three |
| 232 |
separate macros. The main advantage of using a single macro |
| 233 |
is that the mutual exclusivity is enforced. Also, because |
| 234 |
FunnelWeb ensures that the number of formal and actual |
| 235 |
parameters are the same, this method lessens the chance that |
| 236 |
a machine will be forgotten in some places. |
| 237 |
|
| 238 |
|
| 239 |
<P> |
| 240 |
<TABLE WIDTH="100%"> |
| 241 |
<TR> |
| 242 |
<TD ALIGN="left" VALIGN="bottom"><A HREF="hints_eols.html"><IMG SRC="binary/fw_left.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Prev"></A></TD> |
| 243 |
<TD ALIGN="center" VALIGN="bottom"><A HREF="hints.html"><IMG SRC="binary/fw_up.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Up"></A></TD> |
| 244 |
<TD ALIGN="right" VALIGN="bottom"><A HREF="hints_headings.html"><IMG SRC="binary/fw_right.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Next"></A></TD> |
| 245 |
</TR> |
| 246 |
</TABLE> |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
<HR> |
| 251 |
<FONT SIZE="2"> |
| 252 |
<A HREF="mailto:[email protected]">Webmaster</A> |
| 253 |
<A HREF="copyright.html">Copyright © Ross N. Williams 1992,1999. All rights reserved.</A><BR> |
| 254 |
</FONT> |
| 255 |
|
| 256 |
</FONT> |
| 257 |
</TD> |
| 258 |
</TR> |
| 259 |
</TABLE> |
| 260 |
|
| 261 |
|
| 262 |
</BODY> |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
</HTML> |