| 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.4 Additive 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.4 Additive Macros</FONT><BR> |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
<P>Sometimes it is convenient to build up the definition |
| 106 |
of a macro in stages throughout the input file. In |
| 107 |
FunnelWeb, this can be done using an <STRONG>additive |
| 108 |
macro</STRONG>. An additive macro is identical to an ordinary macro |
| 109 |
except that |
| 110 |
|
| 111 |
<P> |
| 112 |
<OL> |
| 113 |
<LI> It has <SAMP>+=</SAMP> instead of <SAMP>==</SAMP>. |
| 114 |
<LI> It can be defined in one or more parts throughout the input file. |
| 115 |
The definition of the macro is the concatenation of all the parts in the |
| 116 |
order in which they appear. |
| 117 |
</OL> |
| 118 |
|
| 119 |
<P>The following example shows how additive macros can be |
| 120 |
used to scatter and regroup information, in this case |
| 121 |
assisting in the lucid construction of a data |
| 122 |
abstraction in a language |
| 123 |
(Pascal) that does not support them |
| 124 |
explicitly. |
| 125 |
|
| 126 |
<P> |
| 127 |
<PRE> |
| 128 |
@!****************************** |
| 129 |
|
| 130 |
@O@<prog.pas@>==@{@- |
| 131 |
program adt(input,output); |
| 132 |
@<Types@> |
| 133 |
@<Variables@> |
| 134 |
@<Procedures@> |
| 135 |
begin startproc; end. |
| 136 |
@} |
| 137 |
|
| 138 |
@!****************************** |
| 139 |
|
| 140 |
@$@<Types@>+=@{@- |
| 141 |
type buffer_type = |
| 142 |
record |
| 143 |
length : integer; |
| 144 |
buf : array[1..100] of char; |
| 145 |
end; |
| 146 |
@} |
| 147 |
|
| 148 |
@$@<Variables@>+=@{@- |
| 149 |
bigbuf : buffer_type; |
| 150 |
@} |
| 151 |
|
| 152 |
@$@<Procedures@>+=@{@- |
| 153 |
procedure buf_init (var b : buffer_type ) |
| 154 |
{Body of buf_init} |
| 155 |
procedure buf_add (var b : buffer_type; ch : char) |
| 156 |
{Body of buf_add} |
| 157 |
procedure buf_get (var b : buffer_type; var ch : char) |
| 158 |
{Body of buf_get} |
| 159 |
@} |
| 160 |
|
| 161 |
@!****************************** |
| 162 |
|
| 163 |
@$@<Types@>+=@{@- |
| 164 |
type complex_type = record r,i : real; end; |
| 165 |
@} |
| 166 |
|
| 167 |
@$@<Procedures@>+=@{@- |
| 168 |
procedure cm_set (var c: complex_type; a,b: real) |
| 169 |
{Body of cm_set} |
| 170 |
procedure cm_add (a,b: complex_type; var c: complex_type) |
| 171 |
{Body of cm_add} |
| 172 |
{Other procedures and functions} |
| 173 |
@} |
| 174 |
|
| 175 |
@!****************************** |
| 176 |
|
| 177 |
{...more pieces of program...} |
| 178 |
|
| 179 |
@!****************************** |
| 180 |
</PRE> |
| 181 |
|
| 182 |
<P>It is important to remember that the definition of each |
| 183 |
macro does not change throughout the input file. FunnelWeb |
| 184 |
parses the entire input file and assembles all the macro |
| 185 |
definitions before it even starts to expand macros. As a |
| 186 |
result, each additive macro can only have one definition, |
| 187 |
and that definition is the concatenation of all its parts. |
| 188 |
|
| 189 |
<P>The example above shows how additive macros can be used |
| 190 |
to rearrange the presentation of a computer program in the |
| 191 |
order in which the user wishes to discuss it rather than the |
| 192 |
order in which the compiler requires that it be consumed. It |
| 193 |
is easy, however, to abuse the feature of additive macros. |
| 194 |
In many cases, the same effect can be obtained more clearly |
| 195 |
by replacing each part of an additive macro in-situ using |
| 196 |
uniquely named non-additive macros, and then collect them |
| 197 |
together as a group at the point where the additive macro is |
| 198 |
called. Doing this is more work, and is more error prone, |
| 199 |
but can result in a clearer exposition. The following |
| 200 |
program illustrates this alternative approach. |
| 201 |
|
| 202 |
<P> |
| 203 |
<PRE> |
| 204 |
@!****************************** |
| 205 |
|
| 206 |
@O@<prog.pas@>==@{@- |
| 207 |
program adt(input,output); |
| 208 |
@<Types@> |
| 209 |
@<Variables@> |
| 210 |
@<Procedures@> |
| 211 |
begin startproc; end. |
| 212 |
@} |
| 213 |
|
| 214 |
@$@<Types@>==@{@- |
| 215 |
@<Buffer type@> |
| 216 |
@<Complex type@> |
| 217 |
@} |
| 218 |
|
| 219 |
@$@<Variables@>==@{@- |
| 220 |
@<Buffer variable@> |
| 221 |
@} |
| 222 |
|
| 223 |
@$@<Procedures@>==@{@- |
| 224 |
@<Buffer procedures@> |
| 225 |
@<Complex procedures@> |
| 226 |
@} |
| 227 |
|
| 228 |
@!****************************** |
| 229 |
|
| 230 |
@$@<Buffer type@>==@{@- |
| 231 |
type buffer_type = record |
| 232 |
length : integer; |
| 233 |
buf : array[1..100] of char; |
| 234 |
end; |
| 235 |
@} |
| 236 |
|
| 237 |
@$@<Buffer variable@>==@{@- |
| 238 |
bigbuf : buffer_type; |
| 239 |
@} |
| 240 |
|
| 241 |
@$@<Buffer procedures@>==@{@- |
| 242 |
procedure buf_init(var b : buffer_type) |
| 243 |
{Body of buf_init} |
| 244 |
procedure buf_add(var b : buffer_type; ch : char) |
| 245 |
{Body of buf_add} |
| 246 |
procedure buf_get(var b : buffer_type; var ch : char) |
| 247 |
{Body of buf_get} |
| 248 |
@} |
| 249 |
|
| 250 |
@!****************************** |
| 251 |
|
| 252 |
@$@<Complex type@>==@{@- |
| 253 |
type complex_type = record r,i : real; end; |
| 254 |
@} |
| 255 |
|
| 256 |
@$@<Complex procedures@>+=@{@- |
| 257 |
procedure cm_set(var c: complex_type; a,b : real) |
| 258 |
{Body of cm_set} |
| 259 |
procedure cm_add(a,b : complex_type; var c: complex_type) |
| 260 |
{Body of cm_add} |
| 261 |
{Other procedures and functions} |
| 262 |
@} |
| 263 |
|
| 264 |
@!****************************** |
| 265 |
|
| 266 |
{...more pieces of program...} |
| 267 |
|
| 268 |
@!****************************** |
| 269 |
</PRE> |
| 270 |
|
| 271 |
<P>One of advantages of FunnelWeb (and literate |
| 272 |
programming in general) is that (as shown above) it allows |
| 273 |
the user to lay out the program in whatever order is |
| 274 |
desired with near total |
| 275 |
independence from the ordering requirements of the target |
| 276 |
programming language. |
| 277 |
|
| 278 |
<P>Additive macros are allowed to be tagged with |
| 279 |
<SAMP>@Z</SAMP> and <SAMP>@M</SAMP> just as other macros can, |
| 280 |
but the tags must appear only on the first definition of the |
| 281 |
macro. Additive macros cannot be connected directly to |
| 282 |
product files. |
| 283 |
|
| 284 |
|
| 285 |
<P> |
| 286 |
<TABLE WIDTH="100%"> |
| 287 |
<TR> |
| 288 |
<TD ALIGN="left" VALIGN="bottom"><A HREF="macro_indent.html"><IMG SRC="binary/fw_left.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Prev"></A></TD> |
| 289 |
<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> |
| 290 |
<TD ALIGN="right" VALIGN="bottom"><A HREF="macro_param.html"><IMG SRC="binary/fw_right.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Next"></A></TD> |
| 291 |
</TR> |
| 292 |
</TABLE> |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
<HR> |
| 297 |
<FONT SIZE="2"> |
| 298 |
<A HREF="mailto:[email protected]">Webmaster</A> |
| 299 |
<A HREF="copyright.html">Copyright © Ross N. Williams 1992,1999. All rights reserved.</A><BR> |
| 300 |
</FONT> |
| 301 |
|
| 302 |
</FONT> |
| 303 |
</TD> |
| 304 |
</TR> |
| 305 |
</TABLE> |
| 306 |
|
| 307 |
|
| 308 |
</BODY> |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
</HTML> |