home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / unix / volume26 / funnlweb / part19 < prev    next >
Encoding:
Text File  |  1993-04-10  |  71.6 KB  |  1,849 lines

  1. Newsgroups: comp.sources.unix
  2. From: ross@spam.adelaide.edu.au (Ross Williams)
  3. Subject: v26i139: funnelweb - a tool for literate programming in C, Part19/20
  4. Sender: unix-sources-moderator@vix.com
  5. Approved: paul@vix.com
  6.  
  7. Submitted-By: ross@spam.adelaide.edu.au (Ross Williams)
  8. Posting-Number: Volume 26, Issue 139
  9. Archive-Name: funnelweb/part19
  10.  
  11. #! /bin/sh
  12. # This is a shell archive.  Remove anything before this line, then unpack
  13. # it by saving it into a file and typing "sh file".  To overwrite existing
  14. # files, type "sh file -c".  You can also feed this as standard input via
  15. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  16. # will see the following message at the end:
  17. #        "End of archive 19 (of 20)."
  18. # Contents:  userman/u_ch1.tex
  19. # Wrapped by vixie@gw.home.vix.com on Sun Apr 11 11:00:34 1993
  20. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  21. if test -f 'userman/u_ch1.tex' -a "${1}" != "-c" ; then 
  22.   echo shar: Will not clobber existing file \"'userman/u_ch1.tex'\"
  23. else
  24. echo shar: Extracting \"'userman/u_ch1.tex'\" \(70598 characters\)
  25. sed "s/^X//" >'userman/u_ch1.tex' <<'END_OF_FILE'
  26. X%==============================================================================%
  27. X%                                Start of Ch1.tex                              %
  28. X%==============================================================================%
  29. X%
  30. X% Copyright
  31. X% ---------
  32. X% Copyright (C) 1992 Ross N. Williams.
  33. X% This file contains a chapter of the FunnelWeb User's Manual.
  34. X% See the main TeX file for this manual for further information.
  35. X%
  36. X%==============================================================================%
  37. X
  38. X\chapter{A Tutorial Introduction}
  39. X\label{chapintroduction}
  40. X\x{tutorial}
  41. X
  42. X\section{What is Literate Programming?}
  43. X\x{literate programming}
  44. X
  45. A traditional computer program consists of a text file containing program
  46. code. Scattered in amongst the program code are comments which describe the
  47. various parts of the code. 
  48. X
  49. In \newterm{literate programming} the emphasis is reversed. Instead of
  50. writing code containing documentation, the literate programmer writes
  51. documentation containing code. No longer does the English commentary
  52. injected into a program have to be hidden in
  53. comment delimiters at the top of the
  54. file, or under procedure headings, or at the end of lines.
  55. Instead, it is wrenched into the daylight and made the main focus.
  56. The \dq{program} then becomes primarily a document directed at humans,
  57. with the code being herded between \dq{code delimiters} from where it
  58. can be extracted and shuffled out sideways to the language system
  59. by literate programming tools.
  60. X
  61. The effect of this simple shift of emphasis can be so
  62. profound as to change one's whole approach to programming. Under the
  63. literate programming paradigm, the central activity of programming becomes
  64. that of conveying meaning to other intelligent beings rather than merely
  65. convincing the computer to behave in a particular way. It is the difference
  66. between performing and exposing a magic trick.\x{magic trick}
  67. X
  68. In order to program in a literate style, particular tools are
  69. required.\xx{tools}{literate programming}
  70. The traditional approach (used in the FunnelWeb system) is to have
  71. some sort of text-file-in/text-file-out utility that reads a
  72. literate program (containing a program commentary peppered with scraps
  73. of program text) and writes out a file containing all the program code and
  74. a file containing typesetter commands representing the entire input document,
  75. documentation, code, and all (\figarchitecture{}).
  76. X
  77. X\begin{figure}[htbp]
  78. X\begin{verbatim}
  79. X
  80. X                +-----------------------------------------+
  81. X                | File containing the program description |
  82. X                | peppered with scraps of program code.   |
  83. X                | This is what the programmer works on.   |
  84. X                |          (e.g. sloth.web)               |
  85. X                +-----------------------------------------+
  86. X                                    |
  87. X                                    v
  88. X                      o---------------------------o
  89. X                      | Literate Programming Tool |
  90. X                      o---------------------------o
  91. X                                    |
  92. X                +-------------------+-------------------+
  93. X                |                                       |
  94. X                v                                       v
  95. X       +------------------+           +----------------------------------+
  96. X       |   Traditional    |           | Documentation file suitable for  |
  97. X       | Computer Program |           | input into a typesetting program |
  98. X       |  (e.g. sloth.c)  |           |        (e.g. sloth.tex)          |
  99. X       +------------------+           +----------------------------------+
  100. X
  101. X\end{verbatim}
  102. X\mylabel{\figarchitecture{}: Traditional architecture of literate programming tools.}{%
  103. X%
  104. Literate programming tools could be organized in a number of ways. However,
  105. to fit in with current file and command line based environments, most tools
  106. conform to the traditional architecture shown here in which the user feeds
  107. in a file containing a literate program, and the literate programming
  108. utility generates program files and a documentation file.
  109. X%
  110. X}
  111. X\end{figure}
  112. X
  113. Given the coming age of hypertext\x{hypertext} systems,
  114. this is probably not the best approach.
  115. However, it does mesh beautifully with current text
  116. files and command line interfaces, the expectation of linear presentations in
  117. the documents we read, and the particular requirements of current programming
  118. languages and typesetting systems. It is certainly not a bad approach.
  119. X
  120. With this structure in place, the literate programming system can provide
  121. far more than just a reversal of the priority of comments and code.
  122. In its full blown form, a good literate programming facility can provide
  123. total support for the essential thrust of literate programming, which is that
  124. computer programs should be written more for the human reader
  125. than for the compiler. In particular, a literate programming system can
  126. provide:\xs{literate programming}{facilities}
  127. X
  128. X\narrowthing{Re-ordering of code:}{Programming languages often force the
  129. programmer to give the various parts of a computer program in a particular
  130. order.\xx{program}{ordering}
  131. XFor example, the Pascal\x{Pascal} programming language\paper{BSI82}
  132. imposes the ordering:
  133. constants, types, variables, procedures, code. Pascal also requires that
  134. procedures appear in an order consistent with the partial ordering
  135. imposed by the static call graph (but forward
  136. declarations allow this to be bypassed). In contrast, the literate style
  137. requires that the programmer be free to present the computer program in
  138. any order whatsoever. The facility to do this is implemented in literate
  139. programming tools by providing text \i{macros} that can be defined
  140. and used in any order.}
  141. X
  142. X\narrowthing{Typeset code and documentation:}{Traditionally program
  143. listings are
  144. dull affairs consisting of pages of fan-form paper imprinted with meandering
  145. coastlines of structured text in a boring font. In contrast, literate
  146. programming systems are capable of producing documentation that is
  147. superior in two ways. First, because most of the documentation text is
  148. fed straight to the typesetter, the programmer can make use of all the
  149. power of the underlying typesetter, resulting in documentation that has the
  150. same presentation as an ordinary typeset document. Second, because the
  151. literate programming utility sees all the code, it can use its knowledge
  152. of the programming language and the features of the typesetting language
  153. to typeset the program code as if it were appearing in a
  154. technical journal. It is the difference
  155. between:}
  156. X
  157. X\begin{verbatim}
  158. X    while sloth<walrus loop
  159. X       sloth:=sloth+1;
  160. X    end loop
  161. X\end{verbatim}
  162. X
  163. X\narrowtext{and}
  164. X
  165. X\begin{verse}
  166. X\b{while} \i{sloth}$<$\i{walrus} \b{loop}\\
  167. X\qquad\qquad \i{sloth}$\leftarrow$\i{sloth}$+1$;\\
  168. X\b{end} \b{loop}
  169. X\end{verse}
  170. X
  171. X\narrowtext{Unfortunately, while FunnelWeb provides full typesetting
  172. of the
  173. documentation, it typesets all of its code in the style of the first of these
  174. two examples. To typeset in the style of the second requires knowledge of
  175. the programming language, and the current version of FunnelWeb is
  176. programming language independent. At a later stage, it is possible that
  177. XFunnelWeb will be modified to read in a file containing information
  178. about the target programming language to be used to assist in
  179. typesetting the code properly.}
  180. X
  181. X\narrowthing{Cross referencing:}{Because\x{cross referencing}
  182. the literate tool sees all the
  183. code and documentation, it is able to generate extensive cross referencing
  184. information in the typeset documentation. This makes the printed program
  185. document more easy to navigate and partially compensates for the lack of
  186. an automatic searching facility when reading printed documentation.}
  187. X
  188. In the end, the details don't matter. The most significant
  189. benefit that literate programming offers
  190. is \i{its capacity to transform the state of mind of the
  191. programmer}.\xs{literate programming}{most significant benefit}
  192. It is now legend that the act of explaining something can transform
  193. one's understanding of it. This is one of the justifications behind the
  194. powerful combination of research and teaching in
  195. universities\paper{Rosovsky90}.\x{universities}
  196. Similarly, by constantly explaining the unfolding program code in English to an
  197. imaginary reader, the programmer transforms his perception of the code,
  198. laying it open, prone, to the critical eye.\xx{explaining}{code}
  199. X
  200. The result of this exposure is a higher quality of programming.
  201. When exposed to the harsh light of the literate eye,
  202. bugs crawl out, special cases vanish, and sloppy code evaporates.
  203. As a rule literate programs
  204. take longer to write than ordinary programs, but the total
  205. development time\xx{development}{time}
  206. is the same or less because the time taken to write and
  207. document the program carefully is compensated for by a reduced debugging
  208. and maintenance time.
  209. Thus literate programming does not merely assist in the preparation of
  210. documentation, but also makes significant contributes to the process of
  211. programming itself. In practice this has turned out to be a contribution
  212. far more important than the mere capacity to produce typeset documentation.
  213. X
  214. XFor more information on literate programming, the reader is directed
  215. to Knuth's early founding work \paper{Knuth83} and \paper{Knuth84}.
  216. XFor more recent information refer to \paper{Smith91},
  217. which provides a comprehensive bibliography up to 1990.
  218. X
  219. X\section{What is FunnelWeb?}
  220. X\xx{FunnelWeb}{overview}
  221. X
  222. XFunnelWeb is a particular literate programming system that is implemented
  223. by a single C program. FunnelWeb takes as input a single \p{.fw}
  224. X\newterm{input file}
  225. and writes one or more \newterm{product files}
  226. and a \newterm{documentation file} (\figfunarch{}).
  227. X
  228. X\begin{figure}[htbp]
  229. X\begin{verbatim}
  230. X                              +-----------+
  231. X                              | sloth.fw  |
  232. X                              +-----------+
  233. X                                    |
  234. X                                    v
  235. X                          o-------------------o
  236. X                          | FUNNELWEB Program |
  237. X                          o-------------------o
  238. X                                    |
  239. X                +-------------------+----------------+
  240. X                |                                    |
  241. X                v                                    v
  242. X  +-----------------------------+    +--------------------------------+
  243. X  | Product File (e.g. sloth.c) |    | Documentation File (sloth.tex) |
  244. X  +-----------------------------+    +--------------------------------+
  245. X\end{verbatim}
  246. X\mylabel{\figfunarch{}: Architecture of FunnelWeb.}{%
  247. X%
  248. XFunnelWeb follows the traditional architecture of literate programming
  249. tools.
  250. X%
  251. X}
  252. X\end{figure}
  253. X
  254. In literate programming systems, it is usual to refer to the product
  255. file as a \dq{program file}. However, as FunnelWeb is a general tool that
  256. can be used to prepare all sorts of text files that are not computer programs,
  257. the more generic term \dq{product file} was chosen. Product files should
  258. be carefully distinguished from the term \newterm{output files} which refers
  259. to all of the output files produced by FunnelWeb.
  260. X
  261. XFunnelWeb is distinguished by the following characteristics:
  262. X
  263. X\narrowthing{Simplicity:}{A\x{simplicity}
  264. governing design goal of FunnelWeb is to provide
  265. a \i{simple} tool that could be easily learnt and completely mastered.
  266. This manual is thick because it is comprehensive and lingers on the
  267. ways in which FunnelWeb can be used. The tool itself is quite simple.}
  268. X
  269. X\narrowthing{Reliability:}{Another\x{reliability}
  270. design goal is to provide a tool that will
  271. protect the user as much as possible from silly errors. Macro preprocessors
  272. are notorious for causing obscure errors. Every attempt has been made in
  273. XFunnelWeb to keep the syntax robust.
  274. XFor example, in FunnelWeb the syntax of
  275. macro calls has been purposely designed to be highly visible so that the
  276. reader is always aware when the macro facility is being invoked.}
  277. X
  278. X\narrowthing{Language and Typesetter Independence:}{Unlike
  279. Knuth's original\xx{language}{independence}\xx{typesetter}{independence}
  280. Web system which was specific to the Pascal programming language\paper{BSI82}
  281. and the \TeX{} typesetting language\paper{Knuth84}, FunnelWeb strives to
  282. be language and typesetter independent. The current version of FunnelWeb
  283. is completely language independent, but is still somewhat dependent on the
  284. X\TeX{}\x{TeX} typesetter language.}
  285. X
  286. X\narrowthing{Portability:}{FunnelWeb\x{portability} has been written in
  287. the~C programming language with great emphasis on portability.
  288. XFunnelWeb currently runs on the Sun, VAX, IBM PC, and Mac.}
  289. X
  290. X\narrowthing{Controllable:}{FunnelWeb\x{controllability}
  291. is an extremely controllable tool.
  292. To protect users' investment in source files constructed in the FunnelWeb
  293. macro language, the C source code to FunnelWeb has been released under GNU
  294. license.\xx{GNU}{license}
  295. This means that it will always be available to everyone. Furthermore,
  296. license has been granted for the FunnelWeb User's Manual and FunnelWeb
  297. Hacker's Manual to be copied freely so long as they are not modified. All
  298. this means that FunnelWeb is not going to disappear suddenly.}
  299. X
  300. X\narrowthing{A Production Tool:}{Above\x{production tool}
  301. all, FunnelWeb has been designed to be
  302. a production tool and every effort has been made to ensure that it will
  303. operate effectively in a professional environment. FunnelWeb is \dq{open}
  304. and portable. There is a comprehensive user manual. Its error messages
  305. are comprehensive. It is fast. Finally, it has been designed with the
  306. experience of three years of using FunnelWeb~V1.}
  307. X
  308. XFor more information on the history and design of FunnelWeb, see
  309. the \i{FunnelWeb Hacker's Manual}.
  310. X
  311. X\section{The Name FunnelWeb}
  312. X\xx{FunnelWeb}{name}
  313. X
  314. The name \dq{FunnelWeb} was chosen because it contains the name \dq{WEB},
  315. which is the name of Knuth's system. It was also chosen because it
  316. has a distinctly Australian flavour.
  317. X
  318. XFunnel-web spiders\xx{Funnel-web}{spider}
  319. are found in Northern and Eastern Australia. They are
  320. about three to four centimetres long and are very poisonous. The
  321. Sydney\x{Sydney}
  322. XFunnel-web spider (\i{Atrax robustus}\x{Atrax robustus}),
  323. common in Sydney,
  324. has caused the most trouble and has been responsible for several deaths.
  325. XFunnel-web spiders love to crawl into temporarily discarded shoes where they
  326. later react in a hostile manner to an unsuspecting foot. They are known
  327. to hang on once they sink their fangs in. Funnel-web spiders derive
  328. their name from the shape of their webs which are horizontally-aligned
  329. narrowing tubes, open at one end\paper{ANZE}.
  330. X
  331. The Funnel-web spider, like the tiger snake\xx{tiger}{snake}
  332. and the white pointer shark,\xx{white pointer}{shark}
  333. is secretly regarded by Australians as a kind of national
  334. treasure.\xn{Edna}{Everage}\xn{Barry}{Humphries}
  335. X
  336. X\begin{verse}
  337. X\b{F} is for Funnel-web\\
  338. Our furry-legged foe.\\
  339. He sleeps in your slipper\\
  340. And breakfasts on toe.\\
  341. X--- One verse from \i{A Megastar's Mantras: Things that Mean a Lot to Me},\\
  342. X\qquad{} by Dame Edna Everage\paper{Humphries91}.\\
  343. X\end{verse}
  344. X
  345. X\section{A FunnelWeb Tutorial}
  346. X\x{tutorial}\xx{tutorial}{introduction}
  347. X
  348. The remainder of this chapter consists of an introductory tutorial on
  349. XFunnelWeb. Ideally you should have a working version of FunnelWeb in front
  350. of you when reading this chapter so that you can try out the examples
  351. yourself. There is no need to try all the examples so long as you
  352. type in enough to feel comfortable with what you are reading. There
  353. is no harm in reading this chapter offline too, so long as you get to the
  354. computer within a couple of days to reinforce what you have read.
  355. X
  356. XFor best effect, you should create a new, temporary, empty directory in
  357. which to experiment with FunnelWeb. That way, it will be more obvious
  358. when FunnelWeb creates an output file. You can either type in the
  359. examples in this chapter directly, or copy and paste them from the \LaTeX{}
  360. source file for this chapter or the FunnelWeb
  361. test suite. The source file for this chapter should be available
  362. in a file such as \p{/fwdir/userman/ch1.tex}. The test suite should be
  363. available in a directory such as \p{/fwdir/tests/}. The
  364. test files called \p{ex01.fw} through \p{ex16.fw} contain the examples in this
  365. chapter. The test files called \p{hi01.fw} through \p{hi10.fw} contain the
  366. examples in the next chapter.
  367. X
  368. If you do not yet have an installed copy of FunnelWeb, refer to
  369. Chapter~\ref{chapinstall} for full details on how to obtain and install
  370. a copy of FunnelWeb. If you are not sure if you have an installed copy,
  371. try invoking FunnelWeb by giving the command \dqp{fw}. If this yields an error
  372. such as \dq{command not found} then you do not have a properly
  373. installed version of FunnelWeb.
  374. X
  375. X\section{A Hello World Document}
  376. X\x{hello world document}
  377. X
  378. Just as one starts the process of learning a new programming language with a
  379. X\dq{Hello World} program, when learning  FunnelWeb, you can start with a
  380. X\dq{Hello World} document! And here it is! Edit a text file
  381. called \p{hello.fw} and put the following text in it. (Note: The second
  382. character is the letter \dq{Oh}, not the digit \dq{Zero}).
  383. X
  384. X\begin{verbatim}
  385. X@O@<hello.txt@>@{Hello World@+@}
  386. X\end{verbatim}
  387. X
  388. To \dq{run} this \dq{program}, invoke FunnelWeb using the \dqp{fw}
  389. command as follows.\x{invoking FunnelWeb}
  390. X
  391. X\begin{verbatim}
  392. fw hello
  393. X\end{verbatim}
  394. X
  395. X\i{If this command doesn't work, then chances are that
  396. XFunnelWeb has not been installed on your machine. Refer to
  397. Chapter~\ref{chapinstall} for full details on how to obtain and install
  398. a copy of FunnelWeb.}
  399. X
  400. There should be no errors. If there are, have a look
  401. at the listing file \p{hello.lis}, which should contain an explanation of the
  402. error, and compare the area in the file where the error occurred with the
  403. text above. If there are no errors, you will find that the following
  404. two files have been created.
  405. X
  406. X\begin{verbatim}
  407. hello.lis   - The LISTING file.
  408. hello.txt   - The PRODUCT file.
  409. X\end{verbatim}
  410. X
  411. Take a look at \p{hello.txt}. It should contain a single line with the
  412. text \p{Hello World}. Let's take another look at the input file.
  413. X
  414. X\begin{verbatim}
  415. X@O@<hello.txt@>@{Hello World@+@}
  416. X\end{verbatim}
  417. X
  418. The whole structure of the input file is controlled by \dqp{@},
  419. called the \newterm{special character}, which
  420. introduces \newterm{special sequence}s. A scanner's-eye view of the command
  421. line looks like this:
  422. X
  423. X\begin{verbatim}
  424. X@O  @<  "hello.txt"  @>  @{  "Hello World"  @+  @}
  425. X\end{verbatim}
  426. X
  427. The \p{@} character controls everything. In this file we have six different
  428. special sequences that together
  429. form a single macro definition. The \p{@<}\x{@<} and \p{@>}\x{@>}
  430. delimit the name of
  431. the macro. The \p{@O}\x{@O} signals the start of the macro
  432. definition and indicates that the macro is to be connected to a product
  433. file with the same name as the macro (This is is why we got a product file when
  434. we ran FunnelWeb). The \p{@\{} and \p{@\}}\x{@braces}
  435. delimit the body of the macro.
  436. XFinally, the \p{@+}\x{@+} instructs that an end of line sequence should be
  437. inserted at that point in the product file.
  438. X
  439. If you think this syntax looks messy, then you're right.
  440. It \i{is}
  441. messy. FunnelWeb \i{could} have employed a \dq{simpler} notation
  442. in which more of the \p{@} sequences were eliminated. For example:
  443. X
  444. X
  445. X\begin{verbatim}
  446. Warning: This example is NOT legal FunnelWeb.
  447. X
  448. X#hello.txt{Hello World+}
  449. X\end{verbatim}
  450. X
  451. However, if such a syntax were used, the user (you!) would have to remember
  452. that \p{\#} starts a new macro. You would also have to remember that
  453. the characters \p{\}} and \p{+} cannot be used in a macro body without a fuss.
  454. And so on. FunnelWeb is messier, but provides one simple
  455. rule:\xx{simple}{rule} \i{Nothing special happens
  456. unless the special character \p{@} appears.}
  457. X
  458. This means that in FunnelWeb, you can look at large blocks of text in the
  459. confidence that (unlike for the C
  460. pre-processor\xx{C}{preprocessor}) there are no macro calls
  461. hidden in there. If there were, there would be an \p{@}
  462. character!\footnote{The only
  463. exception to this rule occurs where the user has explicitly changed the
  464. special character using the \p{@=}\x{@=} special sequence.}
  465. X
  466. Let's take another look at the hello world program.
  467. X
  468. X\begin{verbatim}
  469. X@O@<hello.txt@>@{Hello World@+@}
  470. X\end{verbatim}
  471. X
  472. In its current form, it consists of a single macro definition. This definition,
  473. while completely valid on its own, only represents half the power of
  474. XFunnelWeb. In fact you could say that it is a \dq{Hello
  475. Northern Hemisphere Program}.\x{Hello Northern Hemisphere Program}
  476. To turn it into a proper FunnelWeb \dq{Hello
  477. World} program, we need to add some documentation!
  478. X
  479. A FunnelWeb input file consists of a sequence of macro definitions surrounded
  480. by a sea of documentation which is just ordinary text. Modify your hello
  481. world document so that it looks like this:
  482. X
  483. X\begin{verbatim}
  484. This hello world document was created by -insert your name here-.
  485. X
  486. X@O@<hello.txt@>@{Hello World@+@}
  487. X
  488. It writes out a file called hello.txt containing the string ``Hello World''.
  489. X\end{verbatim}
  490. X
  491. Now run it through FunnelWeb, but this time, add a \p{+t} to the command line.
  492. X
  493. X\begin{verbatim}
  494. fw hello +t
  495. X\end{verbatim}
  496. X
  497. If all goes well, you should find that you now have
  498. X
  499. X\begin{verbatim}
  500. hello.lis   - A LISTING       file.
  501. hello.tex   - A DOCUMENTATION file (in TeX format).
  502. hello.txt   - A PRODUCT       file.
  503. X\end{verbatim}
  504. X
  505. Take a look at \p{hello.txt}. You will find that it is identical to the
  506. X\p{hello.txt} of the previous run. Only macro
  507. definitions affect the product files that FunnelWeb produces (as a result
  508. of \p{@O} macro definitions). The surrounding documentation has \i{no}
  509. effect. In contrast, the new file, \p{hello.tex} (have a look at it now)
  510. which was created as
  511. a result of your adding the \p{+t} option contains a fairly full
  512. representation of the input file. Whereas \p{hello.txt} is the
  513. X\i{product file} of FunnelWeb, \p{hello.tex} is the \i{documentation file}.
  514. Try typesetting the documentation file now using
  515. the \TeX{} typesetting program.  Then print it.
  516. The following commands are an example of the
  517. sort of commands you will have to give to do this.
  518. X
  519. X\begin{verbatim}
  520. tex hello                  ! Typeset the documentation.
  521. lpr -Pcslw -d hello.dvi    ! Print the typeset documentation.
  522. X\end{verbatim}
  523. X
  524. The result should be a single page containing the two lines of documentation
  525. along with a typeset representation of the macro.
  526. At this point, you have exercised the two main aspects of
  527. XFunnelWeb.\xx{FunnelWeb}{two main aspects}
  528. Starting
  529. with an input file containing macros (or in this case macro) and documentation,
  530. you have successfully generated a product file based on the macros, and
  531. a documentation file, based on the entire document. Congratulations!
  532. X
  533. The remainder of this tutorial is divided into two parts, which focus on
  534. XFunnelWeb's macro facilities and its typesetting facilities. By tradition,
  535. the generation of program files from a literate text is called
  536. X\newterm{Tangling}, and the generation of typeset documentation is
  537. called \newterm{Weaving}.\footnote{In FunnelWeb, these two functions
  538. are aspects of a single computer program. However, in Knuth's WEB\x{WEB}
  539. system,
  540. the two functions are embodied in two separate computer programs called
  541. Tangle and Weave, presumably because, as everyone knows, \dq{it takes
  542. two to Tangle}.}
  543. X
  544. X\section{Macro Facilities}
  545. X\xx{tutorial}{macro facilities}
  546. X
  547. The hello world program of the previous section exercised both the macro
  548. expansion (product-file) aspect of FunnelWeb, and the typesetting
  549. X(documentation file) aspect
  550. of FunnelWeb. This section contains an exposition of the macro facilities,
  551. and totally ignores the documentation side. This is partly to increase the
  552. focus of the tutorial, and partly because documentation is usually bulky and
  553. would take too long for the reader to type in to make the tutorial effective.
  554. X
  555. X\subsection{Simple Macros}
  556. X\xx{tutorial}{simple macros}\xx{tutorial}{macros simple}
  557. X
  558. The original \dq{Hello World} program consisted of a single macro definition.
  559. X
  560. X\begin{verbatim}
  561. X@O@<hello.txt@>@{Hello World@+@}
  562. X\end{verbatim}
  563. X
  564. In fact, this is a rather exceptional macro, as it causes its expansion to
  565. be written to a
  566. product file. The \p{@O}\x{@O} (for \b{O}utput) signals this.
  567. In FunnelWeb, most macros are
  568. defined using \p{@\$}.\x{@dollar}
  569. This  results in a macro that does not generate a product
  570. file, but which can be called in other macros (including \p{@O} macros).
  571. Let us expand the hello world program to include some other macros.
  572. X
  573. X\begin{verbatim}
  574. X@O@<hello.txt@>@{@<Greetings@>@+@}
  575. X
  576. X@$@<H@>==@{Hello@}
  577. X@$@<W@>==@{World@}
  578. X
  579. X@$@<Greetings@>==@{@<H@> @<W@>@}
  580. X\end{verbatim}
  581. X
  582. Type in the file and run it through FunnelWeb using the command:
  583. X
  584. X\begin{verbatim}
  585. fw hello
  586. X\end{verbatim}
  587. X
  588. The product file (result.out) should look like this:
  589. X
  590. X\begin{verbatim}
  591. Hello World
  592. X\end{verbatim}
  593. X
  594. This short program illustrates some of the features of ordinary macros in
  595. XFunnelWeb. Consider the \p{@O} macro. Instead of containing
  596. straight text (\dq{Hello World}), it now contains the macro call
  597. X\p{@<Greetings@>}. A FunnelWeb macro can be called from within the body of
  598. another macro just by giving the macro
  599. name delimited in \p{@<} and \p{@>}.
  600. X
  601. At the bottom of the file is the
  602. definition of the \p{@<Greetings@>}
  603. macro. The definition is similar to the definition of \p{hello.txt} except
  604. that it starts with \p{@\$} to indicate that no product file
  605. is desired from this macro (directly). It also employs the optional
  606. X\p{==} syntax which has no semantic impact, but 
  607. can be used to make definitions clearer.
  608. The body of the \p{@<Greetings@>} macro consists of
  609. calls to the \p{H} and \p{W} macros which are defined immediately above.
  610. X
  611. Note that the macros are not constrained to be
  612. defined in any particular order. One of the main
  613. features of literate programming tools is that
  614. they allow the different parts of the text document being developed (usually
  615. a computer program) to be layed out in any
  616. order.\xx{order}{program}\xx{program}{layout} So long as there is a
  617. definition somewhere in the input file for every macro call, FunnelWeb
  618. will sort it all out.
  619. X
  620. In fact, FunnelWeb's macro facility is very simple.\xx{macro}{bindings}
  621. Unlike many macro preprocessors which allow macros to define other macros,
  622. XFunnelWeb completely
  623. finishes parsing and analysing the macros in the input file before it
  624. starts expanding them into product files. Other preprocessors allow
  625. macros to
  626. be redefined like variables (as in, say, \TeX{}\x{TeX}) taking on
  627. many different values as the macro pre-processor travels through the input
  628. file. In contrast,
  629. XFunnelWeb has no concept of \dq{different times} and
  630. treats the input as one huge static orderless, timeless,
  631. collection of definitions.
  632. In FunnelWeb, there is only ever one time, and so there can only ever be
  633. one value/definition for each macro.
  634. X
  635. X
  636. X\subsection{Number of Times Called}
  637. X\x{number of times called}\xx{calls}{number}\xx{invocation}{number}
  638. X
  639. So far we have seen only tiny, degenerate input files. The next example
  640. moves up to the level of \dq{trivial}, but starts to convey the flavour of
  641. the way FunnelWeb can be used in practice. Normally, there would be
  642. documentation text appearing between the macros, but this has been omitted
  643. so as to keep the focus on the macros themselves. Although the next
  644. example is
  645. much longer than the previous example, the only new construct is \p{@-}
  646. which can appear only at the end of a line,
  647. and suppresses it,\xx{EOL}{suppression}
  648. preventing it from appearing in the text. The \p{@-}\x{@-} construct allows
  649. the text of a macro to be aligned at the left margin, rather than
  650. having the first line hanging at the end of the
  651. X\p{@\{}. FunnelWeb could have been set up so that this end of
  652. line marker was suppressed. However, it would have been a special case that
  653. would have broken the very memorable rule \dq{the text of a macro is the
  654. text appearing between the \p{@\{} and \p{@\}}}.
  655. X
  656. Type the following text into the file \p{hello.fw}
  657. and run it through FunnelWeb. The file contains some intentional errors so
  658. be sure to type it in exactly and worry only if FunnelWeb \i{doesn't}
  659. generate some errors.
  660. X
  661. X\begin{verbatim}
  662. X@O@<hello.c@>==@{@-
  663. X@<Include Files@>
  664. X@<Include Files@>
  665. X@<Main Program@>
  666. X@}
  667. X
  668. X@$@<Main Program@>==@{@-
  669. main()
  670. X{
  671. X doit();
  672. X}
  673. X@}
  674. X
  675. X@$@<Subroutine@>==@{@-
  676. void doit()
  677. X{
  678. X int i;
  679. X for (i=0;i<10;i++)
  680. X   {
  681. X    @<Print@>
  682. X    @<Print@>
  683. X   }
  684. X}@}
  685. X
  686. X@$@<Print@>==@{@-
  687. printf("Hello World!");
  688. printf("\n");@}
  689. X
  690. X@$@<Scan@>==@{scanf@}
  691. X
  692. X@$@<Include Files@>==@{@-
  693. X#include <stdio.h>
  694. X#include <stdlib.h>@}
  695. X\end{verbatim}
  696. X
  697. What happened? Well, if you haven't typed the file in properly, you may
  698. get some miscellaneous syntax errors. Fix these before continuing.
  699. If the file has been correctly typed, you should be faced with some error
  700. messages to do with the number of times some of the macros are called.
  701. X
  702. By default, FunnelWeb insists that each macro defined is invoked
  703. exactly once. However, the file above defines macros that are used
  704. more than once and a macro that is not used at all. Let us examine the
  705. errors.
  706. X
  707. XFirst, we see that FunnelWeb has alerted us to the fact that
  708. the \p{Include Files} macro has been called twice. Once alerted to this,
  709. a quick look at the program convinces us that calling the macro twice is
  710. a mistake, and that one of the calls should be eliminated.
  711. X
  712. Second, we note that FunnelWeb has alerted us to the fact that the
  713. X\p{@<subroutine@>}
  714. macro is never called. Again, a quick look at the program tells us that this
  715. is a mistake (and a very common one in the use of FunnelWeb), and that
  716. a call to the \p{@<subroutine@>} macro should be inserted just above
  717. the call to the \p{@<Main Program@>} macro
  718. in the definition of \p{@<hello.c@>}.
  719. X
  720. These two cases demonstrate why these checks have been placed in
  721. XFunnelWeb. It is nearly always acceptable for a macro to be called once.
  722. However, if a macro is not called at all, or called more than once,
  723. this is often a sign
  724. that the user has made a mistake.
  725. X
  726. These checks have a dark side too. In addition to the
  727. errors mentioned above, FunnelWeb has generated two similar
  728. errors that do not help us.
  729. X
  730. XFirst, we are alerted to the fact that the
  731. X\p{@<print@>} macro has been called twice. Clearly, in this case, this is not
  732. a problem, and so here FunnelWeb's fussiness is a nuisance.
  733. X
  734. Second, we are alerted to the fact that the \p{@<scan@>} macro has never
  735. been called. Like the \p{@<print@>} macro,
  736. this macro was defined as a notational
  737. convenience, and clearly it does not matter here if it is not used. Again,
  738. XFunnelWeb is being a nuisance.
  739. X
  740. The four cases above demonstrate the light and dark side of
  741. XFunnelWeb's insistence that each macro be called exactly once. To resolve
  742. the conflict without reducing the strength of the checking, FunnelWeb
  743. provides two special sequences \p{@Z} (for \b{Z}ero) and \p{@M} (for \b{M}any)
  744. that can be attached to macro definitions. Presence of the
  745. X\p{@Z}\xx{@Z}{tutorial}
  746. tag allows the designated macro to be called zero times.
  747. Presence of the
  748. X\p{@M}\xx{@M}{tutorial}
  749. tag allows the designated macro to be called more than once.
  750. A single macro may carry both tags.
  751. It is always true that all macros are allowed to be called exactly once.
  752. X
  753. Here is the revised program with the errors fixed,
  754. by eliminating or adding macro calls, or by adding tags.
  755. Try processing the file now. There should be no errors.
  756. X
  757. X\begin{verbatim}
  758. X@O@<hello.c@>==@{@-
  759. X@<Include Files@>
  760. X@<Function@>
  761. X@<Main Program@>
  762. X@}
  763. X
  764. X@$@<Main Program@>==@{@-
  765. main()
  766. X{
  767. X doit();
  768. X}
  769. X@}
  770. X
  771. X@$@<Function@>==@{@-
  772. void doit()
  773. X{
  774. X int i;
  775. X for (i=0;i<10;i++)
  776. X   {
  777. X    @<Print@>
  778. X    @<Print@>
  779. X   }
  780. X}@}
  781. X
  782. X@$@<Print@>@M==@{@-
  783. printf("Hello World!");
  784. printf("\n");@}
  785. X
  786. X@$@<Scan@>@Z==@{scanf@}
  787. X
  788. X@$@<Include Files@>==@{@-
  789. X#include <stdio.h>
  790. X#include <stdlib.h>@}
  791. X\end{verbatim}
  792. X
  793. X\subsection{Indentation}
  794. X\xx{indentation}{macro calls}
  795. X
  796. The body of the \p{print} macro of the previous example
  797. contains two lines of text.
  798. A literal substitution of this macro's body in its context would result in:
  799. X
  800. X\begin{verbatim}
  801. X   {
  802. X    printf("Hello World!");
  803. printf("\n");
  804. X    printf("Hello World!");
  805. printf("\n");
  806. X   }
  807. X\end{verbatim}
  808. X
  809. But instead, it comes out as (have a look at this part of \p{hello.c} now):
  810. X
  811. X\begin{verbatim}
  812. X   {
  813. X    printf("Hello World!");
  814. X    printf("\n");
  815. X    printf("Hello World!");
  816. X    printf("\n");
  817. X   }
  818. X\end{verbatim}
  819. X
  820. The explanation is that FunnelWeb indents each line of multiline macros
  821. by the level of indentation at the point of call.
  822. This means that, as in the case above, program texts, which are usually
  823. highly indented, come out looking \dq{right}.
  824. X
  825. In other circumstances, where the model of the text is one dimensional,
  826. XFunnelWeb's indentation could become an impediment or even a danger.
  827. In these cases, it
  828. can be switched off by including the FunnelWeb \newterm{pragma} line
  829. X
  830. X\begin{verbatim}
  831. X@p indentation = none
  832. X\end{verbatim}
  833. X
  834. anywhere in the input file.
  835. X
  836. One of the design goals of FunnelWeb is to allow the user total control over
  837. the product files. This contrasts with the approach of Knuth's WEB\x{WEB}
  838. system
  839. X\paper{Knuth83} (upon which FunnelWeb is based), which mangles the input
  840. text at the Pascal\x{Pascal} program syntax level, truncating identifiers,
  841. converting
  842. the text to upper case, and paragraphing text. Here is an example of part of
  843. a Pascal program produced by WEB\xx{output}{WEB}
  844. X(from page~14 of \paper{Knuth83}):
  845. X
  846. X\begin{verbatim}
  847. IF R=0 THEN XREF[P]:=XREFPTR ELSE XMEM[R].XLINKFIELD:=XREFPTR;END;{:51}
  848. X{58:}FUNCTION IDLOOKUP(T:EIGHTBITS):NAMEPOINTER;LABEL 31;
  849. VAR I:0..LONGBUFSIZE;H:0..HASHSIZE;K:0..MAXBYTES;W:0..1;
  850. L:0..LONGBUFSIZE;P:NAMEPOINTER;BEGIN L:=IDLOC-IDFIRST;{59:}
  851. H:=BUFFER[IDFIRST];I=IDFIRST+1;
  852. WHILE I<IDLOC DO BEGIN H:=(H+H+BUFFER[I])MOD HASHSIZE;I=I+1;END{:59};
  853. X\end{verbatim}\checked{I can't believe I actually typed this mess in.}
  854. X
  855. Knuth's theory is that the program generated by a literate programming
  856. system should be
  857. treated as object code and hence should look like object code
  858. too.\x{object code} While this
  859. may be an admirable approach in the long run, the present programming
  860. environment is one of faulty compilers and buggy tools.
  861. The FunnelWeb view is that, in this environment,
  862. the programmer needs all the help he can get
  863. and that therefore he should be allowed total control over
  864. the product file. Another reason for FunnelWeb's providing total
  865. control over the product file, is that FunnelWeb is intended to be
  866. target language independent, and so even if Knuth's view were adopted, it
  867. would not be clear what a legitimate transformation of the text could be.
  868. X
  869. X\subsection{Additive Macros}
  870. X\xx{additive}{macros}
  871. X
  872. Sometimes it is convenient to build up the definition of a macro in
  873. stages throughout the input file. In FunnelWeb, this can be done using
  874. an \newterm{additive macro}.
  875. An additive macro is identical to an ordinary macro except
  876. that\xx{tutorial}{==}\xx{tutorial}{+=}
  877. X
  878. X\begin{enumerate}
  879. X\item It has \p{+=} instead of \p{==}.
  880. X\item It can be defined in one or more parts throughout the input file.
  881. The definition of the macro is the concatenation of all the parts in the
  882. order in which they appear.
  883. X\end{enumerate}
  884. X
  885. The following example shows how additive macros
  886. can be used to scatter and regroup information, in this
  887. case assisting in the lucid construction of a
  888. data abstraction\xx{data}{abstraction} in a language (Pascal\x{Pascal})
  889. that does not support them explicitly.
  890. X
  891. X\begin{verbatim}
  892. X@!******************************
  893. X
  894. X@O@<prog.pas@>==@{@-
  895. program adt(input,output);
  896. X@<Types@>
  897. X@<Variables@>
  898. X@<Procedures@>
  899. begin startproc; end.
  900. X@}
  901. X
  902. X@!******************************
  903. X
  904. X@$@<Types@>+=@{@-
  905. type buffer_type =
  906. X   record
  907. X   length : integer;
  908. X   buf : array[1..100] of char;
  909. X   end;
  910. X@}
  911. X
  912. X@$@<Variables@>+=@{@-
  913. bigbuf : buffer_type;
  914. X@}
  915. X
  916. X@$@<Procedures@>+=@{@-
  917. procedure buf_init (var b : buffer_type               ) {Body of buf_init}
  918. procedure buf_add  (var b : buffer_type;     ch : char) {Body of buf_add}
  919. procedure buf_get  (var b : buffer_type; var ch : char) {Body of buf_get}
  920. X@}
  921. X
  922. X@!******************************
  923. X
  924. X@$@<Types@>+=@{@-
  925. type complex_type = record r,i : real; end;
  926. X@}
  927. X
  928. X@$@<Procedures@>+=@{@-
  929. procedure cm_set (var c: complex_type; a,b: real)         {Body of cm_set}
  930. procedure cm_add (a,b: complex_type; var c: complex_type) {Body of cm_add}
  931. X{Other procedures and functions}
  932. X@}
  933. X
  934. X@!******************************
  935. X
  936. X{...more pieces of program...}
  937. X
  938. X@!******************************
  939. X\end{verbatim}
  940. X
  941. It is important to remember that the definition of each macro does not change
  942. throughout the input file. FunnelWeb parses the entire input file and
  943. assembles all the macro definitions before it even starts to expand macros.
  944. As a result, each additive macro can only have one definition, and that
  945. definition is the concatenation of all its parts.
  946. X
  947. The example above shows how additive macros can be used to rearrange the
  948. presentation of a computer program in the order in which the user wishes
  949. to discuss it rather than the order in which the compiler requires that it
  950. be consumed. It is easy, however, to abuse the feature of additive macros.
  951. In many cases, the same effect can be obtained more clearly by replacing
  952. each part of an additive macro in-situ using uniquely named non-additive
  953. macros, and then collect them together as a group at the point where the
  954. additive macro is called. Doing this is more work, and is more error
  955. prone, but can result in a clearer exposition.
  956. The following program illustrates this alternative approach.
  957. X
  958. X\begin{verbatim}
  959. X@!******************************
  960. X
  961. X@O@<prog.pas@>==@{@-
  962. program adt(input,output);
  963. X@<Types@>
  964. X@<Variables@>
  965. X@<Procedures@>
  966. begin startproc; end.
  967. X@}
  968. X
  969. X@$@<Types@>==@{@-
  970. X@<Buffer type@>
  971. X@<Complex type@>
  972. X@}
  973. X
  974. X@$@<Variables@>==@{@-
  975. X@<Buffer variable@>
  976. X@}
  977. X
  978. X@$@<Procedures@>==@{@-
  979. X@<Buffer procedures@>
  980. X@<Complex procedures@>
  981. X@}
  982. X
  983. X@!******************************
  984. X
  985. X@$@<Buffer type@>==@{@-
  986. type buffer_type = record
  987. X                   length : integer;
  988. X                   buf : array[1..100] of char;
  989. X                   end;
  990. X@}
  991. X
  992. X@$@<Buffer variable@>==@{@-
  993. bigbuf : buffer_type;
  994. X@}
  995. X
  996. X@$@<Buffer procedures@>==@{@-
  997. procedure buf_init(var b : buffer_type) {Body of buf_init}
  998. procedure buf_add(var b : buffer_type; ch : char) {Body of buf_add}
  999. procedure buf_get(var b : buffer_type; var ch : char) {Body of buf_get}
  1000. X@}
  1001. X
  1002. X@!******************************
  1003. X
  1004. X@$@<Complex type@>==@{@-
  1005. type complex_type = record r,i : real; end;
  1006. X@}
  1007. X
  1008. X@$@<Complex procedures@>+=@{@-
  1009. procedure cm_set(var c: complex_type; a,b : real)  {Body of cm_set}
  1010. procedure cm_add(a,b : complex_type; var c: complex_type) {Body of cm_add}
  1011. X{Other procedures and functions}
  1012. X@}
  1013. X
  1014. X@!******************************
  1015. X
  1016. X{...more pieces of program...}
  1017. X
  1018. X@!******************************
  1019. X\end{verbatim}
  1020. X
  1021. One of advantages of FunnelWeb (and literate programming in general) is that
  1022. X(as shown above) it allows the user to lay out the program in whatever order is
  1023. desired\xx{program}{layout}
  1024. with near total independence from the ordering requirements of the
  1025. target programming language.
  1026. X
  1027. Additive macros are allowed to be tagged with \p{@Z} and \p{@M} just as other
  1028. macros can, but the tags must appear only on the first definition of the
  1029. macro. Additive macros cannot be connected directly to product files.
  1030. X
  1031. X\subsection{Parameterized Macros}
  1032. X\xx{parameterized}{macros}
  1033. X
  1034. No self-respecting macro preprocessor would be complete without some form
  1035. of macro parameterization, and FunnelWeb is no exception. FunnelWeb allows
  1036. each macro to have from zero to nine formal parameters\xx{formal}{parameters}
  1037. named \p{@1}\x{@1...}, \p{@2},
  1038. X\p{@3}, \p{@4}, \p{@5}, \p{@6}, \p{@7}, \p{@8}, and \p{@9}.
  1039. X
  1040. To define a macro with one or more parameters, insert a formal parameter
  1041. list\xx{formal}{parameters}
  1042. just after the macro name in the macro definition. Because macro
  1043. parameters have fixed names (\p{@1}$\ldots$\p{@9}), there is no need to
  1044. specify the names of formal parameters in the formal parameter list.
  1045. All that need be conveyed is how many parameters the macro has.
  1046. Here is an example of the definition of a macro having three parameters:
  1047. X
  1048. X\begin{verbatim}
  1049. X@$@<While loop@>@(@3@)@M==@{@-
  1050. X@1
  1051. while (@2)
  1052. X  {
  1053. X   @3
  1054. X  }
  1055. X@}
  1056. X\end{verbatim}
  1057. X
  1058. To call a parameterized macro, an actual
  1059. parameter\xx{actual}{parameters} list must be supplied
  1060. that contains exactly the same number of actual parameters as there are
  1061. formal parameters in the definition of the macro being called. An actual
  1062. parameter list is delimited by \p{@(}\x{@(}
  1063. and \p{@)},\x{@)} and parameters are
  1064. X\i{separated} by \dqp{@,}.\x{@,} The actual parameters themselves are general
  1065. XFunnelWeb expressions (see Chapter~\ref{chapdefinition}
  1066. for the exact syntax) and can
  1067. be inserted into the list directly or can be delimited by \p{@"}\x{@"} so as to
  1068. allow some white space to assist in formatting the actual parameters.
  1069. Here are some examples of calls of the \p{While loop} macro defined above.
  1070. X
  1071. X\begin{verbatim}
  1072. X@! First form of actual parameters without whitespace and double quotes.
  1073. X@<While loop@>@(x=1;@,x<=10@,printf("X=%u\n",x);@)
  1074. X
  1075. X@! Second form of actual parameters. The double quotes allow non-active
  1076. X@! whitespace that helps to lay out the actual parameters neatly.
  1077. X@! This call is functionally identical to the one above.
  1078. X@<While loop@>@(
  1079. X   @"x:=1;@" @,
  1080. X   @"x<=10@" @,
  1081. X   @"printf("X=%u\n",x);@" @)
  1082. X
  1083. X@! The two forms can be mixed in a single call.
  1084. X@<While loop@>@(x=1;@,x<=10@,
  1085. X              @"printf("X=%u\n",x);@" @)
  1086. X\end{verbatim}
  1087. X
  1088. A few rules about parameterized macros are worth mentioning.
  1089. Macros that do not have any parameters must have no formal or
  1090. actual parameter lists.\xs{parameter list}{absent}
  1091. Additive macros can have parameters, but the formal parameter list
  1092. must appear in the first definition part only.
  1093. X
  1094. Here is another example of the use of parameterized macros. This time,
  1095. parameters and macro calls are used in a FunnelWeb input file
  1096. that constructs an $O(n)$ representation
  1097. of a song\x{song}\x{twelve bugs of christmas} whose full size is
  1098. X$O(n^2)$ in the number $n$ of unique
  1099. lines.\x{rec.humor.funny}\xn{Pat}{Scannel}
  1100. X
  1101. X\begin{verbatim}
  1102. X
  1103. X@O@<Twelve_bugs.txt@>==@{@-
  1104. The Twelve Bugs of Christmas
  1105. X----------------------------
  1106. X@<Verse@>@(@"first@"    @,@<1@>@)
  1107. X@<Verse@>@(@"second@"   @,@<2@>@)
  1108. X@<Verse@>@(@"third@"    @,@<3@>@)
  1109. X@<Verse@>@(@"fourth@"   @,@<4@>@)
  1110. X@<Verse@>@(@"fifth@"    @,@<5@>@)
  1111. X@<Verse@>@(@"sixth@"    @,@<6@>@)
  1112. X@<Verse@>@(@"seventh@"  @,@<7@>@)
  1113. X@<Verse@>@(@"eighth@"   @,@<8@>@)
  1114. X@<Verse@>@(@"ninth@"    @,@<9@>@)
  1115. X@<Verse@>@(@"tenth@"    @,@<A@>@)
  1116. X@<Verse@>@(@"eleventh@" @,@<B@>@)
  1117. X@<Verse@>@(@"twelfth@"  @,@<C@>@)
  1118. X
  1119. This song appeared in the internet newsgroup rec.humor.funny on 24-Dec-1991.
  1120. It was contributed by Pat Scannell (scannell@@darkstar.ma30.bull.com).
  1121. X@}
  1122. X
  1123. X@$@<Verse@>@(@2@)@M==@{@-
  1124. XFor the @1 bug of Christmas, my manager said to me
  1125. X     @2
  1126. X@}
  1127. X
  1128. X@$@<1@>@M==@{See if they can do it again.@}
  1129. X@$@<2@>@M==@{Ask them how they did it and@+@<1@>@}
  1130. X@$@<3@>@M==@{Try to reproduce it@+@<2@>@}
  1131. X@$@<4@>@M==@{Run with the debugger@+@<3@>@}
  1132. X@$@<5@>@M==@{Ask for a dump@+@<4@>@}
  1133. X@$@<6@>@M==@{Reinstall the software@+@<5@>@}
  1134. X@$@<7@>@M==@{Say they need an upgrade@+@<6@>@}
  1135. X@$@<8@>@M==@{Find a way around it@+@<7@>@}
  1136. X@$@<9@>@M==@{Blame it on the hardware@+@<8@>@}
  1137. X@$@<A@>@M==@{Change the documentation@+@<9@>@}
  1138. X@$@<B@>@M==@{Say it's not supported@+@<A@>@}
  1139. X@$@<C@>@M==@{Tell them it's a feature@+@<B@>@}
  1140. X\end{verbatim}
  1141. X
  1142. X\subsection{Macro Expansion}
  1143. X\xx{macro}{expansion}
  1144. X
  1145. One of the strengths of FunnelWeb is that, when writing product files, it
  1146. does not attempt to evaluate any text expression (\eg{}text block, parameter,
  1147. macro call) in memory\xx{memory}{use of}
  1148. and then write the result out. Instead, it always
  1149. writes out what it is expanding dynamically and directly. This means that
  1150. the user need not fear defining macros that expand to huge amounts of text
  1151. and then calling those macros in other macros, or passing those huge macros
  1152. as parameters to other macros. In all cases, FunnelWeb expands directly to
  1153. the product file, and there can be no danger in running out of memory during
  1154. expansion (except for running out of stack space and other
  1155. marginally used resources in pathological cases).
  1156. X
  1157. The only thing to remember in this regard is that FunnelWeb always stores
  1158. the entire \i{input} file and all included files, in their entirety in
  1159. memory, for the duration of the run.
  1160. X
  1161. Here is an example, that illustrates how robust FunnelWeb is:
  1162. X
  1163. X\begin{verbatim}
  1164. X@! FunnelWeb copes well with the following macro definitions.
  1165. X@! (Providing that it has a little over ten megabytes of memory).
  1166. X
  1167. X@O@<woppa.txt@>==@{@<Quote@>@(@<Humungeous@>@)@+@}
  1168. X
  1169. X@$@<Quote@>@(@1@)==@{"@1"@}
  1170. X
  1171. X@$@<Humungeous@>==@{@-
  1172. X...Ten Megabytes of Text...
  1173. X@}
  1174. X\end{verbatim}
  1175. X
  1176. X\subsection{Include Files}
  1177. X\xx{include}{files}
  1178. X
  1179. XFunnelWeb provides a nested include file facility that can be used for a
  1180. number of purposes. When FunnelWeb runs into a single line containing the
  1181. special sequence \p{@i}\x{@i} followed by a blank, followed by a file name,
  1182. it reads in the designated
  1183. file and replaces the line containing the command (including the end of
  1184. line marker at the end of the line) with the entire contents
  1185. of the designated file. For example, if there was a file called
  1186. X\p{camera.txt} containing the two lines:\xx{poem}{camera}\xx{animal}{poem}
  1187. X
  1188. X\begin{verbatim}
  1189. X'Cos I shoot with a camera instead of a gun.
  1190. The animals flock to be petted and fed,
  1191. X\end{verbatim}
  1192. X
  1193. and another file called \p{poem.fw}
  1194. containing the following four lines\x{shooting}
  1195. X
  1196. X\begin{verbatim}
  1197. I like to go shooting, it's a whole lot of fun,
  1198. X@i camera.txt
  1199. Cos they know my camera isn't loaded with lead.
  1200. X- RNW, 04-Jan-1991.
  1201. X\end{verbatim}
  1202. X
  1203. Then, if FunnelWeb were to process \p{poem.fw}, the result would be
  1204. as if FunnelWeb had read in:
  1205. X
  1206. X\begin{verbatim}
  1207. I like to go shooting, it's a whole lot of fun,
  1208. X'Cos I shoot with a camera instead of a gun.
  1209. The animals flock to be petted and fed,
  1210. X'Cos they know my camera isn't loaded with lead.
  1211. X- RNW, 04-Jan-1991.
  1212. X\end{verbatim}
  1213. X
  1214. XFunnelWeb expands include files before it starts scanning and parsing the
  1215. included text. The result is that include files can contain anything that
  1216. can be found in a FunnelWeb file. The following example illustrates
  1217. the level at which the include mechanism operates. If \p{main.fw} contains
  1218. X
  1219. X\begin{verbatim}
  1220. X@O@<output.dat@>==@{@-
  1221. X@i inc.fw
  1222. This is the text of the sloth macro.
  1223. X@}
  1224. X\end{verbatim}
  1225. X
  1226. and inc.fw contains
  1227. X
  1228. X\begin{verbatim}
  1229. X@<Sloth@>
  1230. X@}
  1231. X
  1232. X@$@<Sloth@>==@{@-
  1233. X\end{verbatim}
  1234. X
  1235. Then if FunnelWeb were applied to \p{main.fw}, it would see:
  1236. X
  1237. X\begin{verbatim}
  1238. X@O@<output.dat@>==@{@-
  1239. X@<Sloth@>
  1240. X@}
  1241. X
  1242. X@$@<Sloth@>==@{@-
  1243. This is the text of the sloth macro.
  1244. X@}
  1245. X\end{verbatim}
  1246. X
  1247. which it would process in the normal manner. The only special
  1248. sequence processing that takes place at a level lower
  1249. than include files is the processing of the \p{<special>=<newspecial>}
  1250. sequence which changes the special character.\xx{special character}{changing}
  1251. X
  1252. A few other facts about include files are worth mentioning here. Include
  1253. files inherit the directory specification supplied using the \p{+I} command
  1254. line option. The special character is saved at the start of each include
  1255. file and restored to its previous value at the end of each include file.
  1256. Include files can be nested up to ten levels.
  1257. Recursive included files\xx{include files}{recursive}
  1258. will always cause an infinite recursion as there
  1259. is no bottoming out mechanism available.
  1260. Include files
  1261. must contain an integer number of lines (\ie{}the last line must be
  1262. terminated with an end of line marker). Once FunnelWeb has seen
  1263. X\dqp{@i\ } at the start of a line, it will grab the rest of the
  1264. line raw and treat it as a file name. There is no place on the 
  1265. line for things like FunnelWeb comments (see later) or extraneous text.
  1266. X
  1267. Include files can be used for many purposes, but are particularly useful for
  1268. hauling in macro libraries.\xx{macro}{libraries}
  1269. X
  1270. X\section{Typesetting Facilities}
  1271. X\xx{tutorial}{typesetting}
  1272. X
  1273. The first half of this tutorial focuses solely on the macro facilities
  1274. of FunnelWeb (which it more or less covers completely). As a result,
  1275. the example documents you have seen so far have been gross
  1276. distortions of \dq{normal} FunnelWeb documents which often contain as much
  1277. documentation as
  1278. code.\x{documentation vs code}\x{code vs documentation}\footnote{As
  1279. an example, the author used
  1280. XFunnelWeb to develop a largish computer program and found that on average
  1281. his style of using FunnelWeb resulted in about 30\% documentation and
  1282. X70\% macros (code) (measured by numbers of lines).}
  1283. While there are applications where FunnelWeb can be used solely
  1284. as a macro preprocessor, most applications will use its
  1285. typesetting facilities as well.
  1286. X
  1287. This section restores the balance in this tutorial
  1288. by presenting FunnelWeb's typesetting facilities.
  1289. X
  1290. X\subsection{Overview}
  1291. X\xx{overview}{typesetting}
  1292. X
  1293. The macro definitions discussed in the macro tutorial completely define
  1294. the contents of the product files that FunnelWeb will generate. These macro
  1295. definitions can be arranged in any order and nothing external to them
  1296. can affect the contents of the product files. The macros
  1297. can be thought of as a group of self-contained islands.
  1298. X
  1299. Although FunnelWeb will can process the macros all on their own,
  1300. the full power of FunnelWeb
  1301. is realized only when the macros are surrounded by a sea of documentation.
  1302. This sea can take two forms: directives and free text.
  1303. Some of the directives control things such as the maximum input line
  1304. length. However, most of them are typesetting directives that affect
  1305. the printed documentation.
  1306. Thus a FunnelWeb document can be viewed as a sequence
  1307. of \newterm{macro definitions},
  1308. X\newterm{directives}, and \newterm{free text}.
  1309. X
  1310. Unlike the product files which consist of unscrambled macro calls,
  1311. the documentation file is more or less a
  1312. direct representation of the input file. Each part of the input file
  1313. appears in the documentation file in the order in which it appears in the
  1314. input file. However, each different kind of part is
  1315. typeset\footnote{Here the term \dq{typeset} is used loosely to refer to
  1316. XFunnelWeb's generation of typesetter commands for each construct in the
  1317. input file. Strictly, the term should be used only to describe the actions
  1318. of a typesetter program (\eg{}\TeX{}).} in a different
  1319. manner. Macros are typeset in a particular style, with the macro body
  1320. appearing in \p{tt font} (see some FunnelWeb printed documentation for
  1321. an example). Typesetter directives have specific defined
  1322. effects (more later).
  1323. XFree text is typeset exactly as it is, except that each block of text
  1324. between blank lines is filled and justified as a paragraph.
  1325. X
  1326. The following example demonstrates how all this works. Type in the following
  1327. as \p{example.fw} and run it through FunnelWeb with the
  1328. command \dqp{fw~example~+t}. The \dqp{+t} instructs FunnelWeb to generate
  1329. a documentation file called \p{example.tex}.
  1330. Run the file through \TeX{} and print it.
  1331. XExamine the files \p{example.out} and \p{example.tex}.
  1332. X
  1333. X\begin{verbatim}
  1334. You are reading some free text before the macro. Free text can consist
  1335. of any text (not containing the FunnelWeb special character) including
  1336. typesetter commands
  1337. such as $, %, #, and \TeX{} which
  1338. will be typeset to appear exactly as they do in the input file!
  1339. Look out! Here comes a macro!
  1340. X
  1341. X@O@<example.out@>==@{@-
  1342. This text is part of
  1343. a macro definition.
  1344. X@}
  1345. X
  1346. This is free text following the macro. This sentence contains
  1347. two @{inline@} typesetter @/directives@/.
  1348. Now here is a non-inline typesetting directive.
  1349. X
  1350. X@t new_page
  1351. X
  1352. This sentence will appear on the next page.
  1353. X\end{verbatim}
  1354. X
  1355. At the top of the \p{example.tex} documentation file will be a set of \TeX{}
  1356. macro definitions. The \TeX{} code corresponding to the input above
  1357. appears at the end of the file. It should look something like this.
  1358. X
  1359. X\begin{verbatim}
  1360. You are reading some free text before the macro. Free text can consist
  1361. of any text (not containing the FunnelWeb special character) including
  1362. typesetter commands
  1363. such as \$, \%, \#, and $\backslash$TeX$\{$$\}$ which
  1364. will be typeset to appear exactly as they do in the input file!
  1365. Look out! Here comes a macro!
  1366. X
  1367. X\fwbeginmacro
  1368. X\fwfilename{example.out}{1}\fwequals \fwodef \fwbtx[This text is part of
  1369. a macro definition.
  1370. X]fwetx=%
  1371. X\fwcdef 
  1372. X\fwbeginmacronotes
  1373. X\fwisafile{This macro is attached to an output file.}
  1374. X\fwendmacronotes
  1375. X\fwendmacro
  1376. X
  1377. This is free text following the macro. This sentence contains
  1378. two \fwlit{inline} typesetter \fwemp{directives}.
  1379. Now here is a non-inline typesetting directive.
  1380. X
  1381. X\fwnewpage
  1382. X
  1383. This sentence will appear on the next page.
  1384. X\end{verbatim}
  1385. X
  1386. The following points explain the \p{example.tex} file.
  1387. X
  1388. X\narrowthing{You don't have to know TeX:}{If you don't know
  1389. X\TeX{}, don't pay too much
  1390. attention to this section. You don't need to know \TeX{} to use FunnelWeb.}
  1391. X
  1392. X\narrowthing{In order:}{FunnelWeb has merely transformed the
  1393. input. It hasn't rearranged it.}
  1394. X
  1395. X\narrowthing{Free text:}{Most of the free text has been simply copied over.
  1396. The \TeX{} typesetter justifies and fills all paragraphs fed to it by
  1397. default, so most of the text has just been copied verbatim.}
  1398. X
  1399. X\narrowthing{TeX codes:}{The characters and sequences which
  1400. X\TeX{} treats as special have been neutralized in the documentation
  1401. file. For example, \dqp{\$} has become \dqp{\bs\$}. By
  1402. default, FunnelWeb allows the user to write any text as free text and not
  1403. have to worry about accidentally invoking typesetter features.}
  1404. X
  1405. X\narrowthing{fw sequences:}{The \p{fw} sequences (\eg{}\p{\bs{}fwbeginmacro})
  1406. invoke \TeX{} macros defined
  1407. earlier in the documentation file (and not shown here).}
  1408. X
  1409. X\narrowthing{The macro:}{The macro is typeset using a set of predefined
  1410. X\TeX{} macros. See the printed documentation to see what this looks like
  1411. on paper.}
  1412. X
  1413. X\narrowthing{Typesetter directives:}{Unlike the \TeX{}
  1414. command sequences (which
  1415. were neutralized), the FunnelWeb typesetter directives turn into \TeX{} macro
  1416. calls. For example, \dqp{@\{inline@\}} became \dqp{\bs{}fwlit\{inline\}}.}
  1417. X
  1418. In summary, FunnelWeb produces typeset documentation that transforms,
  1419. but does not reorder, the input file. Macros are typeset in a specific
  1420. style. FunnelWeb typesetter directives have particular well-defined effects.
  1421. XFree text is filled and justified, but will otherwise appear in the
  1422. printed documentation exactly as it appears in the input file.
  1423. X
  1424. X\subsection{Typesetter Independence}
  1425. X\xx{typesetter}{independence}
  1426. X
  1427. Although the current version of FunnelWeb can only generate documentation
  1428. files in \TeX{} form,
  1429. it encourages typesetter independence
  1430. by neutralizing all \TeX{} control sequences before writing them out. The
  1431. result is that you don't have worry about upsetting or depending on \TeX{}
  1432. by accidentally including some special character or sequence. By default your
  1433. input file is \newterm{typesetter independent}.
  1434. X
  1435. This scheme differs from other literate programming tools, including
  1436. all earlier versions of FunnelWeb, which copy their free text  directly
  1437. to the documentation file, the justification being that the programmer
  1438. can use the full power of the typesetter language to describe the program.
  1439. The disadvantages of doing this are first that the
  1440. programmer is required to know the typesetting language and second that
  1441. the input file becomes typesetter dependent. FunnelWeb avoids these problems
  1442. by knobbling the free text be default.
  1443. X
  1444. However, FunnelWeb does provide a trapdoor for those who want their
  1445. free text to be fed directly to \TeX{}. To open the trapdoor,
  1446. simply include the following pragma somewhere in your input file.
  1447. X
  1448. X\begin{verbatim}
  1449. X@p typesetter = tex
  1450. X\end{verbatim}
  1451. X
  1452. See Section~\ref{typesetterpragma} for more information.
  1453. X
  1454. XFunnelWeb leaves the degree to which
  1455. the user wishes to bind a particular document to a particular typesetter
  1456. up to the user. In some cases, the extra typesetting power may compensate
  1457. for the lack of portability. However, as a rule, it is best to
  1458. avoid typesetter-specific commands, so as to allow your input files to
  1459. be formatted at a later date for different typesetters.
  1460. XFunnelWeb includes a number of its own typesetter commands so as to
  1461. support typesetter-independent input files.
  1462. The following sections describe some of these commands. In particular,
  1463. the next section describes the most powerful FunnelWeb typesetting directives
  1464. which allow the user to structure the document hierarchically.
  1465. X
  1466. X\subsection{Hierarchical Structure}
  1467. X\xx{hierarchical}{structure}
  1468. X
  1469. The tree structure is one of the most effective structuring tools that
  1470. exists, deriving its power from the principal of divide and conquor.
  1471. So effective is it that the internal organization of
  1472. most technical books are tree
  1473. structures which are concisely summarized in the table of contents.
  1474. In contrast, computer programs
  1475. are usually presented as flat sequences of text to be consumed by an
  1476. anonymous compiler.
  1477. X
  1478. In order to bring program documentation up to the structural
  1479. sophistication commonplace in technical books, FunnelWeb provides five levels
  1480. of section headings\xx{section}{headings}
  1481. implemented by the five special sequences \p{@A},\x{@A...} \p{@B},
  1482. X\p{@C}, \p{@D}, and \p{@E}. These must always appear at the start of a line.
  1483. X\p{@A} is the highest level section
  1484. X(\eg{}like \LaTeX{}'s \p{\bs{}chapter}) and \p{@E} is the lowest level
  1485. section (\eg{}like \LaTeX{}'s \p{\bs{}subsubsubsection}).
  1486. Section headings can appear anywhere in
  1487. the free text of a FunnelWeb input file (\ie{}anywhere except inside a
  1488. macro definition).
  1489. X
  1490. XEach section heading\xx{name}{section} in a FunnelWeb document
  1491. has an associated name.
  1492. The name of a section can be provided explicitly by supplying it
  1493. delimited by \p{@<} and \p{@>} immediately after the section sequence
  1494. X(\eg{}\p{@A}), or implicitly by not providing an explicit name, in which
  1495. case the section takes the name of the first macro defined between the
  1496. section header in question and the following section header. An error is
  1497. generated if a section has not been given an explicit name and does not
  1498. contain any macro definitions. Here are some example headings:
  1499. X
  1500. X\begin{verbatim}
  1501. X@A@<Feed the Penguins and Save the World@>
  1502. X@B@<Feed the Penguins@>
  1503. X@C@<Feed the little penguins@>
  1504. X@C@<Feed the big penguins@>
  1505. X@B@<Save the World@>
  1506. X@C@<Save Europe@>
  1507. X@C@<Save Africa@>
  1508. X
  1509. X@C This heading hasn't been given an explicit name, but will inherit the
  1510. name \p{Save the rest of the world} from the macro definition below.
  1511. X
  1512. X@$@<Save the rest of the world@>@Z==@{...@}
  1513. X\end{verbatim}
  1514. X
  1515. The feature of having unnamed sections inherit the name of the first
  1516. macro defined within their scope is present because a common style of
  1517. writing in FunnelWeb is to have one section per macro definition. Because,
  1518. under this style,
  1519. each section describes a single macro, it usually turns out that the
  1520. macro name makes a good name for the section too. The inheritance mechanism
  1521. prevents duplication of the name.\xx{section name}{inheritance}
  1522. X
  1523. Apart from the requirement that each section have an explicit or implicit
  1524. name and that its special sequence appear at the start of a line, the only
  1525. other restriction on section headings is that a section heading at level
  1526. X$n$ cannot appear immediately after a section heading at level $n-1$ or less.
  1527. In other words, the hierarchy cannot be broken. For
  1528. example, an \p{@C} cannot appear after an \p{@A} heading unless there
  1529. is an intervening \p{@B} heading.
  1530. X
  1531. X\begin{verbatim}
  1532. X@A@<The Top Heading@>
  1533. X@C@<Level C here is not allowed after an A and will cause an error@>
  1534. X\end{verbatim}
  1535. X
  1536. This rule extends to the start of the
  1537. file; if there are any headings at all, the first one must be an \p{@A}
  1538. heading. The following file, while short, is in error.
  1539. X
  1540. X\begin{verbatim}
  1541. This FunnelWeb input file is in error because its first section heading
  1542. is at level C rather than level A.
  1543. X@C@<2@>
  1544. X\end{verbatim}
  1545. X
  1546. X\subsection{Understanding the Printed Documentation}
  1547. X
  1548. Type in the following file, and use FunnelWeb and \TeX{} to generate the
  1549. corresponding printed
  1550. documentation.\xx{programmer's}{cheer}\xx{hacker's}{cheer}
  1551. X\xx{hacker's}{dictionary}
  1552. X
  1553. X\begin{verbatim}
  1554. X@A@<Table of Contents@>
  1555. X
  1556. X@t table_of_contents
  1557. X
  1558. X@A@<Macros for Moral Support@>
  1559. X
  1560. The following macro contain comments that provide moral support in the
  1561. output code.
  1562. X
  1563. X@$@<Programmer's Cheer@>@M==@{
  1564. X-- Shift to the left!
  1565. X-- Shift to the right!
  1566. X-- Pop up, push down!
  1567. X-- Byte! Byte! Byte!
  1568. X-- (From "The New Hacker's Dictionary").
  1569. X@}
  1570. X
  1571. The next macro is similar but is distributed throughout the program.
  1572. X@$@<Hacker's Cheer@>+=@{@+-- Pointer to the left@+@}
  1573. X
  1574. X@A@<An Extremely Imperative Stack Abstraction@>
  1575. X
  1576. X@B@<Define the Stack@>
  1577. X@$@<Hacker's Cheer@>+=@{-- Pointer to the right@+@}
  1578. X@$@<Stack Type@>@Z==@{type stack = record ... end;@}
  1579. X
  1580. X@B@<Push the Stack@>
  1581. X@$@<Hacker's Cheer@>+=@{-- Hack that code@+@}
  1582. X@$@<Push Procedure@>@Z==@{@-
  1583. procedure push(var b:stack; v:value); @<Programmer's Cheer@> {...}@}
  1584. X
  1585. X@B@<Pop the Stack@>
  1586. X@$@<Hacker's Cheer@>+=@{-- Tight! Tight! Tight!@+@}
  1587. X@$@<Pop Procedure@>@Z==@{@-
  1588. procedure pop(var b:stack); @<Programmer's Cheer@> {...}@}
  1589. X
  1590. X@B@<Rough the Stack Up a Bit@>
  1591. X@$@<Hacker's Cheer@>+=@{-- (RNW, 04-Jan-1991).@+@}
  1592. X@$@<Rough Procedure@>@Z==@{@-
  1593. procedure rough(var b:stack); @<Hacker's Cheer@> {...}@}
  1594. X
  1595. X@O@<dummy.txt@>==@{dummy@+@}
  1596. X\end{verbatim}
  1597. X
  1598. An examination of the printed documentation reveals a lot about how
  1599. XFunnelWeb's presentation works.
  1600. X
  1601. XFirst, notice how the \p{@t} typesetter directive
  1602. at the top of the file has caused a table of contents to appear.
  1603. This is one of FunnelWeb's typesetting features and is discussed in a later
  1604. section. The table of contents shows that the sections have been
  1605. numbered hierarchically.
  1606. X
  1607. Now take a look at the typeset macro definitions. Most important are the
  1608. numbers in square brackets that follow each macro name. As well as
  1609. numbering the headings \i{hierarchically}, FunnelWeb \i{independently}
  1610. numbers
  1611. the macro definitions \i{sequentially}. The first macro definition
  1612. X(for \dq{Programmer's Cheer}) is numbered 1.
  1613. The second (for \dq{Hacker's Cheer}) is numbered 2 and so on.
  1614. Note that it is not macros that are numbered, but macro definitions.
  1615. The distinction is necessary because some macros (such as the
  1616. X\dq{Hacker's Cheer} macro) are additive.
  1617. It is important to
  1618. realize that there is no relationship between the numbers of the headings
  1619. and the numbers of the macro definitions.
  1620. X
  1621. Now take a look at the notes beneath the body of each macro definition.
  1622. All macro definitions are followed by a note indicating the definitions in
  1623. which the macro is called. Additive macros have an additional list, listing
  1624. the definitions in which they are defined.
  1625. X
  1626. XFinally, take a look at the macro \i{call} of \dq{Programmer's Cheer}
  1627. in section~3.2 of the printed documentation.
  1628. Macro calls are set in slanted roman (so that they can
  1629. be distinguished from the \p{tt font} code) and are followed by
  1630. the number of the defining macro definition. In this case, the macro
  1631. was defined in definition~1. Further down, the call to
  1632. the \dq{Hacker's Cheer} macro
  1633. indicates that the macro was defined in definition~2. In fact the macro is
  1634. additive and definition~2 is just the first of many definitions. To list
  1635. all definitions in a call to an additive macro would be unnecessarily messy.
  1636. X
  1637. X\subsection{Literals and Emphasis}
  1638. X\x{literal construct}\x{emphasis construct}
  1639. X
  1640. When writing about program code, it is often desirable to be able to indicate
  1641. that a particular word or phrase be typeset in the same manner as the code
  1642. being discussed. For example, one might talk about the variable \p{topval}
  1643. or the procedure \p{stack\_pop} and wish for them to be typeset
  1644. as they are in this sentence. This, of course,
  1645. is simple to do using \TeX{} macros, but use of the
  1646. X(more general) FunnelWeb typesetting directives to do the same work has the
  1647. added benefit of keeping the document portable to other typesetters.
  1648. X
  1649. XFunnelWeb provides two in-text type modification constructs: \p{@\{...@\}}
  1650. and \p{@/...@/}\x{@slash}\x{@braces}
  1651. where $\ldots$ is raw text. The \p{@\{...@\}}
  1652. construct sets the enclosed
  1653. text in the same manner as the text of macro definitions is set. The
  1654. X\p{@/...@/} construct emphasises its enclosed text in some
  1655. typesetter-dependent fashion. Typically the emphasised text is set in
  1656. italics.
  1657. X
  1658. Here is an example of how these constructs might be used:
  1659. X
  1660. X\begin{verbatim}
  1661. The following procedure @{put_sloth@} writes the @{sloth@} variable to
  1662. the output file. Note: @/The output file must be opened for writing
  1663. at this point or the program will crash!@/
  1664. X\end{verbatim}
  1665. X
  1666. X\subsection{Adding a Header Page}
  1667. X\x{header page}
  1668. X
  1669. XFunnelWeb provides a few typesetter-independent typesetting constructs
  1670. which are specifically designed for the construction of header pages.
  1671. These constructs are usually best placed at the top
  1672. of your input file, but can be placed anywhere the document if desired to
  1673. create header pages right through. The two main restrictions on these
  1674. constructs is that the \p{@t} must start at the start of a line (which
  1675. cannot contain comments), and that the constructs cannot appear inside
  1676. a macro definition. Here is what the top of an input file might look like:
  1677. X
  1678. X\begin{verbatim}
  1679. X@t vskip 40 mm
  1680. X@t title titlefont centre "Hairy Wombat"
  1681. X@t title titlefont centre "Simulation"
  1682. X@t vskip 10 mm
  1683. X@t title smalltitlefont centre "A Program in Six Parts"
  1684. X@t title smalltitlefont centre "Simulating the Life of Some Hairy Wombats"
  1685. X@t vskip 20 mm
  1686. X@t title normalfont left "By Zqitzypbuswapzra Ypongtatoslrtzz"
  1687. X@t new_page
  1688. X@t table_of_contents
  1689. X@t new_page
  1690. X\end{verbatim}
  1691. X
  1692. The \p{@t} at the start of each line indicates that each entire line is
  1693. a typesetter directive.
  1694. The \p{vskip}\xx{vskip}{directive}
  1695. directive instructs FunnelWeb to skip some vertical space
  1696. X(measured in millimetres). The \p{title} directive\xx{title}{directive}
  1697. instructs FunnelWeb to
  1698. position a string of text on a single line of its own. Options are
  1699. provided for font and alignment. The first word after \p{title} is the font
  1700. which can be one of (in decreasing order of size) \p{titlefont},
  1701. X\p{smalltitlefont}, and \p{normalfont}. The second word after \p{title} is
  1702. the desired alignment of the text. The options here are \p{left}, \p{right},
  1703. and \p{centre}. The \p{new\_page}
  1704. directive\xx{newpage}{directive}
  1705. instructs FunnelWeb to skip to a new page. Finally, the
  1706. X\p{table\_of\_contents} directive\xx{table of contents}{directive}
  1707. instructs FunnelWeb to insert a table of
  1708. contents at that point in the text.
  1709. X
  1710. X\subsection{Comments}
  1711. X\x{comments}
  1712. X
  1713. A FunnelWeb comment commences with the \p{@!}\x{@!} sequence and continues up to,
  1714. but not including, the end of line marker at the end of the line that the
  1715. comment sequence is on. Comments can be placed on any line except
  1716. X\p{@i} include, \p{@p} pragma, and \p{@t} typesetter directive lines.
  1717. X
  1718. The text following the FunnelWeb comment sequence \p{@!} will not appear
  1719. in the product files or the documentation file. It is
  1720. only for the eyes of those who bother to look at the original \p{.fw}
  1721. input file. Typically FunnelWeb comments are used to describe the way in
  1722. which particular FunnelWeb constructs are being used. Example:
  1723. X
  1724. X\begin{verbatim}
  1725. X@! This macro is really revolting. Please forgive me. I had to do it!
  1726. X@$@<Revolt Me@>==@{@-
  1727. X@#X@(@#Y@(@#Z@,@"@#Z@"@)=6@,Teapot@,@"@#Q@(45@)@"@,Tiger@)@}
  1728. X\end{verbatim}
  1729. X
  1730. X\section{A Complete Example}
  1731. X\xx{complete}{example}\xx{FunnelWeb}{Example}
  1732. X
  1733. To finish off the chapter, a complete example of a FunnelWeb input file
  1734. is presented. Although unrealistically short, it gives a better
  1735. idea of what a typical FunnelWeb \p{.fw} file looks like.
  1736. X
  1737. X\begin{verbatim}
  1738. X@!---------------------------------------!
  1739. X@!  Start of FunnelWeb Example .fw File  !
  1740. X@!---------------------------------------!
  1741. X
  1742. X@t vskip 40 mm
  1743. X@t title titlefont centre "Powers:"
  1744. X@t title titlefont centre "An Example of"
  1745. X@t title titlefont centre "A Short"
  1746. X@t title titlefont centre "FunnelWeb .fw File"
  1747. X@t vskip 10 mm
  1748. X@t title smalltitlefont centre "by Ross Williams"
  1749. X@t title smalltitlefont centre "26 January 1992"
  1750. X@t vskip 20 mm
  1751. X@t table_of_contents
  1752. X
  1753. X@A@<FunnelWeb Example Program@>
  1754. X
  1755. This  program writes  out each  of the  first @{p@}  powers of  the first  @{n@}
  1756. integers. These  constant parameters are located  here so that they  are easy to
  1757. change.
  1758. X
  1759. X@$@<Constants@>==@{@-
  1760. n : constant natural := 10;     -- How many numbers? (Ans: [1,n]).
  1761. p : constant natural :=  5;     -- How many powers?  (Ans: [1,p]).@}
  1762. X
  1763. X@B Here is  the outline of the  program. This FunnelWeb file  generates a single
  1764. Ada output file  called @{Power.ada@}. The main program consists  of a loop that
  1765. iterates once for each number to be written out.
  1766. X
  1767. X@O@<Power.ada@>==@{@-
  1768. X@<Pull in packages@>
  1769. X
  1770. procedure example is
  1771. X   @<Constants@>
  1772. begin -- example
  1773. X   for i in 1..n loop
  1774. X      @<Write out the first p powers of i on a single line@>
  1775. X   end loop;
  1776. end example;
  1777. X@}
  1778. X
  1779. X@B In this section,  we pull in the packages that this program  needs to run. In
  1780. fact, all we need is the IO package so that we can write out the results. To use
  1781. the IO package, we first of all need  to haul it in (@{with text_io@}) and then
  1782. we need to make all its identifiers visible at the top level (@{use text_io@}).
  1783. X
  1784. X@$@<Pull in packages@>==@{with text_io; use text_io;@}
  1785. X
  1786. X@B Here is  the bit that writes out  the first @{p@} powers of  @{i@}. The power
  1787. values  are  calculated  incrementally  in  @{ip@}  to  avoid  the  use  of  the
  1788. exponentiation operator.
  1789. X
  1790. X@$@<Write out the first p powers of i on a single line@>==@{@-
  1791. declare
  1792. X   ip : natural := 1;
  1793. begin
  1794. X   for power in 1..p loop
  1795. X      ip:=ip*i;
  1796. X      put(natural'image(ip) & " ");
  1797. X   end loop;
  1798. X   new_line;
  1799. end;@}
  1800. X
  1801. X@!---------------------------------------!
  1802. X@!   End of FunnelWeb Example .fw File   !
  1803. X@!---------------------------------------!
  1804. X\end{verbatim}
  1805. X
  1806. X\section{Summary}
  1807. X
  1808. This chapter has provided an introduction to FunnelWeb and a tutorial
  1809. that covers most of its features. FunnelWeb's functionality can be split into
  1810. two parts: a macro preprocessor, and support for typesetting.
  1811. The reader should be aware that
  1812. the examples in this chapter, constructed as they were to demonstrate
  1813. particular features of FunnelWeb, do not present a realistic picture
  1814. of the best use of the tool. Only the final example of this chapter
  1815. comes close.
  1816. The reader should study this last example carefully
  1817. and then write some real
  1818. programs using FunnelWeb before proceeding to Chapter~\ref{chapadvanced}
  1819. which provides more advanced information. At this stage it does not
  1820. particularly matter exactly how you use Funnelweb, as everyone develops
  1821. their own style anyway. The important thing is to try it.
  1822. X
  1823. X%==============================================================================%
  1824. X%                               End of Ch1.tex                                 %
  1825. X%==============================================================================%
  1826. END_OF_FILE
  1827. if test 70598 -ne `wc -c <'userman/u_ch1.tex'`; then
  1828.     echo shar: \"'userman/u_ch1.tex'\" unpacked with wrong size!
  1829. fi
  1830. # end of 'userman/u_ch1.tex'
  1831. fi
  1832. echo shar: End of archive 19 \(of 20\).
  1833. cp /dev/null ark19isdone
  1834. MISSING=""
  1835. for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ; do
  1836.     if test ! -f ark${I}isdone ; then
  1837.     MISSING="${MISSING} ${I}"
  1838.     fi
  1839. done
  1840. if test "${MISSING}" = "" ; then
  1841.     echo You have unpacked all 20 archives.
  1842.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  1843. else
  1844.     echo You still need to unpack the following archives:
  1845.     echo "        " ${MISSING}
  1846. fi
  1847. ##  End of shell archive.
  1848. exit 0
  1849.