% This program is copyright (C) 1984 by D. E. Knuth; all rights are reserved. % Copying of this file is authorized only if (1) you are D. E. Knuth, or if % (2) you make absolutely no changes to your copy. (The WEB system provides % for alterations via an auxiliary file; the master file should stay intact.) % In other words, METAFONT is under essentially the same ground rules as TeX. % Version 0 was completed on July 28, 1984. % Version 1 is expected to be identical to the present near-final version. % A reward of $5.12 will be paid to the first finder of any remaining bug. % (But typographic errors are worth nothing, at the moment...) % Although considerable effort has been expended to make the METAFONT program % correct and reliable, no warranty is implied; the author disclaims any % obligation or liability for damages, including but not limited to % special, indirect, or consequential damages arising out of or in % connection with the use or performance of this software. This work has % been a ``labor of love'' and the author hopes that users enjoy it. % Here is TeX material that gets inserted after \input webmac \def\hang{\hangindent 3em\noindent\ignorespaces} \def\textindent#1{\hangindent2.5em\noindent\hbox to2.5em{\hss#1 }\ignorespaces} \font\ninerm=cmr9 \let\mc=\ninerm % medium caps for names like SAIL \def\PASCAL{Pascal} \def\ph{\hbox{Pascal-H}} \def\psqrt#1{\sqrt{\mathstrut#1}} \def\k{←{k+1}} \font\tenlogo=logo10 % font used for the METAFONT logo \font\logos=logosl10 \font\eightlogo=logo8 \def\MF{{\logo META}\-{\logo FONT}} \def\<#1>{$\langle#1\rangle$} \def\section{\mathhexbox278} \let\swap=\leftrightarrow \def\round{\mathop{\rm round}\nolimits} \def\(#1){} % this is used to make section names sort themselves better \def\9#1{} % this is used for sort keys in the index via @@:sort key}{entry@@> \outer\def\N#1. \[#2]#3.{\MN#1.\vfil\eject % begin starred section \def\rhead{PART #2:\uppercase{#3}} % define running headline \message{*\modno} % progress report \edef\next{\write\cont{\Z{\?#2]#3}{\modno}{\the\pageno}}}\next \ifon\startsection{\bf\ignorespaces#3.\quad}\ignorespaces} \let\?=\relax % we want to be able to \write a \? \def\title{{\eightlogo METAFONT}} \def\topofcontents{\hsize 5.5in \vglue -30pt plus 1fil minus 1.5in \def\?##1]{\hbox to 1in{\hfil##1.\ }} } \def\botofcontents{\vskip 0pt plus 1fil minus 1.5in} \pageno=3 \def\glob{13} % this should be the section number of "<Global...>" \def\gglob{20, 26} % this should be the next two sections of "<Global...>" @* \[1] Introduction. This is \MF, a font compiler intended to produce high-quality typefaces. The \PASCAL\ program that follows is the definition of \MF84, a standard @:PASCAL}{\PASCAL@> @!@:METAFONT84}{\MF84@> version of \MF\ that is designed to be highly portable so that identical output will be obtainable on a great variety of different computers. The conventions of \MF84 are the same as those of \TeX82. The main purpose of the following program is to explain the algorithms of \MF\ as clearly as possible. As a result, the program will not necessarily be very efficient when a particular \PASCAL\ compiler has translated it into a particular machine language. However, the program has been written so that it can be tuned to run efficiently in a wide variety of operating environments by making comparatively few changes. Such flexibility is possible because the documentation that follows is written in the \.{WEB} language, which is at a higher level than \PASCAL; the preprocessing step that converts \.{WEB} to \PASCAL\ is able to introduce most of the necessary refinements. Semi-automatic translation to other languages is also feasible, because the program below does not make extensive use of features that are peculiar to \PASCAL. A large piece of software like \MF\ has inherent complexity that cannot be reduced below a certain level of difficulty, although each individual part is fairly simple by itself. The \.{WEB} language is intended to make the algorithms as readable as possible, by reflecting the way the individual program pieces fit together and by providing the cross-references that connect different parts. Detailed comments about what is going on, and about why things were done in certain ways, have been liberally sprinkled throughout the program. These comments explain features of the implementation, but they rarely attempt to explain the \MF\ language itself, since the reader is supposed to be familiar with {\sl The {\logos METAFONT\/}book}. @.WEB@> @:METAFONTbook}{\sl The {\logos METAFONT\/}book@> @ The present implementation has a long ancestry, beginning in the spring of~1977, when its author wrote a prototype set of subroutines and macros @↑Knuth, Donald Ervin@> that were used to develop the first Computer Modern fonts. This original proto-\MF\ required the user to recompile a {\mc SAIL} program whenever any character was changed, because it was not a ``language'' for font design; the language was {\mc SAIL}. After several hundred characters had been designed in that way, the author developed an interpretable language called \MF, in which it was possible to express the Computer Modern programs less cryptically. A complete \MF\ processor was designed and coded by the author in 1979. This program, written in {\mc SAIL}, was adapted for use with a variety of typesetting equipment and display terminals by Leo Guibas, Lyle Ramshaw, and David Fuchs. @↑Guibas, Leonidas Ioannis@> @↑Ramshaw, Lyle Harold@> @↑Fuchs, David Raymond@> Major improvements to the design of Computer Modern fonts were made in the spring of 1982, after which it became clear that a new language would better express the needs of letterform designers. Therefore an entirely new \MF\ language and system were designed in 1984; the present system retains the name and some of the spirit of \MF79, but all of the details have changed. No doubt there still is plenty of room for improvement, but the author is firmly committed to keeping \MF84 ``frozen'' from now on; stability and reliability are to be its main virtues. On the other hand, the \.{WEB} description can be extended without changing the core of \MF84 itself, and the program has been designed so that such extensions are not extremely difficult to make. The |banner| string defined here should be changed whenever \MF\ undergoes any modifications, so that it will be clear which version of \MF\ might be the guilty party when a problem arises. @↑extensions to \MF@> @↑system dependencies@> @d banner=='This is METAFONT, Version 0.9999' {printed when \MF\ starts} @ Different \PASCAL s have slightly different conventions, and the present @!@:PASCAL H}{\ph@> program expresses \MF\ in terms of the \PASCAL\ that was available to the author in 1984. The methods used here to work with this particular compiler, which we shall call \ph, should help the reader to see how to make an appropriate interface for other systems if necessary. (\ph\ is Charles Hedrick's modification of a compiler @↑Hedrick, Charles Locke@> for the DECsystem-10 that was originally developed at the University of Hamburg; cf.\ {\sl SOFTWARE---Practice \AM\ Experience \bf6} (1976), 29--42. The \MF\ program below is intended to be adaptable, without extensive changes, to most other versions of \PASCAL, so it does not fully use the admirable features of \ph. Indeed, a conscious effort has been made here to avoid using several idiosyncratic features of standard \PASCAL\ itself, so that most of the code can be translated mechanically into other high-level languages. For example, the `\&{with}' and `\\{new}' features are not used, nor are pointer types, set types, or enumerated scalar types; there are no `\&{var}' parameters, except in the case of files; there are no tag fields on variant records; there are no |real| variables; no procedures are declared local to other procedures.) The portions of this program that involve system-dependent code, where changes might be necessary because of differences between \PASCAL\ compilers and/or differences between operating systems, can be identified by looking at the sections whose numbers are listed under `system dependencies' in the index. Furthermore, the index entries for `dirty \PASCAL' list all places where the restrictions of \PASCAL\ have not been followed perfectly, for one reason or another. @!@↑system dependencies@> @!@↑dirty \PASCAL@> @ The program begins with a normal \PASCAL\ program heading, whose components will be filled in later, using the conventions of \.{WEB}. @.WEB@> For example, the portion of the program called `\X\glob:Global variables\X' here will be replaced by a sequence of variable declarations that starts in $\section\glob$ of this documentation. In this way, we are able to define each individual global variable when we are prepared to understand what it means; we do not have to define all of the globals at once. Cross references in $\section\glob$, where it says ``See also sections \gglob, \dots,'' also make it possible to look at the set of all global variables, if desired. Similar remarks apply to the other portions of the program heading. Actually the heading shown here is not quite normal: The |program| line does not mention any |output| file, because \ph\ would ask the \MF\ user to specify a file name if |output| were specified here. @↑system dependencies@> @d mtype==t@&y@&p@&e {this is a \.{WEB} coding trick:} @f mtype==type {`\&{mtype}' will be equivalent to `\&{type}'} @f type==true {but `|type|' will not be treated as a reserved word} @p @t\4@>@<Compiler directives@>@/ program MF; {all file names are defined dynamically} label @<Labels in the outer block@>@/ const @<Constants in the outer block@>@/ mtype @<Types in the outer block@>@/ var @<Global variables@>@/ @# procedure initialize; {this procedure gets things started properly} var @<Local variables for initialization@>@/ begin @<Set initial values of key variables@>@/ end;@# @t\4@>@<Basic printing procedures@>@/ @t\4@>@<Error handling procedures@>@/ @ The overall \MF\ program begins with the heading just shown, after which comes a bunch of procedure declarations and function declarations. Finally we will get to the main program, which begins with the comment `|start←here|'. If you want to skip down to the main program now, you can look up `|start←here|' in the index. But the author suggests that the best way to understand this program is to follow pretty much the order of \MF's components as they appear in the \.{WEB} description you are now reading, since the present ordering is intended to combine the advantages of the ``bottom up'' and ``top down'' approaches to the problem of understanding a somewhat complicated system. @ Three labels must be declared in the main program, so we give them symbolic names. @d start←of←MF=1 {go here when \MF's variables are initialized} @d end←of←MF=9998 {go here to close files and terminate gracefully} @d final←end=9999 {this label marks the ending of the program} @<Labels in the out...@>= start←of←MF@t\hskip-2pt@>, end←of←MF@t\hskip-2pt@>,@,final←end; {key control points} @ Some of the code below is intended to be used only when diagnosing the strange behavior that sometimes occurs when \MF\ is being installed or when system wizards are fooling around with \MF\ without quite knowing what they are doing. Such code will not normally be compiled; it is delimited by the codewords `$|debug|\ldots|gubed|$', with apologies to people who wish to preserve the purity of English. Similarly, there is some conditional code delimited by `$|stat|\ldots|tats|$' that is intended for use when statistics are to be kept about \MF's memory usage. The |stat| $\ldots$ |tats| code also implements special diagnostic information that is printed when $\\{tracingedges}>1$. @↑debugging@> @d debug==@{ {change this to `$\\{debug}\equiv\null$' when debugging} @d gubed==@t@>@} {change this to `$\\{gubed}\equiv\null$' when debugging} @f debug==begin @f gubed==end @# @d stat==@{ {change this to `$\\{stat}\equiv\null$' when gathering usage statistics} @d tats==@t@>@} {change this to `$\\{tats}\equiv\null$' when gathering usage statistics} @f stat==begin @f tats==end @ This program has two important variations: (1) There is a long and slow version called \.{INIMF}, which does the extra calculations needed to @.INIMF@> initialize \MF's internal tables; and (2)~there is a shorter and faster production version, which cuts the initialization to a bare minimum. Parts of the program that are needed in (1) but not in (2) are delimited by the codewords `$|init|\ldots|tini|$'. @d init== {change this to `$\\{init}\equiv\.{@@\{}$' in the production version} @d tini== {change this to `$\\{tini}\equiv\.{@@\}}$' in the production version} @f init==begin @f tini==end @ If the first character of a \PASCAL\ comment is a dollar sign, \ph\ treats the comment as a list of ``compiler directives'' that will affect the translation of this program into machine language. The directives shown below specify full checking and inclusion of the \PASCAL\ debugger when \MF\ is being debugged, but they cause range checking and other redundant code to be eliminated when the production system is being generated. Arithmetic overflow will be detected in all cases. @↑system dependencies@> @↑Overflow in arithmetic@> @<Compiler directives@>= @{@&$C-,A+,D-@} {no range check, catch arithmetic overflow, no debug overhead} @!debug @{@&$C+,D+@}@+ gubed {but turn everything on when debugging} @ This \MF\ implementation conforms to the rules of the {\sl Pascal User @:PASCAL}{\PASCAL@> @↑system dependencies@> Manual} published by Jensen and Wirth in 1975, except where system-dependent @↑Wirth, Niklaus@> @↑Jensen, Kathleen@> code is necessary to make a useful system program, and except in another respect where such conformity would unnecessarily obscure the meaning and clutter up the code: We assume that |case| statements may include a default case that applies if no matching label is found. Thus, we shall use constructions like $$\vbox{\halign{\ignorespaces#\hfil\cr |case x of|\cr 1: $\langle\,$code for $x=1\,\rangle$;\cr 3: $\langle\,$code for $x=3\,\rangle$;\cr |othercases| $\langle\,$code for |x<>1| and |x<>3|$\,\rangle$\cr |endcases|\cr}}$$ since most \PASCAL\ compilers have plugged this hole in the language by incorporating some sort of default mechanism. For example, the \ph\ compiler allows `|others|:' as a default label, and other \PASCAL s allow syntaxes like `\&{else}' or `\&{otherwise}' or `\\{otherwise}:', etc. The definitions of |othercases| and |endcases| should be changed to agree with local conventions. Note that no semicolon appears before |endcases| in this program, so the definition of |endcases| should include a semicolon if the compiler wants one. (Of course, if no default mechanism is available, the |case| statements of \MF\ will have to be laboriously extended by listing all remaining cases. People who are stuck with such \PASCAL s have in fact done this, successfully but not happily!) @d othercases == others: {default for cases not listed explicitly} @d endcases == @+end {follows the default case in an extended |case| statement} @f othercases == else @f endcases == end @ The following parameters can be changed at compile time to extend or reduce \MF's capacity. They may have different values in \.{INIMF} and in production versions of \MF. @.INIMF@> @↑system dependencies@> @<Constants...@>= @!mem←max=30000; {greatest index in \MF's internal |mem| array; must be strictly less than |max←halfword|; must be equal to |mem←top| in \.{INIMF}, otherwise |>=mem←top|} @!max←internal=100; {maximum number of internal quantities} @!buf←size=500; {maximum number of characters simultaneously present in current lines of open files; must not exceed |max←halfword|} @!error←line=72; {width of context lines on terminal error messages} @!half←error←line=42; {width of first lines of contexts in terminal error messages; should be between 30 and |error←line-15|} @!max←print←line=79; {width of longest text lines output; should be at least 60} @!screen←width=768; {number of pixels in each row of screen display} @!screen←depth=1024; {number of pixels in each column of screen display} @!stack←size=30; {maximum number of simultaneous input sources} @!max←strings=2000; {maximum number of strings; must not exceed |max←halfword|} @!string←vacancies=8000; {the minimum number of characters that should be available for the user's identifier names and strings, after \MF's own error messages are stored} @!pool←size=32000; {maximum number of characters in strings, including all error messages and help texts, and the names of all identifiers; must exceed |string←vacancies| by the total length of \MF's own strings, which is currently about 22000} @!move←size=5000; {space for storing moves in a single octant} @!max←wiggle=300; {number of autorounded points per cycle} @!gf←buf←size=800; {size of the output buffer, must be a multiple of 8} @!file←name←size=40; {file names shouldn't be longer than this} @!pool←name='MFbases:MF.POOL '; {string of length |file←name←size|; tells where the string pool appears} @.MFbases@> @!path←size=300; {maximum number of knots between breakpoints of a path} @!bistack←size=785; {size of stack for bisection algorithms; should probably be left at this value} @!header←size=100; {maximum number of \.{TFM} header words, times~4} @!lig←table←size=300; {maximum number of ligature/kern steps} @!max←font←dimen=50; {maximum number of \&{fontdimen} parameters} @ Like the preceding parameters, the following quantities can be changed at compile time to extend or reduce \MF's capacity. But if they are changed, it is necessary to rerun the initialization program \.{INIMF} @.INIMF@> to generate new tables for the production \MF\ program. One can't simply make helter-skelter changes to the following constants, since certain rather complex initialization numbers are computed from them. They are defined here using \.{WEB} macros, instead of being put into \PASCAL's |const| list, in order to emphasize this distinction. @d mem←min=0 {smallest index in the |mem| array, must not be less than |min←halfword|} @d mem←top==30000 {largest index in the |mem| array dumped by \.{INIMF}; must be substantially larger than |mem←min| and not greater than |mem←max|} @d hash←size=2100 {maximum number of symbolic tokens, must be less than |max←halfword-3*param←size|} @d hash←prime=1777 {a prime number equal to about 85\% of |hash←size|} @d max←in←open=6 {maximum number of input files and error insertions that can be going on simultaneously} @d param←size=150 {maximum number of simultaneous macro parameters} @↑system dependencies@> @ In case somebody has inadvertently made bad settings of the ``constants,'' \MF\ checks them using a global variable called |bad|. This is the first of many sections of \MF\ where global variables are defined. @<Glob...@>= @!bad:integer; {is some ``constant'' wrong?} @ Later on we will say `\ignorespaces|if mem←max>=max←halfword then bad:=10|', or something similar. (We can't do that until |max←halfword| has been defined.) @<Check the ``constant'' values for consistency@>= bad:=0; if (half←error←line<30)or(half←error←line>error←line-15) then bad:=1; if max←print←line<60 then bad:=2; if gf←buf←size mod 8<>0 then bad:=3; if mem←min+1100>mem←top then bad:=4; if hash←prime>hash←size then bad:=5; if header←size mod 4 <> 0 then bad:=6; @ Labels are given symbolic names by the following definitions, so that occasional |goto| statements will be meaningful. We insert the label `|exit|:' just before the `\ignorespaces|end|\unskip' of a procedure in which we have used the `|return|' statement defined below; the label `|restart|' is occasionally used at the very beginning of a procedure; and the label `|reswitch|' is occasionally used just prior to a |case| statement in which some cases change the conditions and we wish to branch to the newly applicable case. Loops that are set up with the |loop| construction defined below are commonly exited by going to `|done|' or to `|found|' or to `|not←found|', and they are sometimes repeated by going to `|continue|'. If two or more parts of a subroutine start differently but end up the same, the shared code may be gathered together at `|common←ending|'. Incidentally, this program never declares a label that isn't actually used, because some fussy \PASCAL\ compilers will complain about redundant labels. @d exit=10 {go here to leave a procedure} @d restart=20 {go here to start a procedure again} @d reswitch=21 {go here to start a case statement again} @d continue=22 {go here to resume a loop} @d done=30 {go here to exit a loop} @d done1=31 {like |done|, when there is more than one loop} @d done2=32 {for exiting the second loop in a long block} @d done3=33 {for exiting the third loop in a very long block} @d done4=34 {for exiting the fourth loop in an extremely long block} @d done5=35 {for exiting the fifth loop in an immense block} @d done6=36 {for exiting the sixth loop in a block} @d found=40 {go here when you've found it} @d found1=41 {like |found|, when there's more than one per routine} @d found2=42 {like |found|, when there's more than two per routine} @d not←found=45 {go here when you've found nothing} @d common←ending=50 {go here when you want to merge with another branch} @ Here are some macros for common programming idioms. @d incr(#) == #:=#+1 {increase a variable by unity} @d decr(#) == #:=#-1 {decrease a variable by unity} @d negate(#) == #:=-# {change the sign of a variable} @d double(#) == #:=#+# {multiply a variable by two} @d loop == @+ while true do@+ {repeat over and over until a |goto| happens} @f loop == xclause {\.{WEB}'s |xclause| acts like `\ignorespaces|while true do|\unskip'} @d do←nothing == {empty statement} @d return == goto exit {terminate a procedure call} @f return == nil {\.{WEB} will henceforth say |return| instead of \\{return}} @* \[2] The character set. In order to make \MF\ readily portable between a wide variety of computers, all of its input text is converted to an internal seven-bit code that is essentially standard ASCII, the ``American Standard Code for Information Interchange.'' This conversion is done immediately when each character is read in. Conversely, characters are converted from ASCII to the user's external representation just before they are output to a text file. @↑ASCII code@> Such an internal code is relevant to users of \MF\ only with respect to the \&{char} and \&{ASCII} operations, and the comparison of strings. @ Characters of text that have been converted to \MF's internal form are said to be of type |ASCII←code|, which is a subrange of the integers. @<Types...@>= @!ASCII←code=0..127; {seven-bit numbers} @ The original \PASCAL\ compiler was designed in the late 60s, when six-bit character sets were common, so it did not make provision for lowercase letters. Nowadays, of course, we need to deal with both capital and small letters in a convenient way, especially in a program for font design; so the present specification of \MF\ has been written under the assumption that the \PASCAL\ compiler and run-time system permit the use of text files with more than 64 distinguishable characters. More precisely, we assume that the character set contains at least the letters and symbols associated with ASCII codes @'40 through @'176; all of these characters are now available on most computer terminals. Since we are dealing with more characters than were present in the first \PASCAL\ compilers, we have to decide what to call the associated data type. Some \PASCAL s use the original name |char| for the characters in text files, even though there now are more than 64 such characters, while other \PASCAL s consider |char| to be a 64-element subrange of a larger data type that has some other name. In order to accommodate this difference, we shall use the name |text←char| to stand for the data type of the characters that are converted to and from |ASCII←code| when they are input and output. We shall also assume that |text←char| consists of the elements |chr(first←text←char)| through |chr(last←text←char)|, inclusive. The following definitions should be adjusted if necessary. @↑system dependencies@> @d text←char == char {the data type of characters in text files} @d first←text←char=0 {ordinal number of the smallest element of |text←char|} @d last←text←char=127 {ordinal number of the largest element of |text←char|} @<Local variables for init...@>= i:0..last←text←char; @ The \MF\ processor converts between ASCII code and the user's external character set by means of arrays |xord| and |xchr| that are analogous to \PASCAL's |ord| and |chr| functions. @<Glob...@>= @!xord: array [text←char] of ASCII←code; {specifies conversion of input characters} @!xchr: array [ASCII←code] of text←char; {specifies conversion of output characters} @ Since we are assuming that our \PASCAL\ system is able to read and write the visible characters of standard ASCII (although not necessarily using the ASCII codes to represent them), the following assignment statements initialize most of the |xchr| array properly, without needing any system-dependent changes. On the other hand, it is possible to implement \MF\ with less complete character sets, and in such cases it will be necessary to change something here. @↑system dependencies@> @<Set init...@>= xchr[@'40]:=' '; xchr[@'41]:='!'; xchr[@'42]:='"'; xchr[@'43]:='#'; xchr[@'44]:='$'; xchr[@'45]:='%'; xchr[@'46]:='&'; xchr[@'47]:='''';@/ xchr[@'50]:='('; xchr[@'51]:=')'; xchr[@'52]:='*'; xchr[@'53]:='+'; xchr[@'54]:=','; xchr[@'55]:='-'; xchr[@'56]:='.'; xchr[@'57]:='/';@/ xchr[@'60]:='0'; xchr[@'61]:='1'; xchr[@'62]:='2'; xchr[@'63]:='3'; xchr[@'64]:='4'; xchr[@'65]:='5'; xchr[@'66]:='6'; xchr[@'67]:='7';@/ xchr[@'70]:='8'; xchr[@'71]:='9'; xchr[@'72]:=':'; xchr[@'73]:=';'; xchr[@'74]:='<'; xchr[@'75]:='='; xchr[@'76]:='>'; xchr[@'77]:='?';@/ xchr[@'100]:='@@'; xchr[@'101]:='A'; xchr[@'102]:='B'; xchr[@'103]:='C'; xchr[@'104]:='D'; xchr[@'105]:='E'; xchr[@'106]:='F'; xchr[@'107]:='G';@/ xchr[@'110]:='H'; xchr[@'111]:='I'; xchr[@'112]:='J'; xchr[@'113]:='K'; xchr[@'114]:='L'; xchr[@'115]:='M'; xchr[@'116]:='N'; xchr[@'117]:='O';@/ xchr[@'120]:='P'; xchr[@'121]:='Q'; xchr[@'122]:='R'; xchr[@'123]:='S'; xchr[@'124]:='T'; xchr[@'125]:='U'; xchr[@'126]:='V'; xchr[@'127]:='W';@/ xchr[@'130]:='X'; xchr[@'131]:='Y'; xchr[@'132]:='Z'; xchr[@'133]:='['; xchr[@'134]:='\'; xchr[@'135]:=']'; xchr[@'136]:='↑'; xchr[@'137]:='←';@/ xchr[@'140]:='`'; xchr[@'141]:='a'; xchr[@'142]:='b'; xchr[@'143]:='c'; xchr[@'144]:='d'; xchr[@'145]:='e'; xchr[@'146]:='f'; xchr[@'147]:='g';@/ xchr[@'150]:='h'; xchr[@'151]:='i'; xchr[@'152]:='j'; xchr[@'153]:='k'; xchr[@'154]:='l'; xchr[@'155]:='m'; xchr[@'156]:='n'; xchr[@'157]:='o';@/ xchr[@'160]:='p'; xchr[@'161]:='q'; xchr[@'162]:='r'; xchr[@'163]:='s'; xchr[@'164]:='t'; xchr[@'165]:='u'; xchr[@'166]:='v'; xchr[@'167]:='w';@/ xchr[@'170]:='x'; xchr[@'171]:='y'; xchr[@'172]:='z'; xchr[@'173]:='{'; xchr[@'174]:='|'; xchr[@'175]:='}'; xchr[@'176]:='~';@/ xchr[0]:=' '; xchr[@'177]:=' '; {ASCII codes 0 and |@'177| do not appear in text} @ The ASCII code is ``standard'' only to a certain extent, since many computer installations have found it advantageous to have ready access to more than 94 printing characters. If \MF\ is being used on a garden-variety \PASCAL\ for which only standard ASCII codes will appear in the input and output files, it doesn't really matter what codes are specified in |xchr[1..@'37]|, but the safest policy is to blank everything out by using the code shown below. However, other settings of |xchr| will make \MF\ more friendly on computers that have an extended character set, so that users can type things like `\.↑↑Z' instead of `\.{<>}'. People with extended character sets can assign codes arbitrarily, giving an |xchr| equivalent to whatever characters the users of \MF\ are allowed to have in their input files. Appropriate changes to \MF's |char←class| table should then be made. (Unlike \TeX, each installation of \MF\ has a fixed assignment of category codes, called the |char←class|.) Such changes make portability of programs more difficult, so they should be introduced cautiously if at all. @↑character set dependencies@> @↑system dependencies@> @<Set init...@>= for i:=1 to @'37 do xchr[i]:=' '; @ The following system-independent code makes the |xord| array contain a suitable inverse to the information in |xchr|. Note that if |xchr[i]=xchr[j]| where |i<j<@'177|, the value of |xord[xchr[i]]| will turn out to be |j| or more; hence, standard ASCII code numbers will be used instead of codes below @'40 in case there is a coincidence. @<Set init...@>= for i:=first←text←char to last←text←char do xord[chr(i)]:=@'177; for i:=1 to @'176 do xord[xchr[i]]:=i; @* \[3] Input and output. The bane of portability is the fact that different operating systems treat input and output quite differently, perhaps because computer scientists have not given sufficient attention to this problem. People have felt somehow that input and output are not a part of ``real'' programming. Well, it is true that some kinds of programming are more fun than others. With existing input/output conventions being so diverse and so messy, the only sources of joy in such parts of the code are the rare occasions when one can find a way to make the program a little less bad than it might have been. We have two choices: either to attack I/O now and get it over with, or to postpone it until near the end. Neither prospect is very attractive, so let's get it over with. The basic operations we need to do are (1)~inputting and outputting of text, to or from a file or the user's terminal; (2)~inputting and outputting of eight-bit bytes, to or from a file; (3)~instructing the operating system to initiate (``open'') or to terminate (``close'') input or output from a specified file; (4)~testing whether the end of an input file has been reached; (5)~display of bits on the user's screen. The bit-display operation will be discussed in a later section; we shall deal here only with more traditional kinds of I/O. \MF\ needs to deal with two kinds of files. We shall use the term |alpha←file| for a file that contains textual data, and the term |byte←file| for a file that contains eight-bit binary information. These two types turn out to be the same on many computers, but sometimes there is a significant distinction, so we shall be careful to distinguish between them. Standard protocols for transferring such files from computer to computer, via high-speed networks, are now becoming available to more and more communities of users. The program actually makes use also of a third kind of file, called a |word←file|, when dumping and reloading base information for its own initialization. We shall define a word file later; but it will be possible for us to specify simple operations on word files before they are defined. @<Types...@>= @!eight←bits=0..255; {unsigned one-byte quantity} @!alpha←file=packed file of text←char; {files that contain textual data} @!byte←file=packed file of eight←bits; {files that contain binary data} @ Most of what we need to do with respect to input and output can be handled by the I/O facilities that are standard in \PASCAL, i.e., the routines called |get|, |put|, |eof|, and so on. But standard \PASCAL\ does not allow file variables to be associated with file names that are determined at run time, so it cannot be used to implement \MF; some sort of extension to \PASCAL's ordinary |reset| and |rewrite| is crucial for our purposes. We shall assume that |name←of←file| is a variable of an appropriate type such that the \PASCAL\ run-time system being used to implement \MF\ can open a file whose external name is specified by |name←of←file|. @↑system dependencies@> @<Glob...@>= @!name←of←file:packed array[1..file←name←size] of char;@;@/ {on some systems this may be a \&{record} variable} @!name←length:0..file←name←size;@/{this many characters are actually relevant in |name←of←file| (the rest are blank)} @ The \ph\ compiler with which the present version of \MF\ was prepared has extended the rules of \PASCAL\ in a very convenient way. To open file~|f|, we can write $$\vbox{\halign{#\hfil\qquad&#\hfil\cr |reset(f,@t\\{name}@>,'/O')|&for input;\cr |rewrite(f,@t\\{name}@>,'/O')|&for output.\cr}}$$ The `\\{name}' parameter, which is of type `\ignorespaces|packed array[@t\<\\{any}>@>] of text←char|', stands for the name of the external file that is being opened for input or output. Blank spaces that might appear in \\{name} are ignored. The `\.{/O}' parameter tells the operating system not to issue its own error messages if something goes wrong. If a file of the specified name cannot be found, or if such a file cannot be opened for some other reason (e.g., someone may already be trying to write the same file), we will have |@!erstat(f)<>0| after an unsuccessful |reset| or |rewrite|. This allows \MF\ to undertake appropriate corrective action. @:PASCAL H}{\ph@> @↑system dependencies@> \MF's file-opening procedures return |false| if no file identified by |name←of←file| could be opened. @d reset←OK(#)==erstat(#)=0 @d rewrite←OK(#)==erstat(#)=0 @p function a←open←in(var @!f:alpha←file):boolean; {open a text file for input} begin reset(f,name←of←file,'/O'); a←open←in:=reset←OK(f); end; @# function a←open←out(var @!f:alpha←file):boolean; {open a text file for output} begin rewrite(f,name←of←file,'/O'); a←open←out:=rewrite←OK(f); end; @# function b←open←out(var @!f:byte←file):boolean; {open a binary file for output} begin rewrite(f,name←of←file,'/O'); b←open←out:=rewrite←OK(f); end; @# function w←open←in(var @!f:word←file):boolean; {open a word file for input} begin reset(f,name←of←file,'/O'); w←open←in:=reset←OK(f); end; @# function w←open←out(var @!f:word←file):boolean; {open a word file for output} begin rewrite(f,name←of←file,'/O'); w←open←out:=rewrite←OK(f); end; @ Files can be closed with the \ph\ routine `|close(f)|', which @↑system dependencies@> should be used when all input or output with respect to |f| has been completed. This makes |f| available to be opened again, if desired; and if |f| was used for output, the |close| operation makes the corresponding external file appear on the user's area, ready to be read. @p procedure a←close(var @!f:alpha←file); {close a text file} begin close(f); end; @# procedure b←close(var @!f:byte←file); {close a binary file} begin close(f); end; @# procedure w←close(var @!f:word←file); {close a word file} begin close(f); end; @ Binary input and output are done with \PASCAL's ordinary |get| and |put| procedures, so we don't have to make any other special arrangements for binary~I/O. Text output is also easy to do with standard \PASCAL\ routines. The treatment of text input is more difficult, however, because of the necessary translation to |ASCII←code| values, and because \MF's conventions should be efficient and they should blend nicely with the user's operating environment. @ Input from text files is read one line at a time, using a routine called |input←ln|. This function is defined in terms of global variables called |buffer|, |first|, and |last| that will be described in detail later; for now, it suffices for us to know that |buffer| is an array of |ASCII←code| values, and that |first| and |last| are indices into this array representing the beginning and ending of a line of text. @<Glob...@>= @!buffer:array[0..buf←size] of ASCII←code; {lines of characters being read} @!first:0..buf←size; {the first unused position in |buffer|} @!last:0..buf←size; {end of the line just input to |buffer|} @!max←buf←stack:0..buf←size; {largest index used in |buffer|} @ The |input←ln| function brings the next line of input from the specified field into available positions of the buffer array and returns the value |true|, unless the file has already been entirely read, in which case it returns |false| and sets |last:=first|. In general, the |ASCII←code| numbers that represent the next line of the file are input into |buffer[first]|, |buffer[first+1]|, \dots, |buffer[last-1]|; and the global variable |last| is set equal to |first| plus the length of the line. Trailing blanks are removed from the line; thus, either |last=first| (in which case the line was entirely blank) or |buffer[last-1]<>" "|. @↑inner loop@> An overflow error is given, however, if the normal actions of |input←ln| would make |last>=buf←size|; this is done so that other parts of \MF\ can safely look at the contents of |buffer[last+1]| without overstepping the bounds of the |buffer| array. Upon entry to |input←ln|, the condition |first<buf←size| will always hold, so that there is always room for an ``empty'' line. The variable |max←buf←stack|, which is used to keep track of how large the |buf←size| parameter must be to accommodate the present job, is also kept up to date by |input←ln|. If the |bypass←eoln| parameter is |true|, |input←ln| will do a |get| before looking at the first character of the line; this skips over an |eoln| that was in |f↑|. The procedure does not do a |get| when it reaches the end of the line; therefore it can be used to acquire input from the user's terminal as well as from ordinary text files. Standard \PASCAL\ says that a file should have |eoln| immediately before |eof|, but \MF\ needs only a weaker restriction: If |eof| occurs in the middle of a line, the system function |eoln| should return a |true| result (even though |f↑| will be undefined). @p function input←ln(var @!f:alpha←file;@!bypass←eoln:boolean):boolean; {inputs the next line or returns |false|} var @!last←nonblank:0..buf←size; {|last| with trailing blanks removed} begin if bypass←eoln then if not eof(f) then get(f); {input the first character of the line into |f↑|} last:=first; {cf.\ Matthew 19\thinspace:\thinspace30} if eof(f) then input←ln:=false else begin last←nonblank:=first; while not eoln(f) do begin if last>=max←buf←stack then begin max←buf←stack:=last+1; if max←buf←stack=buf←size then overflow("buffer size",buf←size); @:METAFONT capacity exceeded buffer size}{\quad buffer size@> end; buffer[last]:=xord[f↑]; get(f); incr(last); if buffer[last-1]<>" " then last←nonblank:=last; end; last:=last←nonblank; input←ln:=true; end; end; @ The user's terminal acts essentially like other files of text, except that it is used both for input and for output. When the terminal is considered an input file, the file variable is called |term←in|, and when it is considered an output file the file variable is |term←out|. @↑system dependencies@> @<Glob...@>= @!term←in:alpha←file; {the terminal as an input file} @!term←out:alpha←file; {the terminal as an output file} @ Here is how to open the terminal files in \ph. The `\.{/I}' switch suppresses the first |get|. @↑system dependencies@> @d t←open←in==reset(term←in,'TTY:','/O/I') {open the terminal for text input} @d t←open←out==rewrite(term←out,'TTY:','/O') {open the terminal for text output} @ Sometimes it is necessary to synchronize the input/output mixture that happens on the user's terminal, and three procedures are used for this purpose. The first of these, |update←terminal|, is called when we want to make sure that everything we have output to the terminal so far has actually left the computer's internal buffers and been sent. The second, |clear←terminal|, is called when we wish to cancel any input that the user may have typed ahead (since we are about to issue an unexpected error message). The third, |wake←up←terminal|, is supposed to revive the terminal if the user has disabled it by some instruction to the operating system. The following macros show how these operations can be specified in \ph: @↑system dependencies@> @d update←terminal == break(term←out) {empty the terminal output buffer} @d clear←terminal == break←in(term←in,true) {clear the terminal input buffer} @d wake←up←terminal == do←nothing {cancel the user's cancellation of output} @ We need a special routine to read the first line of \MF\ input from the user's terminal. This line is different because it is read before we have opened the transcript file; there is sort of a ``chicken and egg'' problem here. If the user types `\.{input cmr10}' on the first line, or if some macro invoked by that line does such an \.{input}, the transcript file will be named `\.{cmr10.log}'; but if no \.{input} commands are performed during the first line of terminal input, the transcript file will acquire its default name `\.{mfput.log}'. (The transcript file will not contain error messages generated by the first line before the first \.{input} command.) The first line is even more special if we are lucky enough to have an operating system that treats \MF\ differently from a run-of-the-mill \PASCAL\ object program. It's nice to let the user start running a \MF\ job by typing a command line like `\.{MF cmr10}'; in such a case, \MF\ will operate as if the first line of input were `\.{cmr10}', i.e., the first line will consist of the remainder of the command line, after the part that invoked \MF. @ Different systems have different ways to get started. But regardless of what conventions are adopted, the routine that initializes the terminal should satisfy the following specifications: \yskip\textindent{1)}It should open file |term←in| for input from the terminal. (The file |term←out| will already be open for output to the terminal.) \textindent{2)}If the user has given a command line, this line should be considered the first line of terminal input. Otherwise the user should be prompted with `\.{**}', and the first line of input should be whatever is typed in response. \textindent{3)}The first line of input, which might or might not be a command line, should appear in locations |first| to |last-1| of the |buffer| array. \textindent{4)}The global variable |loc| should be set so that the character that \MF\ reads next is in |buffer[loc]|. This character should not be blank, and we should have |loc<last|. \yskip\noindent(It may be necessary to prompt the user several times before a non-blank line comes in. The prompt is `\.{**}' instead of the later `\.*' because the meaning is slightly different: `\.{input}' need not be typed immediately after~`\.{**}'.) @d loc==cur←input.loc←field {location of first unread character in |buffer|} @ The following program does the required initialization without retrieving a possible command line. It should be clear how to modify this routine to deal with command lines, if the system permits them. @↑system dependencies@> @p function init←terminal:boolean; {gets the terminal input started} label exit; begin t←open←in; loop@+begin wake←up←terminal; write(term←out,'**'); update←terminal; @.**@> if not input←ln(term←in,true) then {this shouldn't happen} begin write←ln(term←out); write(term←out,'! End of file on the terminal... why?'); @.End of file on the terminal@> init←terminal:=false; return; end; loc:=first; while (loc<last)and(buffer[loc]=" ") do incr(loc); if loc<last then begin init←terminal:=true; return; {return unless the line was all blank} end; write←ln(term←out,'Please type the name of your input file.'); end; exit:end; @* \[4] String handling. Symbolic token names and diagnostic messages are variable-length strings of seven-bit characters. Since \PASCAL\ does not have a well-developed string mechanism, \MF\ does all of its string processing by homegrown methods. Elaborate facilities for dynamic strings are not needed, so all of the necessary operations can be handled with a simple data structure. The array |str←pool| contains all of the (seven-bit) ASCII codes in all of the strings, and the array |str←start| contains indices of the starting points of each string. Strings are referred to by integer numbers, so that string number |s| comprises the characters |str←pool[j]| for |str←start[s]<=j<str←start[s+1]|. Additional integer variables |pool←ptr| and |str←ptr| indicate the number of entries used so far in |str←pool| and |str←start|, respectively; locations |str←pool[pool←ptr]| and |str←start[str←ptr]| are ready for the next string to be allocated. String numbers 0 to 127 are reserved for strings that correspond to single ASCII characters. This is in accordance with the conventions of \.{WEB}, @.WEB@> which converts single-character strings into the ASCII code number of the single character involved, while it converts other strings into integers and builds a string pool file. Thus, when the string constant \.{"."} appears in the program below, \.{WEB} converts it into the integer 46, which is the ASCII code for a period, while \.{WEB} will convert a string like \.{"hello"} into some integer greater than~127. String number 46 will presumably be the single character `\..'\thinspace; but some ASCII codes have no standard visible representation, and \MF\ may need to be able to print an arbitrary ASCII character, so the first 128 strings are used to specify exactly what should be printed for each of the 128 possibilities. Elements of the |str←pool| array must be ASCII codes that can actually be printed; i.e., they must have an |xchr| equivalent in the local character set. (This restriction applies only to preloaded strings, not to those generated dynamically by the user.) @<Types...@>= @!pool←pointer = 0..pool←size; {for variables that point into |str←pool|} @!str←number = 0..max←strings; {for variables that point into |str←start|} @ @<Glob...@>= @!str←pool:packed array[pool←pointer] of ASCII←code; {the characters} @!str←start : array[str←number] of pool←pointer; {the starting pointers} @!pool←ptr : pool←pointer; {first unused position in |str←pool|} @!str←ptr : str←number; {start of the current string being created} @!init←pool←ptr : pool←pointer; {the starting value of |pool←ptr|} @!init←str←ptr : str←number; {the starting value of |str←ptr|} @!max←pool←ptr : pool←pointer; {the maximum so far of |pool←ptr|} @!max←str←ptr : str←number; {the maximum so far of |str←ptr|} @ Several of the elementary string operations are performed using \.{WEB} macros instead of using \PASCAL\ procedures, because many of the operations are done quite frequently and we want to avoid the overhead of procedure calls. For example, here is a simple macro that computes the length of a string. @.WEB@> @d length(#)==(str←start[#+1]-str←start[#]) {the number of characters in string number \#} @ The length of the current string is called |cur←length|: @d cur←length == (pool←ptr - str←start[str←ptr]) @ Strings are created by appending character codes to |str←pool|. The macro called |append←char|, defined here, does not check to see if the value of |pool←ptr| has gotten too high; this test is supposed to be made before |append←char| is used. To test if there is room to append |l| more characters to |str←pool|, we shall write |str←room(l)|, which aborts \MF\ and gives an apologetic error message if there isn't enough room. @d append←char(#) == {put |ASCII←code| \# at the end of |str←pool|} begin str←pool[pool←ptr]:=#; incr(pool←ptr); end @d str←room(#) == {make sure that the pool hasn't overflowed} begin if pool←ptr+# > max←pool←ptr then begin if pool←ptr+# > pool←size then overflow("pool size",pool←size-init←pool←ptr); @:METAFONT capacity exceeded pool size}{\quad pool size@> max←pool←ptr:=pool←ptr+#; end; end @ \MF's string expressions are implemented in a brute-force way: Every new string or substring that is needed is simply copied into the string pool. Such a scheme can be justified because string expressions aren't a big deal in \MF\ applications; strings rarely need to be saved from one statement to the next. But it would waste space needlessly if we didn't try to reclaim the space of strings that are going to be used only once. Therefore a simple reference count mechanism is provided: If there are @↑reference counts@> no references to a certain string from elsewhere in the program, and if there are no references to any strings created subsequent to it, then the string space will be reclaimed. The number of references to string number |s| will be |str←ref[s]|. The special value |str←ref[s]=max←str←ref=127| is used to denote an unknown positive number of references; such strings will never be recycled. If a string is ever referred to more than 126 times, simultaneously, we put it in this category. Hence a single byte suffices to store each |str←ref|. @d max←str←ref=127 {``infinite'' number of references} @d add←str←ref(#)==begin if str←ref[#]<max←str←ref then incr(str←ref[#]); end @<Glob...@>= @!str←ref:array[str←number] of 0..max←str←ref; @ Here's what we do when a string reference disappears: @d delete←str←ref(#)== begin if str←ref[#]<max←str←ref then if str←ref[#]>1 then decr(str←ref[#])@+else flush←string(#); end @<Declare the procedure called |flush←string|@>= procedure flush←string(@!s:str←number); begin if s<str←ptr-1 then str←ref[s]:=0 else repeat decr(str←ptr); until str←ref[str←ptr-1]<>0; pool←ptr:=str←start[str←ptr]; end; @ Once a sequence of characters has been appended to |str←pool|, it officially becomes a string when the function |make←string| is called. This function returns the identification number of the new string as its value. @p function make←string : str←number; {current string enters the pool} begin if str←ptr=max←str←ptr then begin if str←ptr=max←strings then overflow("number of strings",max←strings-init←str←ptr); @:METAFONT capacity exceeded number of strings}{\quad number of strings@> incr(max←str←ptr); end; str←ref[str←ptr]:=1; incr(str←ptr); str←start[str←ptr]:=pool←ptr; make←string:=str←ptr-1; end; @ The following subroutine compares string |s| with another string of the same length that appears in |buffer| starting at position |k|; the result is |true| if and only if the strings are equal. @p function str←eq←buf(@!s:str←number;@!k:integer):boolean; {test equality of strings} label not←found; {loop exit} var @!j: pool←pointer; {running index} @!result: boolean; {result of comparison} begin j:=str←start[s]; while j<str←start[s+1] do begin if str←pool[j]<>buffer[k] then begin result:=false; goto not←found; end; incr(j); incr(k); end; result:=true; not←found: str←eq←buf:=result; end; @ Here is a similar routine, but it compares two strings in the string pool, and it does not assume that they have the same length. If the first string is lexicographically greater than, less than, or equal to the second, the result is respectively positive, negative, or zero. @p function str←vs←str(@!s,@!t:str←number):integer; {test equality of strings} label exit; var @!j,@!k: pool←pointer; {running indices} @!ls,@!lt:integer; {lengths} @!l:integer; {length remaining to test} begin ls:=length(s); lt:=length(t); if ls<=lt then l:=ls@+else l:=lt; j:=str←start[s]; k:=str←start[t]; while l>0 do begin if str←pool[j]<>str←pool[k] then begin str←vs←str:=str←pool[j]-str←pool[k]; return; end; incr(j); incr(k); decr(l); end; str←vs←str:=ls-lt; exit:end; @ The initial values of |str←pool|, |str←start|, |pool←ptr|, and |str←ptr| are computed by the \.{INIMF} program, based in part on the information that \.{WEB} has output while processing \MF. @.INIMF@> @↑string pool@> @p @!init function get←strings←started:boolean; {initializes the string pool, but returns |false| if something goes wrong} label done,exit; var @!k,@!l:0..127; {small indices or counters} @!m,@!n:text←char; {characters input from |pool←file|} @!g:str←number; {garbage} @!a:integer; {accumulator for check sum} @!c:boolean; {check sum has been checked} begin pool←ptr:=0; str←ptr:=0; max←pool←ptr:=0; max←str←ptr:=0; str←start[0]:=0; @<Make the first 128 strings@>; @<Read the other strings from the \.{MF.POOL} file and return |true|, or give an error message and return |false|@>; exit:end; tini @ @<Make the first 128...@>= for k:=0 to 127 do begin if (@<Character |k| cannot be printed@>) then begin append←char("↑"); append←char("↑"); if k<@'100 then append←char(k+@'100) else append←char(k-@'100); end else append←char(k); g:=make←string; str←ref[g]:=max←str←ref; end @ The first 128 strings will contain 95 standard ASCII characters, and the other 33 characters will be printed in three-symbol form like `\.{\↑\↑A}' unless a system-dependent change is made here. Installations that have an extended character set, where for example |xchr[@'32]=@t\.{\'↑↑Z\'}@>|, would like string @'32 to be the single character @'32 instead of the three characters @'136, @'136, @'132 (\.{\↑\↑Z}). On the other hand, even people with an extended character set will want to represent string @'15 by \.{\↑\↑M}, since @'15 is ASCII's ``carriage return'' code; the idea is to produce visible strings instead of tabs or line-feeds or carriage-returns or bell-rings or characters that are treated anomalously in text files. The boolean expression defined here should be |true| unless \MF\ internal code number~|k| corresponds to a non-troublesome visible symbol in the local character set. At MIT, for example, the appropriate formula would be `|k in [0,@'10..@'12,@'14,@'15,@'33,@'177]|'. If character |k| cannot be printed, then character |k+@'100| or |k-@'100| must be printable; thus, at least 64 printable characters are needed. @↑character set dependencies@> @↑system dependencies@> @<Character |k| cannot be printed@>= (k<" ")or(k>"~") @ When the \.{WEB} system program called \.{TANGLE} processes the \.{MF.WEB} description that you are now reading, it outputs the \PASCAL\ program \.{MF.PAS} and also a string pool file called \.{MF.POOL}. The \.{INIMF} @.WEB@>@.INIMF@> program reads the latter file, where each string appears as a two-digit decimal length followed by the string itself, and the information is recorded in \MF's string memory. @<Glob...@>= @!init @!pool←file:alpha←file; {the string-pool file output by \.{TANGLE}} tini @ @d bad←pool(#)==begin wake←up←terminal; write←ln(term←out,#); a←close(pool←file); get←strings←started:=false; return; end @<Read the other strings...@>= name←of←file:=pool←name; {we needn't set |name←length|} if a←open←in(pool←file) then begin c:=false; repeat @<Read one string, but return |false| if the string memory space is getting too tight for comfort@>; until c; a←close(pool←file); get←strings←started:=true; end else bad←pool('! I can''t read MF.POOL.') @.I can't read MF.POOL@> @ @<Read one string...@>= begin if eof(pool←file) then bad←pool('! MF.POOL has no check sum.'); @.MF.POOL has no check sum@> read(pool←file,m,n); {read two digits of string length} if m='*' then @<Check the pool check sum@> else begin if (xord[m]<"0")or(xord[m]>"9")or@| (xord[n]<"0")or(xord[n]>"9") then bad←pool('! MF.POOL line doesn''t begin with two digits.'); @.MF.POOL line doesn't...@> l:=xord[m]*10+xord[n]-"0"*11; {compute the length} if pool←ptr+l+string←vacancies>pool←size then bad←pool('! You have to increase POOLSIZE.'); @.You have to increase POOLSIZE@> for k:=1 to l do begin if eoln(pool←file) then m:=' '@+else read(pool←file,m); append←char(xord[m]); end; read←ln(pool←file); g:=make←string; str←ref[g]:=max←str←ref; end; end @ The \.{WEB} operation \.{@@\$} denotes the value that should be at the end of this \.{MF.POOL} file; any other value means that the wrong pool file has been loaded. @↑check sum@> @<Check the pool check sum@>= begin a:=0; k:=1; loop@+ begin if (xord[n]<"0")or(xord[n]>"9") then bad←pool('! MF.POOL check sum doesn''t have nine digits.'); @.MF.POOL check sum...@> a:=10*a+xord[n]-"0"; if k=9 then goto done; incr(k); read(pool←file,n); end; done: if a<>@$ then bad←pool('! MF.POOL doesn''t match; TANGLE me again.'); @.MF.POOL doesn't match@> c:=true; end @* \[5] On-line and off-line printing. Messages that are sent to a user's terminal and to the transcript-log file are produced by several `|print|' procedures. These procedures will direct their output to a variety of places, based on the setting of the global variable |selector|, which has the following possible values: \yskip \hang |term←and←log|, the normal setting, prints on the terminal and on the transcript file. \hang |log←only|, prints only on the transcript file. \hang |term←only|, prints only on the terminal. \hang |no←print|, doesn't print at all. This is used only in rare cases before the transcript file is open. \hang |pseudo|, puts output into a cyclic buffer that is used by the |show←context| routine; see that routine for the reasoning behind this curious mode. \hang |new←string|, appends the output to the current string in the string pool. \yskip \noindent The symbolic names `|term←and←log|', etc., have been assigned numeric codes that satisfy the convenient relations |no←print+1=term←only|, |no←print+2=log←only|, |term←only+2=log←only+1=term←and←log|. Three additional global variables, |tally| and |term←offset| and |file←offset|, record the number of characters that have been printed since they were most recently cleared to zero. We use |tally| to record the length of (possibly very long) stretches of printing; |term←offset| and |file←offset|, on the other hand, keep track of how many characters have appeared so far on the current line that has been output to the terminal or to the transcript file, respectively. @d no←print=0 {|selector| setting that makes data disappear} @d term←only=1 {printing is destined for the terminal only} @d log←only=2 {printing is destined for the transcript file only} @d term←and←log=3 {normal |selector| setting} @d pseudo=4 {special |selector| setting for |show←context|} @d new←string=5 {printing is deflected to the string pool} @d max←selector=5 {highest selector setting} @<Glob...@>= @!log←file : alpha←file; {transcript of \MF\ session} @!selector : 0..max←selector; {where to print a message} @!dig : array[0..22] of 0..15; {digits in a number being output} @!tally : integer; {the number of characters recently printed} @!term←offset : 0..max←print←line; {the number of characters on the current terminal line} @!file←offset : 0..max←print←line; {the number of characters on the current file line} @!trick←buf:array[0..error←line] of ASCII←code; {circular buffer for pseudoprinting} @!trick←count: integer; {threshold for pseudoprinting, explained later} @!first←count: integer; {another variable for pseudoprinting} @ @<Initialize the output routines@>= selector:=term←only; tally:=0; term←offset:=0; file←offset:=0; @ Macro abbreviations for output to the terminal and to the log file are defined here for convenience. Some systems need special conventions for terminal output, and it is possible to adhere to those conventions by changing |wterm|, |wterm←ln|, and |wterm←cr| here. @↑system dependencies@> @d wterm(#)==write(term←out,#) @d wterm←ln(#)==write←ln(term←out,#) @d wterm←cr==write←ln(term←out) @d wlog(#)==write(log←file,#) @d wlog←ln(#)==write←ln(log←file,#) @d wlog←cr==write←ln(log←file) @ To end a line of text output, we call |print←ln|. @<Basic print...@>= procedure print←ln; {prints an end-of-line} begin case selector of term←and←log: begin wterm←cr; wlog←cr; term←offset:=0; file←offset:=0; end; log←only: begin wlog←cr; file←offset:=0; end; term←only: begin wterm←cr; term←offset:=0; end; no←print,pseudo,new←string: do←nothing; end; {there are no other cases} end; {note that |tally| is not affected} @ The |print←char| procedure sends one character to the desired destination, using the |xchr| array to map it into an external character compatible with |input←ln|. All printing comes through |print←ln| or |print←char|. @<Basic printing...@>= procedure print←char(@!s:ASCII←code); {prints a single character} begin case selector of term←and←log: begin wterm(xchr[s]); wlog(xchr[s]); incr(term←offset); incr(file←offset); if term←offset=max←print←line then begin wterm←cr; term←offset:=0; end; if file←offset=max←print←line then begin wlog←cr; file←offset:=0; end; end; log←only: begin wlog(xchr[s]); incr(file←offset); if file←offset=max←print←line then print←ln; end; term←only: begin wterm(xchr[s]); incr(term←offset); if term←offset=max←print←line then print←ln; end; no←print: do←nothing; pseudo: if tally<trick←count then trick←buf[tally mod error←line]:=s; new←string: begin if pool←ptr<pool←size then append←char(s); end; {we drop characters if the string space is full} end; {there are no other cases} incr(tally); end; @ An entire string is output by calling |print|. Note that if we are outputting the single standard ASCII character \.c, we could call |print("c")|, since |"c"=99| is the number of a single-character string, as explained above. But |print←char("c")| is quicker, so \MF\ goes directly to the |print←char| routine when it knows that this is safe. (The present implementation assumes that it is always safe to print a visible ASCII character.) @↑system dependencies@> @<Basic print...@>= procedure print(@!s:integer); {prints string |s|} var @!j:pool←pointer; {current character code position} begin if (s<0)or(s>=str←ptr) then s:="???"; {this can't happen} @.???@> j:=str←start[s]; while j<str←start[s+1] do begin print←char(str←pool[j]); incr(j); end; end; @ Sometimes it's necessary to print a string whose characters may not be visible ASCII codes. In that case |slow←print| is used. @<Basic print...@>= procedure slow←print(@!s:integer); {prints string |s|} var @!j:pool←pointer; {current character code position} begin if (s<0)or(s>=str←ptr) then s:="???"; {this can't happen} @.???@> j:=str←start[s]; while j<str←start[s+1] do begin print(str←pool[j]); incr(j); end; end; @ Here is the very first thing that \MF\ prints: a headline that identifies the version number and base name. The |term←offset| variable is temporarily incorrect, but the discrepancy is not serious since we assume that the banner and base identifier together will occupy at most |max←print←line| character positions. @<Initialize the output...@>= wterm(banner); if base←ident=0 then wterm←ln(' (no base preloaded)') else begin print(base←ident); print←ln; end; update←terminal; @ The procedure |print←nl| is like |print|, but it makes sure that the string appears at the beginning of a new line. @<Basic print...@>= procedure print←nl(@!s:str←number); {prints string |s| at beginning of line} begin if ((term←offset>0)and(odd(selector)))or@| ((file←offset>0)and(selector>=log←only)) then print←ln; print(s); end; @ An array of digits in the range |0..9| is printed by |print←the←digs|. @<Basic print...@>= procedure print←the←digs(@!k:eight←bits); {prints |dig[k-1]|$\,\ldots\,$|dig[0]|} begin while k>0 do begin decr(k); print←char("0"+dig[k]); end; end; @ The following procedure, which prints out the decimal representation of a given integer |n|, has been written carefully so that it works properly if |n=0| or if |(-n)| would cause overflow. It does not apply |mod| or |div| to negative arguments, since such operations are not implemented consistently by all \PASCAL\ compilers. @<Basic print...@>= procedure print←int(@!n:integer); {prints an integer in decimal form} var k:0..23; {index to current digit; we assume that $|n|<10↑{23}$} @!m:integer; {used to negate |n| in possibly dangerous cases} begin k:=0; if n<0 then begin print←char("-"); if n>-100000000 then negate(n) else begin m:=-1-n; n:=m div 10; m:=(m mod 10)+1; k:=1; if m<10 then dig[0]:=m else begin dig[0]:=0; incr(n); end; end; end; repeat dig[k]:=n mod 10; n:=n div 10; incr(k); until n=0; print←the←digs(k); end; @ \MF\ also makes use of a trivial procedure to print two digits. The following subroutine is usually called with a parameter in the range |0<=n<=99|. @p procedure print←dd(@!n:integer); {prints two least significant digits} begin n:=abs(n) mod 100; print←char("0"+(n div 10)); print←char("0"+(n mod 10)); end; @ Here is a procedure that asks the user to type a line of input, assuming that the |selector| setting is either |term←only| or |term←and←log|. The input is placed into locations |first| through |last-1| of the |buffer| array, and echoed on the transcript file if appropriate. This procedure is never called when |interaction<scroll←mode|. @d prompt←input(#)==begin wake←up←terminal; print(#); term←input; end {prints a string and gets a line of input} @p procedure term←input; {gets a line from the terminal} var @!k:0..buf←size; {index into |buffer|} begin update←terminal; {Now the user sees the prompt for sure} if not input←ln(term←in,true) then fatal←error("End of file on the terminal!"); @.End of file on the terminal@> term←offset:=0; {the user's line ended with \<\rm return>} decr(selector); {prepare to echo the input} if last<>first then for k:=first to last-1 do print(buffer[k]); print←ln; buffer[last]:="%"; incr(selector); {restore previous status} end; @* \[6] Reporting errors. When something anomalous is detected, \MF\ typically does something like this: $$\vbox{\halign{#\hfil\cr |print←err("Something anomalous has been detected");|\cr |help3("This is the first line of my offer to help.")|\cr |("This is the second line. I'm trying to")|\cr |("explain the best way for you to proceed.");|\cr |error;|\cr}}$$ A two-line help message would be given using |help2|, etc.; these informal helps should use simple vocabulary that complements the words used in the official error message that was printed. (Outside of the U.S.A., the help messages should preferably be translated into the local vernacular. Each line of help is at most 60 characters long, in the present implementation, so that |max←print←line| will not be exceeded.) The |print←err| procedure supplies a `\.!' before the official message, and makes sure that the terminal is awake if a stop is going to occur. The |error| procedure supplies a `\..' after the official message, then it shows the location of the error; and if |interaction=error←stop←mode|, it also enters into a dialog with the user, during which time the help message may be printed. @↑system dependencies@> @ The global variable |interaction| has four settings, representing increasing amounts of user interaction: @d batch←mode=0 {omits all stops and omits terminal output} @d nonstop←mode=1 {omits all stops} @d scroll←mode=2 {omits error stops} @d error←stop←mode=3 {stops at every opportunity to interact} @d print←err(#)==begin if interaction=error←stop←mode then wake←up←terminal; print←nl("! "); print(#); @.!@> end @<Glob...@>= @!interaction:batch←mode..error←stop←mode; {current level of interaction} @ @<Set init...@>=interaction:=error←stop←mode; @ \MF\ is careful not to call |error| when the print |selector| setting might be unusual. The only possible values of |selector| at the time of error messages are \yskip\hang|no←print| (when |interaction=batch←mode| and |log←file| not yet open); \hang|term←only| (when |interaction>batch←mode| and |log←file| not yet open); \hang|log←only| (when |interaction=batch←mode| and |log←file| is open); \hang|term←and←log| (when |interaction>batch←mode| and |log←file| is open). @<Initialize the print |selector| based on |interaction|@>= if interaction=batch←mode then selector:=no←print@+else selector:=term←only @ A global variable |deletions←allowed| is set |false| if the |get←next| routine is active when |error| is called; this ensures that |get←next| will never be called recursively. @↑recursion@> The global variable |history| records the worst level of error that has been detected. It has four possible values: |spotless|, |warning←issued|, |error←message←issued|, and |fatal←error←stop|. Another global variable, |error←count|, is increased by one when an |error| occurs without an interactive dialog, and it is reset to zero at the end of every statement. If |error←count| reaches 100, \MF\ decides that there is no point in continuing further. @d spotless=0 {|history| value when nothing has been amiss yet} @d warning←issued=1 {|history| value when |begin←diagnostic| has been called} @d error←message←issued=2 {|history| value when |error| has been called} @d fatal←error←stop=3 {|history| value when termination was premature} @<Glob...@>= @!deletions←allowed:boolean; {is it safe for |error| to call |get←next|?} @!history:spotless..fatal←error←stop; {has the source input been clean so far?} @!error←count:-1..100; {the number of scrolled errors since the last statement ended} @ The value of |history| is initially |fatal←error←stop|, but it will be changed to |spotless| if \MF\ survives the initialization process. @<Set init...@>= deletions←allowed:=true; error←count:=0; {|history| is initialized elsewhere} @ Since errors can be detected almost anywhere in \MF, we want to declare the error procedures near the beginning of the program. But the error procedures in turn use some other procedures, which need to be declared |forward| before we get to |error| itself. It is possible for |error| to be called recursively if some error arises when |get←next| is being used to delete a token, or if some fatal error occurs while \MF\ is trying to fix a non-fatal one. But such recursion @↑recursion@> is never more than one level deep. @<Error handling...@>= procedure@?normalize←selector; forward;@t\2@>@/ procedure@?get←next; forward;@t\2@>@/ procedure@?term←input; forward;@t\2@>@/ procedure@?show←context; forward;@t\2@>@/ procedure@?begin←file←reading; forward;@t\2@>@/ procedure@?open←log←file; forward;@t\2@>@/ procedure@?close←files←and←terminate; forward;@t\2@>@/ procedure@?clear←for←error←prompt; forward;@t\2@>@/ @t\4\hskip-\fontdimen2\font@>@;@+@!debug@+procedure@?debug←help; forward;@;@+gubed@;@/ @t\4@>@<Declare the procedure called |flush←string|@> @ Individual lines of help are recorded in the array |help←line|, which contains entries in positions |0..(help←ptr-1)|. They should be printed in reverse order, i.e., with |help←line[0]| last. @d hlp1(#)==help←line[0]:=#;@+end @d hlp2(#)==help←line[1]:=#; hlp1 @d hlp3(#)==help←line[2]:=#; hlp2 @d hlp4(#)==help←line[3]:=#; hlp3 @d hlp5(#)==help←line[4]:=#; hlp4 @d hlp6(#)==help←line[5]:=#; hlp5 @d help0==help←ptr:=0 {sometimes there might be no help} @d help1==@+begin help←ptr:=1; hlp1 {use this with one help line} @d help2==@+begin help←ptr:=2; hlp2 {use this with two help lines} @d help3==@+begin help←ptr:=3; hlp3 {use this with three help lines} @d help4==@+begin help←ptr:=4; hlp4 {use this with four help lines} @d help5==@+begin help←ptr:=5; hlp5 {use this with five help lines} @d help6==@+begin help←ptr:=6; hlp6 {use this with six help lines} @<Glob...@>= @!help←line:array[0..5] of str←number; {helps for the next |error|} @!help←ptr:0..6; {the number of help lines present} @!use←err←help:boolean; {should the |err←help| string be shown?} @!err←help:str←number; {a string set up by \&{errhelp}} @ @<Set init...@>= help←ptr:=0; use←err←help:=false; err←help:=0; @ The |jump←out| procedure just cuts across all active procedure levels and goes to |end←of←MF|. This is the only nonlocal |@!goto| statement in the whole program. It is used when there is no recovery from a particular error. Some \PASCAL\ compilers do not implement non-local |goto| statements. @↑system dependencies@> In such cases the body of |jump←out| should simply be `|close←files←and←terminate|;\thinspace' followed by a call on some system procedure that quietly terminates the program. @<Error hand...@>= procedure jump←out; begin goto end←of←MF; end; @ Here now is the general |error| routine. @<Error hand...@>= procedure error; {completes the job of error reporting} label continue,exit; var @!c:ASCII←code; {what the user types} @!s1,@!s2,@!s3:integer; {used to save global variables when deleting tokens} @!j:pool←pointer; {character position being printed} begin if history<error←message←issued then history:=error←message←issued; print←char("."); show←context; if interaction=error←stop←mode then @<Get user's advice and |return|@>; incr(error←count); if error←count=100 then begin print←nl("(That makes 100 errors; please try again.)"); @.That makes 100 errors...@> history:=fatal←error←stop; jump←out; end; @<Put help message on the transcript file@>; exit:end; @ @<Get user's advice...@>= loop@+begin continue: clear←for←error←prompt; prompt←input("? "); @.?@> if last=first then return; c:=buffer[first]; if c>="a" then c:=c+"A"-"a"; {convert to uppercase} @<Interpret code |c| and |return| if done@>; end @ It is desirable to provide an `\.E' option here that gives the user an easy way to return from \MF\ to the system editor, with the offending line ready to be edited. But such an extension requires some system wizardry, so the present implementation simply types out what file should be edited and the relevant line number. @↑system dependencies@> There is a secret `\.D' option available when the debugging routines have not been commented out. @↑debugging@> @<Interpret code |c| and |return| if done@>= case c of "0","1","2","3","4","5","6","7","8","9": if deletions←allowed then @<Delete |c-"0"| tokens and |goto continue|@>; @t\4\4@>@;@+@!debug "D":begin debug←help;goto continue;@+end;@+gubed@/ "E": if file←ptr>0 then begin print←nl("You want to edit file "); @.You want to edit file x@> print(input←stack[file←ptr].name←field); print(" at line "); print←int(line); interaction:=scroll←mode; jump←out; end; "H": @<Print the help information and |goto continue|@>; "I":@<Introduce new material from the terminal and |return|@>; "Q","R","S":@<Change the interaction level and |return|@>; "X":begin interaction:=scroll←mode; jump←out; end; othercases do←nothing endcases;@/ @<Print the menu of available options@> @ @<Print the menu...@>= begin print("Type <return> to proceed, S to scroll future error messages,");@/ @.Type <return> to proceed...@> print←nl("R to run without stopping, Q to run quietly,");@/ print←nl("I to insert something, "); if file←ptr>0 then print("E to edit your file,"); if deletions←allowed then print←nl("1 or ... or 9 to ignore the next 1 to 9 tokens of input,"); print←nl("H for help, X to quit."); end @ Here the author of \MF\ apologizes for making use of the numerical relation between |"Q"|, |"R"|, |"S"|, and the desired interaction settings |batch←mode|, |nonstop←mode|, |scroll←mode|. @↑Knuth, Donald Ervin@> @<Change the interaction...@>= begin error←count:=0; interaction:=batch←mode+c-"Q"; print("OK, entering "); case c of "Q":begin print("batchmode"); decr(selector); end; "R":print("nonstopmode"); "S":print("scrollmode"); end; {there are no other cases} print("..."); print←ln; update←terminal; return; end @ When the following code is executed, |buffer[(first+1)..(last-1)]| may contain the material inserted by the user; otherwise another prompt will be given. In order to understand this part of the program fully, you need to be familiar with \MF's input stacks. @<Introduce new material...@>= begin begin←file←reading; {enter a new syntactic level for terminal input} if last>first+1 then begin loc:=first+1; buffer[first]:=" "; end else begin prompt←input("insert>"); loc:=first; @.insert>@> end; first:=last+1; cur←input.limit←field:=last; return; end @ We allow deletion of up to 99 tokens at a time. @<Delete |c-"0"| tokens...@>= begin s1:=cur←cmd; s2:=cur←mod; s3:=cur←sym; OK←to←interrupt:=false; if (last>first+1) and (buffer[first+1]>="0")and(buffer[first+1]<="9") then c:=c*10+buffer[first+1]-"0"*11 else c:=c-"0"; while c>0 do begin get←next; {one-level recursive call of |error| is possible} @<Decrease the string reference count, if the current token is a string@>; decr(c); end; cur←cmd:=s1; cur←mod:=s2; cur←sym:=s3; OK←to←interrupt:=true; help2("I have just deleted some text, as you asked.")@/ ("You can now delete more, or insert, or whatever."); show←context; goto continue; end @ @<Print the help info...@>= begin if use←err←help then begin @<Print the string |err←help|, possibly on several lines@>; use←err←help:=false; end else begin if help←ptr=0 then help2("Sorry, I don't know how to help in this situation.")@/ @t\kern1em@>("Maybe you should try asking a human?"); repeat decr(help←ptr); print(help←line[help←ptr]); print←ln; until help←ptr=0; end; help4("Sorry, I already gave what help I could...")@/ ("Maybe you should try asking a human?")@/ ("An error might have occurred before I noticed any problems.")@/ ("``If all else fails, read the instructions.''");@/ goto continue; end @ @<Print the string |err←help|, possibly on several lines@>= j:=str←start[err←help]; while j<str←start[err←help+1] do begin if str←pool[j]<>"%" then print(str←pool[j]) else if j+1=str←start[err←help+1] then print←ln else if str←pool[j+1]<>"%" then print←ln else begin incr(j); print←char("%"); end; incr(j); end @ @<Put help message on the transcript file@>= if interaction>batch←mode then decr(selector); {avoid terminal output} if use←err←help then begin print←nl(""); @<Print the string |err←help|, possibly on several lines@>; end else while help←ptr>0 do begin decr(help←ptr); print←nl(help←line[help←ptr]); end; print←ln; if interaction>batch←mode then incr(selector); {re-enable terminal output} print←ln @ In anomalous cases, the print selector might be in an unknown state; the following subroutine is called to fix things just enough to keep running a bit longer. The normal idea of |batch←mode| is that nothing at all should be written on the terminal. However, in the unusual case that a fatal error has occurred but no log file could be opened, we make an exception and allow an explanatory message to be seen. @p procedure normalize←selector; begin if job←name>0 then selector:=term←and←log else selector:=term←only; if job←name=0 then open←log←file; if interaction=batch←mode then decr(selector); end; @ The following procedure prints \MF's last words before dying. @d succumb==begin if interaction=error←stop←mode then interaction:=scroll←mode; {no more interaction} error; @!debug if interaction>batch←mode then debug←help;@+gubed@;@/ history:=fatal←error←stop; jump←out; {irrecoverable error} end @<Error hand...@>= procedure fatal←error(@!s:str←number); {prints |s|, and that's it} begin normalize←selector;@/ print←err("Emergency stop"); help1(s); succumb; @.Emergency stop@> end; @ Here is the most dreaded error message. @<Error hand...@>= procedure overflow(@!s:str←number;@!n:integer); {stop due to finiteness} begin normalize←selector; print←err("METAFONT capacity exceeded, sorry ["); @.METAFONT capacity exceeded ...@> print(s); print←char("="); print←int(n); print←char("]"); help2("If you really absolutely need more capacity,")@/ ("you can ask a wizard to enlarge me."); succumb; end; @ The program might sometime run completely amok, at which point there is no choice but to stop. If no previous error has been detected, that's bad news; a message is printed that is really intended for the \MF\ maintenance person instead of the user (unless the user has been particularly diabolical). The index entries for `this can't happen' may help to pinpoint the problem. @↑dry rot@> @<Error hand...@>= procedure confusion(@!s:str←number); {consistency check violated; |s| tells where} begin normalize←selector; if history<error←message←issued then begin print←err("This can't happen ("); print(s); print←char(")"); @.This can't happen@> help1("I'm broken. Please show this to someone who can fix can fix"); end else begin print←err("I can't go on meeting you like this"); @.I can't go on...@> help2("One of your faux pas seems to have wounded me deeply...")@/ ("in fact, I'm barely conscious. Please fix it and try again."); end; succumb; end; @ Users occasionally want to interrupt \MF\ while it's running. If the \PASCAL\ runtime system allows this, one can implement a routine that sets the global variable |interrupt| to some nonzero value when such an interrupt is signalled. Otherwise there is probably at least a way to make |interrupt| nonzero using the \PASCAL\ debugger. @↑system dependencies@> @↑debugging@> @d check←interrupt==begin if interrupt<>0 then pause←for←instructions; end @<Global...@>= @!interrupt:integer; {should \MF\ pause for instructions?} @!OK←to←interrupt:boolean; {should interrupts be observed?} @ @<Set init...@>= interrupt:=0; OK←to←interrupt:=true; @ When an interrupt has been detected, the program goes into its highest interaction level and lets the user have the full flexibility of the |error| routine. \MF\ checks for interrupts only at times when it is safe to do this. @p procedure pause←for←instructions; begin if OK←to←interrupt then begin interaction:=error←stop←mode; if (selector=log←only)or(selector=no←print) then incr(selector); print←err("Interruption"); @.Interruption@> help3("You rang?")@/ ("Try to insert some instructions for me (e.g.,`I show x'),")@/ ("unless you just want to quit by typing `X'."); deletions←allowed:=false; error; deletions←allowed:=true; interrupt:=0; end; end; @ Many of \MF's error messages state that a missing token has been inserted behind the scenes. We can save string space and program space by putting this common code into a subroutine. @p procedure missing←err(@!s:str←number); begin print←err("Missing `"); print(s); print("' has been inserted"); @.Missing...inserted@> end; @* \[7] Arithmetic with scaled numbers. The principal computations performed by \MF\ are done entirely in terms of integers less than $2↑{31}$ in magnitude; thus, the arithmetic specified in this program can be carried out in exactly the same way on a wide variety of computers, including some small ones. @↑small computers@> But \PASCAL\ does not define the @!|div| operation in the case of negative dividends; for example, the result of |(-2*n-1) div 2| is |-(n+1)| on some computers and |-n| on others. There are two principal types of arithmetic: ``translation-preserving,'' in which the identity |(a+q*b)div b=(a div b)+q| is valid; and ``negation-preserving,'' in which |(-a)div b=-(a div b)|. This leads to two \MF s, which can produce different results, although the differences should be negligible when the language is being used properly. The \TeX\ processor has been defined carefully so that both varieties of arithmetic will produce identical output, but it would be too inefficient to constrain \MF\ in a similar way. @d el←gordo == @'17777777777 {$2↑{31}-1$, the largest value that \MF\ likes} @ One of \MF's most common operations is the calculation of $\lfloor{a+b\over2}\rfloor$, the midpoint of two given integers |a| and~|b|. The only decent way to do this in \PASCAL\ is to write `|(a+b) div 2|'; but on most machines it is far more efficient to calculate `|(a+b)| right shifted one bit'. Therefore the midpoint operation will always be denoted by `|half(a+b)|' in this program. If \MF\ is being implemented with languages that permit binary shifting, the |half| macro should be changed to make this operation as efficient as possible. @d half(#)==(#) div 2 @ A single computation might use several subroutine calls, and it is desirable to avoid producing multiple error messages in case of arithmetic overflow. So the routines below set the global variable |arith←error| to |true| instead of reporting errors directly to the user. @<Glob...@>= @!arith←error:boolean; {has arithmetic overflow occurred recently?} @ @<Set init...@>= arith←error:=false; @ At crucial points the program will say |check←arith|, to test if an arithmetic error has been detected. @d check←arith==begin if arith←error then clear←arith;@+end @p procedure clear←arith; begin print←err("Arithmetic overflow"); @.Arithmetic overflow@> help4("Uh, oh. A little while ago one of the quantities that I was")@/ ("computing got too large, so I'm afraid your answers will be")@/ ("somewhat askew. You'll probably have to adopt different")@/ ("tactics next time. But I shall try to carry on anyway."); error; arith←error:=false; end; @ Addition is not always checked to make sure that it doesn't overflow, but in places where overflow isn't too unlikely the |slow←add| routine is used. @p function slow←add(@!x,@!y:integer):integer; begin if x>=0 then if y<=el←gordo-x then slow←add:=x+y else begin arith←error:=true; slow←add:=el←gordo; end else if -y<=el←gordo+x then slow←add:=x+y else begin arith←error:=true; slow←add:=-el←gordo; end; end; @ Fixed-point arithmetic is done on {\sl scaled integers\/} that are multiples of $2↑{-16}$. In other words, a binary point is assumed to be sixteen bit positions from the right end of a binary computer word. @d quarter←unit == @'40000 {$2↑{14}$, represents 0.250000} @d half←unit == @'100000 {$2↑{15}$, represents 0.50000} @d three←quarter←unit == @'140000 {$3\cdot2↑{14}$, represents 0.75000} @d unity == @'200000 {$2↑{16}$, represents 1.00000} @d two == @'400000 {$2↑{17}$, represents 2.00000} @d three == @'600000 {$2↑{17}+2↑{16}$, represents 3.00000} @<Types...@>= @!scaled = integer; {this type is used for scaled integers} @!small←number=0..63; {this type is self-explanatory} @ The following function is used to create a scaled integer from a decimal fraction $(.d←0d←1\ldots d←{k-1})$, where |0<=k<=17|. The digit $d←i$ is given in |dig[i]|, and the calculation produces a correctly rounded result. @p function round←decimals(@!k:small←number) : scaled; {converts a decimal fraction} var @!a:integer; {the accumulator} begin a:=0; while k>0 do begin decr(k); a:=(a+dig[k]*two) div 10; end; round←decimals:=half(a+1); end; @ Conversely, here is a procedure analogous to |print←int|. If the output of this procedure is subsequently read by \MF\ and converted by the |round←decimals| routine above, it turns out that the original value will be reproduced exactly. A decimal point is printed only if the value is not an integer. If there is more than one way to print the result with the optimum number of digits following the decimal point, the closest possible value is given. The invariant relation in the \&{repeat} loop is that a sequence of decimal digits yet to be printed will yield the original number if and only if they form a fraction~$f$ in the range $s-\delta\L10\cdot2↑{16}f<s$. We can stop if and only if $f=0$ satisfies this condition; the loop will terminate before $s$ can possibly become zero. @<Basic printing...@>= procedure print←scaled(@!s:scaled); {prints scaled real, rounded to five digits} var @!delta:scaled; {amount of allowable inaccuracy} begin if s<0 then begin print←char("-"); negate(s); {print the sign, if negative} end; print←int(s div unity); {print the integer part} s:=10*(s mod unity)+5; if s<>5 then begin delta:=10; print←char("."); repeat if delta>unity then s:=s+@'100000-(delta div 2); {round the final digit} print←char("0"+(s div unity)); s:=10*(s mod unity); delta:=delta*10; until s<=delta; end; end; @ We often want to print two scaled quantities in parentheses, separated by a comma. @<Basic printing...@>= procedure print←two(@!x,@!y:scaled); {prints `|(x,y)|'} begin print←char("("); print←scaled(x); print←char(","); print←scaled(y); print←char(")"); end; @ The |scaled| quantities in \MF\ programs are generally supposed to be less than $2↑{12}$ in absolute value, so \MF\ does much of its internal arithmetic with 28~significant bits of precision. A |fraction| denotes a scaled integer whose binary point is assumed to be 28 bit positions from the right. @d fraction←half==@'1000000000 {$2↑{27}$, represents 0.50000000} @d fraction←one==@'2000000000 {$2↑{28}$, represents 1.00000000} @d fraction←two==@'4000000000 {$2↑{29}$, represents 2.00000000} @d fraction←three==@'6000000000 {$3\cdot2↑{28}$, represents 3.00000000} @d fraction←four==@'10000000000 {$2↑{30}$, represents 4.00000000} @<Types...@>= @!fraction=integer; {this type is used for scaled fractions} @ In fact, the two sorts of scaling discussed above aren't quite sufficient; \MF\ has yet another, used internally to keep track of angles in units of $2↑{-20}$ degrees. @d forty←five←deg==@'264000000 {$45\cdot2↑{20}$, represents $45↑\circ$} @d ninety←deg==@'550000000 {$90\cdot2↑{20}$, represents $90↑\circ$} @d one←eighty←deg==@'1320000000 {$180\cdot2↑{20}$, represents $180↑\circ$} @d three←sixty←deg==@'2640000000 {$360\cdot2↑{20}$, represents $360↑\circ$} @<Types...@>= @!angle=integer; {this type is used for scaled angles} @ The |make←fraction| routine produces the |fraction| equivalent of |p/q|, given integers |p| and~|q|; it computes the integer $f=\lfloor2↑{28}p/q+{1\over2}\rfloor$, when $p$ and $q$ are positive. If |p| and |q| are both of the same scaled type |t|, the ``type relation'' |make←fraction(t,t)=fraction| is valid; and it's also possible to use the subroutine ``backwards,'' using the relation |make←fraction(t,fraction)=t| between scaled types. If the result would have magnitude $2↑{31}$ or more, |make←fraction| sets |arith←error:=true|. Most of \MF's internal computations have been designed to avoid this sort of error. If this subroutine were programmed in assembly language on a typical machine, we could simply compute |(@t$2↑{28}$@>*p)div q|, since a double-precision product can often be input to a fixed-point division instruction. But when we are restricted to \PASCAL\ arithmetic it is necessary either to resort to multiple-precision maneuvering or to use a simple but slow iteration. The multiple-precision technique would be about three times faster than the code adopted here, but it would be comparatively long and tricky, involving about sixteen additional multiplications and divisions. This operation is part of \MF's ``inner loop''; indeed, it will consume nearly 10\% of the running time (exclusive of input and output) if the code below is left unchanged. A machine-dependent recoding will therefore make \MF\ run faster. The present implementation is highly portable, but slow; it avoids multiplication and division except in the initial stage. System wizards should be careful to replace it with a routine that is guaranteed to produce identical results in all cases. @↑system dependencies@> As noted below, a few more routines should also be replaced by machine-dependent code, for efficiency. But when a procedure is not part of the ``inner loop,'' such changes aren't advisable; simplicity and robustness are preferable to trickery, unless the cost is too high. @↑inner loop@> @p function make←fraction(@!p,@!q:integer):fraction; var @!f:integer; {the fraction bits, with a leading 1 bit} @!n:integer; {the integer part of $\vert p/q\vert$} @!negative:boolean; {should the result be negated?} @!be←careful:integer; {disables certain compiler optimizations} begin if p>=0 then negative:=false else begin negate(p); negative:=true; end; if q<=0 then begin debug if q=0 then confusion("/");@+gubed@;@/ @:this can't happen /}{\quad \./@> negate(q); negative:=not negative; end; n:=p div q; p:=p mod q; if n>=8 then begin arith←error:=true; if negative then make←fraction:=-el←gordo@+else make←fraction:=el←gordo; end else begin n:=(n-1)*fraction←one; @<Compute $f=\lfloor 2↑{28}(1+p/q)+{1\over2}\rfloor$@>; if negative then make←fraction:=-(f+n)@+else make←fraction:=f+n; end; end; @ The |repeat| loop here preserves the following invariant relations: (i)~|0<=p<q|; (ii)~$fq+p=2↑k(q+p←0)$, where $k$ is an integer and $p←0$ is the original value of~$p$. Notice that the computation specifies |(p-q)+p| instead of |(p+p)-q|, because the latter could overflow. Let us hope that optimizing compilers do not miss this point; a special variable |be←careful| is used to emphasize the necessary order of computation. Optimizing compilers should keep |be←careful| in a register, not store it in memory. @↑inner loop@> @<Compute $f=\lfloor 2↑{28}(1+p/q)+{1\over2}\rfloor$@>= f:=1; repeat be←careful:=p-q; p:=be←careful+p; if p>=0 then f:=f+f+1 else begin double(f); p:=p+q; end; until f>=fraction←one; be←careful:=p-q; if be←careful+p>=0 then incr(f) @ The dual of |make←fraction| is |take←fraction|, which multiplies a given integer~|q| by a fraction~|f|. When the operands are positive, it computes $p=\lfloor qf/2↑{28}+{1\over2}\rfloor$, a symmetric function of |q| and~|f|. This routine is even more ``inner loopy'' than |make←fraction|; the present implementation consumes almost 20\% of \MF's computation time during typical jobs, so a machine-language substitute is advisable. @↑inner loop@> @↑system dependencies@> @p function take←fraction(@!q:integer;@!f:fraction):integer; var @!p:integer; {the fraction so far} @!negative:boolean; {should the result be negated?} @!n:integer; {additional multiple of $q$} @!be←careful:integer; {disables certain compiler optimizations} begin @<Reduce to the case that |f>=0| and |q>0|@>; if f<fraction←one then n:=0 else begin n:=f div fraction←one; f:=f mod fraction←one; if q<=el←gordo div n then n:=n*q else begin arith←error:=true; n:=el←gordo; end; end; f:=f+fraction←one; @<Compute $p=\lfloor qf/2↑{28}+{1\over2}\rfloor-q$@>; be←careful:=n-el←gordo; if be←careful+p>0 then begin arith←error:=true; n:=el←gordo-p; end; if negative then take←fraction:=-(n+p) else take←fraction:=n+p; end; @ @<Reduce to the case that |f>=0| and |q>0|@>= if f>=0 then negative:=false else begin negate(f); negative:=true; end; if q<0 then begin negate(q); negative:=not negative; end; @ The invariant relations in this case are (i)~$\lfloor(qf+p)/2↑k\rfloor =\lfloor qf←0/2↑{28}+{1\over2}\rfloor$, where $k$ is an integer and $f←0$ is the original value of~$f$; (ii)~$2↑k\L f<2↑{k+1}$. @↑inner loop@> @<Compute $p=\lfloor qf/2↑{28}+{1\over2}\rfloor-q$@>= p:=fraction←half; {that's $2↑{27}$; the invariants hold now with $k=28$} if q<fraction←four then repeat if odd(f) then p:=half(p+q)@+else p:=half(p); f:=half(f); until f=1 else repeat if odd(f) then p:=p+half(q-p)@+else p:=half(p); f:=half(f); until f=1 @ When we want to multiply something by a |scaled| quantity, we use a scheme analogous to |take←fraction| but with a different scaling. Given positive operands, |take←scaled| computes $p=\lfloor qf/2↑{16}+{1\over2}\rfloor$. Once again it is a good idea to use a machine-language replacement if possible; otherwise |take←scaled| will use more than 2\% of the running time when the Computer Modern fonts are being generated. @↑inner loop@> @p function take←scaled(@!q:integer;@!f:scaled):integer; var @!p:integer; {the fraction so far} @!negative:boolean; {should the result be negated?} @!n:integer; {additional multiple of $q$} @!be←careful:integer; {disables certain compiler optimizations} begin @<Reduce to the case that |f>=0| and |q>0|@>; if f<unity then n:=0 else begin n:=f div unity; f:=f mod unity; if q<=el←gordo div n then n:=n*q else begin arith←error:=true; n:=el←gordo; end; end; f:=f+unity; @<Compute $p=\lfloor qf/2↑{16}+{1\over2}\rfloor-q$@>; be←careful:=n-el←gordo; if be←careful+p>0 then begin arith←error:=true; n:=el←gordo-p; end; if negative then take←scaled:=-(n+p) else take←scaled:=n+p; end; @ @<Compute $p=\lfloor qf/2↑{16}+{1\over2}\rfloor-q$@>= p:=half←unit; {that's $2↑{15}$; the invariants hold now with $k=16$} @↑inner loop@> if q<fraction←four then repeat if odd(f) then p:=half(p+q)@+else p:=half(p); f:=half(f); until f=1 else repeat if odd(f) then p:=p+half(q-p)@+else p:=half(p); f:=half(f); until f=1 @ For completeness, there's also |make←scaled|, which computes a quotient as a |scaled| number instead of as a |fraction|. In other words, the result is $\lfloor2↑{16}p/q+{1\over2}\rfloor$, if the operands are positive. \ (This procedure is not used especially often, so it is not part of \MF's inner loop.) @p function make←scaled(@!p,@!q:integer):scaled; var @!f:integer; {the fraction bits, with a leading 1 bit} @!n:integer; {the integer part of $\vert p/q\vert$} @!negative:boolean; {should the result be negated?} @!be←careful:integer; {disables certain compiler optimizations} begin if p>=0 then negative:=false else begin negate(p); negative:=true; end; if q<=0 then begin debug if q=0 then confusion("/");@+gubed@;@/ @:this can't happen /}{\quad \./@> negate(q); negative:=not negative; end; n:=p div q; p:=p mod q; if n>=@'100000 then begin arith←error:=true; if negative then make←scaled:=-el←gordo@+else make←scaled:=el←gordo; end else begin n:=(n-1)*unity; @<Compute $f=\lfloor 2↑{16}(1+p/q)+{1\over2}\rfloor$@>; if negative then make←scaled:=-(f+n)@+else make←scaled:=f+n; end; end; @ @<Compute $f=\lfloor 2↑{16}(1+p/q)+{1\over2}\rfloor$@>= f:=1; repeat be←careful:=p-q; p:=be←careful+p; if p>=0 then f:=f+f+1 else begin double(f); p:=p+q; end; until f>=unity; be←careful:=p-q; if be←careful+p>=0 then incr(f) @ Here is a typical example of how the routines above can be used. It computes the function $${1\over3\tau}f(\theta,\phi)= {\tau↑{-1}\bigl(2+\sqrt2\,(\sin\theta-{1\over16}\sin\phi) (\sin\phi-{1\over16}\sin\theta)(\cos\theta-\cos\phi)\bigr)\over 3\,\bigl(1+{1\over2}(\sqrt5-1)\cos\theta+{1\over2}(3-\sqrt5\,)\cos\phi\bigr)},$$ where $\tau$ is a |scaled| ``tension'' parameter. This is \MF's magic fudge factor for placing the first control point of a curve that starts at an angle $\theta$ and ends at an angle $\phi$ from the straight path. (Actually, if the stated quantity exceeds 4, \MF\ reduces it to~4.) The trigonometric quantity to be multiplied by $\sqrt2$ is less than $\sqrt2$. (It's a sum of eight terms whose absolute values can be bounded using relations such as $\sin\theta\cos\theta\L{1\over2}$.) Thus the numerator is positive; and since the tension $\tau$ is constrained to be at least $3\over4$, the numerator is less than $16\over3$. The denominator is nonnegative and at most~6. Hence the fixed-point calculations below are guaranteed to stay within the bounds of a 32-bit computer word. The angles $\theta$ and $\phi$ are given implicitly in terms of |fraction| arguments |st|, |ct|, |sf|, and |cf|, representing $\sin\theta$, $\cos\theta$, $\sin\phi$, and $\cos\phi$, respectively. @p function velocity(@!st,@!ct,@!sf,@!cf:fraction;@!t:scaled):fraction; var @!acc,@!num,@!denom:integer; {registers for intermediate calculations} begin acc:=take←fraction(st-(sf div 16), sf-(st div 16)); acc:=take←fraction(acc,ct-cf); num:=fraction←two+take←fraction(acc,379625062); {$2↑{28}\sqrt2\approx379625062.497$} denom:=fraction←three+take←fraction(ct,497706707)+take←fraction(cf,307599661); {$3\cdot2↑{27}\cdot(\sqrt5-1)\approx497706706.78$ and $3\cdot2↑{27}\cdot(3-\sqrt5\,)\approx307599661.22$} if t<>unity then num:=make←scaled(num,t); {|make←scaled(fraction,scaled)=fraction|} if num div 4>=denom then velocity:=fraction←four else velocity:=make←fraction(num,denom); end; @ The following somewhat different subroutine tests rigorously if $ab$ is greater than, equal to, or less than~$cd$, given integers $(a,b,c,d)$. In most cases a quick decision is reached. The result is $+1$, 0, or~$-1$ in the three respective cases. @d return←sign(#)==begin ab←vs←cd:=#; return; end @p function ab←vs←cd(@!a,b,c,d:integer):integer; label exit; var @!q,@!r:integer; {temporary registers} begin @<Reduce to the case that |a,c>=0|, |b,d>0|@>; loop@+ begin q := a div d; r := c div b; if q<>r then if q>r then return←sign(1)@+else return←sign(-1); q := a mod d; r := c mod b; if r=0 then if q=0 then return←sign(0)@+else return←sign(1); if q=0 then return←sign(-1); a:=b; b:=q; c:=d; d:=r; end; {now |a>d>0| and |c>b>0|} exit:end; @ @<Reduce to the case that |a...@>= if a<0 then begin negate(a); negate(b); end; if c<0 then begin negate(c); negate(d); end; if d<=0 then begin if b>=0 then if ((a=0)or(b=0))and((c=0)or(d=0)) then return←sign(0) else return←sign(1); if d=0 then if a=0 then return←sign(0)@+else return←sign(-1); q:=a; a:=c; c:=q; q:=-b; b:=-d; d:=q; end else if b<=0 then begin if b<0 then if a>0 then return←sign(-1); if c=0 then return←sign(0) else return←sign(-1); end @ We conclude this set of elementary routines with some simple rounding and truncation operations that are coded in a machine-independent fashion. The routines are slightly complicated because we want them to work without overflow whenever $-2↑{31}\L x<2↑{31}$. @p function floor←scaled(@!x:scaled):scaled; {$2↑{16}\lfloor x/2↑{16}\rfloor$} var @!be←careful:integer; {temporary register} begin if x>=0 then floor←scaled:=x-(x mod unity) else begin be←careful:=x+1; floor←scaled:=x+((-be←careful) mod unity)+1-unity; end; end; @# function floor←unscaled(@!x:scaled):integer; {$\lfloor x/2↑{16}\rfloor$} var @!be←careful:integer; {temporary register} begin if x>=0 then floor←unscaled:=x div unity else begin be←careful:=x+1; floor←unscaled:=-(1+((-be←careful) div unity)); end; end; @# function round←unscaled(@!x:scaled):integer; {$\lfloor x/2↑{16}+.5\rfloor$} var @!be←careful:integer; {temporary register} begin if x>=half←unit then round←unscaled:=1+((x-half←unit) div unity) else if x>=-half←unit then round←unscaled:=0 else begin be←careful:=x+1; round←unscaled:=-(1+((-be←careful-half←unit) div unity)); end; end; @# function round←fraction(@!x:fraction):scaled; {$\lfloor x/2↑{12}+.5\rfloor$} var @!be←careful:integer; {temporary register} begin if x>=2048 then round←fraction:=1+((x-2048) div 4096) else if x>=-2048 then round←fraction:=0 else begin be←careful:=x+1; round←fraction:=-(1+((-be←careful-2048) div 4096)); end; end; @* \[8] Algebraic and transcendental functions. \MF\ computes all of the necessary special functions from scratch, without relying on |real| arithmetic or system subroutines for sines, cosines, etc. @ To get the square root of a |scaled| number |x|, we want to calculate $s=\lfloor 2↑8\!\sqrt x +{1\over2}\rfloor$. If $x>0$, this is the unique integer such that $2↑{16}x-s\L s↑2<2↑{16}x+s$. The following subroutine determines $s$ by an iterative method that maintains the invariant relations $x=2↑{46-2k}x←0\bmod 2↑{30}$, $0<y=\lfloor 2↑{16-2k}x←0\rfloor -s↑2+s\L q=2s$, where $x←0$ is the initial value of $x$. The value of~$y$ might, however, be zero at the start of the first iteration. @p function square←rt(@!x:scaled):scaled; var @!k:small←number; {iteration control counter} @!y,@!q:integer; {registers for intermediate calculations} begin if x<=0 then @<Handle square root of zero or negative argument@> else begin k:=23; q:=2; while x<fraction←two do {i.e., |while x<@t$2↑{29}$@>|\unskip} begin decr(k); x:=x+x+x+x; end; if x<fraction←four then y:=0 else begin x:=x-fraction←four; y:=1; end; repeat @<Decrease |k| by 1, maintaining the invariant relations between |x|, |y|, and~|q|@>; until k=0; square←rt:=half(q); end; end; @ @<Handle square root of zero...@>= begin if x<0 then begin print←err("Square root of "); @.Square root...replaced by 0@> print←scaled(x); print(" has been replaced by 0"); help2("Since I don't take square roots of negative numbers,")@/ ("I'm zeroing this one. Proceed, with fingers crossed."); error; end; square←rt:=0; end @ @<Decrease |k| by 1, maintaining...@>= double(x); double(y); if x>=fraction←four then {note that |fraction←four=@t$2↑{30}$@>|} begin x:=x-fraction←four; incr(y); end; double(x); y:=y+y-q; double(q); if x>=fraction←four then begin x:=x-fraction←four; incr(y); end; if y>q then begin y:=y-q; q:=q+2; end else if y<=0 then begin q:=q-2; y:=y+q; end; decr(k) @ Pythagorean addition $\psqrt{a↑2+b↑2}$ is implemented by an elegant iterative scheme due to Cleve Moler and Donald Morrison [{\sl IBM Journal @↑Moler, Cleve Barry@> @↑Morrison, Donald Ross@> of Research and Development\/ \bf27} (1983), 577--581]. It modifies |a| and~|b| in such a way that their pythagorean sum remains invariant, while the smaller argument decreases. @p function pyth←add(@!a,@!b:integer):integer; label done; var @!r:fraction; {register used to transform |a| and |b|} @!big:boolean; {is the result dangerously near $2↑{31}$?} begin a:=abs(a); b:=abs(b); if a<b then begin r:=b; b:=a; a:=r; end; {now |0<=b<=a|} if a>0 then begin if a<fraction←two then big:=false else begin a:=a div 4; b:=b div 4; big:=true; end; {we reduced the precision to avoid arithmetic overflow} @<Replace |a| by an approximation to $\psqrt{a↑2+b↑2}$@>; if big then if a<fraction←two then a:=a+a+a+a else begin arith←error:=true; a:=el←gordo; end; end; pyth←add:=a; end; @ The key idea here is to reflect the vector $(a,b)$ about the line through $(a,b/2)$. @<Replace |a| by an approximation to $\psqrt{a↑2+b↑2}$@>= loop@+ begin r:=make←fraction(b,a); r:=take←fraction(r,r); {now $r\approx b↑2/a↑2$} if r=0 then goto done; r:=make←fraction(r,fraction←four+r); a:=a+take←fraction(a+a,r); b:=take←fraction(b,r); end; done: @ Here is a similar algorithm for $\psqrt{a↑2-b↑2}$. It converges slowly when $b$ is near $a$, but otherwise it works fine. @p function pyth←sub(@!a,@!b:integer):integer; label done; var @!r:fraction; {register used to transform |a| and |b|} @!big:boolean; {is the input dangerously near $2↑{31}$?} begin a:=abs(a); b:=abs(b); if a<=b then @<Handle erroneous |pyth←sub| and set |a:=0|@> else begin if a<fraction←four then big:=false else begin a:=half(a); b:=half(b); big:=true; end; @<Replace |a| by an approximation to $\psqrt{a↑2-b↑2}$@>; if big then a:=a+a; end; pyth←sub:=a; end; @ @<Replace |a| by an approximation to $\psqrt{a↑2-b↑2}$@>= loop@+ begin r:=make←fraction(b,a); r:=take←fraction(r,r); {now $r\approx b↑2/a↑2$} if r=0 then goto done; r:=make←fraction(r,fraction←four-r); a:=a-take←fraction(a+a,r); b:=take←fraction(b,r); end; done: @ @<Handle erroneous |pyth←sub| and set |a:=0|@>= begin if a<b then begin print←err("Pythagorean subtraction "); print←scaled(a); print("+-+"); print←scaled(b); print(" has been replaced by 0"); @.Pythagorean...@> help2("Since I don't take square roots of negative numbers,")@/ ("I'm zeroing this one. Proceed, with fingers crossed."); error; end; a:=0; end @ The subroutines for logarithm and exponential involve two tables. The first is simple: |two←to←the[k]| equals $2↑k$. The second involves a bit more calculation, which the author claims to have done correctly: |spec←log[k]| is $2↑{27}$ times $\ln\bigl(1/(1-2↑{-k})\bigr)= 2↑{-k}+{1\over2}2↑{-2k}+{1\over3}2↑{-3k}+\cdots\,$, rounded to the nearest integer. @<Glob...@>= @!two←to←the:array[0..30] of integer; {powers of two} @!spec←log:array[1..28] of integer; {special logarithms} @ @<Local variables for initialization@>= @!k:integer; {all-purpose loop index} @ @<Set init...@>= two←to←the[0]:=1; for k:=1 to 30 do two←to←the[k]:=2*two←to←the[k-1]; spec←log[1]:=93032640; spec←log[2]:=38612034; spec←log[3]:=17922280; spec←log[4]:=8662214; spec←log[5]:=4261238; spec←log[6]:=2113709; spec←log[7]:=1052693; spec←log[8]:=525315; spec←log[9]:=262400; spec←log[10]:=131136; spec←log[11]:=65552; spec←log[12]:=32772; spec←log[13]:=16385; for k:=14 to 27 do spec←log[k]:=two←to←the[27-k]; spec←log[28]:=1; @ Here is the routine that calculates $2↑8$ times the natural logarithm of a |scaled| quantity; it is an integer approximation to $2↑{24}\ln(x/2↑{16})$, when |x| is a given positive integer. The method is based on exercise 1.2.2--25 in {\sl The Art of Computer Programming\/}: During the main iteration we have $1\L 2↑{-30}x<1/(1-2↑{1-k})$, and the logarithm of $2↑{30}x$ remains to be added to an accumulator register called~$y$. Three auxiliary bits of accuracy are retained in~$y$ during the calculation, and sixteen auxiliary bits to extend |y| are kept in~|z| during the initial argument reduction. (We add $100\cdot2↑{16}=6553600$ to~|z| and subtract 100 from~|y| so that |z| will not become negative; also, the actual amount subtracted from~|y| is~96, not~100, because we want to add~4 for rounding before the final division by~8.) @p function m←log(@!x:scaled):scaled; var @!y,@!z:integer; {auxiliary registers} @!k:integer; {iteration counter} begin if x<=0 then @<Handle non-positive logarithm@> else begin y:=1302456956+4-100; {$14\times2↑{27}\ln2\approx1302456956.421063$} z:=27595+6553600; {and $2↑{16}\times .421063\approx 27595$} while x<fraction←four do begin double(x); y:=y-93032639; z:=z-48782; end; {$2↑{27}\ln2\approx 93032639.74436163$ and $2↑{16}\times.74436163\approx 48782$} y:=y+(z div unity); k:=2; while x>fraction←four+4 do @<Increase |k| until |x| can be multiplied by a factor of $2↑{-k}$, and adjust $y$ accordingly@>; m←log:=y div 8; end; end; @ @<Increase |k| until |x| can...@>= begin z:=((x-1) div two←to←the[k])+1; {$z=\lceil x/2↑k\rceil$} while x<fraction←four+z do begin z:=half(z+1); k:=k+1; end; y:=y+spec←log[k]; x:=x-z; end @ @<Handle non-positive logarithm@>= begin print←err("Logarithm of "); @.Logarithm...replaced by 0@> print←scaled(x); print(" has been replaced by 0"); help2("Since I don't take logs of non-positive numbers,")@/ ("I'm zeroing this one. Proceed, with fingers crossed."); error; m←log:=0; end @ Conversely, the exponential routine calculates $\exp(x/2↑8)$, when |x| is |scaled|. The result is an integer approximation to $2↑{16}\exp(x/2↑{24})$, when |x| is regarded as an integer. @p function m←exp(@!x:scaled):scaled; var @!k:small←number; {loop control index} @!y,@!z:integer; {auxiliary registers} begin if x>174436200 then {$2↑{24}\ln((2↑{31}-1)/2↑{16})\approx 174436199.51$} begin arith←error:=true; m←exp:=el←gordo; end else if x<-197694359 then m←exp:=0 {$2↑{24}\ln(2↑{-1}/2↑{16})\approx-197694359.45$} else begin if x<=0 then begin z:=-8*x; y:=@'4000000; {$y=2↑{20}$} end else begin if x<=127919879 then z:=1023359037-8*x {$2↑{27}\ln((2↑{31}-1)/2↑{20})\approx 1023359037.125$} else z:=8*(174436200-x); {|z| is always nonnegative} y:=el←gordo; end; @<Multiply |y| by $\exp(-z/2↑{27})$@>; if x<=127919879 then m←exp:=(y+8) div 16@+else m←exp:=y; end; end; @ The idea here is that subtracting |spec←log[k]| from |z| corresponds to multiplying |y| by $1-2↑{-k}$. A subtle point (which had to be checked) was that if $x=127919879$, the value of~|y| will decrease so that |y+8| doesn't overflow. In fact, $z$ will be 5 in this case, and |y| will decrease by~64 when |k=25| and by~16 when |k=27|. @<Multiply |y| by...@>= k:=1; while z>0 do begin while z>=spec←log[k] do begin z:=z-spec←log[k]; y:=y-1-((y-two←to←the[k-1]) div two←to←the[k]); end; incr(k); end @ The trigonometric subroutines use an auxiliary table such that |spec←atan[k]| contains an approximation to the |angle| whose tangent is~$1/2↑k$. @<Glob...@>= @!spec←atan:array[1..26] of angle; {$\arctan2↑{-k}$ times $2↑{20}\cdot180/\pi$} @ @<Set init...@>= spec←atan[1]:=27855475; spec←atan[2]:=14718068; spec←atan[3]:=7471121; spec←atan[4]:=3750058; spec←atan[5]:=1876857; spec←atan[6]:=938658; spec←atan[7]:=469357; spec←atan[8]:=234682; spec←atan[9]:=117342; spec←atan[10]:=58671; spec←atan[11]:=29335; spec←atan[12]:=14668; spec←atan[13]:=7334; spec←atan[14]:=3667; spec←atan[15]:=1833; spec←atan[16]:=917; spec←atan[17]:=458; spec←atan[18]:=229; spec←atan[19]:=115; spec←atan[20]:=57; spec←atan[21]:=29; spec←atan[22]:=14; spec←atan[23]:=7; spec←atan[24]:=4; spec←atan[25]:=2; spec←atan[26]:=1; @ Given integers |x| and |y|, not both zero, the |n←arg| function returns the |angle| whose tangent points in the direction $(x,y)$. This subroutine first determines the correct octant, then solves the problem for |0<=y<=x|, then converts the result appropriately to return an answer in the range |-one←eighty←deg<=@t$\theta$@><=one←eighty←deg|. (The answer is |+one←eighty←deg| if |y=0| and |x<0|, but an answer of |-one←eighty←deg| is possible if, for example, |y=-1| and $x=-2↑{30}$.) The octants are represented in a ``Gray code,'' since that turns out to be computationally simplest. @d negate←x=1 @d negate←y=2 @d switch←x←and←y=4 @d first←octant=1 @d second←octant=first←octant+switch←x←and←y @d third←octant=first←octant+switch←x←and←y+negate←x @d fourth←octant=first←octant+negate←x @d fifth←octant=first←octant+negate←x+negate←y @d sixth←octant=first←octant+switch←x←and←y+negate←x+negate←y @d seventh←octant=first←octant+switch←x←and←y+negate←y @d eighth←octant=first←octant+negate←y @p function n←arg(@!x,@!y:integer):angle; var @!z:angle; {auxiliary register} @!t:integer; {temporary storage} @!k:small←number; {loop counter} @!octant:first←octant..sixth←octant; {octant code} begin if x>=0 then octant:=first←octant else begin negate(x); octant:=first←octant+negate←x; end; if y<0 then begin negate(y); octant:=octant+negate←y; end; if x<y then begin t:=y; y:=x; x:=t; octant:=octant+switch←x←and←y; end; if x=0 then @<Handle undefined arg@> else begin @<Set variable |z| to the arg of $(x,y)$@>; @<Return an appropriate answer based on |z| and |octant|@>; end; end; @ @<Handle undefined arg@>= begin print←err("angle(0,0) is taken as zero"); @.angle(0,0)...zero@> help2("The `angle' between two identical points is undefined.")@/ ("I'm zeroing this one. Proceed, with fingers crossed."); error; n←arg:=0; end @ @<Return an appropriate answer...@>= case octant of first←octant:n←arg:=z; second←octant:n←arg:=ninety←deg-z; third←octant:n←arg:=ninety←deg+z; fourth←octant:n←arg:=one←eighty←deg-z; fifth←octant:n←arg:=z-one←eighty←deg; sixth←octant:n←arg:=-z-ninety←deg; seventh←octant:n←arg:=z-ninety←deg; eighth←octant:n←arg:=-z; end {there are no other cases} @ At this point we have |x>=y>=0|, and |x>0|. The numbers are scaled up or down until $2↑{28}\L x<2↑{29}$, so that accurate fixed-point calculations will be made. @<Set variable |z| to the arg...@>= while x>=fraction←two do begin x:=half(x); y:=half(y); end; z:=0; if y>0 then begin while x<fraction←one do begin double(x); double(y); end; @<Increase |z| to the arg of $(x,y)$@>; end @ During the calculations of this section, variables |x| and~|y| represent actual coordinates $(x,2↑{-k}y)$. We will maintain the condition |x>=y|, so that the tangent will be at most $2↑{-k}$. If $x<2y$, the tangent is greater than $2↑{-k-1}$. The transformation $(a,b)\mapsto(a+b\tan\phi,b-a\tan\phi)$ replaces $(a,b)$ by coordinates whose angle has decreased by~$\phi$; in the special case $a=x$, $b=2↑{-k}y$, and $\tan\phi=2↑{-k-1}$, this operation reduces to the particularly simple iteration shown here. [Cf.~John E. Meggitt, @↑Meggitt, John E.@> {\sl IBM Journal of Research and Development\/ \bf6} (1962), 210--226.] The initial value of |x| will be multiplied by at most $(1+{1\over2})(1+{1\over8})(1+{1\over32})\cdots\approx 1.7584$; hence there is no chance of integer overflow. @<Increase |z|...@>= k:=0; repeat double(y); incr(k); if y>x then begin z:=z+spec←atan[k]; t:=x; x:=x+(y div two←to←the[k+k]); y:=y-t; end; until k=15; repeat double(y); incr(k); if y>x then begin z:=z+spec←atan[k]; y:=y-x; end; until k=26 @ Conversely, the |n←sin←cos| routine takes an |angle| and produces the sine and cosine of that angle. The results of this routine are stored in global integer variables |n←sin| and |n←cos|. @<Glob...@>= @!n←sin,@!n←cos:fraction; {results computed by |n←sin←cos|} @ Given an integer |z| that is $2↑{20}$ times an angle $\theta$ in degrees, the purpose of |n←sin←cos(z)| is to set |x=@t$r\cos\theta$@>| and |y=@t$r\sin\theta$@>| (approximately), for some rather large number~|r|. The maximum of |x| and |y| will be between $2↑{28}$ and $2↑{30}$, so that there will be hardly any loss of accuracy. Then |x| and~|y| are divided by~|r|. @p procedure n←sin←cos(@!z:angle); {computes a multiple of the sine and cosine} var @!k:small←number; {loop control variable} @!q:0..7; {specifies the quadrant} @!r:fraction; {magnitude of |(x,y)|} @!x,@!y,@!t:integer; {temporary registers} begin while z<0 do z:=z+three←sixty←deg; z:=z mod three←sixty←deg; {now |0<=z<three←sixty←deg|} q:=z div forty←five←deg; z:=z mod forty←five←deg; x:=fraction←one; y:=x; if not odd(q) then z:=forty←five←deg-z; @<Subtract angle |z| from |(x,y)|@>; @<Convert |(x,y)| to the octant determined by~|q|@>; r:=pyth←add(x,y); n←cos:=make←fraction(x,r); n←sin:=make←fraction(y,r); end; @ In this case the octants are numbered sequentially. @<Convert |(x,...@>= case q of 0:do←nothing; 1:begin t:=x; x:=y; y:=t; end; 2:begin t:=x; x:=-y; y:=t; end; 3:negate(x); 4:begin negate(x); negate(y); end; 5:begin t:=x; x:=-y; y:=-t; end; 6:begin t:=x; x:=y; y:=-t; end; 7:negate(y); end {there are no other cases} @ The main iteration of |n←sin←cos| is similar to that of |n←arg| but applied in reverse. The values of |spec←atan[k]| decrease slowly enough that this loop is guaranteed to terminate before the (nonexistent) value |spec←atan[27]| would be required. @<Subtract angle |z|...@>= k:=1; while z>0 do begin if z>=spec←atan[k] then begin z:=z-spec←atan[k]; t:=x;@/ x:=t+y div two←to←the[k]; y:=y-t div two←to←the[k]; end; incr(k); end; if y<0 then y:=0 {this precaution may never be needed} @ And now let's complete our collection of numeric utility routines by considering random number generation. \MF\ generates pseudo-random numbers with the additive scheme recommended in Section 3.6 of {\sl The Art of Computer Programming}; however, the results are random fractions between 0 and |fraction←one-1|, inclusive. There's an auxiliary array |randoms| that contains 55 pseudo-random fractions. Using the recurrence $x←n=(x←{n-55}-x←{n-31})\bmod 2↑{28}$, we generate batches of 55 new $x←n$'s at a time by calling |new←randoms|. The global variable |j←random| tells which element has most recently been consumed. @<Glob...@>= @!randoms:array[0..54] of fraction; {the last 55 random values generated} @!j←random:0..54; {the number of unused |randoms|} @ To consume a random fraction, the program below will say `|next←random|' and then it will fetch |randoms[j←random]|. @d next←random==if j←random=0 then new←randoms else decr(j←random) @p procedure new←randoms; var @!k:0..54; {index into |randoms|} @!x:fraction; {accumulator} begin for k:=0 to 23 do begin x:=randoms[k]-randoms[k+31]; if x<0 then x:=x+fraction←one; randoms[k]:=x; end; for k:=24 to 54 do begin x:=randoms[k]-randoms[k-24]; if x<0 then x:=x+fraction←one; randoms[k]:=x; end; j←random:=54; end; @ To initialize the |randoms| table, we call the following routine. @p procedure init←randoms(@!seed:scaled); var @!j,@!jj,@!k:fraction; {more or less random integers} @!i:0..54; {index into |randoms|} begin j:=abs(seed); while j>=fraction←one do j:=half(j); k:=1; for i:=0 to 54 do begin jj:=k; k:=j-k; j:=jj; if k<0 then k:=k+fraction←one; randoms[(i*21)mod 55]:=j; end; new←randoms; new←randoms; new←randoms; {``warm up'' the array} end; @ To produce a uniform random number in the range |0<=u<x| or |0>=u>x| or |0=u=x|, given a |scaled| value~|x|, we proceed as shown here. Note that the call of |take←fraction| will produce the values 0 and~|x| with about half the probability that it will produce any other particular values between 0 and~|x|, because it rounds its answers. @p function unif←rand(@!x:scaled):scaled; var @!y:scaled; {trial value} begin next←random; y:=take←fraction(abs(x),randoms[j←random]); if y=abs(x) then unif←rand:=0 else if x>0 then unif←rand:=y else unif←rand:=-y; end; @ Finally, a normal deviate with mean zero and unit standard deviation can readily be obtained with the ratio method (Algorithm 3.4.1R in {\sl The Art of Computer Programming\/}). @p function norm←rand:scaled; var @!x,@!u,@!l:integer; {what the book would call $2↑{16}X$, $2↑{28}U$, and $-2↑{24}\ln U$} begin repeat repeat next←random; x:=take←fraction(112429,randoms[j←random]-fraction←half); {$2↑{16}\sqrt{8/e}\approx 112428.82793$} next←random; u:=randoms[j←random]; until abs(x)<u; x:=make←fraction(x,u); l:=139548960-m←log(u); {$2↑{24}\cdot12\ln2\approx139548959.6165$} until ab←vs←cd(1024,l,x,x)>=0; norm←rand:=x; end; @* \[9] Packed data. In order to make efficient use of storage space, \MF\ bases its major data structures on a |memory←word|, which contains either a (signed) integer, possibly scaled, or a small number of fields that are one half or one quarter of the size used for storing integers. If |x| is a variable of type |memory←word|, it contains up to four fields that can be referred to as follows: $$\vbox{\halign{\hfil#&#\hfil&#\hfil\cr |x|&.|int|&(an |integer|)\cr |x|&.|sc|\qquad&(a |scaled| integer)\cr |x.hh.lh|, |x.hh|&.|rh|&(two halfword fields)\cr |x.hh.b0|, |x.hh.b1|, |x.hh|&.|rh|&(two quarterword fields, one halfword field)\cr |x.qqqq.b0|, |x.qqqq.b1|, |x.qqqq|&.|b2|, |x.qqqq.b3|\hskip-100pt &\qquad\qquad\qquad(four quarterword fields)\cr}}$$ This is somewhat cumbersome to write, and not very readable either, but macros will be used to make the notation shorter and more transparent. The \PASCAL\ code below gives a formal definition of |memory←word| and its subsidiary types, using packed variant records. \MF\ makes no assumptions about the relative positions of the fields within a word. Since we are assuming 32-bit integers, a halfword must contain at least 16 bits, and a quarterword must contain at least 8 bits. @↑system dependencies@> But it doesn't hurt to have more bits; for example, with enough 36-bit words you might be able to have |mem←max| as large as 262142. N.B.: Valuable memory space will be dreadfully wasted unless \MF\ is compiled by a \PASCAL\ that packs all of the |memory←word| variants into the space of a single integer. Some \PASCAL\ compilers will pack an integer whose subrange is `|0..255|' into an eight-bit field, but others insist on allocating space for an additional sign bit; on such systems you can get 256 values into a quarterword only if the subrange is `|-128..127|'. The present implementation tries to accommodate as many variations as possible, so it makes rather general assumptions. If integers having the subrange `|min←quarterword..max←quarterword|' can be packed into a quarterword, and if integers having the subrange `|min←halfword..max←halfword|' can be packed into a halfword, everything should work satisfactorily. It is usually most efficient to have |min←quarterword=min←halfword=0|, so one should try to achieve this unless it causes a severe problem. The values defined here are recommended for most 32-bit computers. @d min←quarterword=0 {smallest allowable value in a |quarterword|} @d max←quarterword=255 {largest allowable value in a |quarterword|} @d min←halfword==0 {smallest allowable value in a |halfword|} @d max←halfword==65535 {largest allowable value in a |halfword|} @ Here are the inequalities that the quarterword and halfword values must satisfy (or rather, the inequalities that they mustn't satisfy): @<Check the ``constant''...@>= init if mem←max<>mem←top then bad:=10;@+tini@;@/ if mem←max<mem←top then bad:=10; if (min←quarterword>0)or(max←quarterword<127) then bad:=11; if (min←halfword>0)or(max←halfword<32767) then bad:=12; if (min←quarterword<min←halfword)or@| (max←quarterword>max←halfword) then bad:=13; if (mem←min<min←halfword)or(mem←max>=max←halfword) then bad:=14; if max←strings>max←halfword then bad:=15; if buf←size>max←halfword then bad:=16; if (max←quarterword-min←quarterword<255)or@| (max←halfword-min←halfword<65535) then bad:=17; @ The operation of subtracting |min←halfword| occurs rather frequently in \MF, so it is convenient to abbreviate this operation by using the macro |ho| defined here. \MF\ will run faster with respect to compilers that don't optimize the expression `|x-0|', if this macro is simplified in the obvious way when |min←halfword=0|. Similarly, |qi| and |qo| are used for input to and output from quarterwords. @↑system dependencies@> @d ho(#)==#-min←halfword {to take a sixteen-bit item from a halfword} @d qo(#)==#-min←quarterword {to read eight bits from a quarterword} @d qi(#)==#+min←quarterword {to store eight bits in a quarterword} @ The reader should study the following definitions closely: @↑system dependencies@> @d sc==int {|scaled| data is equivalent to |integer|} @<Types...@>= @!quarterword = min←quarterword..max←quarterword; {1/4 of a word} @!halfword=min←halfword..max←halfword; {1/2 of a word} @!two←choices = 1..2; {used when there are two variants in a record} @!three←choices = 1..3; {used when there are three variants in a record} @!two←halves = packed record@;@/ @!rh:halfword; case two←choices of 1: (@!lh:halfword); 2: (@!b0:quarterword; @!b1:quarterword); end; @!four←quarters = packed record@;@/ @!b0:quarterword; @!b1:quarterword; @!b2:quarterword; @!b3:quarterword; end; @!memory←word = record@;@/ case three←choices of 1: (@!int:integer); 2: (@!hh:two←halves); 3: (@!qqqq:four←quarters); end; @!word←file = file of memory←word; @ When debugging, we may want to print a |memory←word| without knowing what type it is; so we print it in all modes. @↑dirty \PASCAL@>@↑debugging@> @p @!debug procedure print←word(@!w:memory←word); {prints |w| in all ways} begin print←int(w.int); print←char(" ");@/ print←scaled(w.sc); print←char(" "); print←scaled(w.sc div @'10000); print←ln;@/ print←int(w.hh.lh); print←char("="); print←int(w.hh.b0); print←char(":"); print←int(w.hh.b1); print←char(";"); print←int(w.hh.rh); print←char(" ");@/ print←int(w.qqqq.b0); print←char(":"); print←int(w.qqqq.b1); print←char(":"); print←int(w.qqqq.b2); print←char(":"); print←int(w.qqqq.b3); end; gubed @* \[10] Dynamic memory allocation. The \MF\ system does nearly all of its own memory allocation, so that it can readily be transported into environments that do not have automatic facilities for strings, garbage collection, etc., and so that it can be in control of what error messages the user receives. The dynamic storage requirements of \MF\ are handled by providing a large array |mem| in which consecutive blocks of words are used as nodes by the \MF\ routines. Pointer variables are indices into this array, or into another array called |eqtb| that will be explained later. A pointer variable might also be a special flag that lies outside the bounds of |mem|, so we allow pointers to assume any |halfword| value. The minimum halfword value represents a null pointer. @d pointer==halfword {a flag or a location in |mem| or |eqtb|} @d null==mem←min {the null pointer} @<Glob...@>= @!temp←ptr:pointer; {a pointer variable for occasional emergency use} @ The |mem| array is divided into two regions that are allocated separately, but the dividing line between these two regions is not fixed; they grow together until finding their ``natural'' size in a particular job. Locations less than or equal to |lo←mem←max| are used for storing variable-length records consisting of two or more words each. This region is maintained using an algorithm similar to the one described in exercise 2.5--19 of {\sl The Art of Computer Programming}. However, no size field appears in the allocated nodes; the program is responsible for knowing the relevant size when a node is freed. Locations greater than or equal to |hi←mem←min| are used for storing one-word records; a conventional \.{AVAIL} stack is used for allocation in this region. Locations of |mem| between |mem←min| and |mem←top| may be dumped as part of preloaded format files, by the \.{INIMF} preprocessor. @.INIMF@> Production versions of \MF\ may extend the memory at the top end in order to provide more space; these locations, between |mem←top| and |mem←max|, are always used for single-word nodes. The key pointers that govern |mem| allocation have a prescribed order: $$\hbox{|null<=mem←min<lo←mem←max<hi←mem←min<mem←top<=mem←end<=mem←max|.}$$ @<Glob...@>= @!mem : array[mem←min..mem←max] of memory←word; {the big dynamic storage area} @!lo←mem←max : pointer; {the largest location of variable-size memory in use} @!hi←mem←min : pointer; {the smallest location of one-word memory in use} @ In order to study the memory requirements of particular applications, it is possible to prepare a version of \MF\ that keeps track of current and maximum memory usage. When code between the delimiters |@!stat| $\ldots$ |tats| is not ``commented out,'' \MF\ will run a bit slower but it will report these statistics when |tracing←stats| is positive. @<Glob...@>= @!var←used, @!dyn←used : integer; {how much memory is in use} @ Let's consider the one-word memory region first, since it's the simplest. The pointer variable |mem←end| holds the highest-numbered location of |mem| that has ever been used. The free locations of |mem| that occur between |hi←mem←min| and |mem←end|, inclusive, are of type |two←halves|, and we write |info(p)| and |link(p)| for the |lh| and |rh| fields of |mem[p]| when it is of this type. The single-word free locations form a linked list $$|avail|,\;\hbox{|link(avail)|},\;\hbox{|link(link(avail))|},\;\ldots$$ terminated by |null|. @d link(#) == mem[#].hh.rh {the |link| field of a memory word} @d info(#) == mem[#].hh.lh {the |info| field of a memory word} @<Glob...@>= @!avail : pointer; {head of the list of available one-word nodes} @!mem←end : pointer; {the last one-word node used in |mem|} @ If one-word memory is exhausted, it might mean that the user has forgotten a token like `\&{enddef}' or `\&{endfor}'. We will define some procedures later that try to help pinpoint the trouble. @p @t\4@>@<Declare the procedure called |show←token←list|@>@; @t\4@>@<Declare the procedure called |runaway|@> @ The function |get←avail| returns a pointer to a new one-word node whose |link| field is null. However, \MF\ will halt if there is no more room left. @↑inner loop@> @p function get←avail : pointer; {single-word node allocation} var @!p:pointer; {the new node being got} begin p:=avail; {get top location in the |avail| stack} if p<>null then avail:=link(avail) {and pop it off} else if mem←end<mem←max then {or go into virgin territory} begin incr(mem←end); p:=mem←end; end else begin decr(hi←mem←min); p:=hi←mem←min; if hi←mem←min<=lo←mem←max then begin runaway; {if memory is exhausted, display possible runaway text} overflow("main memory size",mem←max+1-mem←min); {quit; all one-word nodes are busy} @:METAFONT capacity exceeded main memory size}{\quad main memory size@> end; end; link(p):=null; {provide an oft-desired initialization of the new node} @!stat incr(dyn←used);@+tats@;{maintain statistics} get←avail:=p; end; @ Conversely, a one-word node is recycled by calling |free←avail|. @d free←avail(#)== {single-word node liberation} begin link(#):=avail; avail:=#; @!stat decr(dyn←used);@+tats@/ end @ There's also a |fast←get←avail| routine, which saves the procedure-call overhead at the expense of extra programming. This macro is used in the places that would otherwise account for the most calls of |get←avail|. @↑inner loop@> @d fast←get←avail(#)==@t@>@;@/ begin #:=avail; {avoid |get←avail| if possible, to save time} if #=null then #:=get←avail else begin avail:=link(#); link(#):=null; @!stat incr(dyn←used);@+tats@/ end; end @ The available-space list that keeps track of the variable-size portion of |mem| is a nonempty, doubly-linked circular list of empty nodes, pointed to by the roving pointer |rover|. Each empty node has size 2 or more; the first word contains the special value |max←halfword| in its |link| field and the size in its |info| field; the second word contains the two pointers for double linking. Each nonempty node also has size 2 or more. Its first word is of type |two←halves|\kern-1pt, and its |link| field is never equal to |max←halfword|. Otherwise there is complete flexibility with respect to the contents of its other fields and its other words. (We require |mem←max<max←halfword| because terrible things can happen when |max←halfword| appears in the |link| field of a nonempty node.) @d empty←flag == max←halfword {the |link| of an empty variable-size node} @d is←empty(#) == (link(#)=empty←flag) {tests for empty node} @d node←size == info {the size field in empty variable-size nodes} @d llink(#) == info(#+1) {left link in doubly-linked list of empty nodes} @d rlink(#) == link(#+1) {right link in doubly-linked list of empty nodes} @<Glob...@>= @!rover : pointer; {points to some node in the list of empties} @ A call to |get←node| with argument |s| returns a pointer to a new node of size~|s|, which must be 2~or more. The |link| field of the first word of this new node is set to null. An overflow stop occurs if no suitable space exists. If |get←node| is called with $s=2↑{30}$, it simply merges adjacent free areas and returns the value |max←halfword|. @p function get←node(@!s:integer):pointer; {variable-size node liberation} label found,exit,restart; var @!p:pointer; {the node currently under inspection} @!q:pointer; {the node physically after node |p|} @!r:integer; {the newly allocated node, or a candidate for this honor} @!t,@!tt:integer; {temporary registers} @↑inner loop@> begin restart: p:=rover; {start at some free node in the ring} repeat @<Try to allocate within node |p| and its physical successors, and |goto found| if allocation was possible@>; p:=rlink(p); {move to the next node in the ring} until p=rover; {repeat until the whole list has been traversed} if s=@'10000000000 then begin get←node:=max←halfword; return; end; if lo←mem←max+2<hi←mem←min then if lo←mem←max+2<=mem←min+max←halfword then @<Grow more variable-size memory and |goto restart|@>; overflow("main memory size",mem←max+1-mem←min); {sorry, nothing satisfactory is left} @:METAFONT capacity exceeded main memory size}{\quad main memory size@> found: link(r):=null; {this node is now nonempty} @!stat var←used:=var←used+s; {maintain usage statistics} tats@;@/ get←node:=r; exit:end; @ The lower part of |mem| grows by 1000 words at a time, unless we are very close to going under. When it grows, we simply link a new node into the available-space list. This method of controlled growth helps to keep the |mem| usage consecutive when \TeX\ is implemented on ``virtual memory'' systems. @↑virtual memory@> @<Grow more variable-size memory and |goto restart|@>= begin if lo←mem←max+1000<hi←mem←min then t:=lo←mem←max+1000 else t:=(lo←mem←max+hi←mem←min+2) div 2; {|lo←mem←max+2<=t<hi←mem←min|} if t>mem←min+max←halfword then t:=mem←min+max←halfword; p:=llink(rover); q:=lo←mem←max; rlink(p):=q; llink(rover):=q;@/ rlink(q):=rover; llink(q):=p; link(q):=empty←flag; node←size(q):=t-lo←mem←max;@/ lo←mem←max:=t; link(lo←mem←max):=null; info(lo←mem←max):=null; rover:=q; goto restart; end @ @<Try to allocate...@>= q:=p+node←size(p); {find the physical successor} while is←empty(q) do {merge node |p| with node |q|} begin t:=rlink(q); tt:=llink(q); @↑inner loop@> if q=rover then rover:=t; llink(t):=tt; rlink(tt):=t;@/ q:=q+node←size(q); end; r:=q-s; if r>p+1 then @<Allocate from the top of node |p| and |goto found|@>; if r=p then if ((rlink(p)<>rover) or (llink(p)<>rover)) then @<Allocate entire node |p| and |goto found|@>; node←size(p):=q-p {reset the size in case it grew} @ @<Allocate from the top...@>= begin node←size(p):=r-p; {store the remaining size} rover:=p; {start searching here next time} goto found; end @ Here we delete node |p| from the ring, and let |rover| rove around. @<Allocate entire...@>= begin rover:=rlink(p); t:=llink(p); llink(rover):=t; rlink(t):=rover; goto found; end @ Conversely, when some variable-size node |p| of size |s| is no longer needed, the operation |free←node(p,s)| will make its words available, by inserting |p| as a new empty node just before where |rover| now points. @p procedure free←node(@!p:pointer; @!s:halfword); {variable-size node liberation} var @!q:pointer; {|llink(rover)|} begin node←size(p):=s; link(p):=empty←flag; @↑inner loop@> q:=llink(rover); llink(p):=q; rlink(p):=rover; {set both links} llink(rover):=p; rlink(q):=p; {insert |p| into the ring} @!stat var←used:=var←used-s;@+tats@;{maintain statistics} end; @ Just before \.{INIMF} writes out the memory, it sorts the doubly linked available space list. The list is probably very short at such times, so a simple insertion sort is used. The smallest available location will be pointed to by |rover|, the next-smallest by |rlink(rover)|, etc. @p @!init procedure sort←avail; {sorts the available variable-size nodes by location} var @!p,@!q,@!r: pointer; {indices into |mem|} @!old←rover:pointer; {initial |rover| setting} begin p:=get←node(@'10000000000); {merge adjacent free areas} p:=rlink(rover); rlink(rover):=max←halfword; old←rover:=rover; while p<>old←rover do @<Sort |p| into the list starting at |rover| and advance |p| to |rlink(p)|@>; p:=rover; while rlink(p)<>max←halfword do begin llink(rlink(p)):=p; p:=rlink(p); end; rlink(p):=rover; llink(rover):=p; end; tini @ The following |while| loop terminates, since the list that starts at |rover| ends with |max←halfword| during the sorting procedure. @<Sort |p|...@>= if p<rover then begin q:=p; p:=rlink(q); rlink(q):=rover; rover:=q; end else begin q:=rover; while rlink(q)<p do q:=rlink(q); r:=rlink(p); rlink(p):=rlink(q); rlink(q):=p; p:=r; end @* \[11] Memory layout. Some areas of |mem| are dedicated to fixed usage, since static allocation is more efficient than dynamic allocation when we can get away with it. For example, locations |mem←min| to |mem←min+2| are always used to store the specification for null pen coordinates that are `$(0,0)$'. The following macro definitions accomplish the static allocation by giving symbolic names to the fixed positions. Static variable-size nodes appear in locations |mem←min| through |lo←mem←stat←max|, and static single-word nodes appear in locations |hi←mem←stat←min| through |mem←top|, inclusive. @d null←coords==mem←min {specification for pen offsets of $(0,0)$} @d null←pen==null←coords+3 {we will define |coord←node←size=3|} @d dep←head==null←pen+10 {and |pen←node←size=10|} @d zero←val==dep←head+2 {two words for a permanently zero value} @d temp←val==zero←val+2 {two words for a temporary value node} @d end←attr==temp←val {we use |end←attr+2| only} @d inf←val==end←attr+2 {and |inf←val+1| only} @d bad←vardef==inf←val+2 {two words for \&{vardef} error recovery} @d lo←mem←stat←max==bad←vardef+1 {largest statically allocated word in the variable-size |mem|} @# @d sentinel==mem←top {end of sorted lists} @d temp←head==mem←top-1 {head of a temporary list of some kind} @d hi←mem←stat←min==mem←top-1 {smallest statically allocated word in the one-word |mem|} @ The following code gets |mem| off to a good start, when \MF\ is initializing itself the slow way. @<Initialize table entries (done by \.{INIMF} only)@>= @↑data structure assumptions@> rover:=lo←mem←stat←max+1; {initialize the dynamic memory} link(rover):=empty←flag; node←size(rover):=1000; {which is a 1000-word available node} llink(rover):=rover; rlink(rover):=rover;@/ lo←mem←max:=rover+1000; link(lo←mem←max):=null; info(lo←mem←max):=null;@/ for k:=hi←mem←stat←min to mem←top do mem[k]:=mem[lo←mem←max]; {clear list heads} avail:=null; mem←end:=mem←top; hi←mem←min:=hi←mem←stat←min; {initialize the one-word memory} var←used:=lo←mem←stat←max+1-mem←min; dyn←used:=mem←top+1-hi←mem←stat←min; {initialize statistics} @ The procedure |flush←list(p)| frees an entire linked list of one-word nodes that starts at a given position, until coming to |sentinel| or a pointer that is not in the one-word region. Another procedure, |flush←node←list|, frees an entire linked list of one-word and two-word nodes, until coming to a |null| pointer. @↑inner loop@> @p procedure flush←list(@!p:pointer); {makes list of single-word nodes available} label done; var @!q,@!r:pointer; {list traversers} begin if p>=hi←mem←min then if p<>sentinel then begin r:=p; repeat q:=r; r:=link(r); @!stat decr(dyn←used);@+tats@/ if r<hi←mem←min then goto done; until r=sentinel; done: {now |q| is the last node on the list} link(q):=avail; avail:=p; end; end; @# procedure flush←node←list(@!p:pointer); var @!q:pointer; {the node being recycled} begin while p<>null do begin q:=p; p:=link(p); if q<hi←mem←min then free←node(q,2)@+else free←avail(q); end; end; @ If \MF\ is extended improperly, the |mem| array might get screwed up. For example, some pointers might be wrong, or some ``dead'' nodes might not have been freed when the last reference to them disappeared. Procedures |check←mem| and |search←mem| are available to help diagnose such problems. These procedures make use of two arrays called |free| and |was←free| that are present only if \MF's debugging routines have been included. (You may want to decrease the size of |mem| while you @↑debugging@> are debugging.) @<Glob...@>= @!debug @!free: packed array [mem←min..mem←max] of boolean; {free cells} @t\hskip1em@>@!was←free: packed array [mem←min..mem←max] of boolean; {previously free cells} @t\hskip1em@>@!was←mem←end,@!was←lo←max,@!was←hi←min: pointer; {previous |mem←end|, |lo←mem←max|,and |hi←mem←min|} @t\hskip1em@>@!panicking:boolean; {do we want to check memory constantly?} gubed @ @<Set initial...@>= @!debug was←mem←end:=mem←min; {indicate that everything was previously free} was←lo←max:=mem←min; was←hi←min:=mem←max; panicking:=false; gubed @ Procedure |check←mem| makes sure that the available space lists of |mem| are well formed, and it optionally prints out all locations that are reserved now but were free the last time this procedure was called. @p @!debug procedure check←mem(@!print←locs : boolean); label done1,done2; {loop exits} var @!p,@!q,@!r:pointer; {current locations of interest in |mem|} @!clobbered:boolean; {is something amiss?} begin for p:=mem←min to lo←mem←max do free[p]:=false; {you can probably do this faster} for p:=hi←mem←min to mem←end do free[p]:=false; {ditto} @<Check single-word |avail| list@>; @<Check variable-size |avail| list@>; @<Check flags of unavailable nodes@>; @<Check the list of linear dependencies@>; if print←locs then @<Print newly busy locations@>; for p:=mem←min to lo←mem←max do was←free[p]:=free[p]; for p:=hi←mem←min to mem←end do was←free[p]:=free[p]; {|was←free:=free| might be faster} was←mem←end:=mem←end; was←lo←max:=lo←mem←max; was←hi←min:=hi←mem←min; end; gubed @ @<Check single-word...@>= p:=avail; q:=null; clobbered:=false; while p<>null do begin if (p>mem←end)or(p<hi←mem←min) then clobbered:=true else if free[p] then clobbered:=true; if clobbered then begin print←nl("AVAIL list clobbered at "); @.AVAIL list clobbered...@> print←int(q); goto done1; end; free[p]:=true; q:=p; p:=link(q); end; done1: @ @<Check variable-size...@>= p:=rover; q:=null; clobbered:=false; repeat if (p>=lo←mem←max)or(p<mem←min) then clobbered:=true else if (rlink(p)>=lo←mem←max)or(rlink(p)<mem←min) then clobbered:=true else if not(is←empty(p))or(node←size(p)<2)or@| (p+node←size(p)>lo←mem←max)or@| (llink(rlink(p))<>p) then clobbered:=true; if clobbered then begin print←nl("Double-AVAIL list clobbered at "); @.Double-AVAIL list clobbered...@> print←int(q); goto done2; end; for q:=p to p+node←size(p)-1 do {mark all locations free} begin if free[q] then begin print←nl("Doubly free location at "); @.Doubly free location...@> print←int(q); goto done2; end; free[q]:=true; end; q:=p; p:=rlink(p); until p=rover; done2: @ @<Check flags...@>= p:=mem←min; while p<=lo←mem←max do {node |p| should not be empty} begin if is←empty(p) then begin print←nl("Bad flag at "); print←int(p); @.Bad flag...@> end; while (p<=lo←mem←max) and not free[p] do incr(p); while (p<=lo←mem←max) and free[p] do incr(p); end @ @<Print newly busy...@>= begin print←nl("New busy locs:"); @.New busy locs@> for p:=mem←min to lo←mem←max do if not free[p] and ((p>was←lo←max) or was←free[p]) then begin print←char(" "); print←int(p); end; for p:=hi←mem←min to mem←end do if not free[p] and ((p<was←hi←min) or (p>was←mem←end) or was←free[p]) then begin print←char(" "); print←int(p); end; end @ The |search←mem| procedure attempts to answer the question ``Who points to node~|p|?'' In doing so, it fetches |link| and |info| fields of |mem| that might not be of type |two←halves|. Strictly speaking, this is @↑dirty \PASCAL@> undefined in \PASCAL, and it can lead to ``false drops'' (words that seem to point to |p| purely by coincidence). But for debugging purposes, we want to rule out the places that do {\sl not\/} point to |p|, so a few false drops are tolerable. @p @!debug procedure search←mem(@!p:pointer); {look for pointers to |p|} var @!q:integer; {current position being searched} begin for q:=mem←min to lo←mem←max do begin if link(q)=p then begin print←nl("LINK("); print←int(q); print←char(")"); end; if info(q)=p then begin print←nl("INFO("); print←int(q); print←char(")"); end; end; for q:=hi←mem←min to mem←end do begin if link(q)=p then begin print←nl("LINK("); print←int(q); print←char(")"); end; if info(q)=p then begin print←nl("INFO("); print←int(q); print←char(")"); end; end; @<Search |eqtb| for equivalents equal to |p|@>; end; gubed @* \[12] The command codes. Before we can go much further, we need to define symbolic names for the internal code numbers that represent the various commands obeyed by \MF. These codes are somewhat arbitrary, but not completely so. For example, some codes have been made adjacent so that |case| statements in the program need not consider cases that are widely spaced, or so that |case| statements can be replaced by |if| statements. A command can begin an expression if and only if its code lies between |min←primary←command| and |max←primary←command|, inclusive. The first token of a statement that doesn't begin with an expression has a command code between |min←command| and |max←statement←command|, inclusive. The ordering of the highest-numbered commands (|comma<semicolon<end←group<stop|) is crucial for the parsing and error-recovery methods of this program. At any rate, here is the list, for future reference. @d if←test=1 {conditional text (\&{if})} @d fi←or←else=2 {delimiters for conditionals (\&{elseif}, \&{else}, \&{fi}} @d input=3 {input a source file (\&{input}, \&{endinput})} @d iteration=4 {iterate (\&{for}, \&{forsuffixes}, \&{forever}, \&{endfor})} @d repeat←loop=5 {special command substituted for \&{endfor}} @d exit←test=6 {premature exit from a loop (\&{exitif})} @d relax=7 {do nothing (\.{\char`\\})} @d scan←tokens=8 {put a string into the input buffer} @d expand←after=9 {look ahead one token} @d defined←macro=10 {a macro defined by the user} @d min←command=defined←macro+1 @d display←command=11 {online graphic output (\&{display})} @d save←command=12 {save a list of tokens (\&{save})} @d interim←command=13 {save an internal quantity (\&{interim})} @d let←command=14 {redefine a symbolic token (\&{let})} @d new←internal=15 {define a new internal quantity (\&{newinternal})} @d macro←def=16 {define a macro (\&{def}, \&{vardef}, etc.)} @d ship←out←command=17 {output a character (\&{shipout})} @d add←to←command=18 {add to edges (\&{addto})} @d cull←command=19 {cull and normalize edges (\&{cull})} @d tfm←command=20 {command for font metric info (\&{ligtable}, etc.)} @d protection←command=21 {set protection flag (\&{outer}, \&{inner})} @d show←command=22 {diagnostic output (\&{show}, \&{showvariable}, etc.)} @d mode←command=23 {set interaction level (\&{batchmode}, etc.)} @d random←seed=24 {initialize random number generator (\&{randomseed})} @d message←command=25 {communicate to user (\&{message}, \&{errmessage})} @d every←job←command=26 {designate a starting token (\&{everyjob})} @d delimiters=27 {define a pair of delimiters (\&{delimiters})} @d open←window=28 {define a window on the screen (\&{openwindow})} @d special←command=29 {output special info (\&{special}, \&{numspecial})} @d type←name=30 {declare a type (\&{numeric}, \&{pair}, etc.} @d max←statement←command=type←name @d min←primary←command=type←name @d left←delimiter=31 {the left delimiter of a matching pair} @d begin←group=32 {beginning of a group (\&{begingroup})} @d nullary=33 {an operator without arguments (e.g., \&{normaldeviate})} @d unary=34 {an operator with one argument (e.g., \&{sqrt})} @d str←op=35 {convert a suffix to a string (\&{str})} @d cycle=36 {close a cyclic path (\&{cycle})} @d primary←binary=37 {binary operation taking `\&{of}' (e.g., \&{point})} @d capsule←token=38 {a value that has been put into a token list} @d string←token=39 {a string constant (e.g., |"hello"|)} @d internal←quantity=40 {internal numeric parameter (e.g., \&{pausing})} @d min←suffix←token=internal←quantity @d tag←token=41 {a symbolic token without a primitive meaning} @d numeric←token=42 {a numeric constant (e.g., \.{3.14159})} @d max←suffix←token=numeric←token @d plus←or←minus=43 {either `\.+' or `\.-'} @d max←primary←command=plus←or←minus {should also be |numeric←token+1|} @d min←tertiary←command=plus←or←minus @d tertiary←secondary←macro=44 {a macro defined by \&{secondarydef}} @d tertiary←binary=45 {an operator at the tertiary level (e.g., `\.{++}')} @d max←tertiary←command=tertiary←binary @d left←brace=46 {the operator `\.{\char`\{}'} @d min←expression←command=left←brace @d path←join=47 {the operator `\.{..}'} @d ampersand=48 {the operator `\.\&'} @d expression←tertiary←macro=49 {a macro defined by \&{tertiarydef}} @d expression←binary=50 {an operator at the expression level (e.g., `\.<')} @d equals=51 {the operator `\.='} @d max←expression←command=equals @d and←command=52 {the operator `\&{and}'} @d min←secondary←command=and←command @d secondary←primary←macro=53 {a macro defined by \&{primarydef}} @d slash=54 {the operator `\./'} @d secondary←binary=55 {an operator at the binary level (e.g., \&{shifted})} @d max←secondary←command=secondary←binary @d param←type=56 {type of parameter (\&{primary}, \&{expr}, \&{suffix}, etc.)} @d controls=57 {specify control points explicitly (\&{controls})} @d tension=58 {specify tension between knots (\&{tension})} @d at←least=59 {bounded tension value (\&{atleast})} @d curl←command=60 {specify curl at an end knot (\&{curl})} @d macro←special=61 {special macro operators (\&{quote}, \.{\#\AT!}, etc.)} @d right←delimiter=62 {the right delimiter of a matching pair} @d left←bracket=63 {the operator `\.['} @d right←bracket=64 {the operator `\.]'} @d right←brace=65 {the operator `\.{\char`\}}'} @d with←option=66 {option for filling (\&{withpen}, \&{withweight})} @d cull←op=67 {the operator `\&{keeping}' or `\&{dropping}'} @d thing←to←add=68 {variant of \&{addto} (\&{contour}, \&{doublepath}, \&{also})} @d of←token=69 {the operator `\&{of}'} @d from←token=70 {the operator `\&{from}'} @d to←token=71 {the operator `\&{to}'} @d at←token=72 {the operator `\&{at}'} @d in←window=73 {the operator `\&{inwindow}'} @d step←token=74 {the operator `\&{step}'} @d until←token=75 {the operator `\&{until}'} @d lig←kern←token=76 {the operators `\&{kern}' and `\.{=:}'} @d assignment=77 {the operator `\.{:=}'} @d colon=78 {the operator `\.:'} @# @d comma=79 {the operator `\.,'} @d end←of←statement==cur←cmd>comma @d semicolon=80 {the operator `\.;', must be |comma+1|} @d end←group=81 {end a group (\&{endgroup}), must be |semicolon+1|} @d stop=82 {end a job (\&{end}, \&{dump}), must be |end←group+1|} @d max←command←code=stop @d outer←tag=max←command←code+1 {protection code added to command code} @<Types...@>= @!command←code=1..max←command←code; @ Variables and capsules in \MF\ have a variety of ``types,'' distinguished by the following code numbers: @d undefined=0 {no type has been declared} @d unknown←tag=1 {this constant is added to certain type codes below} @d vacuous=1 {no expression was present} @d boolean←type=2 {\&{boolean} with a known value} @d unknown←boolean=boolean←type+unknown←tag @d string←type=4 {\&{string} with a known value} @d unknown←string=string←type+unknown←tag @d pen←type=6 {\&{pen} with a known value} @d unknown←pen=pen←type+unknown←tag @d future←pen=8 {subexpression that will become a \&{pen} at a higher level} @d path←type=9 {\&{path} with a known value} @d unknown←path=path←type+unknown←tag @d picture←type=11 {\&{picture} with a known value} @d unknown←picture=picture←type+unknown←tag @d transform←type=13 {\&{transform} variable or capsule} @d pair←type=14 {\&{pair} variable or capsule} @d numeric←type=15 {variable that has been declared \&{numeric} but not used} @d known=16 {\&{numeric} with a known value} @d dependent=17 {a linear combination with |fraction| coefficients} @d proto←dependent=18 {a linear combination with |scaled| coefficients} @d independent=19 {\&{numeric} with unknown value} @d token←list=20 {variable name or suffix argument or text argument} @d structured=21 {variable with subscripts and attributes} @d unsuffixed←macro=22 {variable defined with \&{vardef} but no \.{\AT!\#}} @d suffixed←macro=23 {variable defined with \&{vardef} and \.{\AT!\#}} @# @d unknown←types==unknown←boolean,unknown←string, unknown←pen,unknown←picture,unknown←path @<Basic printing procedures@>= procedure print←type(@!t:small←number); begin case t of vacuous:print("vacuous"); boolean←type:print("boolean"); unknown←boolean:print("unknown boolean"); string←type:print("string"); unknown←string:print("unknown string"); pen←type:print("pen"); unknown←pen:print("unknown pen"); future←pen:print("future pen"); path←type:print("path"); unknown←path:print("unknown path"); picture←type:print("picture"); unknown←picture:print("unknown picture"); transform←type:print("transform"); pair←type:print("pair"); known:print("known numeric"); dependent:print("dependent"); proto←dependent:print("proto-dependent"); numeric←type:print("numeric"); independent:print("independent"); token←list:print("token list"); structured:print("structured"); unsuffixed←macro:print("unsuffixed macro"); suffixed←macro:print("suffixed macro"); othercases print("undefined") endcases; end; @ Values inside \MF\ are stored in two-word nodes that have a |name←type| as well as a |type|. The possibilities for |name←type| are defined here; they will be explained in more detail later. @d root=0 {|name←type| at the top level of a variable} @d saved←root=1 {same, when the variable has been saved} @d structured←root=2 {|name←type| where a |structured| branch occurs} @d subscr=3 {|name←type| in a subscript node} @d attr=4 {|name←type| in an attribute node} @d x←part←sector=5 {|name←type| in the \&{xpart} of a node} @d y←part←sector=6 {|name←type| in the \&{ypart} of a node} @d xx←part←sector=7 {|name←type| in the \&{xxpart} of a node} @d xy←part←sector=8 {|name←type| in the \&{xypart} of a node} @d yx←part←sector=9 {|name←type| in the \&{yxpart} of a node} @d yy←part←sector=10 {|name←type| in the \&{yypart} of a node} @d capsule=11 {|name←type| in stashed-away subexpressions} @d token=12 {|name←type| in a numeric token or string token} @ Primitive operations that produce values have a secondary identification code in addition to their command code; it's something like genera and species. For example, `\.*' has the command code |primary←binary|, and its secondary identification is |times|. The secondary codes start at 30 so that they don't overlap with the type codes; some type codes (e.g., |string←type|) are used as operators as well as type identifications. @d true←code=30 {operation code for \&{true}} @d false←code=31 {operation code for \&{false}} @d null←picture←code=32 {operation code for \&{nullpicture}} @d null←pen←code=33 {operation code for \&{nullpen}} @d job←name←op=34 {operation code for \&{jobname}} @d read←string←op=35 {operation code for \&{readstring}} @d pen←circle=36 {operation code for \&{pencircle}} @d normal←deviate=37 {operation code for \&{normaldeviate}} @d odd←op=38 {operation code for \&{odd}} @d known←op=39 {operation code for \&{known}} @d unknown←op=40 {operation code for \&{unknown}} @d not←op=41 {operation code for \&{not}} @d decimal=42 {operation code for \&{decimal}} @d reverse=43 {operation code for \&{reverse}} @d make←path←op=44 {operation code for \&{makepath}} @d make←pen←op=45 {operation code for \&{makepen}} @d total←weight←op=46 {operation code for \&{totalweight}} @d oct←op=47 {operation code for \&{oct}} @d hex←op=48 {operation code for \&{hex}} @d ASCII←op=49 {operation code for \&{ASCII}} @d char←op=50 {operation code for \&{char}} @d length←op=51 {operation code for \&{length}} @d turning←op=52 {operation code for \&{turningnumber}} @d x←part=53 {operation code for \&{xpart}} @d y←part=54 {operation code for \&{ypart}} @d xx←part=55 {operation code for \&{xxpart}} @d xy←part=56 {operation code for \&{xypart}} @d yx←part=57 {operation code for \&{yxpart}} @d yy←part=58 {operation code for \&{yypart}} @d sqrt←op=59 {operation code for \&{sqrt}} @d m←exp←op=60 {operation code for \&{mexp}} @d m←log←op=61 {operation code for \&{mlog}} @d sin←d←op=62 {operation code for \&{sind}} @d cos←d←op=63 {operation code for \&{cosd}} @d floor←op=64 {operation code for \&{floor}} @d uniform←deviate=65 {operation code for \&{uniformdeviate}} @d char←exists←op=66 {operation code for \&{charexists}} @d angle←op=67 {operation code for \&{angle}} @d cycle←op=68 {operation code for \&{cycle}} @d plus=69 {operation code for \.+} @d minus=70 {operation code for \.-} @d times=71 {operation code for \.*} @d over=72 {operation code for \./} @d pythag←add=73 {operation code for \.{++}} @d pythag←sub=74 {operation code for \.{+-+}} @d or←op=75 {operation code for \&{or}} @d and←op=76 {operation code for \&{and}} @d less←than=77 {operation code for \.<} @d less←or←equal=78 {operation code for \.{<=}} @d greater←than=79 {operation code for \.>} @d greater←or←equal=80 {operation code for \.{>=}} @d equal←to=81 {operation code for \.=} @d unequal←to=82 {operation code for \.{<>}} @d concatenate=83 {operation code for \.\&} @d rotated←by=84 {operation code for \&{rotated}} @d slanted←by=85 {operation code for \&{slanted}} @d scaled←by=86 {operation code for \&{scaled}} @d shifted←by=87 {operation code for \&{shifted}} @d transformed←by=88 {operation code for \&{transformed}} @d x←scaled=89 {operation code for \&{xscaled}} @d y←scaled=90 {operation code for \&{yscaled}} @d z←scaled=91 {operation code for \&{zscaled}} @d intersect=92 {operation code for \&{intersectiontimes}} @d double←dot=93 {operation code for improper \.{..}} @d substring←of=94 {operation code for \&{substring}} @d min←of=substring←of @d subpath←of=95 {operation code for \&{subpath}} @d direction←time←of=96 {operation code for \&{directiontime}} @d point←of=97 {operation code for \&{point}} @d precontrol←of=98 {operation code for \&{precontrol}} @d postcontrol←of=99 {operation code for \&{postcontrol}} @d pen←offset←of=100 {operation code for \&{penoffset}} @p procedure print←op(@!c:quarterword); begin if c<=numeric←type then print←type(c) else case c of true←code:print("true"); false←code:print("false"); null←picture←code:print("nullpicture"); null←pen←code:print("nullpen"); job←name←op:print("jobname"); read←string←op:print("readstring"); pen←circle:print("pencircle"); normal←deviate:print("normaldeviate"); odd←op:print("odd"); known←op:print("known"); unknown←op:print("unknown"); not←op:print("not"); decimal:print("decimal"); reverse:print("reverse"); make←path←op:print("makepath"); make←pen←op:print("makepen"); total←weight←op:print("totalweight"); oct←op:print("oct"); hex←op:print("hex"); ASCII←op:print("ASCII"); char←op:print("char"); length←op:print("length"); turning←op:print("turningnumber"); x←part:print("xpart"); y←part:print("ypart"); xx←part:print("xxpart"); xy←part:print("xypart"); yx←part:print("yxpart"); yy←part:print("yypart"); sqrt←op:print("sqrt"); m←exp←op:print("mexp"); m←log←op:print("mlog"); sin←d←op:print("sind"); cos←d←op:print("cosd"); floor←op:print("floor"); uniform←deviate:print("uniformdeviate"); char←exists←op:print("charexists"); angle←op:print("angle"); cycle←op:print("cycle"); plus:print←char("+"); minus:print←char("-"); times:print←char("*"); over:print←char("/"); pythag←add:print("++"); pythag←sub:print("+-+"); or←op:print("or"); and←op:print("and"); less←than:print←char("<"); less←or←equal:print("<="); greater←than:print←char(">"); greater←or←equal:print(">="); equal←to:print←char("="); unequal←to:print("<>"); substring←of:print("substring"); subpath←of:print("subpath"); direction←time←of:print("directiontime"); point←of:print("point"); precontrol←of:print("precontrol"); postcontrol←of:print("postcontrol"); pen←offset←of:print("penoffset"); concatenate:print("&"); rotated←by:print("rotated"); slanted←by:print("slanted"); scaled←by:print("scaled"); shifted←by:print("shifted"); transformed←by:print("transformed"); x←scaled:print("xscaled"); y←scaled:print("yscaled"); z←scaled:print("zscaled"); intersect:print("intersectiontimes"); othercases print("..") endcases; end; @ \MF\ also has a bunch of internal parameters that a user might want to fuss with. Every such parameter has an identifying code number, defined here. @d tracing←titles=1 {show titles online when they appear} @d tracing←equations=2 {show each variable when it becomes known} @d tracing←capsules=3 {show capsules too} @d tracing←choices=4 {show the control points chosen for paths} @d tracing←specs=5 {show subdivision of paths into octants before digitizing} @d tracing←pens=6 {show details of pens that are made} @d tracing←commands=7 {show commands and operations before they are performed} @d tracing←restores=8 {show when a variable or internal is restored} @d tracing←macros=9 {show macros before they are expanded} @d tracing←edges=10 {show digitized edges as they are computed} @d tracing←output=11 {show digitized edges as they are output} @d tracing←stats=12 {show memory usage at end of job} @d tracing←online=13 {show long diagnostics on terminal and in the log file} @d year=14 {the current year (e.g., 1984)} @d month=15 {the current month (e.g, 3 $\equiv$ March)} @d day=16 {the current day of the month} @d time=17 {the number of minutes past midnight when this job started} @d char←code=18 {the number of the next character to be output} @d char←ext=19 {the extension code of the next character to be output} @d char←wd=20 {the width of the next character to be output} @d char←ht=21 {the height of the next character to be output} @d char←dp=22 {the depth of the next character to be output} @d char←ic=23 {the italic correction of the next character to be output} @d char←dx=24 {the device's $x$ movement for the next character, in pixels} @d char←dy=25 {the device's $y$ movement for the next character, in pixels} @d design←size=26 {the unit of measure used for |char←wd..char←ic|, in points} @d hppp=27 {the number of horizontal pixels per point} @d vppp=28 {the number of vertical pixels per point} @d x←offset=29 {horizontal displacement of shipped-out characters} @d y←offset=30 {vertical displacement of shipped-out characters} @d pausing=31 {positive to display lines on the terminal before they are read} @d showstopping=32 {positive to stop after each \&{show} command} @d fontmaking=33 {positive if font metric output is to be produced} @d proofing=34 {positive for proof mode, negative to suppress output} @d smoothing=35 {positive if moves are to be ``smoothed''} @d autorounding=36 {controls path modification to ``good'' points} @d granularity=37 {autorounding uses this pixel size} @d fillin=38 {extra darkness of diagonal lines} @d turning←check=39 {controls reorientation of clockwise paths} @d warning←check=40 {controls error message when variable value is large} @d max←given←internal=warning←check @<Glob...@>= @!internal:array[1..max←internal] of scaled; {the values of internal quantities} @!int←name:array[1..max←internal] of str←number; {their names} @!int←ptr:max←given←internal..max←internal; {the maximum internal quantity defined so far} @ @<Set init...@>= for k:=1 to max←given←internal do internal[k]:=0; int←ptr:=max←given←internal; @ The symbolic names for internal quantities are put into \MF's hash table by using a routine called |primitive|, which will be defined later. Let us enter them now, so that we don't have to list all those names again anywhere else. @<Put each of \MF's primitives into the hash table@>= primitive("tracingtitles",internal←quantity,tracing←titles);@/ @!@:tracingtitles←}{\&{tracingtitles} primitive@> primitive("tracingequations",internal←quantity,tracing←equations);@/ @!@:tracing←equations←}{\&{tracingequations} primitive@> primitive("tracingcapsules",internal←quantity,tracing←capsules);@/ @!@:tracing←capsules←}{\&{tracingcapsules} primitive@> primitive("tracingchoices",internal←quantity,tracing←choices);@/ @!@:tracing←choices←}{\&{tracingchoices} primitive@> primitive("tracingspecs",internal←quantity,tracing←specs);@/ @!@:tracing←specs←}{\&{tracingspecs} primitive@> primitive("tracingpens",internal←quantity,tracing←pens);@/ @!@:tracing←pens←}{\&{tracingpens} primitive@> primitive("tracingcommands",internal←quantity,tracing←commands);@/ @!@:tracing←commands←}{\&{tracingcommands} primitive@> primitive("tracingrestores",internal←quantity,tracing←restores);@/ @!@:tracing←restores←}{\&{tracingrestores} primitive@> primitive("tracingmacros",internal←quantity,tracing←macros);@/ @!@:tracing←macros←}{\&{tracingmacros} primitive@> primitive("tracingedges",internal←quantity,tracing←edges);@/ @!@:tracing←edges←}{\&{tracingedges} primitive@> primitive("tracingoutput",internal←quantity,tracing←output);@/ @!@:tracing←output←}{\&{tracingoutput} primitive@> primitive("tracingstats",internal←quantity,tracing←stats);@/ @!@:tracing←stats←}{\&{tracingstats} primitive@> primitive("tracingonline",internal←quantity,tracing←online);@/ @!@:tracing←online←}{\&{tracingonline} primitive@> primitive("year",internal←quantity,year);@/ @!@:year←}{\&{year} primitive@> primitive("month",internal←quantity,month);@/ @!@:month←}{\&{month} primitive@> primitive("day",internal←quantity,day);@/ @!@:day←}{\&{day} primitive@> primitive("time",internal←quantity,time);@/ @!@:time←}{\&{time} primitive@> primitive("charcode",internal←quantity,char←code);@/ @!@:char←code←}{\&{charcode} primitive@> primitive("charext",internal←quantity,char←ext);@/ @!@:char←ext←}{\&{charext} primitive@> primitive("charwd",internal←quantity,char←wd);@/ @!@:char←wd←}{\&{charwd} primitive@> primitive("charht",internal←quantity,char←ht);@/ @!@:char←ht←}{\&{charht} primitive@> primitive("chardp",internal←quantity,char←dp);@/ @!@:char←dp←}{\&{chardp} primitive@> primitive("charic",internal←quantity,char←ic);@/ @!@:char←ic←}{\&{charic} primitive@> primitive("chardx",internal←quantity,char←dx);@/ @!@:char←dx←}{\&{chardx} primitive@> primitive("chardy",internal←quantity,char←dy);@/ @!@:char←dy←}{\&{chardy} primitive@> primitive("designsize",internal←quantity,design←size);@/ @!@:design←size←}{\&{designsize} primitive@> primitive("hppp",internal←quantity,hppp);@/ @!@:hppp←}{\&{hppp} primitive@> primitive("vppp",internal←quantity,vppp);@/ @!@:vppp←}{\&{vppp} primitive@> primitive("xoffset",internal←quantity,x←offset);@/ @!@:x←offset←}{\&{xoffset} primitive@> primitive("yoffset",internal←quantity,y←offset);@/ @!@:y←offset←}{\&{yoffset} primitive@> primitive("pausing",internal←quantity,pausing);@/ @!@:pausing←}{\&{pausing} primitive@> primitive("showstopping",internal←quantity,showstopping);@/ @!@:showstopping←}{\&{showstopping} primitive@> primitive("fontmaking",internal←quantity,fontmaking);@/ @!@:fontmaking←}{\&{fontmaking} primitive@> primitive("proofing",internal←quantity,proofing);@/ @!@:proofing←}{\&{proofing} primitive@> primitive("smoothing",internal←quantity,smoothing);@/ @!@:smoothing←}{\&{smoothing} primitive@> primitive("autorounding",internal←quantity,autorounding);@/ @!@:autorounding←}{\&{autorounding} primitive@> primitive("granularity",internal←quantity,granularity);@/ @!@:granularity←}{\&{granularity} primitive@> primitive("fillin",internal←quantity,fillin);@/ @!@:fillin←}{\&{fillin} primitive@> primitive("turningcheck",internal←quantity,turning←check);@/ @!@:turning←check←}{\&{turningcheck} primitive@> primitive("warningcheck",internal←quantity,warning←check);@/ @!@:warning←check←}{\&{warningcheck} primitive@> @ Well, we do have to list the names one more time, for use in symbolic printouts. @<Initialize table...@>= int←name[tracing←titles]:="tracingtitles"; int←name[tracing←equations]:="tracingequations"; int←name[tracing←capsules]:="tracingcapsules"; int←name[tracing←choices]:="tracingchoices"; int←name[tracing←specs]:="tracingspecs"; int←name[tracing←pens]:="tracingpens"; int←name[tracing←commands]:="tracingcommands"; int←name[tracing←restores]:="tracingrestores"; int←name[tracing←macros]:="tracingmacros"; int←name[tracing←edges]:="tracingedges"; int←name[tracing←output]:="tracingoutput"; int←name[tracing←stats]:="tracingstats"; int←name[tracing←online]:="tracingonline"; int←name[year]:="year"; int←name[month]:="month"; int←name[day]:="day"; int←name[time]:="time"; int←name[char←code]:="charcode"; int←name[char←ext]:="charext"; int←name[char←wd]:="charwd"; int←name[char←ht]:="charht"; int←name[char←dp]:="chardp"; int←name[char←ic]:="charic"; int←name[char←dx]:="chardx"; int←name[char←dy]:="chardy"; int←name[design←size]:="designsize"; int←name[hppp]:="hppp"; int←name[vppp]:="vppp"; int←name[x←offset]:="xoffset"; int←name[y←offset]:="yoffset"; int←name[pausing]:="pausing"; int←name[showstopping]:="showstopping"; int←name[fontmaking]:="fontmaking"; int←name[proofing]:="proofing"; int←name[smoothing]:="smoothing"; int←name[autorounding]:="autorounding"; int←name[granularity]:="granularity"; int←name[fillin]:="fillin"; int←name[turning←check]:="turningcheck"; int←name[warning←check]:="warningcheck"; @ The following procedure, which is called just before \MF\ initializes its input and output, establishes the initial values of the date and time. @↑system dependencies@> Since standard \PASCAL\ cannot provide such information, something special is needed. The program here simply specifies July 4, 1776, at noon; but users probably want a better approximation to the truth. Note that the values are |scaled| integers. Hence \MF\ can no longer be used after the year 32767. @p procedure fix←date←and←time; begin internal[time]:=12*60*unity; {minutes since midnight} internal[day]:=4*unity; {fourth day of the month} internal[month]:=7*unity; {seventh month of the year} internal[year]:=1776*unity; {Anno Domini} end; @ \MF\ is occasionally supposed to print diagnostic information that goes only into the transcript file, unless |tracing←online| is positive. Now that we have defined |tracing←online| we can define two routines that adjust the destination of print commands: @<Basic printing...@>= procedure begin←diagnostic; {prepare to do some tracing} begin old←setting:=selector; if(internal[tracing←online]<=0)and(selector=term←and←log) then begin decr(selector); if history=spotless then history:=warning←issued; end; end; @# procedure end←diagnostic(@!blank←line:boolean); {restore proper conditions after tracing} begin print←nl(""); if blank←line then print←ln; selector:=old←setting; end; @ Of course we had better declare another global variable, if the previous routines are going to work. @<Glob...@>= @!old←setting:0..max←selector; @ We will occasionally use |begin←diagnostic| in connection with line-number printing, as follows. (The parameter |s| is typically |"Path"| or |"Cycle spec"|, etc.) @<Basic printing...@>= procedure print←diagnostic(@!s,@!t:str←number;@!nuline:boolean); begin begin←diagnostic; if nuline then print←nl(s)@+else print(s); print(" at line "); print←int(line); print(t); print←char(":"); end; @ The 128 |ASCII←code| characters are grouped into classes by means of the |char←class| table. Individual class numbers have no semantic or syntactic significance, expect in a few instances defined here. There's also |max←class|, which can be used as a basis for additional class numbers in nonstandard extensions of \MF. @d digit←class=0 {the class number of \.{0123456789}} @d period←class=1 {the class number of `\..'} @d space←class=2 {the class number of spaces and nonstandard characters} @d percent←class=3 {the class number of `\.\%'} @d string←class=4 {the class number of `\."'} @d right←paren←class=8 {the class number of `\.)'} @d isolated←classes==5,6,7,8 {characters that make length-one tokens only} @d letter←class=9 {letters and the underline character} @d left←bracket←class=17 {`\.['} @d right←bracket←class=18 {`\.]'} @d invalid←class=20 {bad character in the input} @d max←class=20 {the largest class number} @<Glob...@>= @!char←class:array[ASCII←code] of 0..max←class; {the class numbers} @ If changes are made to accommodate non-ASCII character sets, they should follow the guidelines in Appendix~C of {\sl The {\logos METAFONT\/}book}. @:METAFONTbook}{\sl The {\logos METAFONT\/}book@> @↑system dependencies@> @<Set init...@>= for k:="0" to "9" do char←class[k]:=digit←class; char←class["."]:=period←class; char←class[" "]:=space←class; char←class["%"]:=percent←class; char←class[""""]:=string←class;@/ char←class[","]:=5; char←class[";"]:=6; char←class["("]:=7; char←class[")"]:=right←paren←class; for k:="A" to "Z" do char←class[k]:=letter←class; for k:="a" to "z" do char←class[k]:=letter←class; char←class["←"]:=letter←class;@/ char←class["<"]:=10; char←class["="]:=10; char←class[">"]:=10; char←class[":"]:=10; char←class["|"]:=10;@/ char←class["`"]:=11; char←class["'"]:=11;@/ char←class["+"]:=12; char←class["-"]:=12;@/ char←class["/"]:=13; char←class["*"]:=13; char←class["\"]:=13;@/ char←class["!"]:=14; char←class["?"]:=14;@/ char←class["#"]:=15; char←class["&"]:=15; char←class["@@"]:=15; char←class["$"]:=15;@/ char←class["↑"]:=16; char←class["~"]:=16;@/ char←class["["]:=left←bracket←class; char←class["]"]:=right←bracket←class;@/ char←class["{"]:=19; char←class["}"]:=19;@/ for k:=0 to " "-1 do char←class[k]:=invalid←class; char←class[127]:=invalid←class; @* \[13] The hash table. Symbolic tokens are stored and retrieved by means of a fairly standard hash table algorithm called the method of ``coalescing lists'' (cf.\ Algorithm 6.4C in {\sl The Art of Computer Programming\/}). Once a symbolic token enters the table, it is never removed. The actual sequence of characters forming a symbolic token is stored in the |str←pool| array together with all the other strings. An auxiliary array |hash| consists of items with two halfword fields per word. The first of these, called |next(p)|, points to the next identifier belonging to the same coalesced list as the identifier corresponding to~|p|; and the other, called |text(p)|, points to the |str←start| entry for |p|'s identifier. If position~|p| of the hash table is empty, we have |text(p)=0|; if position |p| is either empty or the end of a coalesced hash list, we have |next(p)=0|. An auxiliary pointer variable called |hash←used| is maintained in such a way that all locations |p>=hash←used| are nonempty. The global variable |st←count| tells how many symbolic tokens have been defined, if statistics are being kept. The first 128 locations of |hash| are reserved for symbols of length one. There's a parallel array called |eqtb| that contains the current equivalent values of each symbolic token. The entries of this array consist of two halfwords called |eq←type| (a command code) and |equiv| (a secondary piece of information that qualifies the |eq←type|). @d next(#) == hash[#].lh {link for coalesced lists} @d text(#) == hash[#].rh {string number for symbolic token name} @d hash←is←full == (hash←used=1) {test if all positions are occupied} @d eq←type(#) == eqtb[#].lh {the current ``meaning'' of a symbolic token} @d equiv(#) == eqtb[#].rh {parametric part of a token's meaning} @d hash←base=129 {hashing actually starts here} @<Glob...@>= @!hash←used:pointer; {allocation pointer for |hash|} @!st←count:integer; {total number of known identifiers} @ Certain entries in the hash table are ``frozen'' and not redefinable, since they are used in error recovery. @d hash←top==hash←base+hash←size {the first location of the frozen area} @d frozen←inaccessible==hash←top {|hash| location to protect the frozen area} @d frozen←repeat←loop==hash←top+1 {|hash| location of a loop-repeat token} @d frozen←right←delimiter==hash←top+2 {|hash| location of a permanent `\.)'} @d frozen←left←bracket==hash←top+3 {|hash| location of a permanent `\.['} @d frozen←slash==hash←top+4 {|hash| location of a permanent `\./'} @d frozen←colon==hash←top+5 {|hash| location of a permanent `\.:'} @d frozen←semicolon==hash←top+6 {|hash| location of a permanent `\.;'} @d frozen←end←for==hash←top+7 {|hash| location of a permanent \&{endfor}} @d frozen←end←def==hash←top+8 {|hash| location of a permanent \&{enddef}} @d frozen←fi==hash←top+9 {|hash| location of a permanent \&{fi}} @d frozen←end←group==hash←top+10 {|hash| location of a permanent `\.{endgroup}'} @d frozen←bad←vardef==hash←top+11 {|hash| location of `\.{a bad variable}'} @d frozen←undefined==hash←top+12 {|hash| location that never gets defined} @d hash←end==hash←top+12 {the actual size of the |hash| and |eqtb| arrays} @<Glob...@>= @!hash: array[1..hash←end] of two←halves; {the hash table} @!eqtb: array[1..hash←end] of two←halves; {the equivalents} @ @<Set init...@>= next(1):=0; text(1):=0; eq←type(1):=tag←token; equiv(1):=null; for k:=2 to hash←end do begin hash[k]:=hash[1]; eqtb[k]:=eqtb[1]; end; @ @<Initialize table entries...@>= hash←used:=frozen←inaccessible; {nothing is used} st←count:=0;@/ text(frozen←bad←vardef):="a bad variable"; text(frozen←fi):="fi"; text(frozen←end←group):="endgroup"; text(frozen←end←def):="enddef"; text(frozen←end←for):="endfor";@/ text(frozen←semicolon):=";"; text(frozen←colon):=":"; text(frozen←slash):="/"; text(frozen←left←bracket):="["; text(frozen←right←delimiter):=")";@/ text(frozen←inaccessible):=" INACCESSIBLE";@/ eq←type(frozen←right←delimiter):=right←delimiter; @ @<Check the ``constant'' values...@>= if hash←end+max←internal>max←halfword then bad:=21; @ Here is the subroutine that searches the hash table for an identifier that matches a given string of length~|l| appearing in |buffer[j.. (j+l-1)]|. If the identifier is not found, it is inserted; hence it will always be found, and the corresponding hash table address will be returned. @p function id←lookup(@!j,@!l:integer):pointer; {search the hash table} label found; {go here when you've found it} var @!h:integer; {hash code} @!p:pointer; {index in |hash| array} @!k:pointer; {index in |buffer| array} begin if l=1 then @<Treat special case of length 1 and |goto found|@>; @<Compute the hash code |h|@>; p:=h+hash←base; {we start searching here; note that |0<=h<hash←prime|} loop@+ begin if text(p)>0 then if length(text(p))=l then if str←eq←buf(text(p),j) then goto found; if next(p)=0 then @<Insert a new symbolic token after |p|, then make |p| point to it and |goto found|@>; p:=next(p); end; found: id←lookup:=p; end; @ @<Treat special case of length 1...@>= begin p:=buffer[j]+1; text(p):=p-1; goto found; end @ @<Insert a new symbolic...@>= begin if text(p)>0 then begin repeat if hash←is←full then overflow("hash size",hash←size); @:METAFONT capacity exceeded hash size}{\quad hash size@> decr(hash←used); until text(hash←used)=0; {search for an empty location in |hash|} next(p):=hash←used; p:=hash←used; end; str←room(l); for k:=j to j+l-1 do append←char(buffer[k]); text(p):=make←string; str←ref[text(p)]:=max←str←ref; @!stat incr(st←count);@+tats@;@/ goto found; end @ The value of |hash←prime| should be roughly 85\% of |hash←size|, and it should be a prime number. The theory of hashing tells us to expect fewer than two table probes, on the average, when the search is successful. [See J.~S. Vitter, {\sl Journal of the ACM\/ \bf30} (1983), 231--258.] @↑Vitter, Jeffrey Scott@> @<Compute the hash code |h|@>= h:=buffer[j]; for k:=j+1 to j+l-1 do begin h:=h+h+buffer[k]; while h>=hash←prime do h:=h-hash←prime; end @ @<Search |eqtb| for equivalents equal to |p|@>= for q:=1 to hash←end do begin if equiv(q)=p then begin print←nl("EQUIV("); print←int(q); print←char(")"); end; end @ We need to put \MF's ``primitive'' symbolic tokens into the hash table, together with their command code (which will be the |eq←type|) and an operand (which will be the |equiv|). The |primitive| procedure does this, in a way that no \MF\ user can. The global value |cur←sym| contains the new |eqtb| pointer after |primitive| has acted. @p @!init procedure primitive(@!s:str←number;@!c:halfword;@!o:halfword); var @!k:pool←pointer; {index into |str←pool|} @!j:small←number; {index into |buffer|} @!l:small←number; {length of the string} begin k:=str←start[s]; l:=str←start[s+1]-k; {we will move |s| into the (empty) |buffer|} for j:=0 to l-1 do buffer[j]:=str←pool[k+j]; cur←sym:=id←lookup(0,l);@/ if s>=128 then {we don't want to have the string twice} begin flush←string(str←ptr-1); text(cur←sym):=s; end; eq←type(cur←sym):=c; equiv(cur←sym):=o; end; tini @ Many of \MF's primitives need no |equiv|, since they are identifiable by their |eq←type| alone. These primitives are loaded into the hash table as follows: @<Put each of \MF's primitives into the hash table@>= primitive("..",path←join,0);@/ @!@:..←}{\.{..} primitive@> primitive("[",left←bracket,0); eqtb[frozen←left←bracket]:=eqtb[cur←sym];@/ @!@:[ }{\.{[} primitive@> primitive("]",right←bracket,0);@/ @!@:] }{\.{]} primitive@> primitive("}",right←brace,0);@/ @!@:]]}{\.{\char`\}} primitive@> primitive("{",left←brace,0);@/ @!@:][}{\.{\char`\{} primitive@> primitive(":",colon,0); eqtb[frozen←colon]:=eqtb[cur←sym];@/ @!@:: }{\.{:} primitive@> primitive(":=",assignment,0);@/ @!@::=←}{\.{:=} primitive@> primitive(",",comma,0);@/ @!@:, }{\., primitive@> primitive(";",semicolon,0); eqtb[frozen←semicolon]:=eqtb[cur←sym];@/ @!@:; }{\.; primitive@> primitive("\",relax,0);@/ @!@:]]\\}{\.{\char`\\} primitive@> @# primitive("addto",add←to←command,0);@/ @!@:add←to←}{\&{addto} primitive@> primitive("at",at←token,0);@/ @!@:at←}{\&{at} primitive@> primitive("atleast",at←least,0);@/ @!@:at←least←}{\&{atleast} primitive@> primitive("begingroup",begin←group,0); bg←loc:=cur←sym;@/ @!@:begin←group←}{\&{begingroup} primitive@> primitive("controls",controls,0);@/ @!@:controls←}{\&{controls} primitive@> primitive("cull",cull←command,0);@/ @!@:cull←}{\&{cull} primitive@> primitive("curl",curl←command,0);@/ @!@:curl←}{\&{curl} primitive@> primitive("delimiters",delimiters,0);@/ @!@:delimiters←}{\&{delimiters} primitive@> primitive("display",display←command,0);@/ @!@:display←}{\&{display} primitive@> primitive("endgroup",end←group,0); eqtb[frozen←end←group]:=eqtb[cur←sym]; eg←loc:=cur←sym;@/ @!@:endgroup←}{\&{endgroup} primitive@> primitive("everyjob",every←job←command,0);@/ @!@:every←job←}{\&{everyjob} primitive@> primitive("exitif",exit←test,0);@/ @!@:exit←if←}{\&{exitif} primitive@> primitive("expandafter",expand←after,0);@/ @!@:expand←after←}{\&{expandafter} primitive@> primitive("from",from←token,0);@/ @!@:from←}{\&{from} primitive@> primitive("inwindow",in←window,0);@/ @!@:in←window←}{\&{inwindow} primitive@> primitive("interim",interim←command,0);@/ @!@:interim←}{\&{interim} primitive@> primitive("let",let←command,0);@/ @!@:let←}{\&{let} primitive@> primitive("newinternal",new←internal,0);@/ @!@:new←internal←}{\&{newinternal} primitive@> primitive("of",of←token,0);@/ @!@:of←}{\&{of} primitive@> primitive("openwindow",open←window,0);@/ @!@:open←window←}{\&{openwindow} primitive@> primitive("randomseed",random←seed,0);@/ @!@:random←seed←}{\&{randomseed} primitive@> primitive("save",save←command,0);@/ @!@:save←}{\&{save} primitive@> primitive("scantokens",scan←tokens,0);@/ @!@:scan←tokens←}{\&{scantokens} primitive@> primitive("shipout",ship←out←command,0);@/ @!@:ship←out←}{\&{shipout} primitive@> primitive("step",step←token,0);@/ @!@:step←}{\&{step} primitive@> primitive("str",str←op,0);@/ @!@:str←}{\&{str} primitive@> primitive("tension",tension,0);@/ @!@:tension←}{\&{tension} primitive@> primitive("to",to←token,0);@/ @!@:to←}{\&{to} primitive@> primitive("until",until←token,0);@/ @!@:until←}{\&{until} primitive@> @ Each primitive has a corresponding inverse, so that it is possible to display the cryptic numeric contents of |eqtb| in symbolic form. Every call of |primitive| in this program is therefore accompanied by some straightforward code that forms part of the |print←cmd←mod| routine explained below. @<Cases of |print←cmd←mod| for symbolic printing of primitives@>= add←to←command:print("addto"); assignment:print(":="); at←least:print("atleast"); at←token:print("at"); begin←group:print("begingroup"); colon:print(":"); comma:print(","); controls:print("controls"); cull←command:print("cull"); curl←command:print("curl"); delimiters:print("delimiters"); display←command:print("display"); end←group:print("endgroup"); every←job←command:print("everyjob"); exit←test:print("exitif"); expand←after:print("expandafter"); from←token:print("from"); in←window:print("inwindow"); interim←command:print("interim"); left←brace:print("{"); left←bracket:print("["); let←command:print("let"); new←internal:print("newinternal"); of←token:print("of"); open←window:print("openwindow"); path←join:print(".."); random←seed:print("randomseed"); relax:print←char("\"); right←brace:print("}"); right←bracket:print("]"); save←command:print("save"); scan←tokens:print("scantokens"); semicolon:print(";"); ship←out←command:print("shipout"); step←token:print("step"); str←op:print("str"); tension:print("tension"); to←token:print("to"); until←token:print("until"); @ We will deal with the other primitives later, at some point in the program where their |eq←type| and |equiv| values are more meaningful. For example, the primitives for macro definitions will be loaded when we consider the routines that define macros. It is easy to find where each particular primitive was treated by looking in the index at the end; for example, the section where |"def"| entered |eqtb| is listed under `\&{def} primitive'. @* \[14] Token lists. A \MF\ token is either symbolic or numeric or a string, or it denotes a macro parameter or capsule; so there are five corresponding ways to encode it @↑token@> internally: (1)~A symbolic token whose hash code is~|p| is represented by the number |p|, in the |info| field of a single-word node in~|mem|. (2)~A numeric token whose |scaled| value is~|v| is represented in a two-word node of~|mem|; the |type| field is |known|, the |name←type| field is |token|, and the |value| field holds~|v|. The fact that this token appears in a two-word node rather than a one-word node is, of course, clear from the node address. (3)~A string token is also represented in a two-word node; the |type| field is |string←type|, the |name←type| field is |token|, and the |value| field holds the corresponding |str←number|. (4)~Capsules have |name←type=capsule|, and their |type| and |value| fields represent arbitrary values (in ways to be explained later). (5)~Macro parameters are like symbolic tokens in that they appear in |info| fields of one-word nodes. The $k$th parameter is represented by |expr←base+k| if it is of type \&{expr}, or by |suffix←base+k| if it is of type \&{suffix}, or by |text←base+k| if it is of type \&{text}. (Here |0<=k<param←size|.) Actual values of these parameters are kept in a separate stack, as we will see later. The constants |expr←base|, |suffix←base|, and |text←base| are, of course, chosen so that there will be no confusion between symbolic tokens and parameters of various types. @d type(#) == mem[#].hh.b0 {identifies what kind of value this is} @d name←type(#) == mem[#].hh.b1 {a clue to the name of this value} @d token←node←size=2 {the number of words in a large token node} @d value←loc(#)==#+1 {the word that contains the |value| field} @d value(#)==mem[value←loc(#)].int {the value stored in a large token node} @d expr←base==hash←end+1 {code for the zeroth \&{expr} parameter} @d suffix←base==expr←base+param←size {code for the zeroth \&{suffix} parameter} @d text←base==suffix←base+param←size {code for the zeroth \&{text} parameter} @<Check the ``constant''...@>= if text←base+param←size>max←halfword then bad:=22; @ A numeric token is created by the following trivial routine. @p function new←num←tok(@!v:scaled):pointer; var @!p:pointer; {the new node} begin p:=get←node(token←node←size); value(p):=v; type(p):=known; name←type(p):=token; new←num←tok:=p; end; @ A token list is a singly linked list of nodes in |mem|, where each node contains a token and a link. Here's a subroutine that gets rid of a token list when it is no longer needed. @p procedure@?token←recycle; forward;@t\2@>@;@/ procedure flush←token←list(@!p:pointer); var @!q:pointer; {the node being recycled} begin while p<>null do begin q:=p; p:=link(p); if q>=hi←mem←min then free←avail(q) else begin case type(q) of vacuous,boolean←type,known:do←nothing; string←type:delete←str←ref(value(q)); unknown←types,pen←type,path←type,future←pen,picture←type, pair←type,transform←type,dependent,proto←dependent,independent: begin g←pointer:=q; token←recycle; end; othercases confusion("token") @:this can't happen token}{\quad token@> endcases;@/ free←node(q,token←node←size); end; end; end; @ The procedure |show←token←list|, which prints a symbolic form of the token list that starts at a given node |p|, illustrates these conventions. The token list being displayed should not begin with a reference count. However, the procedure is intended to be fairly robust, so that if the memory links are awry or if |p| is not really a pointer to a token list, almost nothing catastrophic can happen. An additional parameter |q| is also given; this parameter is either null or it points to a node in the token list where a certain magic computation takes place that will be explained later. (Basically, |q| is non-null when we are printing the two-line context information at the time of an error message; |q| marks the place corresponding to where the second line should begin.) The generation will stop, and `\.{\char`\ ETC.}' will be printed, if the length of printing exceeds a given limit~|l|; the length of printing upon entry is assumed to be a given amount called |null←tally|. (Note that |show←token←list| sometimes uses itself recursively to print the name of a variable within a capsule.) @↑recursion@> Unusual entries are printed in the form of all-caps symbolic tokens preceded by a space, e.g., `\.{\char`\ BAD}'. @<Declare the procedure called |show←token←list|@>= procedure@?print←capsule; forward; @t\2@>@;@/ procedure show←token←list(@!p,@!q:integer;@!l,@!null←tally:integer); label exit; var @!class,@!c:small←number; {the |char←class| of previous and new tokens} @!r,@!v:integer; {temporary registers} begin class:=percent←class; tally:=null←tally; while (p<>null) and (tally<l) do begin if p=q then @<Do magic computation@>; @<Display token |p| and set |c| to its class; but |return| if there are problems@>; class:=c; p:=link(p); end; if p<>null then print(" ETC."); @.ETC@> exit: end; @ @<Display token |p| and set |c| to its class...@>= c:=letter←class; {the default} if (p<mem←min)or(p>mem←end) then begin print(" CLOBBERED"); return; @.CLOBBERED@> end; if p<hi←mem←min then @<Display two-word token@> else begin r:=info(p); if r>=expr←base then @<Display a parameter token@> else if r<1 then if r=0 then @<Display a collective subscript@> else print(" IMPOSSIBLE") @.IMPOSSIBLE@> else begin r:=text(r); if (r<0)or(r>=str←ptr) then print(" NONEXISTENT") @.NONEXISTENT@> else @<Print string |r| as a symbolic token and set |c| to its class@>; end; end @ @<Display two-word token@>= if name←type(p)=token then if type(p)=known then @<Display a numeric token@> else if type(p)<>string←type then print(" BAD") @.BAD@> else begin print←char(""""); slow←print(value(p)); print←char(""""); c:=string←class; end else if (name←type(p)<>capsule)or(type(p)<vacuous)or(type(p)>independent) then print(" BAD") else begin g←pointer:=p; print←capsule; c:=right←paren←class; end @ @<Display a numeric token@>= begin if class=digit←class then print←char(" "); v:=value(p); if v<0 then begin if class=left←bracket←class then print←char(" "); print←char("["); print←scaled(v); print←char("]"); c:=right←bracket←class; end else begin print←scaled(v); c:=digit←class; end; end @ Strictly speaking, a genuine token will never have |info(p)=0|. But we will see later (in the |print←variable←name| routine) that it is convenient to let |info(p)=0| stand for `\.{[]}'. @<Display a collective subscript@>= begin if class=left←bracket←class then print←char(" "); print("[]"); c:=right←bracket←class; end @ @<Display a parameter token@>= begin if r<suffix←base then begin print("(EXPR"); r:=r-(expr←base); @.EXPR@> end else if r<text←base then begin print("(SUFFIX"); r:=r-(suffix←base); @.SUFFIX@> end else begin print("(TEXT"); r:=r-(text←base); @.TEXT@> end; print←int(r); print←char(")"); c:=right←paren←class; end @ @<Print string |r| as a symbolic token...@>= begin c:=char←class[str←pool[str←start[r]]]; if c=class then case c of letter←class:print←char("."); isolated←classes:do←nothing; othercases print←char(" ") endcases; print(r); end @ The following procedures have been declared |forward| with no parameters, because the author dislikes \PASCAL's convention about |forward| procedures with parameters. It was necessary to do something, because |show←token←list| is recursive (although the recursion is limited to one level), and because |flush←token←list| is syntactically (but not semantically) recursive. @↑recursion@> @<Declare miscellaneous procedures that were declared |forward|@>= procedure print←capsule; begin print←char("("); print←exp(g←pointer,0); print←char(")"); end; @# procedure token←recycle; begin recycle←value(g←pointer); end; @ @<Glob...@>= @!g←pointer:pointer; {(global) parameter to the |forward| procedures} @ Macro definitions are kept in \MF's memory in the form of token lists that have a few extra one-word nodes at the beginning. The first node contains a reference count that is used to tell when the list is no longer needed. To emphasize the fact that a reference count is present, we shall refer to the |info| field of this special node as the |ref←count| field. @↑reference counts@> The next node or nodes after the reference count serve to describe the formal parameters. They either contain a code word that specifies all of the parameters, or they contain zero or more parameter tokens followed by the code `|general←macro|'. @d ref←count==info {reference count preceding a macro definition or pen header} @d add←mac←ref(#)==incr(ref←count(#)) {make a new reference to a macro list} @d general←macro=0 {preface to a macro defined with a parameter list} @d primary←macro=1 {preface to a macro with a \&{primary} parameter} @d secondary←macro=2 {preface to a macro with a \&{secondary} parameter} @d tertiary←macro=3 {preface to a macro with a \&{tertiary} parameter} @d expr←macro=4 {preface to a macro with an undelimited \&{expr} parameter} @d of←macro=5 {preface to a macro with undelimited `\&{expr} |x| \&{of}~|y|' parameters} @d suffix←macro=6 {preface to a macro with an undelimited \&{suffix} parameter} @d text←macro=7 {preface to a macro with an undelimited \&{text} parameter} @p procedure delete←mac←ref(@!p:pointer); {|p| points to the reference count of a macro list that is losing one reference} begin if ref←count(p)=null then flush←token←list(p) else decr(ref←count(p)); end; @ The following subroutine displays a macro, given a pointer to its reference count. @p @t\4@>@<Declare the procedure called |print←cmd←mod|@>@; procedure show←macro(@!p:pointer;@!q,@!l:integer); label exit; var @!r:pointer; {temporary storage} begin p:=link(p); {bypass the reference count} while info(p)>text←macro do begin r:=link(p); link(p):=null; show←token←list(p,null,l,0); link(p):=r; p:=r; if l>0 then l:=l-tally@+else return; end; {control printing of `\.{ETC.}'} @.ETC@> tally:=0; case info(p) of general←macro:print("->"); @.->@> primary←macro,secondary←macro,tertiary←macro:begin print←char("<"); print←cmd←mod(param←type,info(p)); print(">->"); end; expr←macro:print("<expr>->"); of←macro:print("<expr>of<primary>->"); suffix←macro:print("<suffix>->"); text←macro:print("<text>->"); end; {there are no other cases} show←token←list(link(p),q,l-tally,0); exit:end; @* \[15] Data structures for variables. The variables of \MF\ programs can be simple, like `\.x', or they can combine the structural properites of arrays and records, like `\.{x20a.b}'. A \MF\ user assigns a type to a variable like \.{x20a.b} by saying, for example, `\.{boolean} \.{x20a.b}'. It's time for us to study how such things are represented inside of the computer. Each variable value occupies two consecutive words, either in a two-word node called a value node, or as a two-word subfield of a larger node. One of those two words is called the |value| field; it is an integer, containing either a |scaled| numeric value or the representation of some other type of quantity. (It might also be subdivided into halfwords, in which case it is referred to by other names instead of |value|.) The other word is broken into subfields called |type|, |name←type|, and |link|. The |type| field is a quarterword that specifies the variable's type, and |name←type| is a quarterword from which \MF\ can reconstruct the variable's name (sometimes by using the |link| field as well). Thus, only 1.25 words are actually devoted to the value itself; the other three-quarters of a word are overhead, but they aren't wasted because they allow \MF\ to deal with sparse arrays and to provide meaningful diagnostics. In this section we shall be concerned only with the structural aspects of variables, not their values. Later parts of the program will change the |type| and |value| fields, but we shall treat those fields as black boxes whose contents should not be touched. However, if the |type| field is |structured|, there is no |value| field, and the second word is broken into two pointer fields called |attr←head| and |subscr←head|. Those fields point to additional nodes that contain structural information, as we shall see. @d subscr←head←loc(#) == #+1 {where |value|, |subscr←head| and |attr←head| are} @d attr←head(#) == info(subscr←head←loc(#)) {pointer to attribute info} @d subscr←head(#) == link(subscr←head←loc(#)) {pointer to subscript info} @d value←node←size=2 {the number of words in a value node} @ An attribute node is three words long. Two of these words contain |type| and |value| fields as described above, and the third word contains additional information: There is an |attr←loc| field, which contains the hash address of the token that names this attribute; and there's also a |parent| field, which points to the value node of |structured| type at the next higher level (i.e., at the level to which this attribute is subsidiary). The |name←type| in an attribute node is `|attr|'. The |link| field points to the next attribute with the same parent; these are arranged in increasing order, so that |attr←loc(link(p))>attr←loc(p)|. The final attribute node links to the constant |end←attr|, whose |attr←loc| field is greater than any legal hash address. The |attr←head| in the parent points to a node whose |name←type| is |structured←root|; this node represents the null attribute, i.e., the variable that is relevant when no attributes are attached to the parent. The |attr←head| node is either a value node, a subscript node, or an attribute node, depending on what the parent would be if it were not structured; but the subscript and attribute fields are ignored, so it effectively contains only the data of a value node. The |link| field in this special node points to an attribute node whose |attr←loc| field is zero; the latter node represents a collective subscript `\.{[]}' attached to the parent, and its |link| field points to the first non-special attribute node (or to |end←attr| if there are none). A subscript node likewise occupies three words, with |type| and |value| fields plus extra information; its |name←type| is |subscr|. In this case the third word is called the |subscript| field, which is a |scaled| integer. The |link| field points to the subscript node with the next larger subscript, if any; otherwise the |link| points to the attribute node for collective subscripts at this level. We have seen that the latter node contains an upward pointer, so that the parent can be deduced. The |name←type| in a parent-less value node is |root|, and the |link| is the hash address of the token that names this value. In other words, variables have a heirarchical structure that includes enough threads running around so that the program is able to move easily between siblings, parents, and children. An example should be helpful: (The reader is advised to draw a picture while reading the following description, since that will help to firm up the ideas.) Suppose that `\.x' and `\.{x.a}' and `\.{x[]b}' and `\.{x5}' and `\.{x20b}' have been mentioned in a user's program, where \.{x[]b} has been declared to be of \&{boolean} type. Let |h(x)|, |h(a)|, and |h(b)| be the hash addresses of \.x, \.a, and \.b. Then |eq←type(h(x))=name| and |equiv(h(x))=p|, where |p|~is a two-word value node with |name←type(p)=root| and |link(p)=h(x)|. We have |type(p)=structured|, |attr←head(p)=q|, and |subscr←head(p)=r|, where |q| points to a value node and |r| to a subscript node. (Are you still following this? Use a pencil to draw a diagram.) The lone variable `\.x' is represented by |type(q)| and |value(q)|; furthermore |name←type(q)=structured←root| and |link(q)=q1|, where |q1| points to an attribute node representing `\.{x[]}'. Thus |name←type(q1)=attr|, |attr←loc(q1)=collective←subscript=0|, |parent(q1)=p|, |type(q1)=structured|, |attr←head(q1)=qq|, and |subscr←head(q1)=qq1|; |qq| is a value node with |type(qq)=numeric←type| (assuming that \.{x5} is numeric, because |qq| represents `\.{x[]}' with no further attributes), |name←type(qq)=structured←root|, and |link(qq)=qq1|. (Now pay attention to the next part.) Node |qq1| is an attribute node representing `\.{x[][]}', which has never yet occurred; its |type| field is |undefined|, and its |value| field is undefined. We have |name←type(qq1)=attr|, |attr←loc(qq1)=collective←subscript|, |parent(qq1)=q1|, and |link(qq1)=qq2|. Since |qq2| represents `\.{x[]b}', |type(qq2)=unknown←boolean|; also |attr←loc(qq2)=h(b)|, |parent(qq2)=q1|, |name←type(qq2)=attr|, |link(qq2)=end←attr|. (Maybe colored lines will help untangle your picture.) Node |r| is a subscript node with |type| and |value| representing `\.{x5}'; |name←type(r)=subscr|, |subscript(r)=5.0|, and |link(r)=r1| is another subscript node. To complete the picture, see if you can guess what |link(r1)| is; give up? It's~|q1|. Furthermore |subscript(r1)=20.0|, |name←type(r1)=subscr|, |type(r1)=structured|, |attr←head(r1)=qqq|, |subscr←head(r1)=qqq1|, and we finish things off with three more nodes |qqq|, |qqq1|, and |qqq2| hung onto~|r1|. (Perhaps you should start again with a larger sheet of paper.) The value of variable \.{x20b} appears in node~|qqq2|, as you can well imagine. If the example in the previous paragraph doesn't make things crystal clear, a glance at some of the simpler subroutines below will reveal how things work out in practice. The only really unusual thing about these conventions is the use of collective subscript attributes. The idea is to avoid repeating a lot of type information when many elements of an array are identical macros (for which distinct values need not be stored) or when they don't have all of the possible attributes. Branches of the structure below collective subscript attributes do not carry actual values except for macro identifiers; branches of the structure below subscript nodes do not carry significant information in their collective subscript attributes. @d attr←loc←loc(#)==#+2 {where the |attr←loc| and |parent| fields are} @d attr←loc(#)==info(attr←loc←loc(#)) {hash address of this attribute} @d parent(#)==link(attr←loc←loc(#)) {pointer to |structured| variable} @d subscript←loc(#)==#+2 {where the |subscript| field lives} @d subscript(#)==mem[subscript←loc(#)].sc {subscript of this variable} @d attr←node←size=3 {the number of words in an attribute node} @d subscr←node←size=3 {the number of words in a subscript node} @d collective←subscript=0 {code for the attribute `\.{[]}'} @<Initialize table...@>= attr←loc(end←attr):=hash←end+1; parent(end←attr):=null; @ Variables of type \&{pair} will have values that point to four-word nodes containing two numeric values. The first of these values has |name←type=x←part←sector| and the second has |name←type=y←part←sector|; the |link| in the first points back to the node whose |value| points to this four-word node. Variables of type \&{transform} are similar, but in this case their |value| points to a 12-word node containing six values, identified by |x←part←sector|, |y←part←sector|, |xx←part←sector|, |xy←part←sector|, |yx←part←sector|, and |yy←part←sector|. When an entire structured variable is saved, the |root| indication is temporarily replaced by |saved←root|. Some variables have no name; they just are used for temporary storage while expressions are being evaluated. We call them {\sl capsules}. @d x←part←loc(#)==# {where the \&{xpart} is found in a pair or transform node} @d y←part←loc(#)==#+2 {where the \&{ypart} is found in a pair or transform node} @d xx←part←loc(#)==#+4 {where the \&{xxpart} is found in a transform node} @d xy←part←loc(#)==#+6 {where the \&{xypart} is found in a transform node} @d yx←part←loc(#)==#+8 {where the \&{yxpart} is found in a transform node} @d yy←part←loc(#)==#+10 {where the \&{yypart} is found in a transform node} @# @d pair←node←size=4 {the number of words in a pair node} @d transform←node←size=12 {the number of words in a transform node} @<Glob...@>= @!big←node←size:array[transform←type..pair←type] of small←number; @ The |big←node←size| array simply contains two constants that \MF\ occasionally needs to know. @<Set init...@>= big←node←size[transform←type]:=transform←node←size; big←node←size[pair←type]:=pair←node←size; @ If |type(p)=pair←type| or |transform←type| and if |value(p)=null|, the procedure call |init←big←node(p)| will allocate a pair or transform node for~|p|. The individual parts of such nodes are initially of type |independent|. @p procedure init←big←node(@!p:pointer); var @!q:pointer; {the new node} @!s:small←number; {its size} begin s:=big←node←size[type(p)]; q:=get←node(s); repeat s:=s-2; type(q+s):=independent; value(q+s):=0; name←type(q+s):=half(s)+x←part←sector; link(q+s):=null; until s=0; link(q):=p; value(p):=q; end; @ The |id←transform| function creates a capsule for the identity transformation. @p function id←transform:pointer; var @!p,@!q,@!r:pointer; {list manipulation registers} begin p:=get←node(value←node←size); type(p):=transform←type; name←type(p):=capsule; value(p):=null; init←big←node(p); q:=value(p); r:=q+transform←node←size; repeat r:=r-2; type(r):=known; value(r):=0; until r=q; value(xx←part←loc(q)):=unity; value(yy←part←loc(q)):=unity; id←transform:=p; end; @ Tokens are of type |tag←token| when they first appear, but they point to |null| until they are first used as the root of a variable. The following subroutine establishes the root node on such grand occasions. @p procedure new←root(@!x:pointer); var @!p:pointer; {the new node} begin p:=get←node(value←node←size); type(p):=undefined; name←type(p):=root; link(p):=x; equiv(x):=p; end; @ These conventions for variable representation are illustrated by the |print←variable←name| routine, which displays the full name of a variable given only a pointer to its two-word value packet. @p procedure print←variable←name(@!p:pointer); label found,exit; var @!q:pointer; {a token list that will name the variable's suffix} @!r:pointer; {temporary for token list creation} begin while name←type(p)>=x←part←sector do @<Preface the output with a part specifier; |return| in the case of a capsule@>; q:=null; while name←type(p)>saved←root do @<Ascend one level, pushing a token onto list |q| and replacing |p| by its parent@>; r:=get←avail; info(r):=link(p); link(r):=q; if name←type(p)=saved←root then print("(SAVED)"); @.SAVED@> show←token←list(r,null,el←gordo,tally); flush←token←list(r); exit:end; @ @<Ascend one level, pushing a token onto list |q|...@>= begin if name←type(p)=subscr then begin r:=new←num←tok(subscript(p)); repeat p:=link(p); until name←type(p)=attr; end else if name←type(p)=structured←root then begin p:=link(p); goto found; end else begin if name←type(p)<>attr then confusion("var"); @:this can't happen var}{\quad var@> r:=get←avail; info(r):=attr←loc(p); end; link(r):=q; q:=r; found: p:=parent(p); end @ @<Preface the output with a part specifier...@>= begin case name←type(p) of x←part←sector: print←char("x"); y←part←sector: print←char("y"); xx←part←sector: print("xx"); xy←part←sector: print("xy"); yx←part←sector: print("yx"); yy←part←sector: print("yy"); capsule: begin print("%CAPSULE"); print←int(p-null); return; @.CAPSULE@> end; end; {there are no other cases} print("part "); p:=link(p-2*(name←type(p)-x←part←sector)); end @ The |interesting| function returns |true| if a given variable is not in a capsule, or if the user wants to trace capsules. @p function interesting(@!p:pointer):boolean; var @!t:small←number; {a |name←type|} begin if internal[tracing←capsules]>0 then interesting:=true else begin t:=name←type(p); if t>=x←part←sector then if t<>capsule then t:=name←type(link(p-2*(t-x←part←sector))); interesting:=(t<>capsule); end; end; @ Now here is a subroutine that converts an unstructured type into an equivalent structured type, by inserting a |structured| node that is capable of growing. This operation is done only when |name←type(p)=root|, |subscr|, or |attr|. The procedure returns a pointer to the new node that has taken node~|p|'s place in the structure. Node~|p| itself does not move, nor are its |value| or |type| fields changed in any way. @p function new←structure(@!p:pointer):pointer; var @!q,@!r:pointer; {list manipulation registers} begin case name←type(p) of root: begin q:=link(p); r:=get←node(value←node←size); equiv(q):=r; end; subscr: @<Link a new subscript node |r| in place of node |p|@>; attr: @<Link a new attribute node |r| in place of node |p|@>; othercases confusion("struct") @:this can't happen struct}{\quad struct@> endcases;@/ link(r):=link(p); type(r):=structured; name←type(r):=name←type(p); attr←head(r):=p; name←type(p):=structured←root;@/ q:=get←node(attr←node←size); link(p):=q; subscr←head(r):=q; parent(q):=r; type(q):=undefined; name←type(q):=attr; link(q):=end←attr; attr←loc(q):=collective←subscript; new←structure:=r; end; @ @<Link a new subscript node |r| in place of node |p|@>= begin q:=p; repeat q:=link(q); until name←type(q)=attr; q:=parent(q); r:=subscr←head←loc(q); {|link(r)=subscr←head(q)|} repeat q:=r; r:=link(r); until r=p; r:=get←node(subscr←node←size); link(q):=r; subscript(r):=subscript(p); end @ If the attribute is |collective←subscript|, there are two pointers to node~|p|, so we must change both of them. @<Link a new attribute node |r| in place of node |p|@>= begin q:=parent(p); r:=attr←head(q); repeat q:=r; r:=link(r); until r=p; r:=get←node(attr←node←size); link(q):=r;@/ mem[attr←loc←loc(r)]:=mem[attr←loc←loc(p)]; {copy |attr←loc| and |parent|} if attr←loc(p)=collective←subscript then begin q:=subscr←head←loc(parent(p)); while link(q)<>p do q:=link(q); link(q):=r; end; end @ The |find←variable| routine is given a pointer~|t| to a nonempty token list of suffixes; it returns a pointer to the corresponding two-word value. For example, if |t| points to token \.x followed by a numeric token containing the value~7, |find←variable| finds where the value of \.{x7} is stored in memory. This may seem a simple task, and it usually is, except when \.{x7} has never been referenced before. Indeed, \.x may never have even been subscripted before; complexities arise with respect to updating the collective subscript information. If a macro type is detected anywhere along path~|t|, or if the first item on |t| isn't a |tag←token|, the value |null| is returned. Otherwise |p| will be a non-null pointer to a node such that |undefined<type(p)<structured|. @d abort←find==begin find←variable:=null; return;@+end @p function find←variable(@!t:pointer):pointer; label exit; var @!p,@!q,@!r,@!s:pointer; {nodes in the ``value'' line} @!pp,@!qq,@!rr,@!ss:pointer; {nodes in the ``collective'' line} @!n:integer; {subscript or attribute} @!save←word:memory←word; {temporary storage for a word of |mem|} @↑inner loop@> begin p:=info(t); t:=link(t); if eq←type(p) mod outer←tag<>tag←token then abort←find; if equiv(p)=null then new←root(p); p:=equiv(p); pp:=p; while t<>null do begin @<Make sure that both nodes |p| and |pp| are of |structured| type@>; if t<hi←mem←min then @<Descend one level for the subscript |value(t)|@> else @<Descend one level for the attribute |info(t)|@>; t:=link(t); end; if type(pp)>=structured then if type(pp)=structured then pp:=attr←head(pp)@+else abort←find; if type(p)=structured then p:=attr←head(p); if type(p)=undefined then begin if type(pp)=undefined then begin type(pp):=numeric←type; value(pp):=null; end; type(p):=type(pp); value(p):=null; end; find←variable:=p; exit:end; @ Although |pp| and |p| begin together, they diverge when a subscript occurs; |pp|~stays in the collective line while |p|~goes through actual subscript values. @<Make sure that both nodes |p| and |pp|...@>= if type(pp)<>structured then begin if type(pp)>structured then abort←find; ss:=new←structure(pp); if p=pp then p:=ss; pp:=ss; end; {now |type(pp)=structured|} if type(p)<>structured then {it cannot be |>structured|} p:=new←structure(p) {now |type(p)=structured|} @ We want this part of the program to be reasonably fast, in case there are @↑inner loop@> lots of subscripts at the same level of the data structure. Therefore we store an ``infinite'' value in the word that appears at the end of the subscript list, even though that word isn't part of a subscript node. @<Descend one level for the subscript |value(t)|@>= begin n:=value(t); pp:=link(attr←head(pp)); {now |attr←loc(pp)=collective←subscript|} q:=link(attr←head(p)); save←word:=mem[subscript←loc(q)]; subscript(q):=el←gordo; s:=subscr←head←loc(p); {|link(s)=subscr←head(p)|} repeat r:=s; s:=link(s); until n<=subscript(s); if n=subscript(s) then p:=s else begin p:=get←node(subscr←node←size); link(r):=p; link(p):=s; subscript(p):=n; name←type(p):=subscr; type(p):=undefined; end; mem[subscript←loc(q)]:=save←word; end @ @<Descend one level for the attribute |info(t)|@>= begin n:=info(t); ss:=attr←head(pp); repeat rr:=ss; ss:=link(ss); until n<=attr←loc(ss); if n<attr←loc(ss) then begin qq:=get←node(attr←node←size); link(rr):=qq; link(qq):=ss; attr←loc(qq):=n; name←type(qq):=attr; type(qq):=undefined; parent(qq):=pp; ss:=qq; end; if p=pp then begin p:=ss; pp:=ss; end else begin pp:=ss; s:=attr←head(p); repeat r:=s; s:=link(s); until n<=attr←loc(s); if n=attr←loc(s) then p:=s else begin q:=get←node(attr←node←size); link(r):=q; link(q):=s; attr←loc(q):=n; name←type(q):=attr; type(q):=undefined; parent(q):=p; p:=q; end; end; end @ Variables lose their former values when they appear in a type declaration, or when they are defined to be macros or \&{let} equal to something else. A subroutine will be defined later that recycles the storage associated with any particular |type| or |value|; our goal now is to study a higher level process called |flush←variable|, which selectively frees parts of a variable structure. This routine has some complexity because of examples such as $$\hbox{\tt numeric x[]a[]b}$$ which recycles all variables of the form \.{x[i]a[j]b} (and no others), while $$\hbox{\tt vardef x[]a[]=...}$$ discards all variables of the form \.{x[i]a[j]} followed by an arbitrary suffix, except for the collective node \.{x[]a[]} itself. The obvious way to handle such examples is to use recursion; so that's what we do. @↑recursion@> Parameter |p| points to the root information of the variable; parameter |t| points to a list of one-word nodes that represent suffixes, with |info=collective←subscript| for subscripts. @p @t\4@>@<Declare subroutines for printing expressions@>@;@/ @t\4@>@<Declare basic dependency-list subroutines@>@; @t\4@>@<Declare the recycling subroutines@>@; @t\4@>@<Declare the procedure called |flush←cur←exp|@>@; @t\4@>@<Declare the procedure called |flush←below←variable|@>@; procedure flush←variable(@!p,@!t:pointer;@!discard←suffixes:boolean); label exit; var @!q,@!r:pointer; {list manipulation} @!n:halfword; {attribute to match} begin while t<>null do begin if type(p)<>structured then return; n:=info(t); t:=link(t); if n=collective←subscript then begin r:=subscr←head←loc(p); q:=link(r); {|q=subscr←head(p)|} while name←type(q)=subscr do begin flush←variable(q,t,discard←suffixes); if t=null then if type(q)=structured then r:=q else begin link(r):=link(q); free←node(q,subscr←node←size); end else r:=q; q:=link(r); end; end; p:=attr←head(p); repeat r:=p; p:=link(p); until attr←loc(p)>=n; if attr←loc(p)<>n then return; end; if discard←suffixes then flush←below←variable(p) else begin if type(p)=structured then p:=attr←head(p); recycle←value(p); end; exit:end; @ The next procedure is simpler; it wipes out everything but |p| itself, which becomes undefined. @<Declare the procedure called |flush←below←variable|@>= procedure flush←below←variable(@!p:pointer); var @!q,@!r:pointer; {list manipulation registers} begin if type(p)<>structured then recycle←value(p) {this sets |type(p)=undefined|} else begin q:=subscr←head(p); while name←type(q)=subscr do begin flush←below←variable(q); r:=q; q:=link(q); free←node(r,subscr←node←size); end; r:=attr←head(p); q:=link(r); recycle←value(r); if name←type(p)<=saved←root then free←node(r,value←node←size) else free←node(r,subscr←node←size); {we assume that |subscr←node←size=attr←node←size|} repeat flush←below←variable(q); r:=q; q:=link(q); free←node(r,attr←node←size); until q=end←attr; type(p):=undefined; end; end; @ Just before assigning a new value to a variable, we will recycle the old value and make the old value undefined. The |und←type| routine determines what type of undefined value should be given, based on the current type before recycling. @p function und←type(@!p:pointer):small←number; begin case type(p) of undefined,vacuous:und←type:=undefined; boolean←type,unknown←boolean:und←type:=unknown←boolean; string←type,unknown←string:und←type:=unknown←string; pen←type,unknown←pen,future←pen:und←type:=unknown←pen; path←type,unknown←path:und←type:=unknown←path; picture←type,unknown←picture:und←type:=unknown←picture; transform←type,pair←type,numeric←type:und←type:=type(p); known,dependent,proto←dependent,independent:und←type:=numeric←type; end; {there are no other cases} end; @ The |clear←symbol| routine is used when we want to redefine the equivalent of a symbolic token. It must remove any variable structure or macro definition that is currently attached to that symbol. If the |saving| parameter is true, a subsidiary structure is saved instead of destroyed. @p procedure clear←symbol(@!p:pointer;@!saving:boolean); var @!q:pointer; {|equiv(p)|} begin q:=equiv(p); case eq←type(p) mod outer←tag of defined←macro,secondary←primary←macro,tertiary←secondary←macro, expression←tertiary←macro: if not saving then delete←mac←ref(q); tag←token:if q<>null then if saving then name←type(q):=saved←root else begin flush←below←variable(q); free←node(q,value←node←size); end; othercases do←nothing endcases;@/ eqtb[p]:=eqtb[frozen←undefined]; end; @* \[16] Saving and restoring equivalents. The nested structure that is provided by \&{begingroup} and \&{endgroup} allows |eqtb| entries to be saved and restored, so that temporary changes can be made without difficulty. When the user requests a current value to be saved, \MF\ puts that value into its ``save stack.'' An appearance of \&{endgroup} ultimately causes the old values to be removed from the save stack and put back in their former places. The save stack is a linked list containing three kinds of entries, distinguished by their |info| fields. If |p| points to a saved item, then \smallskip\hang |info(p)=0| stands for a group boundary; each \&{begingroup} contributes such an item to the save stack and each \&{endgroup} cuts back the stack until the most recent such entry has been removed. \smallskip\hang |info(p)=q|, where |1<=q<=hash←end|, means that |mem[p+1]| holds the former contents of |eqtb[q]|. Such save stack entries are generated by \&{save} commands or suitable \&{interim} commands. \smallskip\hang |info(p)=hash←end+q|, where |q>0|, means that |value(p)| is a |scaled| integer to be restored to internal parameter number~|q|. Such entries are generated by \&{interim} commands. \smallskip\noindent The global variable |save←ptr| points to the top item on the save stack. @d save←node←size=2 {number of words per non-boundary save-stack node} @d saved←equiv(#)==mem[#+1].hh {where an |eqtb| entry gets saved} @d save←boundary←item(#)==begin #:=get←avail; info(#):=0; link(#):=save←ptr; save←ptr:=#; end @<Glob...@>=@!save←ptr:pointer; {the most recently saved item} @ @<Set init...@>=save←ptr:=null; @ The |save←variable| routine is given a hash address |q|; it salts this address in the save stack, together with its current equivalent, then makes token~|q| behave as though it were brand new. Nothing is stacked when |save←ptr=null|, however; there's no way to remove things from the stack when the program is not inside a group, so there's no point in wasting the space. @p procedure save←variable(@!q:pointer); var @!p:pointer; {temporary register} begin if save←ptr<>null then begin p:=get←node(save←node←size); info(p):=q; link(p):=save←ptr; saved←equiv(p):=eqtb[q]; save←ptr:=p; end; clear←symbol(q,(save←ptr<>null)); end; @ Similarly, |save←internal| is given the location |q| of an internal quantity like |tracing←commands|. It creates a save stack entry of the third kind. @p procedure save←internal(@!q:halfword); var @!p:pointer; {new item for the save stack} begin if save←ptr<>null then begin p:=get←node(save←node←size); info(p):=hash←end+q; link(p):=save←ptr; value(p):=internal[q]; save←ptr:=p; end; end; @ At the end of a group, the |unsave| routine restores all of the saved equivalents in reverse order. This routine will be called only when there is at least one boundary item on the save stack. @p procedure unsave; var @!q:pointer; {index to saved item} @!p:pointer; {temporary register} begin while info(save←ptr)<>0 do begin q:=info(save←ptr); if q>hash←end then begin if internal[tracing←restores]>0 then begin begin←diagnostic; print←nl("{restoring "); print(int←name[q-(hash←end)]); print←char("="); print←scaled(value(save←ptr)); print←char("}"); end←diagnostic(false); end; internal[q-(hash←end)]:=value(save←ptr); end else begin if internal[tracing←restores]>0 then begin begin←diagnostic; print←nl("{restoring "); print(text(q)); print←char("}"); end←diagnostic(false); end; clear←symbol(q,false); eqtb[q]:=saved←equiv(save←ptr); if eq←type(q) mod outer←tag=tag←token then begin p:=equiv(q); if p<>null then name←type(p):=root; end; end; p:=link(save←ptr); free←node(save←ptr,save←node←size); save←ptr:=p; end; p:=link(save←ptr); free←avail(save←ptr); save←ptr:=p; end; @* \[17] Data structures for paths. When a \MF\ user specifies a path, \MF\ will create a list of knots and control points for the associated cubic spline curves. If the knots are $z←0$, $z←1$, \dots, $z←n$, there are control points $z←k↑+$ and $z←{k+1}↑-$ such that the cubic splines between knots $z←k$ and $z←{k+1}$ are defined by B\'ezier's formula @:Bezier}{B\'ezier, Pierre Etienne@> $$\eqalign{z(t)&=B(z←k,z←k↑+,z←{k+1}↑-,z←{k+1};t)\cr &=(1-t)↑3z←k+3(1-t)↑2tz←k↑++3(1-t)t↑2z←{k+1}↑-+t↑3z←{k+1}\cr}$$ for |0<=t<=1|. There is a 7-word node for each knot $z←k$, containing one word of control information and six words for the |x| and |y| coordinates of $z←k↑-$ and $z←k$ and~$z←k↑+$. The control information appears in the |left←type| and |right←type| fields, which each occupy a quarter of the first word in the node; they specify properties of the curve as it enters and leaves the knot. There's also a halfword |link| field, which points to the following knot. If the path is a closed contour, knots 0 and |n| are identical; i.e., the |link| in knot |n-1| points to knot~0. But if the path is not closed, the |left←type| of knot~0 and the |right←type| of knot~|n| are equal to |endpoint|. In the latter case the |link| in knot~|n| points to knot~0, and the control points $z←0↑-$ and $z←n↑+$ are not used. @d left←type(#) == mem[#].hh.b0 {characterizes the path entering this knot} @d right←type(#) == mem[#].hh.b1 {characterizes the path leaving this knot} @d endpoint=0 {|left←type| at path beginning and |right←type| at path end} @d x←coord(#) == mem[#+1].sc {the |x| coordinate of this knot} @d y←coord(#) == mem[#+2].sc {the |y| coordinate of this knot} @d left←x(#) == mem[#+3].sc {the |x| coordinate of previous control point} @d left←y(#) == mem[#+4].sc {the |y| coordinate of previous control point} @d right←x(#) == mem[#+5].sc {the |x| coordinate of next control point} @d right←y(#) == mem[#+6].sc {the |y| coordinate of next control point} @d knot←node←size=7 {number of words in a knot node} @ Before the B\'ezier control points have been calculated, the memory space they will ultimately occupy is taken up by information that can be used to compute them. There are four cases: \yskip \textindent{$\bullet$} If |right←type=open|, the curve should leave the knot in the same direction it entered; \MF\ will figure out a suitable direction. \yskip \textindent{$\bullet$} If |right←type=curl|, the curve should leave the knot in a direction depending on the angle at which it enters the next knot and on the curl parameter stored in |right←curl|. \yskip \textindent{$\bullet$} If |right←type=given|, the curve should leave the knot in a nonzero direction stored as an |angle| in |right←given|. \yskip \textindent{$\bullet$} If |right←type=explicit|, the B\'ezier control point for leaving this knot has already been computed; it is in the |right←x| and |right←y| fields. \yskip\noindent The rules for |left←type| are similar, but they refer to the curve entering the knot, and to \\{left} fields instead of \\{right} fields. Non-|explicit| control points will be chosen based on ``tension'' parameters stored in the |left←tension| and |right←tension| fields. The `\&{atleast}' option is represented by negative tension values. @!@:at←least←}{\&{atleast} primitive@> For example, the \MF\ path specification $$\.{z0..z1..tension atleast 1..\{curl 2\}z2..z3\{-1,-2\}..tension 3 and 4..p},$$ where \.p is the path `\.{z4..controls z45 and z54..z5}', will be represented by the six knots \def\lodash{\hbox to 1.1em{\thinspace\hrulefill\thinspace}} $$\vbox{\halign{#\hfil&&\qquad#\hfil\cr |left←type|&\\{left} info&|x←coord,y←coord|&|right←type|&\\{right} info\cr \noalign{\yskip} |endpoint|&\lodash$,\,$\lodash&$x←0,y←0$&|curl|&$1.0,1.0$\cr |open|&\lodash$,1.0$&$x←1,y←1$&|open|&\lodash$,-1.0$\cr |curl|&$2.0,-1.0$&$x←2,y←2$&|curl|&$2.0,1.0$\cr |given|&$d,1.0$&$x←3,y←3$&|given|&$d,3.0$\cr |open|&\lodash$,4.0$&$x←4,y←4$&|explicit|&$x←{45},y←{45}$\cr |explicit|&$x←{54},y←{54}$&$x←5,y←5$&|endpoint|&\lodash$,\,$\lodash\cr}}$$ Here |d| is the |angle| obtained by calling |n←arg(-unity,-two)|. Of course, this example is more complicated than anything a normal user would ever write. These types must satisfy certain restrictions because of the form of \MF's path syntax: (i)~|open| type never appears in the same node together with |endpoint|, |given|, or |curl|. (ii)~The |right←type| of a node is |explicit| if and only if the |left←type| of the following node is |explicit|. (iii)~|endpoint| types occur only at the ends, as mentioned above. @d left←curl==left←x {curl information when entering this knot} @d left←given==left←x {given direction when entering this knot} @d left←tension==left←y {tension information when entering this knot} @d right←curl==right←x {curl information when leaving this knot} @d right←given==right←x {given direction when leaving this knot} @d right←tension==right←y {tension information when leaving this knot} @d explicit=1 {|left←type| or |right←type| when control points are known} @d given=2 {|left←type| or |right←type| when a direction is given} @d curl=3 {|left←type| or |right←type| when a curl is desired} @d open=4 {|left←type| or |right←type| when \MF\ should choose the direction} @ Here is a diagnostic routine that prints a given knot list in symbolic form. It illustrates the conventions discussed above, and checks for anomalies that might arise while \MF\ is being debugged. @<Declare subroutines for printing expressions@>= procedure print←path(@!h:pointer;@!s:str←number;@!nuline:boolean); label done,done1; var @!p,@!q:pointer; {for list traversal} begin print←diagnostic("Path",s,nuline); print←ln; @.Path at line...@> p:=h; repeat q:=link(p); if (p=null)or(q=null) then begin print←nl("???"); goto done; {this won't happen} @.???@> end; @<Print information for adjacent knots |p| and |q|@>; p:=q; if (p<>h)or(left←type(h)<>endpoint) then @<Print two dots, followed by |given| or |curl| if present@>; until p=h; if left←type(h)<>endpoint then print("cycle"); done:end←diagnostic(true); end; @ @<Print information for adjacent knots...@>= print←two(x←coord(p),y←coord(p)); case right←type(p) of endpoint: begin if left←type(p)=open then print("{open?}"); {can't happen} @.open?@> if (left←type(q)<>endpoint)or(q<>h) then q:=null; {force an error} goto done1; end; explicit: @<Print control points between |p| and |q|, then |goto done1|@>; open: @<Print information for a curve that begins |open|@>; curl,given: @<Print information for a curve that begins |curl| or |given|@>; othercases print("???") {can't happen} @.???@> endcases;@/ if left←type(q)<=explicit then print("..control?") {can't happen} @.control?@> else if (right←tension(p)<>unity)or(left←tension(q)<>unity) then @<Print tension between |p| and |q|@>; done1: @ Since |n←sin←cos| produces |fraction| results, which we will print as if they were |scaled|, the magnitude of a |given| direction vector will be~4096. @<Print two dots...@>= begin print←nl(" .."); if left←type(p)=given then begin n←sin←cos(left←given(p)); print←char("{"); print←scaled(n←cos); print←char(","); print←scaled(n←sin); print←char("}"); end else if left←type(p)=curl then begin print("{curl "); print←scaled(left←curl(p)); print←char("}"); end; end @ @<Print tension between |p| and |q|@>= begin print("..tension "); if right←tension(p)<0 then print("atleast"); print←scaled(abs(right←tension(p))); if right←tension(p)<>left←tension(q) then begin print(" and "); if left←tension(q)<0 then print("atleast"); print←scaled(abs(left←tension(q))); end; end @ @<Print control points between |p| and |q|, then |goto done1|@>= begin print("..controls "); print←two(right←x(p),right←y(p)); print(" and "); if left←type(q)<>explicit then print("??") {can't happen} @.??@> else print←two(left←x(q),left←y(q)); goto done1; end @ @<Print information for a curve that begins |open|@>= if (left←type(p)<>explicit)and(left←type(p)<>open) then print("{open?}") {can't happen} @.open?@> @ A curl of 1 is shown explicitly, so that the user sees clearly that \MF's default curl is present. The code here uses the fact that |left←curl==left←given| and |right←curl==right←given|. @<Print information for a curve that begins |curl|...@>= begin if left←type(p)=open then print("??"); {can't happen} @.??@> if right←type(p)=curl then begin print("{curl "); print←scaled(right←curl(p)); end else begin n←sin←cos(right←given(p)); print←char("{"); print←scaled(n←cos); print←char(","); print←scaled(n←sin); end; print←char("}"); end @ If we want to duplicate a knot node, we can say |copy←knot|: @p function copy←knot(@!p:pointer):pointer; var @!q:pointer; {the copy} @!k:0..knot←node←size-1; {runs through the words of a knot node} begin q:=get←node(knot←node←size); for k:=0 to knot←node←size-1 do mem[q+k]:=mem[p+k]; copy←knot:=q; end; @ The |copy←path| routine makes a clone of a given path. @p function copy←path(@!p:pointer):pointer; label exit; var @!q,@!pp,@!qq:pointer; {for list manipulation} begin q:=get←node(knot←node←size); {this will correspond to |p|} qq:=q; pp:=p; loop@+ begin left←type(qq):=left←type(pp); right←type(qq):=right←type(pp);@/ x←coord(qq):=x←coord(pp); y←coord(qq):=y←coord(pp);@/ left←x(qq):=left←x(pp); left←y(qq):=left←y(pp);@/ right←x(qq):=right←x(pp); right←y(qq):=right←y(pp);@/ if link(pp)=p then begin link(qq):=q; copy←path:=q; return; end; link(qq):=get←node(knot←node←size); qq:=link(qq); pp:=link(pp); end; exit:end; @ Similarly, there's a way to copy the {\sl reverse\/} of a path. This procedure returns a pointer to the first node of the copy, if the path is a cycle, but to the final node of a non-cyclic copy. The global variable |path←tail| will point to the final node of the original path; this trick makes it easier to implement `\&{doublepath}'. All node types are assumed to be |endpoint| or |explicit| only. @p function htap←ypoc(@!p:pointer):pointer; label exit; var @!q,@!pp,@!qq,@!rr:pointer; {for list manipulation} begin q:=get←node(knot←node←size); {this will correspond to |p|} qq:=q; pp:=p; loop@+ begin right←type(qq):=left←type(pp); left←type(qq):=right←type(pp);@/ x←coord(qq):=x←coord(pp); y←coord(qq):=y←coord(pp);@/ right←x(qq):=left←x(pp); right←y(qq):=left←y(pp);@/ left←x(qq):=right←x(pp); left←y(qq):=right←y(pp);@/ if link(pp)=p then begin link(q):=qq; path←tail:=pp; htap←ypoc:=q; return; end; rr:=get←node(knot←node←size); link(rr):=qq; qq:=rr; pp:=link(pp); end; exit:end; @ @<Glob...@>= @!path←tail:pointer; {the node that links to the beginning of a path} @ When a cyclic list of knot nodes is no longer needed, it can be recycled by calling the following subroutine. @<Declare the recycling subroutines@>= procedure toss←knot←list(@!p:pointer); var @!q:pointer; {the node being freed} @!r:pointer; {the next node} begin q:=p; repeat r:=link(q); free←node(q,knot←node←size); q:=r; until q=p; end; @* \[18] Choosing control points. Now we must actually delve into one of \MF's more difficult routines, the |make←choices| procedure that chooses angles and control points for the splines of a curve when the user has not specified them explicitly. The parameter to |make←choices| points to a list of knots and path information, as described above. A path decomposes into independent segments at ``breakpoint'' knots, which are knots whose left and right angles are both prespecified in some way (i.e., their |left←type| and |right←type| aren't both open). @p @t\4@>@<Declare the procedure called |solve←choices|@>@; procedure make←choices(@!knots:pointer); label done; var @!h:pointer; {the first breakpoint} @!p,@!q:pointer; {consecutive breakpoints being processed} @<Other local variables for |make←choices|@>@; begin check←arith; {make sure that |arith←error=false|} if internal[tracing←choices]>0 then print←path(knots,", before choices",true); @<If consecutive knots are equal, join them explicitly@>; @<Find the first breakpoint, |h|, on the path; insert an artificial breakpoint if the path is an unbroken cycle@>; p:=h; repeat @<Fill in the control points between |p| and the next breakpoint, then advance |p| to that breakpoint@>; until p=h; if internal[tracing←choices]>0 then print←path(knots,", after choices",true); if arith←error then @<Report an unexpected problem during the choice-making@>; end; @ @<Report an unexpected problem during the choice...@>= begin print←err("Some number got too big"); @.Some number got too big@> help2("The path that I just computed is out of range.")@/ ("So it will probably look funny. Proceed, for a laugh."); put←get←error; arith←error:=false; end @ Two knots in a row with the same coordinates will always be joined by an explicit ``curve'' whose control points are identical with the knots. @<If consecutive knots are equal, join them explicitly@>= p:=knots; repeat q:=link(p); if x←coord(p)=x←coord(q) then if y←coord(p)=y←coord(q) then if right←type(p)>explicit then begin right←type(p):=explicit; if left←type(p)=open then begin left←type(p):=curl; left←curl(p):=unity; end; left←type(q):=explicit; if right←type(q)=open then begin right←type(q):=curl; right←curl(q):=unity; end; right←x(p):=x←coord(p); left←x(q):=x←coord(p);@/ right←y(p):=y←coord(p); left←y(q):=y←coord(p); end; p:=q; until p=knots @ If there are no breakpoints, it is necessary to compute the direction angles around an entire cycle. In this case the |left←type| of the first node is temporarily changed to |end←cycle|. @d end←cycle=open+1 @<Find the first breakpoint, |h|, on the path...@>= h:=knots; loop@+ begin if left←type(h)<>open then goto done; if right←type(h)<>open then goto done; h:=link(h); if h=knots then begin left←type(h):=end←cycle; goto done; end; end; done: @ If |right←type(p)<given| and |q=link(p)|, we must have |right←type(p)=left←type(q)=explicit| or |endpoint|. @<Fill in the control points between |p| and the next breakpoint...@>= q:=link(p); if right←type(p)>=given then begin while (left←type(q)=open)and(right←type(q)=open) do q:=link(q); @<Fill in the control information between consecutive breakpoints |p| and |q|@>; end; p:=q @ Before we can go further into the way choices are made, we need to consider the underlying theory. The basic ideas implemented in |make←choices| are due to John Hobby, who introduced the notion of ``mock curvature'' @↑Hobby, John Douglas@> at a knot. Angles are chosen so that they preserve mock curvature when a knot is passed, and this has been found to produce excellent results. It is convenient to introduce some notations that simplify the necessary formulas. Let $d←{k,k+1}=\vert z\k-z←k\vert$ be the (nonzero) distance between knots |k| and |k+1|; and let $${z\k-z←k\over z←k-z←{k-1}}={d←{k,k+1}\over d←{k-1,k}}e↑{i\psi←k}$$ so that a polygonal line from $z←{k-1}$ to $z←k$ to $z\k$ turns left through an angle of~$\psi←k$. We assume that $\vert\psi←k\vert\L180↑\circ$. The control points for the spline from $z←k$ to $z\k$ will be denoted by $$\eqalign{z←k↑+&=z←k+ \textstyle{1\over3}\rho←k e↑{i\theta←k}(z\k-z←k),\cr z\k↑-&=z\k- \textstyle{1\over3}\sigma\k e↑{-i\phi\k}(z\k-z←k),\cr}$$ where $\rho←k$ and $\sigma\k$ are nonnegative ``velocity ratios'' at the beginning and end of the curve, while $\theta←k$ and $\phi\k$ are the corresponding ``offset angles.'' These angles satisfy the condition $$\theta←k+\phi←k+\psi←k=0,\eqno(*)$$ whenever the curve leaves an intermediate knot~|k| in the direction that it enters. @ Let $\alpha←k$ and $\beta\k$ be the reciprocals of the ``tension'' of the curve at its beginning and ending points. This means that $\rho←k=\alpha←k f(\theta←k,\phi\k)$ and $\sigma\k=\beta\k f(\phi\k,\theta←k)$, where $f(\theta,\phi)$ is \MF's standard velocity function defined in the |velocity| subroutine. The cubic spline $B(z←k↑{\phantom+},z←k↑+, z\k↑-,z\k↑{\phantom+};t)$ has curvature @↑curvature@> $${2\sigma\k\sin(\theta←k+\phi\k)-6\sin\theta←k\over\rho←k↑2d←{k,k+1}} \qquad{\rm and}\qquad {2\rho←k\sin(\theta←k+\phi\k)-6\sin\phi\k\over\sigma\k↑2d←{k,k+1}}$$ at |t=0| and |t=1|, respectively. The mock curvature is the linear @↑mock curvature@> approximation to this true curvature that arises in the limit for small $\theta←k$ and~$\phi\k$, if second-order terms are discarded. The standard velocity function satisfies $$f(\theta,\phi)=1+O(\theta↑2+\theta\phi+\phi↑2);$$ hence the mock curvatures are respectively $${2\beta\k(\theta←k+\phi\k)-6\theta←k\over\alpha←k↑2d←{k,k+1}} \qquad{\rm and}\qquad {2\alpha←k(\theta←k+\phi\k)-6\phi\k\over\beta\k↑2d←{k,k+1}}.\eqno(**)$$ @ The turning angles $\psi←k$ are given, and equation $(*)$ above determines $\phi←k$ when $\theta←k$ is known, so the task of angle selection is essentially to choose appropriate values for each $\theta←k$. When equation~$(*)$ is used to eliminate $\phi$~variables from $(**)$, we obtain a system of linear equations of the form $$A←k\theta←{k-1}+(B←k+C←k)\theta←k+D←k\theta\k=-B←k\psi←k-D←k\psi\k,$$ where $$A←k={\alpha←{k-1}\over\beta←k↑2d←{k-1,k}}, \qquad B←k={3-\alpha←{k-1}\over\beta←k↑2d←{k-1,k}}, \qquad C←k={3-\beta\k\over\alpha←k↑2d←{k,k+1}}, \qquad D←k={\beta\k\over\alpha←k↑2d←{k,k+1}}.$$ The tensions are always $3\over4$ or more, hence each $\alpha$ and~$\beta$ will be at most $4\over3$. It follows that $B←k\G{5\over4}A←k$ and $C←k\G{5\over4}D←k$; hence the equations are diagonally dominant; hence they have a unique solution. Moreover, in most cases the tensions are equal to~1, so that $B←k=2A←k$ and $C←k=2D←k$. This makes the solution numerically stable, and there is an exponential damping effect: The data at knot $k\pm j$ affects the angle at knot~$k$ by a factor of~$O(2↑{-j})$. @ However, we still must consider the angles at the starting and ending knots of a non-cyclic path. These angles might be given explicitly, or they might be specified implicitly in terms of an amount of ``curl.'' Let's assume that angles need to be determined for a non-cyclic path starting at $z←0$ and ending at~$z←n$. Then equations of the form $$A←k\theta←{k-1}+(B←k+C←k)\theta←k+D←k\theta←{k+1}=R←k$$ have been given for $0<k<n$, and it will be convenient to introduce equations of the same form for $k=0$ and $k=n$, where $$A←0=B←0=C←n=D←n=0.$$ If $\theta←0$ is supposed to have a given value $E←0$, we simply define $C←0=0$, $D←0=0$, and $R←0=E←0$. Otherwise a curl parameter, $\gamma←0$, has been specified at~$z←0$; this means that the mock curvature at $z←0$ should be $\gamma←0$ times the mock curvature at $z←1$; i.e., $${2\beta←1(\theta←0+\phi←1)-6\theta←0\over\alpha←0↑2d←{01}} =\gamma←0{2\alpha←0(\theta←0+\phi←1)-6\phi←1\over\beta←1↑2d←{01}}.$$ This equation simplifies to $$(\alpha←0\chi←0+3-\beta←1)\theta←0+ \bigl((3-\alpha←0)\chi←0+\beta←1\bigr)\theta←1= -\bigl((3-\alpha←0)\chi←0+\beta←1\bigr)\psi←1,$$ where $\chi←0=\alpha←0↑2\gamma←0/\beta←1↑2$; so we can set $C←0= \chi←0\alpha←0+3-\beta←1$, $D←0=(3-\alpha←0)\chi←0+\beta←1$, $R←0=-D←0\psi←1$. It can be shown that $C←0>0$ and $C←0B←1-A←1D←0>0$ when $\gamma←0\G0$, hence the linear equations remain nonsingular. Similar considerations apply at the right end, when the final angle $\phi←n$ may or may not need to be determined. It is convenient to let $\psi←n=0$, hence $\theta←n=-\phi←n$. We either have an explicit equation $\theta←n=E←n$, or we have $$\bigl((3-\beta←n)\chi←n+\alpha←{n-1}\bigr)\theta←{n-1}+ (\beta←n\chi←n+3-\alpha←{n-1})\theta←n=0,\qquad \chi←n={\beta←n↑2\gamma←n\over\alpha←{n-1}↑2}.$$ When |make←choices| chooses angles, it must compute the coefficients of these linear equations, then solve the equations. To compute the coefficients, it is necessary to compute arctangents of the given turning angles~$\psi←k$. When the equations are solved, the chosen directions $\theta←k$ are put back into the form of control points by essentially computing sines and cosines. @ OK, we are ready to make the hard choices of |make←choices|. Most of the work is relegated to an auxiliary procedure called |solve←choices|, which has been introduced to keep |make←choices| from being extremely long. @<Fill in the control information between...@>= @<Calculate the turning angles $\psi←k$ and the distances $d←{k,k+1}$; set $n$ to the length of the path@>; @<Remove |open| types at the breakpoints@>; solve←choices(p,q,n) @ It's convenient to precompute quantities that will be needed several times later. The values of |delta←x[k]| and |delta←y[k]| will be the coordinates of $z\k-z←k$, and the magnitude of this vector will be |delta[k]=@t$d←{k,k+1}$@>|. The path angle $\psi←k$ between $z←k-z←{k-1}$ and $z\k-z←k$ will be stored in |psi[k]|. @<Glob...@>= @!delta←x,@!delta←y,@!delta:array[0..path←size] of scaled; {knot differences} @!psi:array[1..path←size] of angle; {turning angles} @ @<Other local variables for |make←choices|@>= @!k,@!n:0..path←size; {current and final knot numbers} @!r,@!s,@!t:pointer; {registers for list traversal} @!delx,@!dely:scaled; {directions where |open| meets |explicit|} @!sine,@!cosine:fraction; {trig functions of various angles} @ @<Calculate the turning angles...@>= k:=0; s:=p; n:=path←size; repeat t:=link(s); delta←x[k]:=x←coord(t)-x←coord(s); delta←y[k]:=y←coord(t)-y←coord(s); delta[k]:=pyth←add(delta←x[k],delta←y[k]); if k>0 then begin sine:=make←fraction(delta←y[k-1],delta[k-1]); cosine:=make←fraction(delta←x[k-1],delta[k-1]); psi[k]:=n←arg(take←fraction(delta←x[k],cosine)+ take←fraction(delta←y[k],sine), take←fraction(delta←y[k],cosine)- take←fraction(delta←x[k],sine)); end; @:METAFONT capacity exceeded path size}{\quad path size@> incr(k); s:=t; if k=path←size then overflow("path size",path←size); if s=q then n:=k; until (k>=n)and(left←type(s)<>end←cycle); if k=n then psi[n]:=0@+else psi[k]:=psi[1] @ When we get to this point of the code, |right←type(p)| is either |given| or |curl| or |open|. If it is |open|, we must have |left←type(p)=end←cycle| or |left←type(p)=explicit|. In the latter case, the |open| type is converted to |given|; however, if the velocity coming into this knot is zero, the |open| type is converted to a |curl|, since we don't know the incoming direction. Similarly, |left←type(q)| is either |given| or |curl| or |open| or |end←cycle|. The |open| possibility is reduced either to |given| or to |curl|. @<Remove |open| types at the breakpoints@>= if left←type(q)=open then begin delx:=right←x(q)-x←coord(q); dely:=right←y(q)-y←coord(q); if (delx=0)and(dely=0) then begin left←type(q):=curl; left←curl(q):=unity; end else begin left←type(q):=given; left←given(q):=n←arg(delx,dely); end; end; if (right←type(p)=open)and(left←type(p)=explicit) then begin delx:=x←coord(p)-left←x(p); dely:=y←coord(p)-left←y(p); if (delx=0)and(dely=0) then begin right←type(p):=curl; right←curl(p):=unity; end else begin right←type(p):=given; right←given(p):=n←arg(delx,dely); end; end @ Linear equations need to be solved whenever |n>1|; and also when |n=1| and exactly one of the breakpoints involves a curl. The simplest case occurs when |n=1| and there is a curl at both breakpoints; then we simply draw a straight line. But before coding up the simple cases, we might as well face the general case, since we must deal with it sooner or later, and since the general case is likely to give some insight into the way simple cases can be handled best. When there is no cycle, the linear equations to be solved form a tri-diagonal system, and we can apply the standard technique of Gaussian elimination to convert that system to a sequence of equations of the form $$\theta←0+u←0\theta←1=v←0,\quad \theta←1+u←1\theta←2=v←1,\quad\ldots,\quad \theta←{n-1}+u←{n-1}\theta←n=v←{n-1},\quad \theta←n=v←n.$$ It is possible to do this diagonalization while generating the equations. Once $\theta←n$ is known, it is easy to determine $\theta←{n-1}$, \dots, $\theta←1$, $\theta←0$; thus, the equations will be solved. The procedure is slightly more complex when there is a cycle, but the basic idea will be nearly the same. In the cyclic case the right-hand sides will be $v←k+w←k\theta←0$ instead of simply $v←k$, and we will start the process off with $u←0=v←0=0$, $w←0=1$. The final equation will be not $\theta←n=v←n$ but $\theta←n+u←n\theta←1=v←n+w←n\theta←0$; an appropriate ending routine will take account of the fact that $\theta←n=\theta←0$ and eliminate the $w$'s from the system, after which the solution can be obtained as before. When $u←k$, $v←k$, and $w←k$ are being computed, the three pointer variables |r|, |s|,~|t| will point respectively to knots |k-1|, |k|, and~|k+1|. The $u$'s and $w$'s are scaled by $2↑{28}$, i.e., they are of type |fraction|; the $\theta$'s and $v$'s are of type |angle|. @<Glob...@>= @!theta:array[0..path←size] of angle; {values of $\theta←k$} @!uu:array[0..path←size] of fraction; {values of $u←k$} @!vv:array[0..path←size] of angle; {values of $v←k$} @!ww:array[0..path←size] of fraction; {values of $w←k$} @ Our immediate problem is to get the ball rolling by setting up the first equation or by realizing that no equations are needed, and to fit this initialization into a framework suitable for the overall computation. @<Declare the procedure called |solve←choices|@>= @t\4@>@<Declare subroutines needed by |solve←choices|@>@; procedure solve←choices(@!p,@!q:pointer;@!n:halfword); label found,exit; var @!k:0..path←size; {current knot number} @!r,@!s,@!t:pointer; {registers for list traversal} @!sine,@!cosine:fraction; {trig functions of various angles} @<Other local variables for |solve←choices|@>@; begin k:=0; s:=p; loop@+ begin t:=link(s); if k=0 then @<Get the linear equations started; or |return| with the control points in place, if linear equations needn't be solved@> else case left←type(s) of end←cycle,open:@<Set up equation to match mock curvatures at $z←k$; then |goto found| with $\theta←n$ adjusted to equal $\theta←0$, if a cycle has ended@>; curl:@<Set up equation for a curl at $\theta←n$ and |goto found|@>; given:@<Calculate the given value of $\theta←n$ and |goto found|@>; end; {there are no other cases} r:=s; s:=t; incr(k); end; found:@<Finish choosing angles and assigning control points@>; exit:end; @ On the first time through the loop, we have |k=0| and |r| is not yet defined. The first linear equation, if any, will have $A←0=B←0=0$. @<Get the linear equations started...@>= case right←type(s) of given: if left←type(t)=given then @<Reduce to simple case of two givens and |return|@> else @<Set up the equation for a given value of $\theta←0$@>; curl: if left←type(t)=curl then @<Reduce to simple case of straight line and |return|@> else @<Set up the equation for a curl at $\theta←0$@>; open: begin uu[0]:=0; vv[0]:=0; ww[0]:=fraction←one; end; {this begins a cycle} end {there are no other cases} @ The general equation that specifies equality of mock curvature at $z←k$ is $$A←k\theta←{k-1}+(B←k+C←k)\theta←k+D←k\theta\k=-B←k\psi←k-D←k\psi\k,$$ as derived above. We want to combine this with the already-derived equation $\theta←{k-1}+u←{k-1}\theta←k=v←{k-1}+w←{k-1}\theta←0$ in order to obtain a new equation $\theta←k+u←k\theta\k=v←k+w←k\theta←0$. This can be done by dividing the equation $$(B←k-u←{k-1}A←k+C←k)\theta←k+D←k\theta\k=-B←k\psi←k-D←k\psi\k-A←kv←{k-1} -A←kw←{k-1}\theta←0$$ by $B←k-u←{k-1}A←k+C←k$. The trick is to do this carefully with fixed-point arithmetic, avoiding the chance of overflow while retaining suitable precision. The calculations will be performed in several registers that provide temporary storage for intermediate quantities. @<Other local variables for |solve←choices|@>= @!aa,@!bb,@!cc,@!ff,@!acc:fraction; {temporary registers} @!dd,@!ee:scaled; {likewise, but |scaled|} @!lt,@!rt:scaled; {tension values} @ @<Set up equation to match mock curvatures...@>= begin @<Calculate the values $\\{aa}=A←k/B←k$, $\\{bb}=D←k/C←k$, $\\{dd}=(3-\alpha←{k-1})d←{k,k+1}$, $\\{ee}=(3-\beta\k)d←{k-1,k}$, and $\\{cc}=(B←k-u←{k-1}A←k)/B←k$@>; @<Calculate the ratio $\\{ff}=C←k/(C←k+B←k-u←{k-1}A←k)$@>; uu[k]:=take←fraction(ff,bb); @<Calculate the values of $v←k$ and $w←k$@>; if left←type(s)=end←cycle then @<Adjust $\theta←n$ to equal $\theta←0$ and |goto found|@>; end @ Since tension values are never less than 3/4, the values |aa| and |bb| computed here are never more than 4/5. @<Calculate the values $\\{aa}=...@>= if abs(right←tension(r))=unity then begin aa:=fraction←half; dd:=2*delta[k]; end else begin aa:=make←fraction(unity,3*abs(right←tension(r))-unity); dd:=take←fraction(delta[k], fraction←three-make←fraction(unity,abs(right←tension(r)))); end; if abs(left←tension(t))=unity then begin bb:=fraction←half; ee:=2*delta[k-1]; end else begin bb:=make←fraction(unity,3*abs(left←tension(t))-unity); ee:=take←fraction(delta[k-1], fraction←three-make←fraction(unity,abs(left←tension(t)))); end; cc:=fraction←one-take←fraction(uu[k-1],aa) @ The ratio to be calculated in this step can be written in the form $$\beta←k↑2\cdot\\{ee}\over\beta←k↑2\cdot\\{ee}+\alpha←k↑2\cdot \\{cc}\cdot\\{dd},$$ because of the quantities just calculated. The values of |dd| and |ee| will not be needed after this step has been performed. @<Calculate the ratio $\\{ff}=C←k/(C←k+B←k-u←{k-1}A←k)$@>= dd:=take←fraction(dd,cc); lt:=abs(left←tension(s)); rt:=abs(right←tension(s)); if lt<>rt then {$\beta←k↑{-1}<>\alpha←k↑{-1}$} if lt<rt then begin ff:=make←fraction(lt,rt); ff:=take←fraction(ff,ff); {$\alpha←k↑2/\beta←k↑2$} dd:=take←fraction(dd,ff); end else begin ff:=make←fraction(rt,lt); ff:=take←fraction(ff,ff); {$\beta←k↑2/\alpha←k↑2$} ee:=take←fraction(ee,ff); end; ff:=make←fraction(ee,ee+dd) @ The value of $u←{k-1}$ will be |<=1| except when $k=1$ and the previous equation was specified by a curl. In that case we must use a special method of computation to prevent overflow. Fortunately, the calculations turn out to be even simpler in this ``hard'' case. The curl equation makes $w←0=0$ and $v←0=-u←0\psi←1$, hence $-B←1\psi←1-A←1v←0=-(B←1-u←0A←1)\psi←1=-\\{cc}\cdot B←1\psi←1$. @<Calculate the values of $v←k$ and $w←k$@>= acc:=-take←fraction(psi[k+1],uu[k]); if right←type(r)=curl then begin ww[k]:=0; vv[k]:=acc-take←fraction(psi[1],fraction←one-ff); end else begin ff:=make←fraction(fraction←one-ff,cc); {this is $B←k/(C←k+B←k-u←{k-1}A←k)<5$} acc:=acc-take←fraction(psi[k],ff); ff:=take←fraction(ff,aa); {this is $A←k/(C←k+B←k-u←{k-1}A←k)$} vv[k]:=acc-take←fraction(vv[k-1],ff); if ww[k-1]=0 then ww[k]:=0 else ww[k]:=-take←fraction(ww[k-1],ff); end @ When a complete cycle has been traversed, we have $\theta←k+u←k\theta\k= v←k+w←k\theta←0$, for |1<=k<=n|. We would like to determine the value of $\theta←n$ and reduce the system to the form $\theta←k+u←k\theta\k=v←k$ for |0<=k<n|, so that the cyclic case can be finished up just as if there were no cycle. The idea in the following code is to observe that $$\eqalign{\theta←n&=v←n+w←n\theta←0-u←n\theta←1=\cdots\cr &=v←n+w←n\theta←0-u←n\bigl(v←1+w←1\theta←0-u←1(v←2+\cdots -u←{n-2}(v←{n-1}+w←{n-1}\theta←0-u←{n-1}\theta←0))\bigr),\cr}$$ so we can solve for $\theta←n=\theta←0$. @<Adjust $\theta←n$ to equal $\theta←0$ and |goto found|@>= begin aa:=0; bb:=fraction←one; {we have |k=n|} repeat decr(k); if k=0 then k:=n; aa:=vv[k]-take←fraction(aa,uu[k]); bb:=ww[k]-take←fraction(bb,uu[k]); until k=n; {now $\theta←n=\\{aa}+\\{bb}\cdot\theta←n$} aa:=make←fraction(aa,fraction←one-bb); theta[n]:=aa; vv[0]:=aa; for k:=1 to n-1 do vv[k]:=vv[k]+take←fraction(aa,ww[k]); goto found; end @ @d reduce←angle(#)==if abs(#)>one←eighty←deg then if #>0 then #:=#-three←sixty←deg@+else #:=#+three←sixty←deg @<Calculate the given value of $\theta←n$...@>= begin theta[n]:=left←given(s)-n←arg(delta←x[n-1],delta←y[n-1]); reduce←angle(theta[n]); goto found; end @ @<Set up the equation for a given value of $\theta←0$@>= begin vv[0]:=right←given(s)-n←arg(delta←x[0],delta←y[0]); reduce←angle(vv[0]); uu[0]:=0; ww[0]:=0; end @ @<Set up the equation for a curl at $\theta←0$@>= begin cc:=right←curl(s); lt:=abs(left←tension(t)); rt:=abs(right←tension(s)); if (rt=unity)and(lt=unity) then uu[0]:=make←fraction(cc+cc+unity,cc+two) else uu[0]:=curl←ratio(cc,rt,lt); vv[0]:=-take←fraction(psi[1],uu[0]); ww[0]:=0; end @ @<Set up equation for a curl at $\theta←n$...@>= begin cc:=left←curl(s); lt:=abs(left←tension(s)); rt:=abs(right←tension(r)); if (rt=unity)and(lt=unity) then ff:=make←fraction(cc+cc+unity,cc+two) else ff:=curl←ratio(cc,lt,rt); theta[n]:=-make←fraction(take←fraction(vv[n-1],ff), fraction←one-take←fraction(ff,uu[n-1])); goto found; end @ The |curl←ratio| subroutine has three arguments, which our previous notation encourages us to call $\gamma$, $\alpha↑{-1}$, and $\beta↑{-1}$. It is a somewhat tedious program to calculate $${(3-\alpha)\alpha↑2\gamma+\beta↑3\over \alpha↑3\gamma+(3-\beta)\beta↑2},$$ with the result reduced to 4 if it exceeds 4. (This reduction of curl is necessary only if the curl and tension are both large.) The values of $\alpha$ and $\beta$ will be at most~4/3. @<Declare subroutines needed by |solve←choices|@>= function curl←ratio(@!gamma,@!a←tension,@!b←tension:scaled):fraction; var @!alpha,@!beta,@!num,@!denom,@!ff:fraction; {registers} begin alpha:=make←fraction(unity,a←tension); beta:=make←fraction(unity,b←tension);@/ if alpha<=beta then begin ff:=make←fraction(alpha,beta); ff:=take←fraction(ff,ff); gamma:=take←fraction(gamma,ff);@/ beta:=beta div @'10000; {convert |fraction| to |scaled|} denom:=take←fraction(gamma,alpha)+three-beta; num:=take←fraction(gamma,fraction←three-alpha)+beta; end else begin ff:=make←fraction(beta,alpha); ff:=take←fraction(ff,ff); beta:=take←fraction(beta,ff) div @'10000; {convert |fraction| to |scaled|} denom:=take←fraction(gamma,alpha)+(ff div 1365)-beta; {$1365\approx 2↑{12}/3$} num:=take←fraction(gamma,fraction←three-alpha)+beta; end; if num>=denom+denom+denom+denom then curl←ratio:=fraction←four else curl←ratio:=make←fraction(num,denom); end; @ We're in the home stretch now. @<Finish choosing angles and assigning control points@>= for k:=n-1 downto 0 do theta[k]:=vv[k]-take←fraction(theta[k+1],uu[k]); s:=p; k:=0; repeat t:=link(s);@/ n←sin←cos(theta[k]); st:=n←sin; ct:=n←cos;@/ n←sin←cos(-psi[k+1]-theta[k+1]); sf:=n←sin; cf:=n←cos;@/ set←controls(s,t,k);@/ incr(k); s:=t; until k=n @ The |set←controls| routine actually puts the control points into a pair of consecutive nodes |p| and~|q|. Global variables are used to record the values of $\sin\theta$, $\cos\theta$, $\sin\phi$, and $\cos\phi$ needed in this calculation. @<Glob...@>= @!st,@!ct,@!sf,@!cf:fraction; {sines and cosines} @ @<Declare subroutines needed by |solve←choices|@>= procedure set←controls(@!p,@!q:pointer;@!k:integer); var @!rr,@!ss:fraction; {velocities, divided by thrice the tension} @!lt,@!rt:scaled; {tensions} @!sine:fraction; {$\sin(\theta+\phi)$} begin lt:=abs(left←tension(q)); rt:=abs(right←tension(p)); rr:=velocity(st,ct,sf,cf,rt); ss:=velocity(sf,cf,st,ct,lt); if (right←tension(p)<0)or(left←tension(q)<0) then @<Decrease the velocities, if necessary, to stay inside the bounding triangle@>; right←x(p):=x←coord(p)+take←fraction( take←fraction(delta←x[k],ct)-take←fraction(delta←y[k],st),rr); right←y(p):=y←coord(p)+take←fraction( take←fraction(delta←y[k],ct)+take←fraction(delta←x[k],st),rr); left←x(q):=x←coord(q)-take←fraction( take←fraction(delta←x[k],cf)+take←fraction(delta←y[k],sf),ss); left←y(q):=y←coord(q)-take←fraction( take←fraction(delta←y[k],cf)-take←fraction(delta←x[k],sf),ss); right←type(p):=explicit; left←type(q):=explicit; end; @ The boundedness conditions $\\{rr}\L\sin\phi\,/\sin(\theta+\phi)$ and $\\{ss}\L\sin\theta\,/\sin(\theta+\phi)$ are to be enforced if $\sin\theta$, $\sin\phi$, and $\sin(\theta+\phi)$ all have the same sign. Otherwise there is no ``bounding triangle.'' @!@:at←least←}{\&{atleast} primitive@> @<Decrease the velocities, if necessary...@>= if((st>=0)and(sf>=0))or((st<=0)and(sf<=0)) then begin sine:=take←fraction(abs(st),cf)+take←fraction(abs(sf),ct); if sine>0 then begin sine:=take←fraction(sine,fraction←one+unity); {safety factor} if right←tension(p)<0 then if ab←vs←cd(abs(sf),fraction←one,rr,sine)<0 then rr:=make←fraction(abs(sf),sine); if left←tension(q)<0 then if ab←vs←cd(abs(st),fraction←one,ss,sine)<0 then ss:=make←fraction(abs(st),sine); end; end @ Only the simple cases remain to be handled. @<Reduce to simple case of two givens and |return|@>= begin aa:=n←arg(delta←x[0],delta←y[0]);@/ n←sin←cos(right←given(p)-aa); ct:=n←cos; st:=n←sin;@/ n←sin←cos(left←given(q)-aa); cf:=n←cos; sf:=-n←sin;@/ set←controls(p,q,0); return; end @ @<Reduce to simple case of straight line and |return|@>= begin right←type(p):=explicit; left←type(q):=explicit; lt:=abs(left←tension(q)); rt:=abs(right←tension(p)); if rt=unity then begin if delta←x[0]>=0 then right←x(p):=x←coord(p)+((delta←x[0]+1) div 3) else right←x(p):=x←coord(p)+((delta←x[0]-1) div 3); if delta←y[0]>=0 then right←y(p):=y←coord(p)+((delta←y[0]+1) div 3) else right←y(p):=y←coord(p)+((delta←y[0]-1) div 3); end else begin ff:=make←fraction(unity,3*rt); {$\alpha/3$} right←x(p):=x←coord(p)+take←fraction(delta←x[0],ff); right←y(p):=y←coord(p)+take←fraction(delta←y[0],ff); end; if lt=unity then begin if delta←x[0]>=0 then left←x(q):=x←coord(q)-((delta←x[0]+1) div 3) else left←x(q):=x←coord(q)-((delta←x[0]-1) div 3); if delta←y[0]>=0 then left←y(q):=y←coord(q)-((delta←y[0]+1) div 3) else left←y(q):=y←coord(q)-((delta←y[0]-1) div 3); end else begin ff:=make←fraction(unity,3*lt); {$\beta/3$} left←x(q):=x←coord(q)-take←fraction(delta←x[0],ff); left←y(q):=y←coord(q)-take←fraction(delta←y[0],ff); end; return; end @* \[19] Generating discrete moves. The purpose of the next part of \MF\ is to compute discrete approximations to curves described as parametric polynomial functions $z(t)$. We shall start with the low level first, because an efficient ``engine'' is needed to support the high-level constructions. Most of the subroutines are based on variations of a single theme, namely the idea of {\sl bisection}. Given a Bernshte{\u\i}n polynomial @↑Bernshte{\u\i}n, Serge{\u\i} Natanovich@> $$B(z←0,z←1,\ldots,z←n;t)=\sum←k{n\choose k}t↑k(1-t)↑{n-k}z←k,$$ we can conveniently bisect its range as follows: \smallskip \textindent{1)} Let $z←k↑{(0)}=z←k$, for |0<=k<=n|. \smallskip \textindent{2)} Let $z←k↑{(j+1)}={1\over2}(z←k↑{(j)}+z\k↑{(j)})$, for |0<=k<n-j|, for |0<=j<n|. \smallskip\noindent Then $$B(z←0,z←1,\ldots,z←n;t)=B(z←0↑{(0)},z←0↑{(1)},\ldots,z←0↑{(n)};2t) =B(z←0↑{(n)},z←1↑{(n-1)},\ldots,z←n↑{(0)};2t-1).$$ This formula gives us the coefficients of polynomials to use over the ranges $0\L t\L{1\over2}$ and ${1\over2}\L t\L1$. In our applications it will usually be possible to work indirectly with numbers that allow us to deduce relevant properties of the polynomials without actually computing the polynomial values. We will deal with coefficients $Z←k=2↑l(z←k-z←{k-1})$ for |1<=k<=n|, instead of the actual numbers $z←0$, $z←1$, \dots,~$z←n$, and the value of~|l| will increase by~1 at each bisection step. This technique reduces the amount of calculation needed for bisection and also increases the accuracy of evaluation (since one bit of precision is gained at each bisection). Indeed, the bisection process now becomes one level shorter: \smallskip \textindent{$1'$)} Let $Z←k↑{(1)}=Z←k$, for |1<=k<=n|. \smallskip \textindent{$2'$)} Let $Z←k↑{(j+1)}={1\over2}(Z←k↑{(j)}+Z\k↑{(j)})$, for |1<=k<n-j|, for |1<=j<n|. \smallskip\noindent The relevant coefficients $(Z'←1,\ldots,Z'←n)$ and $(Z''←1,\ldots,Z''←n)$ for the two subintervals after bisection are respectively $(Z←1↑{(1)},Z←1↑{(2)},\ldots,Z←1↑{(n)})$ and $(Z←1↑{(n)},Z←2↑{(n-1)},\ldots,Z←n↑{(1)})$. And the values of $z←0$ appropriate for the bisected interval are $z'←0=z←0$ and $z''←0=z←0+(Z←1+Z←2+\cdots+Z←n)/2↑{l+1}$. Step $2'$ involves division by~2, which introduces computational errors of at most $1\over2$ at each step; thus after $l$~levels of bisection the integers $Z←k$ will differ from their true values by at most $(n-1)l/2$. This error rate is quite acceptable, considering that we have $l$~more bits of precision in the $Z$'s by comparison with the~$z$'s. Note also that the $Z$'s remain bounded; there's no danger of integer overflow, even though we have the identity $Z←k=2↑l(z←k-z←{k-1})$ for arbitrarily large~$l$. In fact, we can show not only that the $Z$'s remain bounded, but also that they become nearly equal, since they are control points for a polynomial of one less degree. If $\vert Z\k-Z←k\vert\L M$ initially, it is possible to prove that $\vert Z\k-Z←k\vert\L\lceil M/2↑l\rceil$ after $l$~levels of bisection, even in the presence of rounding errors. Here's the proof [cf.~Lane and Riesenfeld, {\sl IEEE Trans.\ on Pattern Analysis @↑Lane, Jeffrey Michael@> @↑Riesenfeld, Richard Franklin@> and Machine Intelligence\/ \bf PAMI-2} (1980), 35--46]: Assuming that $\vert Z\k-Z←k\vert\L M$ before bisection, we want to prove that $\vert Z\k-Z←k\vert\L\lceil M/2\rceil$ afterward. First we show that $\vert Z\k↑{(j)}-Z←k↑{(j)}\vert\L M$ for all $j$ and~$k$, by induction on~$j$; this follows from the fact that $$\bigl\vert\\{half}(a+b)-\\{half}(b+c)\bigr\vert\L \max\bigl(\vert a-b\vert,\vert b-c\vert\bigr)$$ holds for both of the rounding rules $\\{half}(x)=\lfloor x/2\rfloor$ and $\\{half}(x)={\rm sign}(x)\lfloor\vert x/2\vert\rfloor$. (If $\vert a-b\vert$ and $\vert b-c\vert$ are equal, then $a+b$ and $b+c$ are both even or both odd. The rounding errors either cancel or round the numbers toward each other; hence $$\eqalign{\bigl\vert\\{half}(a+b)-\\{half}(b+c)\bigr\vert &\L\textstyle\bigl\vert{1\over2}(a+b)-{1\over2}(b+c)\bigr\vert\cr &=\textstyle\bigl\vert{1\over2}(a-b)+{1\over2}(b-c)\bigr\vert \L\max\bigl(\vert a-b\vert,\vert b-c\vert\bigr),\cr}$$ as required. A simpler argument applies if $\vert a-b\vert$ and $\vert b-c\vert$ are unequal.) Now it is easy to see that $\vert Z←1↑{(j+1)}-Z←1↑{(j)}\vert\L\bigl\lfloor{1\over2} \vert Z←2↑{(j)}-Z←1↑{(j)}\vert+{1\over2}\bigr\rfloor \L\bigl\lfloor{1\over2}(M+1)\bigr\rfloor=\lceil M/2\rceil$. Another interesting fact about bisection is the identity $$Z←1'+\cdots+Z←n'+Z←1''+\cdots+Z←n''=2(Z←1+\cdots+Z←n+E),$$ where $E$ is the sum of the rounding errors in all of the halving operations ($\vert E\vert\L n(n-1)/4$). @ We will later reduce the problem of digitizing a complex cubic $z(t)=B(z←0,z←1,z←2,z←3;t)$ to the following simpler problem: Given two real cubics $x(t)=B(x←0,x←1,x←2,x←3;t)$ and $y(t)=B(y←0,y←1,y←2,y←3;t)$ that are monotone nondecreasing, determine the set of integer points $$P=\bigl\{\bigl(\lfloor x(t)\rfloor,\lfloor y(t)\rfloor\bigr) \bigm\vert 0\L t\L 1\bigr\}.$$ Well, the problem isn't actually quite so clean as this; when the path goes very near an integer point $(a,b)$, computational errors may make us think that $P$ contains $(a-1,b)$ while in reality it should contain $(a,b-1)$. Furthermore, if the path goes {\sl exactly\/} through the integer points $(a-1,b-1)$ and $(a,b)$, we will want $P$ to contain one of the two points $(a-1,b)$ or $(a,b-1)$, so that $P$ can be described entirely by ``rook moves'' upwards or to the right; no diagonal moves from $(a-1,b-1)$ to~$(a,b)$ will be allowed. Thus, the set $P$ we wish to compute will merely be an approximation to the set described in the formula above. It will consist of $\lfloor x(1)\rfloor-\lfloor x(0)\rfloor$ rightward moves and $\lfloor y(1)\rfloor-\lfloor y(0)\rfloor$ upward moves, intermixed in some order. Our job will be to figure out a suitable order. The following recursive strategy suggests itself, when we recall that $x(0)=x←0$, $x(1)=x←3$, $y(0)=y←0$, and $y(1)=y←3$: \smallskip If $\lfloor x←0\rfloor=\lfloor x←3\rfloor$ then take $\lfloor y←3\rfloor-\lfloor y←0\rfloor$ steps up. Otherwise if $\lfloor y←0\rfloor=\lfloor y←3\rfloor$ then take $\lfloor x←3\rfloor-\lfloor x←0\rfloor$ steps to the right. Otherwise bisect the current cubics and repeat the process on both halves. \yskip\noindent This intuitively appealing formulation does not quite solve the problem, because it may never terminate. For example, it's not hard to see that no steps will {\sl ever\/} be taken if $(x←0,x←1,x←2,x←3)=(y←0,y←1,y←2,y←3)$! However, we can surmount this difficulty with a bit of care; so let's proceed to flesh out the algorithm as stated, before worrying about such details. The bisect-and-double strategy discussed above suggests that we represent $(x←0,x←1,x←2,x←3)$ by $(X←1,X←2,X←3)$, where $X←k=2↑l(x←k-x←{k-1})$ for some~$l$. Initially $l=16$, since the $x$'s are |scaled|. In order to deal with other aspects of the algorithm we will want to maintain also the quantities $m=\lfloor x←3\rfloor-\lfloor x←0\rfloor$ and $R=2↑l(x←0\bmod 1)$. Similarly, $(y←0,y←1,y←2,y←3)$ will be represented by $(Y←1,Y←2,Y←3)$, $n=\lfloor y←3\rfloor-\lfloor y←0\rfloor$, and $S=2↑l(y←0\bmod 1)$. The algorithm now takes the following form: \smallskip If $m=0$ then take $n$ steps up. Otherwise if $n=0$ then take $m$ steps to the right. Otherwise bisect the current cubics and repeat the process on both halves. \smallskip\noindent The bisection process for $(X←1,X←2,X←3,m,R,l)$ reduces, in essence, to the following formulas: $$\vbox{\halign{$#\hfil$\cr X←2'=\\{half}(X←1+X←2),\quad X←2''=\\{half}(X←2+X←3),\quad X←3'=\\{half}(X←2'+X←2''),\cr X←1'=X←1,\quad X←1''=X←3',\quad X←3''=X←3,\cr R'=2R,\quad T=X←1'+X←2'+X←3'+R',\quad R''=T\bmod 2↑{l+1},\cr m'=\lfloor T/2↑{l+1}\rfloor,\quad m''=m-m'.\cr}}$$ @ When $m=n=1$, the computation can be speeded up because we simply need to decide between two alternatives, (up,\thinspace right) versus (right,\thinspace up). There appears to be no simple, direct way to make the correct decision by looking at the values of $(X←1,X←2,X←3,R)$ and $(Y←1,Y←2,Y←3,S)$; but we can streamline the bisection process, and we can use the fact that only one of the two descendants needs to be examined after each bisection. Furthermore, we observed earlier that after several levels of bisection the $X$'s and $Y$'s will be nearly equal; so we will be justified in assuming that the curve is essentially a straight line. (This, incidentally, solves the problem of infinite recursion mentioned earlier.) It is possible to show that $$m=\bigl\lfloor(X←1+X←2+X←3+R+E)\,/\,2↑l\bigr\rfloor,$$ where $E$ is an accumulated rounding error that is at most $3\cdot(2↑{l-16}-1)$ in absolute value. We will make sure that the $X$'s are less than $2↑{28}$; hence when $l=30$ we must have |m<=1|. This proves that the special case $m=n=1$ is bound to be reached by the time $l=30$. Furthermore $l=30$ is a suitable time to make the straight line approximation, if the recursion hasn't already died out, because the maximum difference between $X$'s will then be $<2↑{14}$; this corresponds to an error of $<1$ with respect to the original scaling. (Stating this another way, each bisection makes the curve two bits closer to a straight line, hence 14 bisections are sufficient for 28-bit accuracy.) In the case of a straight line, the curve goes first right, then up, if and only if $(T-2↑l)(2↑l-S)>(U-2↑l)(2↑l-R)$, where $T=X←1+X←2+X←3+R$ and $U=Y←1+Y←2+Y←3+S$. For the actual curve essentially runs from $(R/2↑l,S/2↑l)$ to $(T/2↑l,U/2↑l)$, and we are testing whether or not $(1,1)$ is above the straight line connecting these two points. (This formula assumes that $(1,1)$ is not exactly on the line.) @ We have glossed over the problem of tie-breaking in ambiguous cases when the cubic curve passes exactly through integer points. \MF\ finesses this problem by assuming that coordinates $(x,y)$ actually stand for slightly perturbed values $(x+\xi,y+\eta)$, where $\xi$ and~$\eta$ are infinitesimals whose signs will determine what to do when $x$ and/or~$y$ are exact integers. The quantities $\lfloor x\rfloor$ and~$\lfloor y\rfloor$ in the formulas above should actually read $\lfloor x+\xi\rfloor$ and $\lfloor y+\eta\rfloor$. If $x$ is a |scaled| value, we have $\lfloor x+\xi\rfloor=\lfloor x\rfloor$ if $\xi>0$, and $\lfloor x+\xi\rfloor=\lfloor x-2↑{-16}\rfloor$ if $\xi<0$. It is convenient to represent $\xi$ by the integer |xi←corr|, defined to be 0~if $\xi>0$ and 1~if $\xi<0$; then, for example, the integer $\lfloor x+\xi\rfloor$ can be computed as |floor←unscaled(x-xi←corr)|. Similarly, $\eta$ is conveniently represented by~|eta←corr|. In our applications the sign of $\xi-\eta$ will always be the same as the sign of $\xi$. Therefore it turns out that the rule for straight lines, as stated above, should be modified as follows in the case of ties: The line goes first right, then up, if and only if $(T-2↑l)(2↑l-S)+\xi>(U-2↑l)(2↑l-R)$. And this relation holds iff $|ab←vs←cd|(T-2↑l,2↑l-S,U-2↑l,2↑l-R)-|xi←corr|>=0$. These conventions for rounding are symmetrical, in the sense that the digitized moves obtained from $(x←0,x←1,x←2,x←3,y←0,y←1,y←2,y←3,\xi,\eta)$ will be exactly complementary to the moves that would be obtained from $(-x←3,-x←2,-x←1,-x←0,-y←3,-y←2,-y←1,-y←0,-\xi,-\eta)$, if arithmetic is exact. However, truncation errors in the bisection process might upset the symmetry. We can restore much of the lost symmetry by adding |xi←corr| or |eta←corr| when halving the data. @ One further possibility needs to be mentioned: The algorithm will be applied only to cubic polynomials $B(x←0,x←1,x←2,x←3;t)$ that are nondecreasing as $t$~varies from 0 to~1; this condition turns out to hold if and only if $x←0\L x←1$, $x←2\L x←3$, and either $x←1\L x←2$ or $(x←1-x←2)↑2\L(x←1-x←0)(x←3-x←2)$. If bisection were carried out with perfect accuracy, these relations would remain invariant. But rounding errors can creep in, hence the bisection algorithm can produce non-monotonic subproblems from monotonic initial conditions. This leads to the potential danger that $m$ or~$n$ could become negative in the algorithm described above. For example, if we start with $(x←1-x←0,x←2-x←1,x←3-x←2)= (X←1,X←2,X←3)=(7,-16,58)$, the corresponding polynomial is monotonic, because $16↑2<7\cdot39$. But the bisection algorithm produces the left descendant $(7,-5,3)$, which is nonmonotonic; its right descendant is~$(0,-1,3)$. \def\xt{{\tilde x}} Fortunately we can prove that such rounding errors will never cause the algorithm to make a tragic mistake. At every stage we are working with numbers corresponding to a cubic polynomial $B(\xt←0, \xt←1,\xt←2,\xt←3)$ that approximates some monotonic polynomial $B(x←0,x←1,x←2,x←3)$. The accumulated errors are controlled so that $\vert x←k-\xt←k\vert<\epsilon=3\cdot2↑{-16}$. If bisection is done at some stage of the recursion, we have $m=\lfloor\xt←3\rfloor-\lfloor\xt←0\rfloor>0$, and the algorithm computes a bisection value $\bar x$ such that $m'=\lfloor\bar x\rfloor- \lfloor\xt←0\rfloor$ and $m''=\lfloor\xt←3\rfloor-\lfloor\bar x\rfloor$. We want to prove that neither $m'$ nor $m''$ can be negative. Since $\bar x$ is an approximation to a value in the interval $[x←0,x←3]$, we have $\bar x>x←0-\epsilon$ and $\bar x<x←3+\epsilon$, hence $\bar x> \xt←0-2\epsilon$ and $\bar x<\xt←3+2\epsilon$. If $m'$ is negative we must have $\xt←0\bmod 1<2\epsilon$; if $m''$ is negative we must have $\xt←3\bmod 1>1-2\epsilon$. In either case the condition $\lfloor\xt←3\rfloor-\lfloor\xt←0\rfloor>0$ implies that $\xt←3-\xt←0>1-2\epsilon$, hence $x←3-x←0>1-4\epsilon$. But it can be shown that if $B(x←0,x←1,x←2,x←3;t)$ is a monotonic cubic, then $B(x←0,x←1,x←2,x←3;{1\over2})$ is always between $.14[x←0,x←3]$ and $.86[x←0,x←3]$; and it is impossible for $\bar x$ to be within~$\epsilon$ of such a number. Contradiction! (The constant .14 is actually $(7-\sqrt{28}/12$; the worst case occurs for polynomials like $B(0,28-4\sqrt{28},14-5\sqrt{28},42)$.) @ OK, now that a long theoretical preamble has justified the bisection-and-doubling algorithm, we are ready to proceed with its actual coding. But we still haven't discussed the form of the output. For reasons to be discussed later, we shall find it convenient to record the output as follows: Moving one step up is represented by appending a `1' to a list; moving one step right is represented by adding unity to the element at the end of the list. Thus, for example, the net effect of ``(up, right, right, up, right)'' is to append $(3,2)$. The list is kept in a global array called |move|. Before starting the algorithm, \MF\ should check that $\\{move\←ptr}+\lfloor y←3\rfloor -\lfloor y←0\rfloor\L\\{move\←size}$, so that the list won't exceed the bounds of this array. @<Glob...@>= @!move:array[0..move←size] of integer; {the recorded moves} @!move←ptr:0..move←size; {the number of items in the |move| list} @ When bisection occurs, we ``push'' the subproblem corresponding to the right-hand subinterval onto the |bisect←stack| while we continue to work on the left-hand subinterval. Thus, the |bisect←stack| will hold $(X←1,X←2,X←3,R,m,Y←1,Y←2,Y←3,S,n,l)$ values for subproblems yet to be tackled. At most 15 subproblems will be on the stack at once (namely, for $l=15$,~16, \dots,~29); but the stack is bigger than this, because it is used also for more complicated bisection algorithms. @d stack←x1==bisect←stack[bisect←ptr] {stacked value of $X←1$} @d stack←x2==bisect←stack[bisect←ptr+1] {stacked value of $X←2$} @d stack←x3==bisect←stack[bisect←ptr+2] {stacked value of $X←3$} @d stack←r==bisect←stack[bisect←ptr+3] {stacked value of $R$} @d stack←m==bisect←stack[bisect←ptr+4] {stacked value of $m$} @d stack←y1==bisect←stack[bisect←ptr+5] {stacked value of $Y←1$} @d stack←y2==bisect←stack[bisect←ptr+6] {stacked value of $Y←2$} @d stack←y3==bisect←stack[bisect←ptr+7] {stacked value of $Y←3$} @d stack←s==bisect←stack[bisect←ptr+8] {stacked value of $S$} @d stack←n==bisect←stack[bisect←ptr+9] {stacked value of $n$} @d stack←l==bisect←stack[bisect←ptr+10] {stacked value of $l$} @d move←increment=11 {number of items pushed by |make←moves|} @<Glob...@>= @!bisect←stack:array[0..bistack←size] of integer; @!bisect←ptr:0..bistack←size; @ @<Check the ``constant'' values...@>= if 15*move←increment>bistack←size then bad:=31; @ The |make←moves| subroutine is given |scaled| values $(x←0,x←1,x←2,x←3)$ and $(y←0,y←1,y←2,y←3)$ that represent monotone-nondecreasing polynomials; it makes $\lfloor x←3+\xi\rfloor-\lfloor x←0+\xi\rfloor$ rightward moves and $\lfloor y←3+\eta\rfloor-\lfloor y←0+\eta\rfloor$ upward moves, as explained earlier. (Here $\lfloor x+\xi\rfloor$ actually stands for $\lfloor x/2↑{16}-|xi←corr|\rfloor$, if $x$ is regarded as an integer without scaling.) The unscaled integers $x←k$ and~$y←k$ should be less than $2↑{28}$ in magnitude. It is assumed that $|move←ptr| + \lfloor y←3+\eta\rfloor - \lfloor y←0+\eta\rfloor < |move←size|$ when this procedure is called, so that the capacity of the |move| array will not be exceeded. The variables |r| and |s| in this procedure stand respectively for $R-|xi←corr|$ and $S-|eta←corr|$ in the theory discussed above. @p procedure make←moves(@!xx0,@!xx1,@!xx2,@!xx3,@!yy0,@!yy1,@!yy2,@!yy3: scaled;@!xi←corr,@!eta←corr:small←number); label continue, done, exit; var @!x1,@!x2,@!x3,@!m,@!r,@!y1,@!y2,@!y3,@!n,@!s,@!l:integer; {bisection variables explained above} @!q,@!t,@!u,@!x2a,@!x3a,@!y2a,@!y3a:integer; {additional temporary registers} begin if (xx3<xx0)or(yy3<yy0) then confusion("m"); @:this can't happen m}{\quad m@> l:=16; bisect←ptr:=0;@/ x1:=xx1-xx0; x2:=xx2-xx1; x3:=xx3-xx2; if xx0>=xi←corr then r:=(xx0-xi←corr) mod unity else r:=unity-1-((-xx0+xi←corr-1) mod unity); m:=(xx3-xx0+r) div unity;@/ y1:=yy1-yy0; y2:=yy2-yy1; y3:=yy3-yy2; if yy0>=eta←corr then s:=(yy0-eta←corr) mod unity else s:=unity-1-((-yy0+eta←corr-1) mod unity); n:=(yy3-yy0+s) div unity;@/ if (xx3-xx0>=fraction←one)or(yy3-yy0>=fraction←one) then @<Divide the variables by two, to avoid overflow problems@>; loop@+ begin continue:@<Make moves for current subinterval; if bisection is necessary, push the second subinterval onto the stack, and |goto continue| in order to handle the first subinterval@>; if bisect←ptr=0 then return; @<Remove a subproblem for |make←moves| from the stack@>; end; exit: end; @ @<Remove a subproblem for |make←moves| from the stack@>= bisect←ptr:=bisect←ptr-move←increment;@/ x1:=stack←x1; x2:=stack←x2; x3:=stack←x3; r:=stack←r; m:=stack←m;@/ y1:=stack←y1; y2:=stack←y2; y3:=stack←y3; s:=stack←s; n:=stack←n;@/ l:=stack←l @ Our variables |(x1,x2,x3)| correspond to $(X←1,X←2,X←3)$ in the notation of the theory developed above. We need to keep them less than $2↑{28}$ in order to avoid integer overflow in weird circumstances. For example, data like $x←0=-2↑{28}+2↑{16}-1$ and $x←1=x←2=x←3=2↑{28}-1$ would otherwise be problematical. Hence this part of the code is needed, if only to thwart malicious users. @<Divide the variables by two, to avoid overflow problems@>= begin x1:=half(x1+xi←corr); x2:=half(x2+xi←corr); x3:=half(x3+xi←corr); r:=half(r+xi←corr);@/ y1:=half(y1+eta←corr); y2:=half(y2+eta←corr); y3:=half(y3+eta←corr); s:=half(s+eta←corr);@/ l:=15; end @ @<Make moves...@>= if m=0 then @<Move upward |n| steps@> else if n=0 then @<Move to the right |m| steps@> else if m+n=2 then @<Make one move of each kind@> else begin incr(l); stack←l:=l;@/ stack←x3:=x3; stack←x2:=half(x2+x3+xi←corr); x2:=half(x1+x2+xi←corr); x3:=half(x2+stack←x2+xi←corr); stack←x1:=x3;@/ r:=r+r+xi←corr; t:=x1+x2+x3+r;@/ q:=t div two←to←the[l]; stack←r:=t mod two←to←the[l];@/ stack←m:=m-q; m:=q;@/ stack←y3:=y3; stack←y2:=half(y2+y3+eta←corr); y2:=half(y1+y2+eta←corr); y3:=half(y2+stack←y2+eta←corr); stack←y1:=y3;@/ s:=s+s+eta←corr; u:=y1+y2+y3+s;@/ q:=u div two←to←the[l]; stack←s:=u mod two←to←the[l];@/ stack←n:=n-q; n:=q;@/ bisect←ptr:=bisect←ptr+move←increment; goto continue; end @ @<Move upward |n| steps@>= while n>0 do begin incr(move←ptr); move[move←ptr]:=1; decr(n); end @ @<Move to the right |m| steps@>= move[move←ptr]:=move[move←ptr]+m @ @<Make one move of each kind@>= begin r:=two←to←the[l]-r; s:=two←to←the[l]-s;@/ while l<30 do begin x3a:=x3; x2a:=half(x2+x3+xi←corr); x2:=half(x1+x2+xi←corr); x3:=half(x2+x2a+xi←corr); t:=x1+x2+x3; r:=r+r-xi←corr;@/ y3a:=y3; y2a:=half(y2+y3+eta←corr); y2:=half(y1+y2+eta←corr); y3:=half(y2+y2a+eta←corr); u:=y1+y2+y3; s:=s+s-eta←corr;@/ if t<r then if u<s then @<Switch to the right subinterval@> else begin @<Move up then right@>; goto done; end else if u<s then begin @<Move right then up@>; goto done; end; incr(l); end; r:=r-xi←corr; s:=s-eta←corr; if ab←vs←cd(x1+x2+x3,s,y1+y2+y3,r)-xi←corr>=0 then @<Move right then up@> else @<Move up then right@>; done: end @ @<Switch to the right subinterval@>= begin x1:=x3; x2:=x2a; x3:=x3a; r:=r-t; y1:=y3; y2:=y2a; y3:=y3a; s:=s-u; end @ @<Move right then up@>= begin incr(move[move←ptr]); incr(move←ptr); move[move←ptr]:=1; end @ @<Move up then right@>= begin incr(move←ptr); move[move←ptr]:=2; end @ After |make←moves| has acted, possibly for several curves that move toward the same octant, a ``smoothing'' operation might be done on the |move| array. This removes optical glitches that can arise even when the curve has been digitized without rounding errors. The smoothing process replaces the integers $a←0\ldots a←n$ in |move[b..t]| by ``smoothed'' integers $a←0'\ldots a←n'$ defined as follows: $$a←k'=a←k+\delta\k-\delta←k;\qquad \delta←k=\cases{+1,&if $1<k<n$ and $a←{k-2}\G a←{k-1}\ll a←k\G a\k$;\cr -1,&if $1<k<n$ and $a←{k-2}\L a←{k-1}\gg a←k\L a\k$;\cr 0,&otherwise.\cr}$$ Here $a\ll b$ means that $a\L b-2$, and $a\gg b$ means that $a\G b+2$. The smoothing operation is symmetric in the sense that, if $a←0\ldots a←n$ smoothes to $a←0'\ldots a←n'$, then the reverse sequence $a←n\ldots a←0$ smoothes to $a←n'\ldots a←0'$; also the complementary sequence $(m-a←0)\ldots(m-a←n)$ smoothes to $(m-a←0')\ldots(m-a←n')$. We have $a←0'+\cdots+a←n'=a←0+\cdots+a←n$ because $\delta←0=\delta←{n+1}=0$. @p procedure smooth←moves(@!b,@!t:integer); var@!k:1..move←size; {index into |move|} @!a,@!aa,@!aaa:integer; {original values of |move[k],move[k-1],move[k-2]|} begin if t-b>=3 then begin k:=b+2; aa:=move[k-1]; aaa:=move[k-2]; repeat a:=move[k]; if abs(a-aa)>1 then @<Increase and decrease |move[k-1]| and |move[k]| by $\delta←k$@>; incr(k); aaa:=aa; aa:=a; until k=t; end; end; @ @<Increase and decrease |move[k-1]| and |move[k]| by $\delta←k$@>= if a>aa then begin if aaa>=aa then if a>=move[k+1] then begin incr(move[k-1]); move[k]:=a-1; end; end else begin if aaa<=aa then if a<=move[k+1] then begin decr(move[k-1]); move[k]:=a+1; end; end @* \[20] Edge structures. Now we come to \MF's internal scheme for representing what the user can actually ``see,'' the edges between pixels. Each pixel has an integer weight, obtained by summing the weights on all edges to its left. \MF\ represents only the nonzero edge weights, since most of the edges are weightless; in this way, the data storage requirements grow only linearly with respect to the number of pixels per point, even though two-dimensional data is being represented. (Well, the actual dependence on the underlying resolution is order $n\log n$, but the the $\log n$ factor is buried in our implicit restriction on the maximum raster size.) The sum of all edge weights in each row should be zero. The data structure for edge weights must be compact and flexible, yet it should support efficient updating and display operations. We want to be able to have many different edge structures in memory at once, and we want the computer to be able to translate them, reflect them, and/or merge them together with relative ease. \MF's solution to this problem requires one single-word node per nonzero edge weight, plus one two-word node for each row in a contiguous set of rows. There's also a header node that provides global information about the entire structure. @ Let's consider the edge-weight nodes first. The |info| field of such nodes contains both an $m$~value and a weight~$w$, in the form $8m+w+c$, where $c$ is a constant that depends on data found in the header. We shall consider $c$ in detail later; for now, it's best just to think of it as a way to compensate for the fact that $m$ and~$w$ can be negative, together with the fact that an |info| field must have a value between |min←halfword| and |max←halfword|. The $m$ value is an unscaled $x$-coordinate, so it satisfies $\vert m\vert< 4096$; the $w$ value is always in the range $1\L\vert w\vert\L3$. We can unpack the data in the |info| field by fetching |ho(info(p))= info(p)-min←halfword| and dividing this nonnegative number by~8; the constant~$c$ will be chosen so that the remainder of this division is $4+w$. Thus, for example, a remainder of~3 will correspond to the edge weight $w=-1$. Every row of an edge structure contains two lists of such edge-weight nodes, called the |sorted| and |unsorted| lists, linked together by their |link| fields in the normal way. The difference between them is that we always have |info(p)<=info(link(p))| in the |sorted| list, but there's no such restriction on the elements of the |unsorted| list. The reason for this distinction is that it would take unnecessarily long to maintain edge-weight lists in sorted order while they're being updated; but when we need to process an entire row from left to right in order of the $m$~values, it's fairly easy and quick to sort a short list of unsorted elements and to merge them into place among their sorted cohorts. Furthermore, the fact that the |unsorted| list is empty can sometimes be used to good advantage, because it allows us to conclude that a particular row has not changed since the last time we sorted it. The final |link| of the |sorted| list will be |sentinel|, which points to a special one-word node whose |info| field is essentially infinite; this facilitates the sorting and merging operations. The final |link| of the |unsorted| list will be either |null| or |void|, where |void=null+1| is used to avoid redisplaying data that has not changed: A |void| value is stored at the head of the unsorted list whenever the corresponding row has been displayed. @d zero←w=4 @d void==null+1 @<Initialize table entries...@>= info(sentinel):=max←halfword; {|link(sentinel)=null|} @ The rows themselves are represented by row-header nodes that contain four link fields. Two of these four, |sorted| and |unsorted|, point to the first items of the edge-weight lists just mentioned. The other two, |link| and |knil|, point to the headers of the two adjacent rows. If |p| points to the header for row number~|n|, then |link(p)| points up to the header for row~|n+1|, and |knil(p)| points down to the header for row~|n-1|. This double linking makes it convenient to move through consecutive rows either upward or downward; as usual, we have |link(knil(p))=knil(link(p))=p| for all row headers~|p|. The row associated with a given value of |n| contains weights for edges that run between the lattice points |(m,n)| and |(m,n+1)|. @d knil==info {inverse of the |link| field, in a doubly linked list} @d sorted←loc(#)==#+1 {where the |sorted| link field resides} @d sorted(#)==link(sorted←loc(#)) {beginning of the list of sorted edge weights} @d unsorted(#)==info(#+1) {beginning of the list of unsorted edge weights} @d row←node←size=2 {number of words in a row header node} @ The main header node |h| for an edge structure has |link| and |knil| fields that link it above the topmost row and below the bottommost row. It also has fields called |m←min|, |m←max|, |n←min|, and |n←max| that bound the current extent of the edge data: All |m| values in edge-weight nodes should lie between |m←min(h)-4096| and |m←max(h)-4096|, inclusive. Furthermore the topmost row header, pointed to by |knil(h)|, is for row number |n←max(h)-4096|; the bottommost row header, pointed to by |link(h)|, is for row number |n←min(h)-4096|. The offset constant |c| that's used in all of the edge-weight data is represented implicitly in |m←offset(h)|; its actual value is $$\hbox{|c=min←halfword+zero←w+8*m←offset(h)|.}$$ Notice that it's possible to shift an entire edge structure by an amount $(\Delta m,\Delta n)$ by adding $\Delta n$ to |n←min(h)| and |n←max(h)|, adding $\Delta m$ to |m←min(h)| and |m←max(h)|, and subtracting $\Delta m$ from |m←offset(h)|; none of the other edge data needs to be modified. Initially the |m←offset| field is~4096, but it will change if the user requests such a shift. The contents of these five fields should always be positive and less than 8192; |n←max| should, in fact, be less than 8191. Furthermore |m←min+m←offset-4096| and |m←max+m←offset-4096| must also lie strictly between 0 and 8192, so that the |info| fields of edge-weight nodes will fit in a halfword. The header node of an edge structure also contains two somewhat unusual fields called |last←window(h)| and |last←window←time(h)|. When this structure is displayed in window~|k| of the user's screen, after that window has been updated |t| times, \MF\ sets |last←window(h):=k| and |last←window←time(h):=t|; it also sets |unsorted(p):=void| for all row headers~|p|, after merging any existing unsorted weights with the sorted ones. A subsequent display in the same window will be able to avoid redisplaying rows whose |unsorted| list is still |void|, if the window hasn't been used for something else in the meantime. A pointer to the row header of row |n←pos(h)-4096| is provided in |n←rover(h)|. Most of the algorithms that update an edge structure are able to get by without random row references; they usually access rows that are neighbors of each other or of the current |n←pos| row. Exception: If |link(h)=h| (so that the edge structure contains no rows), we have |n←rover(h)=h|, and |n←pos(h)| is irrelevant. @d zero←field=4096 {amount added to coordinates to make them positive} @d n←min(#)==info(#+1) {minimum row number present, plus |zero←field|} @d n←max(#)==link(#+1) {maximum row number present, plus |zero←field|} @d m←min(#)==info(#+2) {minimum column number present, plus |zero←field|} @d m←max(#)==link(#+2) {maximum column number present, plus |zero←field|} @d m←offset(#)==info(#+3) {translation of $m$ data in edge-weight nodes} @d last←window(#)==link(#+3) {the last display went into this window} @d last←window←time(#)==mem[#+4].int {after this many window updates} @d n←pos(#)==info(#+5) {the row currently in |n←rover|, plus |zero←field|} @d n←rover(#)==link(#+5) {a row recently referenced} @d edge←header←size=6 {number of words in an edge-structure header} @d valid←range(#)==(abs(#-4096)<4096) {is |#| strictly between 0 and 8192?} @d empty←edges(#)==link(#)=# {are there no rows in this edge header?} @p procedure init←edges(@!h:pointer); {initialize an edge header to null values} begin knil(h):=h; link(h):=h;@/ n←min(h):=zero←field+4095; n←max(h):=zero←field-4095; m←min(h):=zero←field+4095; m←max(h):=zero←field-4095; m←offset(h):=zero←field;@/ last←window(h):=0; last←window←time(h):=0;@/ n←rover(h):=h; n←pos(h):=0;@/ end; @ When a lot of work is being done on a particular edge structure, we plant a pointer to its main header in the global variable |cur←edges|. This saves us from having to pass this pointer as a parameter over and over again between subroutines. Similarly, |cur←wt| is a global weight that is being used by several procedures at once. @<Glob...@>= @!cur←edges:pointer; {the edge structure of current interest} @!cur←wt:integer; {the edge weight of current interest} @ The |fix←offset| routine goes through all the edge-weight nodes of |cur←edges| and adds a constant to their |info| fields, so that |m←offset(cur←edges)| can be brought back to |zero←field|. (This is necessary only in unusual cases when the offset has gotten too large or too small.) @p procedure fix←offset; var @!p,@!q:pointer; {list traversers} @!delta:integer; {the amount of change} begin delta:=8*(m←offset(cur←edges)-zero←field); m←offset(cur←edges):=zero←field; q:=link(cur←edges); while q<>cur←edges do begin p:=sorted(q); while p<>sentinel do begin info(p):=info(p)-delta; p:=link(p); end; p:=unsorted(q); while p>void do begin info(p):=info(p)-delta; p:=link(p); end; q:=link(q); end; end; @ The |edge←prep| routine makes the |cur←edges| structure ready to accept new data whose coordinates satisfy |ml<=m<=mr| and |nl<=n<=nr-1|, assuming that |-4096<ml<=mr<4096| and |-4096<nl<=nr<4096|. It makes appropriate adjustments to |m←min|, |m←max|, |n←min|, and |n←max|, adding new empty rows if necessary. @p procedure edge←prep(@!ml,@!mr,@!nl,@!nr:integer); var @!delta:halfword; {amount of change} @!p,@!q:pointer; {for list manipulation} begin ml:=ml+zero←field; mr:=mr+zero←field; nl:=nl+zero←field; nr:=nr-1+zero←field;@/ if ml<m←min(cur←edges) then m←min(cur←edges):=ml; if mr>m←max(cur←edges) then m←max(cur←edges):=mr; if not valid←range(m←min(cur←edges)+m←offset(cur←edges)-zero←field) or@| not valid←range(m←max(cur←edges)+m←offset(cur←edges)-zero←field) then fix←offset; if empty←edges(cur←edges) then {there are no rows} begin n←min(cur←edges):=nr+1; n←max(cur←edges):=nr; end; if nl<n←min(cur←edges) then @<Insert exactly |n←min(cur←edges)-nl| empty rows at the bottom@>; if nr>n←max(cur←edges) then @<Insert exactly |nr-n←max(cur←edges)| empty rows at the top@>; end; @ @<Insert exactly |n←min(cur←edges)-nl| empty rows at the bottom@>= begin delta:=n←min(cur←edges)-nl; n←min(cur←edges):=nl; p:=link(cur←edges); repeat q:=get←node(row←node←size); sorted(q):=sentinel; unsorted(q):=void; knil(p):=q; link(q):=p; p:=q; decr(delta); until delta=0; knil(p):=cur←edges; link(cur←edges):=p; if n←rover(cur←edges)=cur←edges then n←pos(cur←edges):=nl-1; end @ @<Insert exactly |nr-n←max(cur←edges)| empty rows at the top@>= begin delta:=nr-n←max(cur←edges); n←max(cur←edges):=nr; p:=knil(cur←edges); repeat q:=get←node(row←node←size); sorted(q):=sentinel; unsorted(q):=void; link(p):=q; knil(q):=p; p:=q; decr(delta); until delta=0; link(p):=cur←edges; knil(cur←edges):=p; if n←rover(cur←edges)=cur←edges then n←pos(cur←edges):=nr+1; end @ The |print←edges| subroutine gives a symbolic rendition of an edge structure, for use in `\&{show}' commands. A rather terse output format has been chosen since edge structures can grow quite large. @<Declare subroutines for printing expressions@>= @t\4@>@<Declare the procedure called |print←weight|@>@;@/ procedure print←edges(@!s:str←number;@!nuline:boolean;@!x←off,@!y←off:integer); var @!p,@!q,@!r:pointer; {for list traversal} @!n:integer; {row number} begin print←diagnostic("Edge structure",s,nuline); p:=knil(cur←edges); n:=n←max(cur←edges)-zero←field; while p<>cur←edges do begin q:=unsorted(p); r:=sorted(p); if(q>void)or(r<>sentinel) then begin print←nl("row "); print←int(n+y←off); print←char(":"); while q>void do begin print←weight(q,x←off); q:=link(q); end; print(" |"); while r<>sentinel do begin print←weight(r,x←off); r:=link(r); end; end; p:=knil(p); decr(n); end; end←diagnostic(true); end; @ @<Declare the procedure called |print←weight|@>= procedure print←weight(@!q:pointer;@!x←off:integer); var @!w,@!m:integer; {unpacked weight and coordinate} @!d:integer; {temporary data register} begin d:=ho(info(q)); w:=d mod 8; m:=(d div 8)-m←offset(cur←edges); if file←offset>max←print←line-9 then print←nl(" ") else print←char(" "); print←int(m+x←off); while w>zero←w do begin print←char("+"); decr(w); end; while w<zero←w do begin print←char("-"); incr(w); end; end; @ Here's a trivial subroutine that copies an edge structure. (Let's hope that the given structure isn't too gigantic.) @p function copy←edges(@!h:pointer):pointer; var @!p,@!r:pointer; {variables that traverse the given structure} @!hh,@!pp,@!qq,@!rr,@!ss:pointer; {variables that traverse the new structure} begin hh:=get←node(edge←header←size); mem[hh+1]:=mem[h+1]; mem[hh+2]:=mem[h+2]; mem[hh+3]:=mem[h+3]; mem[hh+4]:=mem[h+4]; {we've now copied |n←min|, |n←max|, |m←min|, |m←max|, |m←offset|, |last←window|, and |last←window←time|} n←pos(hh):=n←max(hh)+1;n←rover(hh):=hh;@/ p:=link(h); qq:=hh; while p<>h do begin pp:=get←node(row←node←size); link(qq):=pp; knil(pp):=qq; @<Copy both |sorted| and |unsorted| lists of |p| to |pp|@>; p:=link(p); qq:=pp; end; link(qq):=hh; knil(hh):=qq; copy←edges:=hh; end; @ @<Copy both |sorted| and |unsorted|...@>= r:=sorted(p); rr:=sorted←loc(pp); {|link(rr)=sorted(pp)|} while r<>sentinel do begin ss:=get←avail; link(rr):=ss; rr:=ss; info(rr):=info(r);@/ r:=link(r); end; link(rr):=sentinel;@/ r:=unsorted(p); rr:=temp←head; while r>void do begin ss:=get←avail; link(rr):=ss; rr:=ss; info(rr):=info(r);@/ r:=link(r); end; link(rr):=r; unsorted(pp):=link(temp←head) @ Another trivial routine flips |cur←edges| about the |x|-axis (i.e., negates all the |y| coordinates), assuming that at least one row is present. @p procedure y←reflect←edges; var @!p,@!q,@!r:pointer; {list manipulation registers} begin p:=n←min(cur←edges); n←min(cur←edges):=zero←field+zero←field-1-n←max(cur←edges); n←max(cur←edges):=zero←field+zero←field-1-p; n←pos(cur←edges):=zero←field+zero←field-1-n←pos(cur←edges);@/ p:=link(cur←edges); q:=cur←edges; {we assume that |p<>q|} repeat r:=link(p); link(p):=q; knil(q):=p; q:=p; p:=r; until q=cur←edges; last←window←time(cur←edges):=0; end; @ It's somewhat more difficult, yet not too hard, to reflect about the |y|-axis. @p procedure x←reflect←edges; var @!p,@!q,@!r,@!s:pointer; {list manipulation registers} @!m:integer; {|info| fields will be reflected with respect to this number} begin p:=m←min(cur←edges); m←min(cur←edges):=zero←field+zero←field-m←max(cur←edges); m←max(cur←edges):=zero←field+zero←field-p; m:=(zero←field+m←offset(cur←edges))*8+zero←w+min←halfword+zero←w+min←halfword; m←offset(cur←edges):=zero←field; p:=link(cur←edges); repeat @<Reflect the edge-and-weight data in |sorted(p)|@>; @<Reflect the edge-and-weight data in |unsorted(p)|@>; p:=link(p); until p=cur←edges; last←window←time(cur←edges):=0; end; @ We want to change the sign of the weight as we change the sign of the |x|-coordinate. Fortunately, it's easier to do this than to negate one without the other. @<Reflect the edge-and-weight data in |unsorted(p)|@>= q:=unsorted(p); while q>void do begin info(q):=m-info(q); q:=link(q); end @ Reversing the order of a linked list is best thought of as the process of popping nodes off one stack and pushing them on another. In this case we pop from stack~|q| and push to stack~|r|. @<Reflect the edge-and-weight data in |sorted(p)|@>= q:=sorted(p); r:=sentinel; while q<>sentinel do begin s:=link(q); link(q):=r; r:=q; info(r):=m-info(q); q:=s; end; sorted(p):=r @ Now let's multiply all the $y$-coordinates of a nonempty edge structure by a small integer $s>1$: @p procedure y←scale←edges(@!s:integer); var @!p,@!q,@!pp,@!r,@!rr,@!ss:pointer; {list manipulation registers} @!t:integer; {replication counter} begin if (s*(n←max(cur←edges)+1-zero←field)>=4096) or@| (s*(n←min(cur←edges)-zero←field)<=-4096) then begin print←err("Scaled picture would be too big"); @.Scaled picture...big@> help3("I can't yscale the picture as requested---it would")@/ ("make some coordinates too large or too small.")@/ ("Proceed, and I'll omit the transformation."); put←get←error; end else begin n←max(cur←edges):=s*(n←max(cur←edges)+1-zero←field)-1+zero←field; n←min(cur←edges):=s*(n←min(cur←edges)-zero←field)+zero←field; @<Replicate every row exactly $s$ times@>; last←window←time(cur←edges):=0; end; end; @ @<Replicate...@>= p:=cur←edges; repeat q:=p; p:=link(p); for t:=2 to s do begin pp:=get←node(row←node←size); link(q):=pp; knil(p):=pp; link(pp):=p; knil(pp):=q; q:=pp; @<Copy both |sorted| and |unsorted|...@>; end; until link(p)=cur←edges @ Scaling the $x$-coordinates is, of course, our next task. @p procedure x←scale←edges(@!s:integer); var @!p,@!q:pointer; {list manipulation registers} @!t:0..65535; {unpacked |info| field} @!w:0..7; {unpacked weight} @!delta:integer; {amount added to scaled |info|} begin if (s*(m←max(cur←edges)-zero←field)>=4096) or@| (s*(m←min(cur←edges)-zero←field)<=-4096) then begin print←err("Scaled picture would be too big"); @.Scaled picture...big@> help3("I can't xscale the picture as requested---it would")@/ ("make some coordinates too large or too small.")@/ ("Proceed, and I'll omit the transformation."); put←get←error; end else if (m←max(cur←edges)<>zero←field)or(m←min(cur←edges)<>zero←field) then begin m←max(cur←edges):=s*(m←max(cur←edges)-zero←field)+zero←field; m←min(cur←edges):=s*(m←min(cur←edges)-zero←field)+zero←field; delta:=8*(zero←field-s*m←offset(cur←edges))+min←halfword; m←offset(cur←edges):=zero←field; @<Scale the $x$-coordinates of each row by $s$@>; last←window←time(cur←edges):=0; end; end; @ The multiplications cannot overflow because we know that |s<4096|. @<Scale the $x$-coordinates of each row by $s$@>= q:=link(cur←edges); repeat p:=sorted(q); while p<>sentinel do begin t:=ho(info(p)); w:=t mod 8; info(p):=(t-w)*s+w+delta; p:=link(p); end; p:=unsorted(q); while p>void do begin t:=ho(info(p)); w:=t mod 8; info(p):=(t-w)*s+w+delta; p:=link(p); end; q:=link(q); until q=cur←edges @ Here is a routine that changes the signs of all the weights, without changing anything else. @p procedure negate←edges(@!h:pointer); label done; var @!p,@!q,@!r,@!s,@!t,@!u:pointer; {structure traversers} begin p:=link(h); while p<>h do begin q:=unsorted(p); while q>void do begin info(q):=8-2*((ho(info(q))) mod 8)+info(q); q:=link(q); end; q:=sorted(p); if q<>sentinel then begin repeat info(q):=8-2*((ho(info(q))) mod 8)+info(q); q:=link(q); until q=sentinel; @<Put the list |sorted(p)| back into sort@>; end; p:=link(p); end; last←window←time(h):=0; end; @ \MF\ would work even if the code in this section were omitted, because a list of edge-and-weight data that is sorted only by |m| but not~|w| turns out to be good enough for correct operation. However, the author decided not to make the program even trickier than it is already, since |negate←edges| isn't needed very often. The simpler-to-state condition, ``keep the |sorted| list fully sorted,'' is therefore being preserved at the cost of extra computation. @<Put the list |sorted(p)|...@>= u:=sorted←loc(p); q:=link(u); r:=q; s:=link(r); {|q=sorted(p)|} loop@+ if info(s)>info(r) then begin link(u):=q; if s=sentinel then goto done; u:=r; q:=s; r:=q; s:=link(r); end else begin t:=s; s:=link(t); link(t):=q; q:=t; end; done: link(r):=sentinel @ The |unsorted| edges of a row are merged into the |sorted| ones by a subroutine called |sort←edges|. It uses simple insertion sort, followed by a merge, because the unsorted list is supposedly quite short. However, the unsorted list is assumed to be nonempty. @p procedure sort←edges(@!h:pointer); {|h| is a row header} label done; var @!k:halfword; {key register that we compare to |info(q)|} @!p,@!q,@!r,@!s:pointer; begin r:=unsorted(h); unsorted(h):=null; p:=link(r); link(r):=sentinel; link(temp←head):=r; while p>void do {sort node |p| into the list that starts at |temp←head|} begin k:=info(p); q:=temp←head; repeat r:=q; q:=link(r); until k<=info(q); link(r):=p; r:=link(p); link(p):=q; p:=r; end; @<Merge the |temp←head| list into |sorted(h)|@>; end; @ In this step we use the fact that |@!sorted(h)=link(sorted←loc(h))|. @<Merge the |temp←head| list into |sorted(h)|@>= begin r:=sorted←loc(h); q:=link(r); p:=link(temp←head); loop@+ begin k:=info(p); while k>info(q) do begin r:=q; q:=link(r); end; link(r):=p; s:=link(p); link(p):=q; if s=sentinel then goto done; r:=p; p:=s; end; done:end @ The |cull←edges| procedure ``optimizes'' an edge structure by making all the pixel weights either |w←out| or~|w←in|. The weight will be~|w←in| after the operation if and only if it was in the closed interval |[w←lo,w←hi]| before, where |w←lo<=w←hi|. Either |w0| or |w1| is zero, while the other is $\pm1$, $\pm2$, or $\pm3$. The parameters will be such that zero-weight pixels will remain of weight zero. (This is fortunate, because there are infinitely many of them.) The procedure also computes the tightest possible bounds on the resulting data, by updating |m←min|, |m←max|, |n←min|, and~|n←max|. @p procedure cull←edges(@!w←lo,@!w←hi,@!w←out,@!w←in:integer); label done; var @!p,@!q,@!r,@!s:pointer; {for list manipulation} @!w:integer; {new weight after culling} @!d:integer; {data register for unpacking} @!m:integer; {the previous column number, including |m←offset|} @!mm:integer; {the next column number, including |m←offset|} @!ww:integer; {accumulated weight before culling} @!prev←w:integer; {value of |w| before column |m|} @!n,@!min←n,@!max←n:pointer; {current and extreme row numbers} @!min←d,@!max←d:pointer; {extremes of the new edge-and-weight data} begin min←d:=max←halfword; max←d:=min←halfword; min←n:=max←halfword; max←n:=min←halfword;@/ p:=link(cur←edges); n:=n←min(cur←edges); while p<>cur←edges do begin if unsorted(p)>void then sort←edges(p); if sorted(p)<>sentinel then @<Cull superfluous edge-weight entries from |sorted(p)|@>; p:=link(p); incr(n); end; @<Delete empty rows at the top and/or bottom; update the boundary values in the header@>; last←window←time(cur←edges):=0; end; @ The entire |sorted| list is returned to available memory in this step; a new list is built starting (temporarily) at |temp←head|. Since several edges can occur at the same column, we need to be looking ahead of where the actual culling takes place. This means that it's slightly tricky to get the iteration started and stopped. @<Cull superfluous...@>= begin r:=temp←head; q:=sorted(p); ww:=0; m:=1000000; prev←w:=0; loop@+ begin if q=sentinel then mm:=1000000 else begin d:=ho(info(q)); mm:=d div 8; ww:=ww+(d mod 8)-zero←w; end; if mm>m then begin @<Insert an edge-weight for edge |m|, if the new pixel weight has changed@>; if q=sentinel then goto done; end; m:=mm; if ww>=w←lo then if ww<=w←hi then w:=w←in else w:=w←out else w:=w←out; s:=link(q); free←avail(q); q:=s; end; done: link(r):=sentinel; sorted(p):=link(temp←head); if r<>temp←head then @<Update the max/min amounts@>; end @ @<Insert an edge-weight for edge |m|, if...@>= if w<>prev←w then begin s:=get←avail; link(r):=s; info(s):=8*m+min←halfword+zero←w+w-prev←w; r:=s; prev←w:=w; end @ @<Update the max/min amounts@>= begin if min←n=max←halfword then min←n:=n; max←n:=n; if min←d>info(link(temp←head)) then min←d:=info(link(temp←head)); if max←d<info(r) then max←d:=info(r); end @ @<Delete empty rows at the top and/or bottom...@>= if min←n>max←n then @<Delete all the row headers@> else begin n:=n←min(cur←edges); n←min(cur←edges):=min←n; while min←n>n do begin p:=link(cur←edges); link(cur←edges):=link(p); knil(link(p)):=cur←edges; free←node(p,row←node←size); incr(n); end; n:=n←max(cur←edges); n←max(cur←edges):=max←n; n←pos(cur←edges):=max←n+1; n←rover(cur←edges):=cur←edges; while max←n<n do begin p:=knil(cur←edges); knil(cur←edges):=knil(p); link(knil(p)):=cur←edges; free←node(p,row←node←size); decr(n); end; m←min(cur←edges):=((ho(min←d)) div 8)-m←offset(cur←edges)+zero←field; m←max(cur←edges):=((ho(max←d)) div 8)-m←offset(cur←edges)+zero←field; end @ We get here if the edges have been entirely culled away. @<Delete all the row headers@>= begin p:=link(cur←edges); while p<>cur←edges do begin q:=link(p); free←node(p,row←node←size); p:=q; end; init←edges(cur←edges); end @ The last and most difficult routine for transforming an edge structure---and the most interesting one!---is |xy←swap←edges|, which interchanges the r\↑↑Doles of rows and columns. Its task can be viewed as the job of creating an edge structure that contains only horizontal edges, linked together in columns, given an edge structure that contains only vertical edges linked together in rows; we must do this without changing the implied pixel weights. Given any two adjacent rows of an edge structure, it is not difficult to determine the horizontal edges that lie ``between'' them: We simply look for vertically adjacent pixels that have different weight, and insert a horizontal edge containing the difference in weights. Every horizontal edge determined in this way should be put into an appropriate linked list. Since random access to these linked lists is desirable, we use the |move| array to hold the list heads. If we work through the given edge structure from top to bottom, the constructed lists will not need to be sorted, since they will already be in order. The following algorithm makes use of some ideas suggested by John Hobby. @↑Hobby, John Douglas@> It assumes that the edge structure is non-null, i.e., that |link(cur←edges) <>cur←edges|, hence |m←max(cur←edges)>=m←min(cur←edges)|. @p procedure xy←swap←edges; {interchange |x| and |y| in |cur←edges|} label done; var @!m←magic,@!n←magic:integer; {special values that account for offsets} @!p,@!q,@!r,@!s:pointer; {pointers that traverse the given structure} @<Other local variables for |xy←swap←edges|@>@; begin @<Initialize the array of new edge list heads@>; @<Insert blank rows at the top and bottom, and set |p| to the new top row@>; @<Compute the magic offset values@>; repeat q:=knil(p);@+if unsorted(q)>void then sort←edges(q); @<Insert the horizontal edges defined by adjacent rows |p,q|, and destroy row~|p|@>; p:=q; n←magic:=n←magic-8; until knil(p)=cur←edges; free←node(p,row←node←size); {now all original rows have been recycled} @<Adjust the header to reflect the new edges@>; end; @ Here we don't bother to keep the |link| entries up to date, since the procedure looks only at the |knil| fields as it destroys the former edge structure. @<Insert blank rows at the top and bottom...@>= p:=get←node(row←node←size); sorted(p):=sentinel; unsorted(p):=null;@/ knil(p):=cur←edges; knil(link(cur←edges)):=p; {the new bottom row} p:=get←node(row←node←size); sorted(p):=sentinel; knil(p):=knil(cur←edges); {the new top row} @ The new lists will become |sorted| lists later, so we initialize empty lists to |sentinel|. @<Initialize the array of new edge list heads@>= m←spread:=m←max(cur←edges)-m←min(cur←edges); {this is |>=0| by assumption} if m←spread>move←size then overflow("move table size",move←size); @:METAFONT capacity exceeded move table size}{\quad move table size@> for j:=0 to m←spread do move[j]:=sentinel @ @<Other local variables for |xy←swap←edges|@>= @!m←spread:integer; {the difference between |m←max| and |m←min|} @!j,@!jj:0..move←size; {indices into |move|} @!m,@!mm:integer; {|m| values at vertical edges} @!pd,@!rd:integer; {data fields from edge-and-weight nodes} @!pm,@!rm:integer; {|m| values from edge-and-weight nodes} @!w:integer; {the difference in accumulated weight} @!ww:integer; {as much of |w| that can be stored in a single node} @!dw:integer; {an increment to be added to |w|} @ At the point where we test |w<>0|, variable |w| contains the accumulated weight from edges already passed in row~|p| minus the accumulated weight from edges already passed in row~|q|. @<Insert the horizontal edges defined by adjacent rows |p,q|...@>= r:=sorted(p); free←node(p,row←node←size); p:=r;@/ pd:=ho(info(p)); pm:=pd div 8;@/ r:=sorted(q); rd:=ho(info(r)); rm:=rd div 8; w:=0; loop@+ begin if pm<rm then mm:=pm@+else mm:=rm; if w<>0 then @<Insert horizontal edges of weight |w| between |m| and~|mm|@>; if pd<rd then begin dw:=(pd mod 8)-zero←w; @<Advance pointer |p| to the next vertical edge, after destroying the previous one@>; end else begin if r=sentinel then goto done; {|rd=pd=ho(max←halfword)|} dw:=-((rd mod 8)-zero←w); @<Advance pointer |r| to the next vertical edge@>; end; m:=mm; w:=w+dw; end; done: @ @<Advance pointer |r| to the next vertical edge@>= r:=link(r); rd:=ho(info(r)); rm:=rd div 8 @ @<Advance pointer |p| to the next vertical edge...@>= s:=link(p); free←avail(p); p:=s; pd:=ho(info(p)); pm:=pd div 8 @ Certain ``magic'' values are needed to make the following code work, due to the various offsets in our data structure. For now, let's not worry about their precise values; we shall compute |m←magic| and |n←magic| later, after we see what the code looks like. @ @<Insert horizontal edges of weight |w| between |m| and~|mm|@>= if m<>mm then begin if mm-m←magic>=move←size then confusion("xy"); @:this can't happen xy}{\quad xy@> extras:=(abs(w)-1) div 3; if extras>0 then begin if w>0 then xw:=+3@+else xw:=-3; ww:=w-extras*xw; end else ww:=w; repeat j:=m-m←magic; for k:=1 to extras do begin s:=get←avail; info(s):=n←magic+xw; link(s):=move[j]; move[j]:=s; end; s:=get←avail; info(s):=n←magic+ww; link(s):=move[j]; move[j]:=s;@/ incr(m); until m=mm; end @ @<Other local variables for |xy...@>= @!extras:integer; {the number of additional nodes to make weights |>3|} @!xw:-3..3; {the additional weight in extra nodes} @!k:integer; {loop counter for inserting extra nodes} @ At the beginning of this step, |move[m←spread]=sentinel|, because no horizontal edges will extend to the right of column |m←max(cur←edges)|. @<Adjust the header to reflect the new edges@>= move[m←spread]:=0; j:=0; while move[j]=sentinel do incr(j); if j=m←spread then init←edges(cur←edges) {all edge weights are zero} else begin mm:=m←min(cur←edges); m←min(cur←edges):=n←min(cur←edges); m←max(cur←edges):=n←max(cur←edges)+1; m←offset(cur←edges):=zero←field; jj:=m←spread-1; while move[jj]=sentinel do decr(jj); n←min(cur←edges):=j+mm; n←max(cur←edges):=jj+mm; q:=cur←edges; repeat p:=get←node(row←node←size); link(q):=p; knil(p):=q; sorted(p):=move[j]; unsorted(p):=null; incr(j); q:=p; until j>jj; link(q):=cur←edges; knil(cur←edges):=q; n←pos(cur←edges):=n←max(cur←edges)+1; n←rover(cur←edges):=cur←edges; last←window←time(cur←edges):=0; end; @ The values of |m←magic| and |n←magic| can be worked out by trying the code above on a small example; if they work correctly in simple cases, they should work in general. @<Compute the magic offset values@>= m←magic:=m←min(cur←edges)+m←offset(cur←edges)-zero←field; n←magic:=8*n←max(cur←edges)+8+zero←w+min←halfword @ Now let's look at the subroutine that merges the edges from a given edge structure into |cur←edges|. The given edge structure loses all its edges. @p procedure merge←edges(@!h:pointer); label done; var @!p,@!q,@!r,@!pp,@!qq,@!rr:pointer; {list manipulation registers} @!n:integer; {row number} @!k:halfword; {key register that we compare to |info(q)|} @!delta:integer; {change to the edge/weight data} begin if link(h)<>h then begin if (m←min(h)<m←min(cur←edges))or(m←max(h)>m←max(cur←edges))or@| (n←min(h)<n←min(cur←edges))or(n←max(h)>n←max(cur←edges)) then edge←prep(m←min(h)-zero←field,m←max(h)-zero←field, n←min(h)-zero←field,n←max(h)-zero←field+1); if m←offset(h)<>m←offset(cur←edges) then @<Adjust the data of |h| to account for a difference of offsets@>; n:=n←min(cur←edges); p:=link(cur←edges); pp:=link(h); while n<n←min(h) do begin incr(n); p:=link(p); end; repeat @<Merge row |pp| into row |p|@>; pp:=link(pp); p:=link(p); until pp=h; end; end; @ @<Adjust the data of |h| to account for a difference of offsets@>= begin pp:=link(h); delta:=8*(m←offset(cur←edges)-m←offset(h)); repeat qq:=sorted(pp); while qq<>sentinel do begin info(qq):=info(qq)+delta; qq:=link(qq); end; qq:=unsorted(pp); while qq>void do begin info(qq):=info(qq)+delta; qq:=link(qq); end; pp:=link(pp); until pp=h; end @ The |sorted| and |unsorted| lists are merged separately. After this step, row~|pp| will have no edges remaining, since they will all have been merged into row~|p|. @<Merge row |pp|...@>= qq:=unsorted(pp); if qq>void then if unsorted(p)<=void then unsorted(p):=qq else begin while link(qq)>void do qq:=link(qq); link(qq):=unsorted(p); unsorted(p):=unsorted(pp); end; unsorted(pp):=null; qq:=sorted(pp); if qq<>sentinel then begin if unsorted(p)=void then unsorted(p):=null; sorted(pp):=sentinel; r:=sorted←loc(p); q:=link(r); {|q=sorted(p)|} if q=sentinel then sorted(p):=qq else loop@+begin k:=info(qq); while k>info(q) do begin r:=q; q:=link(r); end; link(r):=qq; rr:=link(qq); link(qq):=q; if rr=sentinel then goto done; r:=qq; qq:=rr; end; end; done: @ The |total←weight| routine computes the total of all pixel weights in a given edge structure. It's not difficult to prove that this is the sum of $(-w)$ times $x$ taken over all edges, where $w$ and~$x$ are the weight and $x$~coordinates stored in an edge. It's not necessarily to worry that this quantity will overflow the size of an |integer| register, because it will be less than~$2↑{31}$ unless the edge structure has more than 174,762 edges. However, we had better not try to compute it as a |scaled| integer, because a total weight of almost $12\times 2↑{12}$ can be produced by only four edges. @p function total←weight(@!h:pointer):integer; {|h| is an edge header} var @!p,@!q:pointer; {variables that traverse the given structure} @!n:integer; {accumulated total so far} @!m:0..65535; {packed $x$ and $w$ values, including offsets} begin n:=0; p:=link(h); while p<>h do begin q:=sorted(p); while q<>sentinel do @<Add the contribution of node |q| to the total weight, and set |q:=link(q)|@>; q:=unsorted(p); while q>void do @<Add the contribution of node |q| to the total weight, and set |q:=link(q)|@>; p:=link(p); end; total←weight:=n; end; @ It's not necessary to add the offsets to the $x$ coordinates, because an entire edge structure can be shifted without affecting its total weight. Similarly, we don't need to subtract |zero←field|. @<Add the contribution of node |q| to the total weight...@>= begin m:=ho(info(q)); n:=n-((m mod 8)-zero←w)*(m div 8); q:=link(q); end @ So far we've done lots of things to edge structures assuming that edges are actually present, but we haven't seen how edges get created in the first place. Let's turn now to the problem of generating new edges. \MF\ will display new edges as they are being computed, if |tracing←edges| is positive. In order to keep such data reasonably compact, only the points at which the path makes a $90↑\circ$ or $180↑\circ$ turn are listed. The tracing algorithm must remember some past history in order to suppress unnecessary data. Three variables |trace←x|, |trace←y|, and |trace←yy| provide this history: The last coordinates printed were |(trace←x,trace←y)|, and the previous edge traced ended at |(trace←x,trace←yy)|. Before anything at all has been traced, |trace←x=-4096|. @<Glob...@>= @!trace←x:integer; {$x$-coordinate most recently shown in a trace} @!trace←y:integer; {$y$-coordinate most recently shown in a trace} @!trace←yy:integer; {$y$-coordinate most recently encountered} @ Edge tracing is initiated by the |begin←edge←tracing| routine, continued by the |trace←a←corner| routine, and terminated by the |end←edge←tracing| routine. @p procedure begin←edge←tracing; begin print←diagnostic("Tracing edges","",true); print(" (weight "); print←int(cur←wt); print←char(")"); trace←x:=-4096; end; @# procedure trace←a←corner; begin if file←offset>max←print←line-13 then print←nl(""); print←char("("); print←int(trace←x); print←char(","); print←int(trace←yy); print←char(")"); trace←y:=trace←yy; end; @# procedure end←edge←tracing; begin if trace←x=-4096 then print←nl("(No new edges added.)") @.No new edges added@> else begin trace←a←corner; print←char("."); end; end←diagnostic(true); end; @ Just after a new edge weight has been put into the |info| field of node~|r|, in row~|n|, the following routine continues an ongoing trace. @p procedure trace←new←edge(@!r:pointer;@!n:integer); var @!d:integer; {temporary data register} @!w:-3..3; {weight associated with an edge transition} @!m,@!n0,@!n1:integer; {column and row numbers} begin d:=ho(info(r)); w:=(d mod 8)-zero←w; m:=(d div 8)-m←offset(cur←edges); if w=cur←wt then begin n0:=n+1; n1:=n; end else begin n0:=n; n1:=n+1; end; {the edges run from |(m,n0)| to |(m,n1)|} if m<>trace←x then begin if trace←x=-4096 then begin print←nl(""); trace←yy:=n0; end else if trace←yy<>n0 then print←char("?") {shouldn't happen} else trace←a←corner; trace←x:=m; trace←a←corner; end else begin if n0<>trace←yy then print←char("!"); {shouldn't happen} if ((n0<n1)and(trace←y>trace←yy))or((n0>n1)and(trace←y<trace←yy)) then trace←a←corner; end; trace←yy:=n1; end; @ One way to put new edge weights into an edge structure is to use the following routine, which simply draws a straight line from |(x0,y0)| to |(x1,y1)|. More precisely, it introduces weights for the edges of the discrete path $\bigl(\lfloor t[x←0,x←1]+{1\over2}+\epsilon\rfloor, \lfloor t[y←0,y←1]+{1\over2}+\epsilon\delta\rfloor\bigr)$, as $t$ varies from 0 to~1, where $\epsilon$ and $\delta$ are extremely small positive numbers. The structure header is assumed to be |cur←edges|; downward edge weights will be |cur←wt|, while upward ones will be |-cur←wt|. Of course, this subroutine will be called only in connection with others that eventually draw a complete cycle, so that the sum of the edge weights in each row will be zero whenever the row is displayed. @p procedure line←edges(@!x0,@!y0,@!x1,@!y1:scaled); label done,done1; var @!m0,@!n0,@!m1,@!n1:integer; {rounded and unscaled coordinates} @!delx,@!dely:scaled; {the coordinate differences of the line} @!yt:scaled; {smallest |y| coordinate that rounds the same as |y0|} @!tx:scaled; {tentative change in |x|} @!p,@!r:pointer; {list manipulation registers} @!base:integer; {amount added to edge-and-weight data} @!n:integer; {current row number} begin n0:=round←unscaled(y0); n1:=round←unscaled(y1); if n0<>n1 then begin m0:=round←unscaled(x0); m1:=round←unscaled(x1); delx:=x1-x0; dely:=y1-y0; yt:=n0*unity-half←unit; y0:=y0-yt; y1:=y1-yt; if n0<n1 then @<Insert upward edges for a line@> else @<Insert downward edges for a line@>; n←rover(cur←edges):=p; n←pos(cur←edges):=n+zero←field; end; end; @ Here we are careful to cancel any effect of rounding error. @<Insert upward edges for a line@>= begin base:=8*m←offset(cur←edges)+min←halfword+zero←w-cur←wt; if m0<=m1 then edge←prep(m0,m1,n0,n1)@+else edge←prep(m1,m0,n0,n1); @<Move to row |n0|, pointed to by |p|@>; y0:=unity-y0; loop@+ begin r:=get←avail; link(r):=unsorted(p); unsorted(p):=r;@/ tx:=take←fraction(delx,make←fraction(y0,dely)); if ab←vs←cd(delx,y0,dely,tx)<0 then decr(tx); {now $|tx|=\lfloor|y0|\cdot|delx|/|dely|\rfloor$} info(r):=8*round←unscaled(x0+tx)+base;@/ y1:=y1-unity; if internal[tracing←edges]>0 then trace←new←edge(r,n); if y1<unity then goto done; p:=link(p); y0:=y0+unity; incr(n); end; done: end @ @<Insert downward edges for a line@>= begin base:=8*m←offset(cur←edges)+min←halfword+zero←w+cur←wt; if m0<=m1 then edge←prep(m0,m1,n1,n0)@+else edge←prep(m1,m0,n1,n0); decr(n0); @<Move to row |n0|, pointed to by |p|@>; loop@+ begin r:=get←avail; link(r):=unsorted(p); unsorted(p):=r;@/ tx:=take←fraction(delx,make←fraction(y0,dely)); if ab←vs←cd(delx,y0,dely,tx)<0 then incr(tx); {now $|tx|=\lceil|y0|\cdot|delx|/|dely|\rceil$, since |dely<0|} info(r):=8*round←unscaled(x0-tx)+base;@/ y1:=y1+unity; if internal[tracing←edges]>0 then trace←new←edge(r,n); if y1>=0 then goto done1; p:=knil(p); y0:=y0+unity; decr(n); end; done1: end @ @<Move to row |n0|, pointed to by |p|@>= n:=n←pos(cur←edges)-zero←field; p:=n←rover(cur←edges); if n<>n0 then if n<n0 then repeat incr(n); p:=link(p); until n=n0 else repeat decr(n); p:=knil(p); until n=n0 @ \MF\ inserts most of its edges into edge structures via the |move←to←edges| subroutine, which uses the data stored in the |move| array to specify a sequence of ``rook moves.'' The starting point |(m0,n0)| and finishing point |(m1,n1)| of these moves, as seen from the standpoint of the first octant, are supplied as parameters; the moves should, however, be rotated into a given octant. (We're going to study octant transformations in great detail later; the reader may wish to come back to this part of the program after mastering the mysteries of octants.) The rook moves themselves are defined as follows, from a |first←octant| point of view: ``Go right |move[k]| steps, then go up one, for |0<=k<n1-n0|; then go right |move[n1-n0]| steps and stop.'' The sum of |move[k]| for |0<=k<=n1-n0| will be equal to |m1-m0|. As in the |line←edges| routine, we use |+cur←wt| as the weight of all downward edges and |-cur←wt| as the weight of all upward edges, after the moves have been rotated to the proper octant direction. There are two main cases to consider: \\{fast\←case} is for moves that travel in the direction of octants 1, 4, 5, and~8, while \\{slow\←case} is for moves that travel toward octants 2, 3, 6, and~7. The latter directions are comparatively cumbersome because they generate more upward or downward edges; a curve that travels horizontally doesn't produce any edges at all, but a curve that travels vertically touches lots of rows. @d fast←case←up=60 {for octants 1 and 4} @d fast←case←down=61 {for octants 5 and 8} @d slow←case←up=62 {for octants 2 and 3} @d slow←case←down=63 {for octants 6 and 7} @p procedure move←to←edges(@!m0,@!n0,@!m1,@!n1:integer); label fast←case←up,fast←case←down,slow←case←up,slow←case←down,done; var @!delta:0..move←size; {extent of |move| data} @!k:0..move←size; {index into |move|} @!p,@!r:pointer; {list manipulation registers} @!dx:integer; {change in edge-weight |info| when |x| changes by 1} @!edge←and←weight:integer; {|info| to insert} @!j:integer; {number of consecutive vertical moves} @!n:integer; {the current row pointed to by |p|} debug @!sum:integer;@+gubed@;@/ begin delta:=n1-n0; debug sum:=move[0]; for k:=1 to delta do sum:=sum+abs(move[k]); if sum<>m1-m0 then confusion("0");@+gubed@;@/ @:this can't happen 0}{\quad 0@> @<Prepare for and switch to the appropriate case, based on |octant|@>; fast←case←up:@<Add edges for first or fourth octants, then |goto done|@>; fast←case←down:@<Add edges for fifth or eighth octants, then |goto done|@>; slow←case←up:@<Add edges for second or third octants, then |goto done|@>; slow←case←down:@<Add edges for sixth or seventh octants, then |goto done|@>; done: n←pos(cur←edges):=n+zero←field; n←rover(cur←edges):=p; end; @ The current octant code appears in a global variable. If, for example, |octant=third←octant|, it means that a curve traveling in a north to north-westerly direction has been rotated for the purposes of internal calculations so that the |move| data travels in an east to north-easterly direction. We want to update the edge structure as if the rotation had not taken place. @<Glob...@>= @!octant:first←octant..sixth←octant; {the current octant of interest} @ @<Prepare for and switch to the appropriate case, based on |octant|@>= case octant of first←octant:begin dx:=8; edge←prep(m0,m1,n0,n1); goto fast←case←up; end; second←octant:begin dx:=8; edge←prep(n0,n1,m0,m1); goto slow←case←up; end; third←octant:begin dx:=-8; edge←prep(-n1,-n0,m0,m1); negate(n0); goto slow←case←up; end; fourth←octant:begin dx:=-8; edge←prep(-m1,-m0,n0,n1); negate(m0); goto fast←case←up; end; fifth←octant:begin dx:=-8; edge←prep(-m1,-m0,-n1,-n0); negate(m0); goto fast←case←down; end; sixth←octant:begin dx:=-8; edge←prep(-n1,-n0,-m1,-m0); negate(n0); goto slow←case←down; end; seventh←octant:begin dx:=8; edge←prep(n0,n1,-m1,-m0); goto slow←case←down; end; eighth←octant:begin dx:=8; edge←prep(m0,m1,-n1,-n0); goto fast←case←down; end; end; {there are only eight octants} @ @<Add edges for first or fourth octants, then |goto done|@>= @<Move to row |n0|, pointed to by |p|@>; if delta>0 then begin k:=0; edge←and←weight:=8*(m0+m←offset(cur←edges))+min←halfword+zero←w-cur←wt; repeat edge←and←weight:=edge←and←weight+dx*move[k]; fast←get←avail(r); link(r):=unsorted(p); info(r):=edge←and←weight; if internal[tracing←edges]>0 then trace←new←edge(r,n); unsorted(p):=r; p:=link(p); incr(k); incr(n); until k=delta; end; goto done @ @<Add edges for fifth or eighth octants, then |goto done|@>= n0:=-n0-1; @<Move to row |n0|, pointed to by |p|@>; if delta>0 then begin k:=0; edge←and←weight:=8*(m0+m←offset(cur←edges))+min←halfword+zero←w+cur←wt; repeat edge←and←weight:=edge←and←weight+dx*move[k]; fast←get←avail(r); link(r):=unsorted(p); info(r):=edge←and←weight; if internal[tracing←edges]>0 then trace←new←edge(r,n); unsorted(p):=r; p:=knil(p); incr(k); decr(n); until k=delta; end; goto done @ @<Add edges for second or third octants, then |goto done|@>= edge←and←weight:=8*(n0+m←offset(cur←edges))+min←halfword+zero←w-cur←wt; n0:=m0; k:=0; @<Move to row |n0|, pointed to by |p|@>; repeat j:=move[k]; while j>0 do begin fast←get←avail(r); link(r):=unsorted(p); info(r):=edge←and←weight; if internal[tracing←edges]>0 then trace←new←edge(r,n); unsorted(p):=r; p:=link(p); decr(j); incr(n); end; edge←and←weight:=edge←and←weight+dx; incr(k); until k>delta; goto done @ @<Add edges for sixth or seventh octants, then |goto done|@>= edge←and←weight:=8*(n0+m←offset(cur←edges))+min←halfword+zero←w+cur←wt; n0:=-m0-1; k:=0; @<Move to row |n0|, pointed to by |p|@>; repeat j:=move[k]; while j>0 do begin fast←get←avail(r); link(r):=unsorted(p); info(r):=edge←and←weight; if internal[tracing←edges]>0 then trace←new←edge(r,n); unsorted(p):=r; p:=knil(p); decr(j); decr(n); end; edge←and←weight:=edge←and←weight+dx; incr(k); until k>delta; goto done @ All the hard work of building an edge structure is undone by the following subroutine. @<Declare the recycling subroutines@>= procedure toss←edges(@!h:pointer); var @!p,@!q:pointer; {for list manipulation} begin q:=link(h); while q<>h do begin flush←list(sorted(q)); if unsorted(q)>void then flush←list(unsorted(q)); p:=q; q:=link(q); free←node(p,row←node←size); end; free←node(h,edge←header←size); end; @* \[21] Subdivision into octants. When \MF\ digitizes a path, it reduces the problem to the special case of paths that travel in ``first octant'' directions; i.e., each cubic $z(t)=\bigl(x(t),y(t)\bigr)$ being digitized will have the property that $0\L y'(t)\L x'(t)$. This assumption makes digitizing simpler and faster than if the direction of motion has to be tested repeatedly. When $z(t)$ is cubic, $x'(t)$ and $y'(t)$ are quadratic, hence the four polynomials $x'(t)$, $y'(t)$, $x'(t)-y'(t)$, and $x'(t)+y'(t)$ cross through~0 at most twice each. If we subdivide the given cubic at these places, we get at most nine subintervals in each of which $x'(t)$, $y'(t)$, $x'(t)-y'(t)$, and $x'(t)+y'(t)$ all have a constant sign. The curve can be transformed in each of these subintervals so that it travels entirely in first octant directions, if we reflect $x\swap-x$, $y\swap-y$, and/or $x\swap y$ as necessary. (Incidentally, it can be shown that a cubic such that $x'(t)=16(2t-1)↑2+2(2t-1)-1$ and $y'(t)=8(2t-1)↑2+4(2t-1)$ does indeed split into nine subintervals.) @ The transformation that rotates coordinates, so that first octant motion can be assumed, is defined by the |skew| subroutine, which sets global variables |cur←x| and |cur←y| to the values that are appropriate in a given octant. (Octants are encoded as they were in the |n←arg| subroutine.) This transformation is ``skewed'' by replacing |(x,y)| by |(x-y,y)|, once first octant motion has been established. It turns out that skewed coordinates are somewhat better to work with when curves are actually digitized. @d set←two←end(#)==cur←y:=#;@+end @d set←two(#)==begin cur←x:=#; set←two←end @p procedure skew(@!x,@!y:scaled;@!octant:small←number); begin case octant of first←octant: set←two(x-y)(y); second←octant: set←two(y-x)(x); third←octant: set←two(y+x)(-x); fourth←octant: set←two(-x-y)(y); fifth←octant: set←two(-x+y)(-y); sixth←octant: set←two(-y+x)(-x); seventh←octant: set←two(-y-x)(x); eighth←octant: set←two(x+y)(-y); end; {there are no other cases} end; @ Conversely, the following subroutine sets |cur←x| and |cur←y| to the original coordinate values of a point, given an octant code and the point's coordinates |(x,y)| after they have been mapped into the first octant and skewed. @<Declare subroutines for printing expressions@>= procedure unskew(@!x,@!y:scaled;@!octant:small←number); begin case octant of first←octant: set←two(x+y)(y); second←octant: set←two(y)(x+y); third←octant: set←two(-y)(x+y); fourth←octant: set←two(-x-y)(y); fifth←octant: set←two(-x-y)(-y); sixth←octant: set←two(-y)(-x-y); seventh←octant: set←two(y)(-x-y); eighth←octant: set←two(x+y)(-y); end; {there are no other cases} end; @ @<Glob...@>= @!cur←x,@!cur←y:scaled; {outputs of |rotate|, |unrotate|, and a few other routines} @ The conversion to skewed and rotated coordinates takes place in stages, and at one point in the transformation we will have negated the $x$ and/or $y$ coordinates so as to make curves travel in the first {\sl quadrant}. At this point the relevant ``octant'' code will be either |first←octant| (when no transformation has been done), or |fourth←octant=first←octant+negate←x| (when $x$ has been negated), or |fifth←octant=first←octant+negate←x+negate←y| (when both have been negated), or |eighth←octant=first←octant+negate←y| (when $y$ has been negated). The |abnegate| routine is sometimes needed to convert from one of these transformations to another. @p procedure abnegate(@!x,@!y:scaled; @!octant←before,@!octant←after:small←number); begin if odd(octant←before)=odd(octant←after) then cur←x:=x else cur←x:=-x; if (octant←before>negate←y)=(octant←after>negate←y) then cur←y:=y else cur←y:=-y; end; @ Now here's a subroutine that's handy for subdivision: Given a quadratic polynomial $B(a,b,c;t)$, the |crossing←point| function returns the unique |fraction| value |t| between 0 and~1 at which $B(a,b,c;t)$ changes from positive to negative, or returns |t=fraction←one+1| if no such value exists. If |a<0| (so that $B(a,b,c;t)$ is already negative at |t=0|), |crossing←point| returns zero. @d no←crossing==begin crossing←point:=fraction←one+1; return; end @d one←crossing==begin crossing←point:=fraction←one; return; end @d zero←crossing==begin crossing←point:=0; return; end @p function crossing←point(@!a,@!b,@!c:integer):fraction; label exit; var @!d:integer; {recursive counter} @!x,@!xx,@!x0,@!x1,@!x2:integer; {temporary registers for bisection} begin if a<0 then zero←crossing; if c>=0 then begin if b>=0 then if c>0 then no←crossing else if (a=0)and(b=0) then no←crossing else one←crossing; if a=0 then zero←crossing; end else if a=0 then if b<=0 then zero←crossing; @<Use bisection to find the crossing point, if one exists@>; exit:end; @ The general bisection method is quite simple when $n=2$, hence |crossing←point| does not take much time. At each stage in the recursion we have a subinterval defined by |l| and~|j| such that $B(a,b,c;2↑{-l}(j+t))=B(x←0,x←1,x←2;t)$, and we want to ``zero in'' on the subinterval where $x←0\G0$ and $\min(x←1,x←2)<0$. It is convenient for purposes of calculation to combine the values of |l| and~|j| in a single variable $d=2↑l+j$, because the operation of bisection then corresponds simply to doubling $d$ and possibly adding~1. Furthermore it proves to be convenient to modify our previous conventions for bisection slightly, maintaining the variables $X←0=2↑lx←0$, $X←1=2↑l(x←0-x←1)$, and $X←2=2↑l(x←1-x←2)$. With these variables the conditions $x←0>=0$ and $\min(x←1,x←2)<0$ are equivalent to $\max(X←1,X←1+X←2)>X←0\G0$. The following code maintains the invariant relations |0<=x0<max(x1,x1+x2)|, $\vert|x1|\vert<2↑{30}$, $\vert|x2|\vert<2↑{30}$; it has been constructed in such a way that no arithmetic overflow will occur if the inputs satisfy $a<2↑{30}$, $\vert a-b\vert<2↑{30}$, and $\vert b-c\vert<2↑{30}$. @<Use bisection to find the crossing point...@>= d:=1; x0:=a; x1:=a-b; x2:=b-c; repeat x:=half(x1+x2); if x1-x0>x0 then begin x2:=x; double(x0); double(d); end else begin xx:=x1+x-x0; if xx>x0 then begin x2:=x; double(x0); double(d); end else begin x0:=x0-xx; if x<=x0 then if x+x2<=x0 then no←crossing; x1:=x; d:=d+d+1; end; end; until d>=fraction←one; crossing←point:=d-fraction←one @ Octant subdivision is applied only to cycles, i.e., to closed paths. A ``cycle spec'' is a data structure that contains specifications of @!@↑cycle spec@> cubic curves and octant mappings for the cycle that has been subdivided into segments belonging to single octants. It is composed entirely of knot nodes, similar to those in the representation of paths; but the |explicit| type indications have been replaced by positive numbers that give further information. Additional |endpoint| data is also inserted at the octant boundaries. Recall that a cubic polynomial is represented by four control points that appear in adjacent nodes |p| and~|q| of a knot list. The |x|~coordinates are |x←coord(p)|, |right←x(p)|, |left←x(q)|, and |x←coord(q)|; the |y|~coordinates are similar. We shall call this ``the cubic following~|p|'' or ``the cubic between |p| and~|q|'' or ``the cubic preceding~|q|.'' Cycle specs are circular lists of cubic curves mixed with octant boundaries. Like cubics, the octant boundaries are represented in consecutive knot nodes |p| and~|q|. In such cases |right←type(p)= left←type(q)=endpoint|, and the fields |right←x(p)|, |right←y(p)|, |left←x(q)|, and |left←y(q)| are replaced by other fields called |right←octant(p)|, |right←transition(p)|, |left←octant(q)|, and |left←transition(q)|, respectively. For example, when the curve direction moves from the third octant to the fourth octant, the nodes say |right←octant(p)=third←octant|, |left←octant(q)=fourth←octant|, and |right←transition(p)=left←transition(q)=diagonal|. A |diagonal| transition occurs when moving between octants 1~\AM~2, 3~\AM~4, 5~\AM~6, or 7~\AM~8; an |axis| transition occurs when moving between octants 8~\AM~1, 2~\AM~3, 4~\AM~5, 6~\AM~7. (Such transition information is redundant but convenient.) Fields |x←coord(p)| and |y←coord(p)| will contain coordinates of the transition point after rotation from third octant to first octant; i.e., if the true coordinates are $(x,y)$, the coordinates $(y,\bar x)$ will appear in node~|p|. Similarly, a fourth-octant transformation will have been applied after the transition, so we will have |x←coord(q)=@t$\bar x$@>| and |y←coord(q)=y|. The cubic between |p| and |q| will contain positive numbers in the fields |right←type(p)| and |left←type(q)|; this makes cubics distinguishable from octant boundaries, because |endpoint=0|. The value of |right←type(p)| will be the current octant code, during the time that cycle specs are being constructed; it will refer later to a pen offset position, if the envelope of a cycle is being computed. A cubic that comes from some subinterval of the $k$th step in the original cyclic path will have |left←type(q)=k|. @d right←octant==right←x {the octant code before a transition} @d left←octant==left←x {the octant after a transition} @d right←transition==right←y {the type of transition} @d left←transition==left←y {ditto, either |axis| or |diagonal|} @d axis=0 {a transition across the $x'$- or $y'$-axis} @d diagonal=1 {a transition where $y'=\pm x'$} @ Here's a routine that prints a cycle spec in symbolic form, so that it is possible to see what subdivision has been made. The point coordinates are converted back from \MF's internal ``rotated'' form to the external ``true'' form. The global variable~|cur←spec| should point to a knot just after the beginning of an octant boundary, i.e., such that |left←type(cur←spec)=endpoint|. @d print←two←true(#)==unskew(#,octant); print←two(cur←x,cur←y) @p procedure print←spec(@!s:str←number); label not←found,done; var @!p,@!q:pointer; {for list traversal} @!octant:small←number; {the current octant code} begin print←diagnostic("Cycle spec",s,true); @.Cycle spec at line...@> p:=cur←spec; octant:=left←octant(p); print←ln; print←two←true(x←coord(cur←spec),y←coord(cur←spec)); print(" % beginning in octant `"); loop@+ begin print(octant←dir[octant]); print←char("'"); loop@+ begin q:=link(p); if right←type(p)=endpoint then goto not←found; @<Print the cubic between |p| and |q|@>; p:=q; end; not←found: if q=cur←spec then goto done; p:=q; octant:=left←octant(p); print←nl("% entering octant `"); end; @.entering the nth octant@> done: print←nl(" & cycle"); end←diagnostic(true); end; @ Symbolic octant direction names are kept in the |octant←dir| array. @<Glob...@>= @!octant←dir:array[first←octant..sixth←octant] of str←number; @ @<Set init...@>= octant←dir[first←octant]:="ENE"; octant←dir[second←octant]:="NNE"; octant←dir[third←octant]:="NNW"; octant←dir[fourth←octant]:="WNW"; octant←dir[fifth←octant]:="WSW"; octant←dir[sixth←octant]:="SSW"; octant←dir[seventh←octant]:="SSE"; octant←dir[eighth←octant]:="ESE"; @ @<Print the cubic between...@>= begin print←nl(" ..controls "); print←two←true(right←x(p),right←y(p)); print(" and "); print←two←true(left←x(q),left←y(q)); print←nl(" .."); print←two←true(x←coord(q),y←coord(q)); print(" % segment "); print←int(left←type(q)-1); end @ A much more compact version of a spec is printed to help users identify ``strange paths.'' @p procedure print←strange(@!s:str←number); var @!p:pointer; {for list traversal} @!f:pointer; {starting point in the cycle} @!q:pointer; {octant boundary to be printed} @!t:integer; {segment number, plus 1} begin if interaction=error←stop←mode then wake←up←terminal; print←nl(">"); @.>@> @<Find the starting point, |f|@>; @<Determine the octant boundary |q| that precedes |f|@>; t:=0; repeat if left←type(p)<>endpoint then begin if left←type(p)<>t then begin t:=left←type(p); print←char(" "); print←int(t-1); end; if q<>null then begin @<Print the turns, if any, that start at |q|, and advance |q|@>; print←char(" "); print(octant←dir[left←octant(q)]); q:=null; end; end else if q=null then q:=p; p:=link(p); until p=f; print←char(" "); print←int(left←type(p)-1); if q<>null then @<Print the turns...@>; print←err(s); end; @ If the segment numbers on the cycle are $t←1$, $t←2$, \dots, $t←m$, we have $t←{k-1}\L t←k$ except for at most one value of~$k$. If there are no exceptions, $f$ will point to $t←1$; otherwise it will point to the exceptional~$t←k$. There is at least one segment number (i.e., we always have $m>0$), because |print←strange| is never called upon to display an entirely ``dead'' cycle. @<Find the starting point, |f|@>= p:=cur←spec; t:=max←quarterword+1; repeat p:=link(p); if left←type(p)<>endpoint then begin if left←type(p)<t then f:=p; t:=left←type(p); end; until p=cur←spec @ @<Determine the octant boundary...@>= p:=cur←spec; q:=p; repeat p:=link(p); if left←type(p)=endpoint then q:=p; until p=f @ When two octant boundaries are adjacent, the path is simply changing direction without moving. Such octant directions are shown in parentheses. @<Print the turns...@>= if left←type(link(q))=endpoint then begin print(" ("); print(octant←dir[left←octant(q)]); q:=link(q); while left←type(link(q))=endpoint do begin print←char(" "); print(octant←dir[left←octant(q)]); q:=link(q); end; print←char(")"); end @ The |make←spec| routine is what subdivides paths into octants: Given a pointer |cur←spec| to a cyclic path, |make←spec| mungs the path data and returns a pointer to the corresponding cyclic spec. All ``dead'' cubics (i.e., cubics that don't move at all from their starting points) will have been removed from the result. @!@↑dead cubics@> The idea of |make←spec| is fairly simple: Each cubic is first subdivided, if necessary, into pieces belonging to single octants; then the octant boundaries are inserted. But some of the details of this transformation are not quite obvious. If |autorounding>0|, the path will be adjusted so that critical tangent directions occur at ``good'' points with respect to the pen called |cur←pen|. The result spec will have all scaled |x| and |y| coordinates at most $2↑{28}-|half←unit|-1-|safety←margin|$ in absolute value. The pointer that is returned will start some octant, as required by |print←spec|. @p @t\4@>@<Declare subroutines needed by |make←spec|@>@; function make←spec(@!h:pointer; @!safety←margin:scaled;@!tracing:integer):pointer; {converts a path to a cycle spec} label continue,done; var @!p,@!q,@!r,@!s:pointer; {for traversing the lists} @!k:integer; {serial number of path segment, or octant code} @!chopped:boolean; {have we truncated any of the data?} @<Other local variables for |make←spec|@>@; begin cur←spec:=h; if tracing>0 then print←path(cur←spec,", before subdivision into octants",true); max←allowed:=fraction←one-half←unit-1-safety←margin; @<Truncate the values of all coordinates that exceed |max←allowed|, and stamp segment numbers in each |left←type| field@>; quadrant←subdivide; {subdivide each cubic into pieces belonging to quadrants} if internal[autorounding]>0 then xy←round; octant←subdivide; {complete the subdivision} if internal[autorounding]>unity then diag←round; @<Remove dead cubics@>; @<Insert octant boundaries and compute the turning number@>; while left←type(cur←spec)<>endpoint do cur←spec:=link(cur←spec); if tracing>0 then if internal[autorounding]<=0 then print←spec(", after subdivision") else if internal[autorounding]>unity then print←spec(", after subdivision and double autorounding") else print←spec(", after subdivision and autorounding"); make←spec:=cur←spec; end; @ The |make←spec| routine has an interesting side effect, namely to set the global variable |turning←number| to the number of times the tangent vector of the given cyclic path winds around the origin. Another global variable |cur←spec| points to the specification as it is being made, since several subroutines must go to work on it. And there are two global variables that affect the rounding decisions, as we'll see later: |cur←pen| and |cur←path←type|. The latter will be |double←path←code| if |make←spec| is being applied to a double path. @d double←path←code=0 {command modifier for `\&{doublepath}'} @d contour←code=1 {command modifier for `\&{contour}'} @d also←code=2 {command modifier for `\&{also}'} @<Glob...@>= @!cur←spec:pointer; {the principal output of |make←spec|} @!turning←number:integer; {another output of |make←spec|} @!cur←pen:pointer; {an implicit input of |make←spec|, used in autorounding} @!cur←path←type:double←path←code..also←code; {likewise} @!max←allowed:scaled; {coordinates must be at most this big} @ First we do a simple preprocessing step. The segment numbers inserted here will propagate to all descendants of cubics that are split into subintervals. These numbers must be nonzero, but otherwise they are present merely for diagnostic purposes. The cubic from |p| to~|q| that represents ``time interval'' |(t-1)..t| usually has |right←type(q)=t|, except when |t| is too large to be stored in a quarterword. @d procrustes(#)==if abs(#)>max←allowed then begin chopped:=true; if #>0 then #:=max←allowed@+else #:=-max←allowed; end @<Truncate the values of all coordinates that exceed...@>= p:=cur←spec; k:=1; chopped:=false; repeat procrustes(left←x(p)); procrustes(left←y(p)); procrustes(x←coord(p)); procrustes(y←coord(p)); procrustes(right←x(p)); procrustes(right←y(p));@/ p:=link(p); left←type(p):=k; if k<max←quarterword then incr(k)@+else k:=1; until p=cur←spec; if chopped then begin print←err("Curve out of range"); @.Curve out of range@> help4("At least one of the coordinates in the path I'm about to")@/ ("digitize was really huge (potentially bigger than 4095).")@/ ("So I've cut it back to the maximum size.")@/ ("The results will probably be pretty wild."); put←get←error; end @ We may need to get rid of constant ``dead'' cubics that clutter up the data structure and interfere with autorounding. @<Declare subroutines needed by |make←spec|@>= procedure remove←cubic(@!p:pointer); {removes the cubic following~|p|} var @!q:pointer; {the node that disappears} begin q:=link(p); right←type(p):=right←type(q); link(p):=link(q);@/ x←coord(p):=x←coord(q); y←coord(p):=y←coord(q);@/ right←x(p):=right←x(q); right←y(p):=right←y(q);@/ free←node(q,knot←node←size); end; @ The subdivision process proceeds by first swapping $x\swap-x$, if necessary, to ensure that $x'\G0$; then swapping $y\swap-y$, if necessary, to ensure that $y'\G0$; and finally swapping $x\swap y$, if necessary, to ensure that $x'\G y'$. Recall that the octant codes have been defined in such a way that, for example, |third←octant=first←octant+negate←x+switch←x←and←y|. The program uses the fact that |negate←x<negate←y<switch←x←and←y| to handle ``double negation'': If |c| is an octant code that possibly involves |negate←x| and/or |negate←y|, but not |switch←x←and←y|, then negating~|y| changes~|c| either to |c+negate←y| or |c-negate←y|, depending on whether |c<=negate←y| or |c>negate←y|. Octant codes are always positive. The first step is to subdivide on |x| and |y| only, so that horizontal and vertical autorounding can be done before we compare $x'$ to $y'$. @<Declare subroutines needed by |make←spec|@>= @t\4@>@<Declare the procedure called |split←cubic|@>@; procedure quadrant←subdivide; label continue,exit; var @!p,@!q,@!r,@!s,@!pp,@!qq:pointer; {for traversing the lists} @!first←x,@!first←y:scaled; {unnegated coordinates of node |cur←spec|} @!del1,@!del2,@!del3,@!del,@!dmax:scaled; {proportional to the control points of a quadratic derived from a cubic} @!t:fraction; {where a quadratic crosses zero} @!dest←x,@!dest←y:scaled; {final values of |x| and |y| in the current cubic} @!constant←x:boolean; {is |x| constant between |p| and |q|?} begin p:=cur←spec; first←x:=x←coord(cur←spec); first←y:=y←coord(cur←spec); repeat continue: q:=link(p); @<Subdivide the cubic between |p| and |q| so that the results travel toward the right halfplane@>; @<Subdivide all cubics between |p| and |q| so that the results travel toward the first quadrant; but |return| or |goto continue| if the cubic from |p| to |q| was dead@>; p:=q; until p=cur←spec; exit:end; @ All three subdivision processes are similar, so it's possible to get the general idea by studying the first one (which is the simplest). The calculation makes use of the fact that the derivatives of Bernshte{\u\i}n polynomials satisfy $B'(z←0,z←1,\ldots,z←n;t)=nB(z←1-z←0,\ldots,z←n-z←{n-1};t)$. At the beginning of this step, |right←type(p)=explicit|; we should set |right←type(p):=first←octant|. However, no assignment is made, because |explicit=first←octant|. The author apologizes for using such trickery here; it is really hard to do redundant computations just for the sake of purity. @<Subdivide the cubic between |p| and |q| so that the results travel toward the right halfplane...@>= if q=cur←spec then begin dest←x:=first←x; dest←y:=first←y; end else begin dest←x:=x←coord(q); dest←y:=y←coord(q); end; del1:=right←x(p)-x←coord(p); del2:=left←x(q)-right←x(p); del3:=dest←x-left←x(q); @<Scale up |del1|, |del2|, and |del3| for greater accuracy; also set |del| to the first nonzero element of |(del1,del2,del3)|@>; if del=0 then constant←x:=true else begin constant←x:=false; if del<0 then @<Complement the |x| coordinates of the cubic between |p| and~|q|@>; t:=crossing←point(del1,del2,del3); if t<fraction←one then @<Subdivide the cubic with respect to $x'$, possibly twice@>; end @ If |del1=del2=del3=0|, it's impossible to obey the title of this section. We just set |del=0| in that case. @↑inner loop@> @<Scale up |del1|, |del2|, and |del3| for greater accuracy...@>= if del1<>0 then del:=del1 else if del2<>0 then del:=del2 else del:=del3; if del<>0 then begin dmax:=abs(del1); if abs(del2)>dmax then dmax:=abs(del2); if abs(del3)>dmax then dmax:=abs(del3); while dmax<fraction←half do begin double(dmax); double(del1); double(del2); double(del3); end; end @ During the subdivision phases of |make←spec|, the |x←coord| and |y←coord| fields of node~|q| are not transformed to agree with the octant stated in |right←type(p)|; they remain consistent with |right←type(q)|. But |left←x(q)| and |left←y(q)| are governed by |right←type(p)|. @<Complement the |x| coordinates...@>= begin negate(x←coord(p)); negate(right←x(p)); negate(left←x(q));@/ negate(del1); negate(del2); negate(del3);@/ negate(dest←x); right←type(p):=first←octant+negate←x; end @ When a cubic is split at a |fraction| value |t|, we obtain two cubics whose B\'ezier control points are obtained by a generalization of the bisection process: The formula `$z←k↑{(j+1)}={1\over2}(z←k↑{(j)}+z\k↑{(j)})$' becomes `$z←k↑{(j+1)}=t[z←k↑{(j)},z\k↑{(j)}]$'. It is convenient to define a \.{WEB} macro |t←of←the←way| such that |t←of←the←way(a)(b)| expands to |a-(a-b)*t|, i.e., to |t[a,b]|. @d t←of←the←way←end(#)==#,t@=)@> @d t←of←the←way(#)==#-take←fraction@=(@>#-t←of←the←way←end @<Declare the procedure called |split←cubic|@>= procedure split←cubic(@!p:pointer;@!t:fraction; @!xq,@!yq:scaled); {splits the cubic after |p|} var @!v:scaled; {an intermediate value} @!q,@!r:pointer; {for list manipulation} begin q:=link(p); r:=get←node(knot←node←size); link(p):=r; link(r):=q;@/ left←type(r):=left←type(q); right←type(r):=right←type(p);@# v:=t←of←the←way(right←x(p))(left←x(q)); right←x(p):=t←of←the←way(x←coord(p))(right←x(p)); left←x(q):=t←of←the←way(left←x(q))(xq); left←x(r):=t←of←the←way(right←x(p))(v); right←x(r):=t←of←the←way(v)(left←x(q)); x←coord(r):=t←of←the←way(left←x(r))(right←x(r));@# v:=t←of←the←way(right←y(p))(left←y(q)); right←y(p):=t←of←the←way(y←coord(p))(right←y(p)); left←y(q):=t←of←the←way(left←y(q))(yq); left←y(r):=t←of←the←way(right←y(p))(v); right←y(r):=t←of←the←way(v)(left←y(q)); y←coord(r):=t←of←the←way(left←y(r))(right←y(r)); end; @ Since $x'(t)$ is a quadratic equation, it can cross through zero at~most twice. When it does cross zero, we make doubly sure that the derivative is really zero at the splitting point, in case rounding errors have caused the split cubic to have an apparently nonzero derivative. @<Subdivide the cubic with respect to $x'$, possibly twice@>= begin split←cubic(p,t,dest←x,dest←y); r:=link(p); if right←type(r)>negate←x then right←type(r):=first←octant else right←type(r):=first←octant+negate←x; if x←coord(r)<x←coord(p) then x←coord(r):=x←coord(p); left←x(r):=x←coord(r); negate(x←coord(r)); right←x(r):=x←coord(r); negate(left←x(q)); negate(dest←x);@/ del2:=t←of←the←way(del2)(del3); {now |0,del2,del3| represent $x'$ on the remaining interval} if del2>0 then del2:=0; t:=crossing←point(0,-del2,-del3); if t<fraction←one then @<Subdivide the cubic a second time with respect to $x'$@> else if x←coord(r)>dest←x then x←coord(r):=dest←x; end @ @<Subdivide the cubic a second time with respect to $x'$@>= begin split←cubic(r,t,dest←x,dest←y); s:=link(r); if x←coord(s)<dest←x then x←coord(s):=dest←x; if x←coord(s)<x←coord(r) then x←coord(s):=x←coord(r); right←type(s):=right←type(p); left←x(s):=x←coord(s); negate(x←coord(s)); right←x(s):=x←coord(s); negate(left←x(q)); end @ The process of subdivision with respect to $y'$ is like that with respect to~$x'$, with the slight additional complication that two or three cubics might now appear between |p| and~|q|. @<Subdivide all cubics between |p| and |q| so that the results travel toward the first quadrant...@>= pp:=p; repeat qq:=link(pp); abnegate(x←coord(qq),y←coord(qq),right←type(qq),right←type(pp)); dest←x:=cur←x; dest←y:=cur←y;@/ del1:=right←y(pp)-y←coord(pp); del2:=left←y(qq)-right←y(pp); del3:=dest←y-left←y(qq); @<Scale up |del1|, |del2|, and |del3| for greater accuracy; also set |del| to the first nonzero element of |(del1,del2,del3)|@>; if del<>0 then {they weren't all zero} begin if del<0 then @<Complement the |y| coordinates of the cubic between |pp| and~|qq|@>; t:=crossing←point(del1,del2,del3); if t<fraction←one then @<Subdivide the cubic with respect to $y'$, possibly twice@>; end else @<Do any special actions needed when |y| is constant; |return| or |goto continue| if a dead cubic from |p| to |q| is removed@>; pp:=qq; until pp=q; if constant←x then @<Correct the octant code in segments with decreasing |y|@> @ @<Complement the |y| coordinates...@>= begin negate(y←coord(pp)); negate(right←y(pp)); negate(left←y(qq));@/ negate(del1); negate(del2); negate(del3);@/ negate(dest←y); right←type(pp):=right←type(pp)+negate←y; end @ @<Subdivide the cubic with respect to $y'$, possibly twice@>= begin split←cubic(pp,t,dest←x,dest←y); r:=link(pp); if right←type(r)>negate←y then right←type(r):=right←type(r)-negate←y else right←type(r):=right←type(r)+negate←y; if x←coord(r)>dest←x then x←coord(r):=dest←x else if x←coord(r)<x←coord(pp) then x←coord(r):=x←coord(pp); if y←coord(r)<y←coord(pp) then y←coord(r):=y←coord(pp); left←y(r):=y←coord(r); negate(y←coord(r)); right←y(r):=y←coord(r); negate(left←y(qq)); negate(dest←y);@/ del2:=t←of←the←way(del2)(del3); {now |0,del2,del3| represent $y'$ on the remaining interval} if del2>0 then del2:=0; t:=crossing←point(0,-del2,-del3); if t<fraction←one then @<Subdivide the cubic a second time with respect to $y'$@> else if y←coord(r)>dest←y then y←coord(r):=dest←y; end @ @<Subdivide the cubic a second time with respect to $y'$@>= begin split←cubic(r,t,dest←x,dest←y); s:=link(r);@/ if x←coord(s)>dest←x then x←coord(s):=dest←x else if x←coord(s)<x←coord(r) then x←coord(s):=x←coord(r); if y←coord(s)<dest←y then y←coord(s):=dest←y; if y←coord(s)<y←coord(r) then y←coord(s):=y←coord(r); right←type(s):=right←type(pp); left←y(s):=y←coord(s); negate(y←coord(s)); right←y(s):=y←coord(s); negate(left←y(qq)); end @ If the cubic is constant in $y$ and increasing in $x$, we have classified it as traveling in the first octant. If the cubic is constant in~$y$ and decreasing in~$x$, it is desirable to classify it as traveling in the fifth octant (not the fourth), because autorounding will be consistent with respect to doublepaths only if the octant number changes by four when the path is reversed. Therefore we negate the $y$~coordinates when they are constant but the curve is decreasing in~$y$; this gives the desired result except in pathological paths. If the cubic is ``dead,'' i.e., constant in both |x| and |y|, we remove it unless it is the only cubic in the entire path. We |goto continue| if it wasn't the final cubic, so that the test |p=cur←spec| does not falsely imply that all cubics have been processed. @<Do any special actions needed when |y| is constant...@>= if constant←x then {|p=pp|, |q=qq|, and the cubic is dead} begin if q<>p then begin remove←cubic(p); {remove the dead cycle and recycle node |q|} if cur←spec<>q then goto continue else begin cur←spec:=p; return; end; {the final cubic was dead and is gone} end; end else if not odd(right←type(pp)) then {the $x$ coordinates were negated} @<Complement the |y| coordinates...@> @ A similar correction to octant codes deserves to be made when |x| is constant and |y| is decreasing. @<Correct the octant code in segments with decreasing |y|@>= begin pp:=p; repeat qq:=link(pp); if right←type(pp)>negate←y then {the $y$ coordinates were negated} begin right←type(pp):=right←type(pp)+negate←x; negate(x←coord(pp)); negate(right←x(pp)); negate(left←x(qq)); end; pp:=qq; until pp=q; end @ Finally, the process of subdividing to make $x'\G y'$ is like the other two subdivisions, with a few new twists. We skew the coordinates at this time. @<Declare subroutines needed by |make←spec|@>= procedure octant←subdivide; var @!p,@!q,@!r,@!s:pointer; {for traversing the lists} @!del1,@!del2,@!del3,@!del,@!dmax:scaled; {proportional to the control points of a quadratic derived from a cubic} @!t:fraction; {where a quadratic crosses zero} @!dest←x,@!dest←y:scaled; {final values of |x| and |y| in the current cubic} begin p:=cur←spec; repeat q:=link(p);@/ x←coord(p):=x←coord(p)-y←coord(p); right←x(p):=right←x(p)-right←y(p); left←x(q):=left←x(q)-left←y(q);@/ @<Subdivide the cubic between |p| and |q| so that the results travel toward the first octant@>; p:=q; until p=cur←spec; end; @ @<Subdivide the cubic between |p| and |q| so that the results travel toward the first octant@>= @<Set up the variables |(del1,del2,del3)| to represent $x'-y'$@>; @<Scale up |del1|, |del2|, and |del3| for greater accuracy; also set |del| to the first nonzero element of |(del1,del2,del3)|@>; if del<>0 then {they weren't all zero} begin if del<0 then @<Swap the |x| and |y| coordinates of the cubic between |p| and~|q|@>; t:=crossing←point(del1,del2,del3); if t<fraction←one then @<Subdivide the cubic with respect to $x'-y'$, possibly twice@>; end @ @<Set up the variables |(del1,del2,del3)| to represent $x'-y'$@>= if q=cur←spec then begin unskew(x←coord(q),y←coord(q),right←type(q)); skew(cur←x,cur←y,right←type(p)); dest←x:=cur←x; dest←y:=cur←y; end else begin abnegate(x←coord(q),y←coord(q),right←type(q),right←type(p)); dest←x:=cur←x-cur←y; dest←y:=cur←y; end; del1:=right←x(p)-x←coord(p); del2:=left←x(q)-right←x(p); del3:=dest←x-left←x(q) @ The swapping here doesn't simply interchange |x| and |y| values, because the coordinates are skewed. It turns out that this is easier than ordinary swapping, because it can be done in two assignment statements rather than three. @ @<Swap the |x| and |y| coordinates...@>= begin y←coord(p):=x←coord(p)+y←coord(p); negate(x←coord(p));@/ right←y(p):=right←x(p)+right←y(p); negate(right←x(p));@/ left←y(q):=left←x(q)+left←y(q); negate(left←x(q));@/ negate(del1); negate(del2); negate(del3);@/ dest←y:=dest←x+dest←y; negate(dest←x);@/ right←type(p):=right←type(p)+switch←x←and←y; end @ @<Subdivide the cubic with respect to $x'-y'$, possibly twice@>= begin split←cubic(p,t,dest←x,dest←y); r:=link(p); if right←type(r)>switch←x←and←y then right←type(r):=right←type(r)-switch←x←and←y else right←type(r):=right←type(r)+switch←x←and←y; if y←coord(r)>dest←y then y←coord(r):=dest←y else if y←coord(r)<y←coord(p) then y←coord(r):=y←coord(p); if x←coord(r)<x←coord(p) then x←coord(r):=x←coord(p); left←x(r):=x←coord(r);@/ y←coord(r):=y←coord(r)+x←coord(r); negate(x←coord(r));@/ right←x(r):=x←coord(r); right←y(r):=right←y(r)-right←x(r);@/ left←y(q):=left←y(q)+left←x(q); negate(left←x(q));@/ dest←y:=dest←y+dest←x; negate(dest←x); del2:=t←of←the←way(del2)(del3); {now |0,del2,del3| represent $x'-y'$ on the remaining interval} if del2>0 then del2:=0; t:=crossing←point(0,-del2,-del3); if t<fraction←one then @<Subdivide the cubic a second time with respect to $x'-y'$@> else if x←coord(r)>dest←x then x←coord(r):=dest←x end @ @<Subdivide the cubic a second time with respect to $x'-y'$@>= begin split←cubic(r,t,dest←x,dest←y); s:=link(r);@/ if y←coord(s)>dest←y then y←coord(s):=dest←y else if y←coord(s)<y←coord(r) then y←coord(s):=y←coord(r); if x←coord(s)<dest←x then x←coord(s):=dest←x; if x←coord(s)<x←coord(r) then x←coord(s):=x←coord(r); right←type(s):=right←type(p); left←x(s):=x←coord(s); y←coord(s):=y←coord(s)+x←coord(s); negate(x←coord(s));@/ right←y(s):=right←y(s)-x←coord(s); right←x(s):=x←coord(s);@/ left←y(q):=left←y(q)+left←x(q); negate(left←x(q)); end @ It's time now to consider ``autorounding,'' which tries to make horizontal, vertical, and diagonal tangents occur at places that will produce appropriate images after the curve is digitized. The first job is to fix things so that |x(t)| is an integer multiple of the current ``granularity'' when the derivative $x'(t)$ crosses through zero. The given cyclic path contains regions where $x'(t)\G0$ and regions where $x'(t)\L0$. The |quadrant←subdivide| routine is called into action before any of the path coordinates have been skewed, but some of them may have been negated. In regions where $x'(t)\G0$ we have |right←type= first←octant| or |right←type=fourth←octant|; in regions where $x'(t)\L0$, we have |right←type=fifth←octant| or |right←type=eighth←octant|. Within any such region the transformed $x$ values increase monotonically from, say, $x←0$ to~$x←1$. We want to modify things by applying a linear transformation to all $x$ coordinates in the region, after which the $x$ values will increase monotonically from round$(x←0)$ to round$(x←1)$. This rounding scheme sounds quite simple, and it usually is. But several complications can arise that might make the task more difficult. In the first place, autorounding is inappropriate at cusps where $x'$ jumps discontinuously past zero without ever being zero. In the second place, the current pen might be unsymmetric in such a way that $x$ coordinates should round differently when $x'$ becomes positive than when it becomes negative. These considerations imply that round$(x←0)$ might be greater than round$(x←1)$, even though $x←0\L x←1$; in such cases we do not want to carry out the linear transformation. Furthermore, it's possible to have round$(x←1)-\hbox{round} (x←0)$ positive but much greater than $x←1-x←0$; then the transformation might distort the curve drastically, and again we want to avoid it. Finally, the rounded points must be consistent between adjacent regions, hence we can't transform one region without knowing about its neighbors. In order to handle all these complications, we must first look at the whole cycle and choose rounded $x$ values that are ``safe.'' The following procedure does this: Given $m$~values $(b←0,b←1,\ldots,b←{m-1})$ before rounding and $m$~corresponding values $(a←0,a←1,\ldots,a←{m-1})$ that would be desirable after rounding, the |make←safe| routine sets $a$'s to $b$'s if necessary so that $0\L(a\k-a←k)/(b\k-b←k)\L2$ afterwards. It is symmetric under cyclic permutation, reversal, and/or negation of the inputs. (Instead of |a|, |b|, and~|m|, the program uses the names |after|, |before|, and |cur←rounding←ptr|.) @<Declare subroutines needed by |make←spec|@>= procedure make←safe; var @!k:0..max←wiggle; {runs through the list of inputs} @!all←safe:boolean; {does everything look OK so far?} @!next←a:scaled; {|after[k]| before it might have changed} @!delta←a,@!delta←b:scaled; {|after[k+1]-after[k]| and |before[k+1]-before[k]|} begin before[cur←rounding←ptr]:=before[0]; {wrap around} node←to←round[cur←rounding←ptr]:=node←to←round[0]; repeat after[cur←rounding←ptr]:=after[0]; all←safe:=true; next←a:=after[0]; for k:=0 to cur←rounding←ptr-1 do begin delta←b:=before[k+1]-before[k]; if delta←b>=0 then delta←a:=after[k+1]-next←a else delta←a:=next←a-after[k+1]; next←a:=after[k+1]; if (delta←a<0)or(delta←a>abs(delta←b+delta←b)) then begin all←safe:=false; after[k]:=before[k]; if k=cur←rounding←ptr-1 then after[0]:=before[0] else after[k+1]:=before[k+1]; end; end; until all←safe; end; @ The global arrays used by |make←safe| are accompanied by an array of pointers into the current knot list. @<Glob...@>= @!before,@!after:array[0..max←wiggle] of scaled; {data for |make←safe|} @!node←to←round:array[0..max←wiggle] of pointer; {reference back to the path} @!cur←rounding←ptr:0..max←wiggle; {how many are being used} @!max←rounding←ptr:0..max←wiggle; {how many have been used} @ @<Set init...@>= max←rounding←ptr:=0; @ New entries go into the tables via the |before←and←after| routine: @<Declare subroutines needed by |make←spec|@>= procedure before←and←after(@!b,@!a:scaled;@!p:pointer); begin if cur←rounding←ptr=max←rounding←ptr then if max←rounding←ptr<max←wiggle then incr(max←rounding←ptr) else overflow("rounding table size",max←wiggle); @:METAFONT capacity exceeded rounding table size}{\quad rounding table size@> after[cur←rounding←ptr]:=a; before[cur←rounding←ptr]:=b; node←to←round[cur←rounding←ptr]:=p; incr(cur←rounding←ptr); end; @ A global variable called |cur←gran| is used instead of |internal[ granularity]|, because we want to work with a number that's guaranteed to be positive. @<Glob...@>= @!cur←gran:scaled; {the current granularity (which normally is |unity|)} @ The |good←val| function computes a number |a| that's as close as possible to~|b|, with the property that |a+o| is a multiple of |cur←gran|. If we assume that |cur←gran| is even (since it will in fact be a multiple of |unity| in all reasonable applications), we have the identity |good←val(-b-1,-o)=-good←val(b,o)|. @<Declare subroutines needed by |make←spec|@>= function good←val(@!b,@!o:scaled):scaled; var @!a:scaled; {accumulator} begin a:=b+o; if a>=0 then a:=a-(a mod cur←gran)-o else a:=a+((-(a+1)) mod cur←gran)-cur←gran+1-o; if b-a<a+cur←gran-b then good←val:=a else good←val:=a+cur←gran; end; @ When we're rounding a doublepath, we might need to compromise between two opposing tendencies, if the pen thickness is not a multiple of the granularity. The following ``compromise'' adjustment, suggested by John Hobby, finds the best way out of the dilemma. (Only the value @↑Hobby, John Douglas@> modulo |cur←gran| is relevant in our applications, so the result turns out to be essentially symmetric in |u| and~|v|.) @<Declare subroutines needed by |make←spec|@>= function compromise(@!u,@!v:scaled):scaled; begin compromise:=half(good←val(u+u,-u-v)); end; @ Here, then, is the procedure that rounds $x$ coordinates as described; it does the same for $y$ coordinates too, independently. @<Declare subroutines needed by |make←spec|@>= procedure xy←round; var @!p,@!q:pointer; {list manipulation registers} @!b,@!a:scaled; {before and after values} @!pen←edge:scaled; {offset that governs rounding} @!alpha:fraction; {coefficient of linear transformation} begin cur←gran:=abs(internal[granularity]); if cur←gran=0 then cur←gran:=unity; p:=cur←spec; cur←rounding←ptr:=0; repeat q:=link(p); @<If node |q| is a transition point for |x| coordinates, compute and save its before-and-after coordinates@>; p:=q; until p=cur←spec; if cur←rounding←ptr>0 then @<Transform the |x| coordinates@>; p:=cur←spec; cur←rounding←ptr:=0; repeat q:=link(p); @<If node |q| is a transition point for |y| coordinates, compute and save its before-and-after coordinates@>; p:=q; until p=cur←spec; if cur←rounding←ptr>0 then @<Transform the |y| coordinates@>; end; @ When |x| has been negated, the |octant| codes are even. We allow for an error of up to .01 pixel (i.e., 655 |scaled| units) in the derivative calculations at transition nodes. @<If node |q| is a transition point for |x| coordinates...@>= if odd(right←type(p))<>odd(right←type(q)) then begin if odd(right←type(q)) then b:=x←coord(q)@+else b:=-x←coord(q); if (abs(x←coord(q)-right←x(q))<655)or@| (abs(x←coord(q)+left←x(q))<655) then @<Compute before-and-after |x| values based on the current pen@> else a:=b; if abs(a)>max←allowed then if a>0 then a:=max←allowed@+else a:=-max←allowed; before←and←after(b,a,q); end @ When we study the data representation for pens, we'll learn that the |x|~coordinate of the current pen's west edge is $$\hbox{|y←coord(link(cur←pen+seventh←octant))|},$$ and that there are similar ways to address other important offsets. An ``|east←west←edge|'' is computed as a compromise between east and west, for use in doublepaths, in case the two edges have conflicting tendencies. @d north←edge(#)==y←coord(link(#+fourth←octant)) @d south←edge(#)==y←coord(link(#+first←octant)) @d east←edge(#)==y←coord(link(#+second←octant)) @d west←edge(#)==y←coord(link(#+seventh←octant)) @d north←south←edge(#)==mem[#+10].int {compromise between north and south} @d east←west←edge(#)==mem[#+11].int {compromise between east and west} @d NE←SW←edge(#)==mem[#+12].int {compromise between northeast and southwest} @d NW←SE←edge(#)==mem[#+13].int {compromise between northwest and southeast} @<Compute before-and-after |x| values based on the current pen@>= begin if cur←pen=null←pen then pen←edge:=0 else if cur←path←type=double←path←code then pen←edge:=compromise(east←edge(cur←pen),west←edge(cur←pen)) else if odd(right←type(q)) then pen←edge:=west←edge(cur←pen) else pen←edge:=east←edge(cur←pen); a:=good←val(b,pen←edge); end @ @<Transform the |x| coordinates@>= begin make←safe; repeat decr(cur←rounding←ptr); if (after[cur←rounding←ptr]<>before[cur←rounding←ptr])or@| (after[cur←rounding←ptr+1]<>before[cur←rounding←ptr+1]) then begin p:=node←to←round[cur←rounding←ptr]; if odd(right←type(p)) then begin b:=before[cur←rounding←ptr]; a:=after[cur←rounding←ptr]; end else begin b:=-before[cur←rounding←ptr]; a:=-after[cur←rounding←ptr]; end; if before[cur←rounding←ptr]=before[cur←rounding←ptr+1] then alpha:=fraction←one else alpha:=make←fraction(after[cur←rounding←ptr+1]-after[cur←rounding←ptr],@| before[cur←rounding←ptr+1]-before[cur←rounding←ptr]); repeat x←coord(p):=take←fraction(alpha,x←coord(p)-b)+a; right←x(p):=take←fraction(alpha,right←x(p)-b)+a; p:=link(p); left←x(p):=take←fraction(alpha,left←x(p)-b)+a; until p=node←to←round[cur←rounding←ptr+1]; end; until cur←rounding←ptr=0; end @ When |y| has been negated, the |octant| codes are |>negate←y|. Otherwise these routines are essentially identical to the routines for |x| coordinates that we have just seen. @<If node |q| is a transition point for |y| coordinates...@>= if (right←type(p)>negate←y)<>(right←type(q)>negate←y) then begin if right←type(q)<=negate←y then b:=y←coord(q)@+else b:=-y←coord(q); if (abs(y←coord(q)-right←y(q))<655)or@| (abs(y←coord(q)+left←y(q))<655) then @<Compute before-and-after |y| values based on the current pen@> else a:=b; if abs(a)>max←allowed then if a>0 then a:=max←allowed@+else a:=-max←allowed; before←and←after(b,a,q); end @ @<Compute before-and-after |y| values based on the current pen@>= begin if cur←pen=null←pen then pen←edge:=0 else if cur←path←type=double←path←code then pen←edge:=compromise(north←edge(cur←pen),south←edge(cur←pen)) else if right←type(q)<=negate←y then pen←edge:=south←edge(cur←pen) else pen←edge:=north←edge(cur←pen); a:=good←val(b,pen←edge); end @ @<Transform the |y| coordinates@>= begin make←safe; repeat decr(cur←rounding←ptr); if (after[cur←rounding←ptr]<>before[cur←rounding←ptr])or@| (after[cur←rounding←ptr+1]<>before[cur←rounding←ptr+1]) then begin p:=node←to←round[cur←rounding←ptr]; if right←type(p)<=negate←y then begin b:=before[cur←rounding←ptr]; a:=after[cur←rounding←ptr]; end else begin b:=-before[cur←rounding←ptr]; a:=-after[cur←rounding←ptr]; end; if before[cur←rounding←ptr]=before[cur←rounding←ptr+1] then alpha:=fraction←one else alpha:=make←fraction(after[cur←rounding←ptr+1]-after[cur←rounding←ptr],@| before[cur←rounding←ptr+1]-before[cur←rounding←ptr]); repeat y←coord(p):=take←fraction(alpha,y←coord(p)-b)+a; right←y(p):=take←fraction(alpha,right←y(p)-b)+a; p:=link(p); left←y(p):=take←fraction(alpha,left←y(p)-b)+a; until p=node←to←round[cur←rounding←ptr+1]; end; until cur←rounding←ptr=0; end @ Rounding at diagonal tangents takes place after the subdivision into octants is complete, hence after the coordinates have been skewed. The details are somewhat tricky, because we want to round to points whose skewed coordinates are halfway between integer multiples of the granularity. Furthermore, both coordinates change when they are rounded; this means we need a generalization of the |make←safe| routine, ensuring safety in both |x| and |y|. In spite of these extra complications, we can take comfort in the fact that the basic structure of the routine is the same as before. @<Declare subroutines needed by |make←spec|@>= procedure diag←round; var @!p,@!q,@!pp:pointer; {list manipulation registers} @!b,@!a,@!bb,@!aa,@!d,@!c,@!dd,@!cc:scaled; {before and after values} @!pen←edge:scaled; {offset that governs rounding} @!alpha,@!beta:fraction; {coefficients of linear transformation} @!next←a:scaled; {|after[k]| before it might have changed} @!all←safe:boolean; {does everything look OK so far?} @!k:0..max←wiggle; {runs through before-and-after values} @!first←x,@!first←y:scaled; {coordinates before rounding} begin p:=cur←spec; cur←rounding←ptr:=0; repeat q:=link(p); @<If node |q| is a transition point between octants, compute and save its before-and-after coordinates@>; p:=q; until p=cur←spec; if cur←rounding←ptr>0 then @<Transform the skewed coordinates@>; end; @ We negate the skewed |x| coordinates in the before-and-after table when the octant code is greater than |switch←x←and←y|. @<If node |q| is a transition point between octants...@>= if right←type(p)<>right←type(q) then begin if right←type(q)>switch←x←and←y then b:=-x←coord(q) else b:=x←coord(q); if abs(right←type(q)-right←type(p))=switch←x←and←y then if (abs(x←coord(q)-right←x(q))<655)or(abs(x←coord(q)+left←x(q))<655) then @<Compute a good coordinate at a diagonal transition@> else a:=b else a:=b; before←and←after(b,a,q); end @ In octants whose code number is even, $x$~has been negated; we want to round ambiguous cases downward instead of upward, so that the rounding will be consistent with octants whose code number is odd. This downward bias can be achieved by subtracting~1 from the first argument of |good←val|. @d diag←offset(#)==x←coord(knil(link(cur←pen+#))) @<Compute a good coordinate at a diagonal transition@>= begin if cur←pen=null←pen then pen←edge:=0 else if cur←path←type=double←path←code then @<Compute a compromise |pen←edge|@> else if right←type(q)<=switch←x←and←y then pen←edge:=diag←offset(right←type(q)) else pen←edge:=-diag←offset(right←type(q)); if odd(right←type(q)) then a:=good←val(b,pen←edge+half(cur←gran)) else a:=good←val(b-1,pen←edge+half(cur←gran)); end @ (It seems a shame to compute these compromise offsets repeatedly. The author would have stored them directly in the pen data structure, if the granularity had been constant.) @<Compute a compromise...@>= case right←type(q) of first←octant,second←octant:pen←edge:=compromise(diag←offset(first←octant),@| -diag←offset(fifth←octant)); fifth←octant,sixth←octant:pen←edge:=-compromise(diag←offset(first←octant),@| -diag←offset(fifth←octant)); third←octant,fourth←octant:pen←edge:=compromise(diag←offset(fourth←octant),@| -diag←offset(eighth←octant)); seventh←octant,eighth←octant:pen←edge:=-compromise(diag←offset(fourth←octant),@| -diag←offset(eighth←octant)); end {there are no other cases} @ @<Transform the skewed coordinates@>= begin p:=node←to←round[0]; first←x:=x←coord(p); first←y:=y←coord(p); @<Make sure that all the diagonal roundings are safe@>; for k:=0 to cur←rounding←ptr-1 do begin a:=after[k]; b:=before[k]; aa:=after[k+1]; bb:=before[k+1]; if (a<>b)or(aa<>bb) then begin p:=node←to←round[k]; pp:=node←to←round[k+1]; @<Determine the before-and-after values of both coordinates@>; if b=bb then alpha:=fraction←one else alpha:=make←fraction(aa-a,bb-b); if d=dd then beta:=fraction←one else beta:=make←fraction(cc-c,dd-d); repeat x←coord(p):=take←fraction(alpha,x←coord(p)-b)+a; y←coord(p):=take←fraction(beta,y←coord(p)-d)+c; right←x(p):=take←fraction(alpha,right←x(p)-b)+a; right←y(p):=take←fraction(beta,right←y(p)-d)+c; p:=link(p); left←x(p):=take←fraction(alpha,left←x(p)-b)+a; left←y(p):=take←fraction(beta,left←y(p)-d)+c; until p=pp; end; end; end @ In node |p|, the coordinates |(b,d)| will be rounded to |(a,c)|; in node |pp|, the coordinates |(bb,dd)| will be rounded to |(aa,cc)|. (We transform the values from node |pp| so that they agree with the conventions of node |p|.) If |aa<>bb|, we know that |abs(right←type(p)-right←type(pp))=switch←x←and←y|. @<Determine the before-and-after values of both coordinates@>= if aa=bb then begin if pp=node←to←round[0] then unskew(first←x,first←y,right←type(pp)) else unskew(x←coord(pp),y←coord(pp),right←type(pp)); skew(cur←x,cur←y,right←type(p)); bb:=cur←x; aa:=bb; dd:=cur←y; cc:=dd; if right←type(p)>switch←x←and←y then begin b:=-b; a:=-a; end; end else begin if right←type(p)>switch←x←and←y then begin bb:=-bb; aa:=-aa; b:=-b; a:=-a; end; if pp=node←to←round[0] then dd:=first←y-bb@+else dd:=y←coord(pp)-bb; if odd(aa-bb) then if right←type(p)>switch←x←and←y then cc:=dd-half(aa-bb+1) else cc:=dd-half(aa-bb-1) else cc:=dd-half(aa-bb); end; d:=y←coord(p); if odd(a-b) then if right←type(p)>switch←x←and←y then c:=d-half(a-b-1) else c:=d-half(a-b+1) else c:=d-half(a-b) @ @<Make sure that all the diagonal roundings are safe@>= before[cur←rounding←ptr]:=before[0]; {cf.~|make←safe|} node←to←round[cur←rounding←ptr]:=node←to←round[0]; repeat after[cur←rounding←ptr]:=after[0]; all←safe:=true; next←a:=after[0]; for k:=0 to cur←rounding←ptr-1 do begin a:=next←a; b:=before[k]; next←a:=after[k+1]; aa:=next←a; bb:=before[k+1]; if (a<>b)or(aa<>bb) then begin p:=node←to←round[k]; pp:=node←to←round[k+1]; @<Determine the before-and-after values of both coordinates@>; if (aa<a)or(cc<c)or(aa-a>2*(bb-b))or(cc-c>2*(dd-d)) then begin all←safe:=false; after[k]:=before[k]; if k=cur←rounding←ptr-1 then after[0]:=before[0] else after[k+1]:=before[k+1]; end; end; end; until all←safe @ Here we get rid of ``dead'' cubics, i.e., polynomials that don't move at all when |t|~changes, since the subdivision process might have introduced such things. If the cycle reduces to a single point, however, we are left with a single dead cubic that will not be removed until later. @<Remove dead cubics@>= p:=cur←spec; repeat continue: q:=link(p); if p<>q then begin if x←coord(p)=right←x(p) then if y←coord(p)=right←y(p) then if x←coord(p)=left←x(q) then if y←coord(p)=left←y(q) then begin unskew(x←coord(q),y←coord(q),right←type(q)); skew(cur←x,cur←y,right←type(p)); if x←coord(p)=cur←x then if y←coord(p)=cur←y then begin remove←cubic(p); {remove the cubic following |p|} if q<>cur←spec then goto continue; cur←spec:=p; q:=p; end; end; end; p:=q; until p=cur←spec; @ Finally we come to the last steps of |make←spec|, when boundary nodes are inserted between cubics that move in different octants. The main complication remaining arises from consecutive cubics whose octants are not adjacent; we should insert more than one octant boundary at such sharp turns, so that the envelope-forming routine will work. For this purpose, conversion tables between numeric and Gray codes for octants are desirable. @<Glob...@>= @!octant←number:array[first←octant..sixth←octant] of 1..8; @!octant←code:array[1..8] of first←octant..sixth←octant; @ @<Set init...@>= octant←code[1]:=first←octant; octant←code[2]:=second←octant; octant←code[3]:=third←octant; octant←code[4]:=fourth←octant; octant←code[5]:=fifth←octant; octant←code[6]:=sixth←octant; octant←code[7]:=seventh←octant; octant←code[8]:=eighth←octant; for k:=1 to 8 do octant←number[octant←code[k]]:=k; @ The main loop for boundary insertion deals with three consecutive nodes |p,q,r|. @<Insert octant boundaries and compute the turning number@>= turning←number:=0; p:=cur←spec; q:=link(p); repeat r:=link(q); if (right←type(p)<>right←type(q))or(q=r) then @<Insert one or more octant boundary nodes just before~|q|@>; p:=q; q:=r; until p=cur←spec; @ The |new←boundary| subroutine comes in handy at this point. It inserts a new boundary node just after a given node |p|, using a given octant code to transform the new node's coordinates. The ``transition'' fields are not computed here. @<Declare subroutines needed by |make←spec|@>= procedure new←boundary(@!p:pointer;@!octant:small←number); var @!q,@!r:pointer; {for list manipulation} begin q:=link(p); {we assume that |right←type(q)<>endpoint|} r:=get←node(knot←node←size); link(r):=q; link(p):=r; left←type(r):=left←type(q); {but possibly |left←type(q)=endpoint|} left←x(r):=left←x(q); left←y(r):=left←y(q); right←type(r):=endpoint; left←type(q):=endpoint; right←octant(r):=octant; left←octant(q):=right←type(q); unskew(x←coord(q),y←coord(q),right←type(q)); skew(cur←x,cur←y,octant); x←coord(r):=cur←x; y←coord(r):=cur←y; end; @ The case |q=r| occurs if and only if |p=q=r=cur←spec|, when we want to turn $360↑\circ$ in eight steps and then remove a solitary dead cubic. The program below happens to work in that case, but the reader isn't expected to understand why. @<Insert one or more octant boundary nodes just before~|q|@>= begin new←boundary(p,right←type(p)); s:=link(p); o1:=octant←number[right←type(p)]; o2:=octant←number[right←type(q)]; case o2-o1 of 1,-7,7,-1: goto done; 2,-6: clockwise:=false; 3,-5,4,-4,5,-3: @<Decide whether or not to go clockwise@>; 6,-2: clockwise:=true; 0:clockwise:=rev←turns; end; {there are no other cases} @<Insert additional boundary nodes, then |goto done|@>; done: if q=r then begin q:=link(q); r:=q; p:=s; link(s):=q; left←octant(q):=right←octant(q); left←type(q):=endpoint; free←node(cur←spec,knot←node←size); cur←spec:=q; end; @<Fix up the transition fields and adjust the turning number@>; end @ @<Other local variables for |make←spec|@>= @!o1,@!o2:small←number; {octant numbers} @!clockwise:boolean; {should we turn clockwise?} @!dx1,@!dy1,@!dx2,@!dy2:integer; {directions of travel at a cusp} @!dmax,@!del:integer; {temporary registers} @ A tricky question arises when a path jumps four octants. We want the direction of turning to be counterclockwise if the curve has changed direction by $180↑\circ$, or by something so close to $180↑\circ$ that the difference is probably due to rounding errors; otherwise we want to turn through an angle of less than $180↑\circ$. This decision needs to be made even when a curve seems to have jumped only three octants, since a curve may approach direction $(-1,0)$ from the fourth octant, then it might leave from direction $(+1,0)$ into the first. The following code solves the problem by analyzing the incoming direction |(dx1,dy1)| and the outgoing direction |(dx2,dy2)|. @<Decide whether or not to go clockwise@>= begin @<Compute the incoming and outgoing directions@>; unskew(dx1,dy1,right←type(p)); del:=pyth←add(cur←x,cur←y);@/ dx1:=make←fraction(cur←x,del); dy1:=make←fraction(cur←y,del); {$\cos\theta←1$ and $\sin\theta←1$} unskew(dx2,dy2,right←type(q)); del:=pyth←add(cur←x,cur←y);@/ dx2:=make←fraction(cur←x,del); dy2:=make←fraction(cur←y,del); {$\cos\theta←2$ and $\sin\theta←2$} del:=take←fraction(dx1,dy2)-take←fraction(dx2,dy1); {$\sin(\theta←2-\theta←1)$} if del>4684844 then clockwise:=false else if del<-4684844 then clockwise:=true {$2↑{28}\cdot\sin 1↑\circ\approx4684844.68$} else clockwise:=rev←turns; end @ Actually the turnarounds are clockwise, not counterclockwise, if the global variable |rev←turns| is |true|; it is usually |false|. @<Glob...@>= @!rev←turns:boolean; {should we make U-turns in the English manner?} @ @<Set init...@>= rev←turns:=false; @ @<Compute the incoming and outgoing directions@>= dx1:=x←coord(s)-left←x(s); dy1:=y←coord(s)-left←y(s); if dx1=0 then if dy1=0 then begin dx1:=x←coord(s)-right←x(p); dy1:=y←coord(s)-right←y(p); if dx1=0 then if dy1=0 then begin dx1:=x←coord(s)-x←coord(p); dy1:=y←coord(s)-y←coord(p); end; {and they {\sl can't} both be zero} end; dmax:=abs(dx1);@+if abs(dy1)>dmax then dmax:=abs(dy1); while dmax<fraction←one do begin double(dmax); double(dx1); double(dy1); end; dx2:=right←x(q)-x←coord(q); dy2:=right←y(q)-y←coord(q); if dx2=0 then if dy2=0 then begin dx2:=left←x(r)-x←coord(q); dy2:=left←y(r)-y←coord(q); if dx2=0 then if dy2=0 then begin if right←type(r)=endpoint then begin cur←x:=x←coord(r); cur←y:=y←coord(r); end else begin unskew(x←coord(r),y←coord(r),right←type(r)); skew(cur←x,cur←y,right←type(q)); end; dx2:=cur←x-x←coord(q); dy2:=cur←y-y←coord(q); end; {and they {\sl can't} both be zero} end; dmax:=abs(dx2);@+if abs(dy2)>dmax then dmax:=abs(dy2); while dmax<fraction←one do begin double(dmax); double(dx2); double(dy2); end @ @<Insert additional boundary nodes...@>= loop@+ begin if clockwise then if o1=1 then o1:=8@+else decr(o1) else if o1=8 then o1:=1@+else incr(o1); if o1=o2 then goto done; new←boundary(s,octant←code[o1]); s:=link(s); left←octant(s):=right←octant(s); end @ Now it remains to insert the redundant information into the |left←transition| and |right←transition| fields between adjacent octants, in the octant boundary nodes that have just been inserted between |link(p)| and~|q|. The turning number is easily computed from these transitions. @<Fix up the transition fields and adjust the turning number@>= p:=link(p); repeat s:=link(p); o1:=octant←number[right←octant(p)]; o2:=octant←number[left←octant(s)]; if abs(o1-o2)=1 then begin if o2<o1 then o2:=o1; if odd(o2) then right←transition(p):=axis else right←transition(p):=diagonal; end else begin if o1=8 then incr(turning←number)@+else decr(turning←number); right←transition(p):=axis; end; left←transition(s):=right←transition(p); p:=s; until p=q @* \[22] Filling a contour. Given the low-level machinery for making moves and for transforming a cyclic path into a cycle spec, we're almost able to fill a digitized path. All we need is a high-level routine that walks through the cycle spec and controls the overall process. Our overall goal is to plot the integer points $\bigl(\round(x(t)), \round(y(t))\bigr)$ and to connect them by rook moves, assuming that $\round(x(t))$ and $\round(y(t))$ don't both jump simultaneously from one integer to another as $t$~varies; these rook moves will be the edge of the contour that will be filled. We have reduced this problem to the case of curves that travel in first octant directions, i.e., curves such that $0\L y'(t)\L x'(t)$, by transforming the original coordinates. \def\xtilde{{\tilde x}} \def\ytilde{{\tilde y}} Another transformation makes the problem still simpler. We shall say that we are working with {\sl biased coordinates\/} when $(x,y)$ has been replaced by $(\xtilde,\ytilde)=(x-y,y+{1\over2})$. When a curve travels in first octant directions, the corresponding curve with biased coordinates travels in first {\sl quadrant\/} directions; the latter condition is symmetric in $x$ and~$y$, so it has advantages for the design of algorithms. The |make←spec| routine gives us skewed coordinates $(x-y,y)$, hence we obtain biased coordinates by simply adding $1\over2$ to the second component. The most important fact about biased coordinates is that we can determine the rounded unbiased path $\bigl(\round(x(t)),\round(y(t))\bigr)$ from the truncated biased path $\bigl(\lfloor\xtilde(t)\rfloor,\lfloor\ytilde(t)\rfloor \bigr)$ and information about the initial and final endpoints. If the unrounded and unbiased path begins at $(x←0,y←0)$ and ends at $(x←1,y←1)$, it's possible to prove (by induction on the length of truncated biased path) that the rounded unbiased path is obtained by the following construction: \yskip\textindent{1)} Start at $\bigl(\round(x←0),\round(y←0)\bigr)$. \yskip\textindent{2)} If $(x←0+{1\over2})\bmod1\G(y←0+{1\over2})\bmod1$, move one step right. \yskip\textindent{3)} Whenever the path $\bigl(\lfloor\xtilde(t)\rfloor,\lfloor\ytilde(t)\rfloor\bigr)$ takes an upward step (i.e., when $\lfloor\xtilde(t+\epsilon)\rfloor=\lfloor\xtilde(t)\rfloor$ and $\lfloor\ytilde(t+\epsilon)\rfloor=\lfloor\ytilde(t)\rfloor+1$), move one step up and then one step right. \yskip\textindent{4)} Whenever the path $\bigl(\lfloor\xtilde(t)\rfloor,\lfloor\ytilde(t)\rfloor\bigr)$ takes a rightward step (i.e., when $\lfloor\xtilde(t+\epsilon)\rfloor=\lfloor\xtilde(t)\rfloor+1$ and $\lfloor\ytilde(t+\epsilon)\rfloor=\lfloor\ytilde(t)\rfloor$), move one step right. \yskip\textindent{5)} Finally, if $(x←1+{1\over2})\bmod1\G(y←1+{1\over2})\bmod1$, move one step left (thereby cancelling the previous move, which was one step right). You will now be at the point $\bigl(\round(x←1),\round(y←1)\bigr)$. @ In order to validate the assumption that $\round(x(t))$ and $\round(y(t))$ don't both jump simultaneously, we shall consider that a coordinate pair $(x,y)$ actually represents $(x+\epsilon,y+\epsilon\delta)$, where $\epsilon$ and $\delta$ are extremely small positive numbers---so small that their precise values never matter. This convention makes rounding unambiguous, since there is always a unique integer point nearest to any given scaled numbers~$(x,y)$. When coordinates are transformed so that \MF\ needs to work only in ``first octant'' directions, the transformations involve negating~$x$, negating~$y$, and/or interchanging $x$ with~$y$. Corresponding adjustments to the rounding conventions must be made so that consistent values will be obtained. For example, suppose that we're working with coordinates that have been transformed so that a third-octant curve travels in first-octant directions. The skewed coordinates $(x,y)$ in our data structure represent unskewed coordinates $(-y,x+y)$, which are actually $-y+\epsilon, x+y+\epsilon\delta$. We should therefore round as if our skewed coordinates were $(x+\epsilon+\epsilon\delta,y-\epsilon)$ instead of $(x,y)$. The following table shows how the skewed coordinates should be perturbed when rounding decisions are made: $$\vcenter{\halign{#\hfil&&\quad$#$\hfil&\hskip4em#\hfil\cr |first←octant|&(x+\epsilon-\epsilon\delta,y+\epsilon\delta)& |fifth←octant|&(x-\epsilon+\epsilon\delta,y-\epsilon\delta)\cr |second←octant|&(x-\epsilon+\epsilon\delta,y+\epsilon)& |sixth←octant|&(x+\epsilon-\epsilon\delta,y-\epsilon)\cr |third←octant|&(x+\epsilon+\epsilon\delta,y-\epsilon)& |seventh←octant|&(x-\epsilon-\epsilon\delta,y+\epsilon)\cr |fourth←octant|&(x-\epsilon-\epsilon\delta,y+\epsilon\delta)& |eighth←octant|&(x+\epsilon+\epsilon\delta,y-\epsilon\delta)\cr}}$$ Four small arrays are set up so that the rounding operations will be fairly easy in any given octant. @<Glob...@>= @!y←corr,@!xy←corr,@!z←corr:array[first←octant..sixth←octant] of 0..1; @!x←corr:array[first←octant..sixth←octant] of -1..1; @ Here |xy←corr| is 1 if and only if the $x$ component of a skewed coordinate is to be decreased by an infinitesimal amount; |y←corr| is similar, but for the $y$ components. The other tables are set up so that the condition $$(x+y+|half←unit|)\bmod|unity|\G(y+|half←unit|)\bmod|unity|$$ is properly perturbed to the condition $$(x+y+|half←unit|-|x←corr|-|y←corr|)\bmod|unity|\G (y+|half←unit|-|y←corr|)\bmod|unity|+|z←corr|.$$ @<Set init...@>= x←corr[first←octant]:=0; y←corr[first←octant]:=0; xy←corr[first←octant]:=0;@/ x←corr[second←octant]:=0; y←corr[second←octant]:=0; xy←corr[second←octant]:=1;@/ x←corr[third←octant]:=-1; y←corr[third←octant]:=1; xy←corr[third←octant]:=0;@/ x←corr[fourth←octant]:=1; y←corr[fourth←octant]:=0; xy←corr[fourth←octant]:=1;@/ x←corr[fifth←octant]:=0; y←corr[fifth←octant]:=1; xy←corr[fifth←octant]:=1;@/ x←corr[sixth←octant]:=0; y←corr[sixth←octant]:=1; xy←corr[sixth←octant]:=0;@/ x←corr[seventh←octant]:=1; y←corr[seventh←octant]:=0; xy←corr[seventh←octant]:=1;@/ x←corr[eighth←octant]:=-1; y←corr[eighth←octant]:=1; xy←corr[eighth←octant]:=0;@/ for k:=1 to 8 do z←corr[k]:=xy←corr[k]-x←corr[k]; @ Here's a procedure that handles the details of rounding at the endpoints: Given skewed coordinates |(x,y)|, it sets |(m1,n1)| to the corresponding rounded lattice points, taking the current |octant| into account. Global variable |d1| is also set to 1 if $(x+y+{1\over2})\bmod1\G(y+{1\over2})\bmod1$. @p procedure end←round(@!x,@!y:scaled); begin y:=y+half←unit-y←corr[octant]; x:=x+y-x←corr[octant]; m1:=floor←unscaled(x); n1:=floor←unscaled(y); if x-unity*m1>=y-unity*n1+z←corr[octant] then d1:=1@+else d1:=0; end; @ The outputs |(m1,n1,d1)| of |end←round| will sometimes be moved to |(m0,n0,d0)|. @<Glob...@>= @!m0,@!n0,@!m1,@!n1:integer; {lattice point coordinates} @!d0,@!d1:0..1; {displacement corrections} @ We're ready now to fill the pixels enclosed by a given cycle spec~|h|; the knot list that represents the cycle is destroyed in the process. The edge structure that gets all the resulting data is |cur←edges|, and the edges are weighted by |cur←wt|. @p procedure fill←spec(@!h:pointer); var @!p,@!q,@!r,@!s:pointer; {for list traversal} begin if internal[tracing←edges]>0 then begin←edge←tracing; p:=h; {we assume that |left←type(h)=endpoint|} repeat octant:=left←octant(p); @<Set variable |q| to the node at the end of the current octant@>; if q<>p then begin @<Determine the starting and ending lattice points |(m0,n0)| and |(m1,n1)|@>; @<Make the moves for the current octant@>; move←to←edges(m0,n0,m1,n1); end; p:=link(q); until p=h; toss←knot←list(h); if internal[tracing←edges]>0 then end←edge←tracing; end; @ @<Set variable |q| to the node at the end of the current octant@>= q:=p; while right←type(q)<>endpoint do q:=link(q) @ @<Determine the starting and ending lattice points |(m0,n0)| and |(m1,n1)|@>= end←round(x←coord(p),y←coord(p)); m0:=m1; n0:=n1; d0:=d1;@/ end←round(x←coord(q),y←coord(q)) @ Here we perform the five-step process that was explained at the very beginning of this part of the program. @<Make the moves for the current octant@>= if n1-n0>=move←size then overflow("move table size",move←size); @:METAFONT capacity exceeded move table size}{\quad move table size@> move[0]:=d0; move←ptr:=0; r:=p; repeat s:=link(r);@/ make←moves(x←coord(r),right←x(r),left←x(s),x←coord(s),@| y←coord(r)+half←unit,right←y(r)+half←unit,left←y(s)+half←unit, y←coord(s)+half←unit,@| xy←corr[octant],y←corr[octant]); r:=s; until r=q; move[move←ptr]:=move[move←ptr]-d1; if internal[smoothing]>0 then smooth←moves(0,move←ptr) @* \[23] Polygonal pens. The next few parts of the program deal with the additional complications associated with ``envelopes,'' leading up to an algorithm that fills a contour with respect to a pen whose boundary is a convex polygon. The mathematics underlying this algorithm is based on simple aspects of the theory of tracings developed by Leo Guibas, Lyle Ramshaw, and Jorge Stolfi [``A kinetic framework for computational geometry,'' {\sl Proc.\ IEEE Symp.\ Foundations of Computer Science\/ \bf24} (1983), 100--111]. @↑Guibas, Leonidas Ioannis@> @↑Ramshaw, Lyle Harold@> @↑Stolfi, Jorge@> If the vertices of the polygon are $w←0$, $w←1$, \dots, $w←{n-1}$, $w←n=w←0$, in counterclockwise order, the convexity condition requires that ``left turns'' are made at each vertex when a person proceeds from $w←0$ to $w←1$ to $\cdots$ to~$w←n$. The envelope is obtained if we offset a given curve $z(t)$ by $w←k$ when that curve is traveling in a direction $z'(t)$ lying between the directions $w←k-w←{k-1}$ an $w\k-w←k$. At times~$t$ when the curve direction $z'(t)$ increases past $w\k-w←k$, we temporarily stop plotting the offset curve and we insert a straight line from $z(t)+w←k$ to $z(t)+w\k$; notice that this straight line is tangent to the offset curve. Similarly, when the curve direction decreases past $w←k-w←{k-1}$, we stop plotting and insert a straight line from $z(t)+w←k$ to $z(t)+w←{k-1}$; the latter line is actually a ``retrograde'' step, which won't be part of the final envelope under \MF's assumptions. The result of this construction is a continuous path that consists of alternating curves and straight line segments. The segments are usually so short, in practice, that they blend with the curves; after all, it's possible to represent any digitized path as a sequence of digitized straight lines. The nicest feature of this approach to envelopes is that it blends perfectly with the octant subdivision process we have already developed. The envelope travels in the same direction as the curve itself, as we plot it, and we need merely be careful what offset is being added. Retrograde motion presents a problem, but we will see that there is a decent way to handle it. @ We shall represent pens by maintaining eight lists of offsets, one for each octant direction. The offsets at the boundary points where a curve turns into a new octant will appear in the lists for both octants. This means that we can restrict consideration to segments of the original polygon whose directions aim in the first octant, as we have done in the simpler case when envelopes were not required. An example should help to clarify this situation: Consider the quadrilateral whose vertices are $w←0=(0,-1)$, $w←1=(3,-1)$, $w←2=(6,1)$, and $w←3=(1,2)$. A curve that travels in the first octant will be offset by $w←1$ or $w←2$, unless its slope drops to zero enroute to the eighth octant; in the latter case we should switch to $w←0$ as we cross the octant boundary. Our list for the first octant will contain the three offsets $w←0$, $w←1$,~$w←2$. By convention we will duplicate a boundary offset if the angle between octants doesn't explicitly appear; in this case there is no explicit line of slope~1 at the end of the list, so the full list is $$w←0\;w←1\;w←2\;w←2\;=\;(0,-1)\;(3,-1)\;(6,1)\;(6,1).$$ With skewed coordinates $(u-v,v)$ instead of $(u,v)$ we obtain the list $$w←0\;w←1\;w←2\;w←2\;\mapsto\;(1,-1)\;(4,-1)\;(5,1)\;(5,1),$$ which is what actually appears in the data structure. In the second octant there's only one offset; we list it three times (with coordinates interchanged, so as to make the second octant look like the first), and skew those coordinates, obtaining $$\tabskip\centering \halign to\hsize{$\hfil#\;\mapsto\;{}$\tabskip=0pt& $#\hfil$&\quad in the #\hfil\tabskip\centering\cr w←2\;w←2\;w←2&(-5,6)\;(-5,6)\;(-5,6)\cr \noalign{\vskip\belowdisplayskip \vbox{\noindent\strut as the list of transformed and skewed offsets to use when curves that travel in the second octant. Similarly, we will have\strut} \vskip\abovedisplayskip} w←2\;w←2\;w←2&(7,-6)\;(7,-6)\;(7,-6)&third;\cr w←2\;w←2\;w←3\;w←3&(-7,1)\;(-7,1)\;(-3,2)\;(-3,2)&fourth;\cr w←3\;w←3\;w←3&(3,-2)\;(3,-2)\;(3,-2)&fifth;\cr w←3\;w←3\;w←0\;w←0&(-3,1)\;(-3,1)\;(1,0)\;(1,0)&sixth;\cr w←0\;w←0\;w←0&(1,0)\;(1,0)\;(1,0)&seventh;\cr w←0\;w←0\;w←0&(-1,1)\;(-1,1)\;(-1,1)&eighth.\cr}$$ Notice that $w←1$ is considered here to be internal to the first octant; it's not part of the eighth. We could equally well have taken $w←0$ out of the first octant list and put it into the eighth; then the first octant list would have been $$w←1\;w←1\;w←2\;w←2\;\mapsto\;(4,-1)\;(4,-1)\;(5,1)\;(5,1)$$ and the eighth octant list would have been $$w←0\;w←0\;w←1\;\mapsto\;(-1,1)\;(-1,1)\;(2,1).$$ Actually, there's one more complication: The order of offsets is reversed in even-numbered octants, because the transformation of coordinates has reversed counterclockwise and clockwise orientations in those octants. The offsets in the fourth octant, for example, are really $w←3$, $w←3$, $w←2$,~$w←2$, not $w←2$, $w←2$, $w←3$,~$w←3$. @ In general, the list of offsets for an octant will have the form $$w←0\;\;w←1\;\;\ldots\;\;w←n\;\;w←{n+1}$$ (if we renumber the subscripts in each list), where $w←0$ and $w←{n+1}$ are offsets common to the neighboring lists. We'll often have $w←0=w←1$ and/or $w←n=w←{n+1}$, but the other $w$'s will be distinct. Curves that travel between slope~0 and direction $w←2-w←1$ will use offset~$w←1$; curves that travel between directions $w←k-w←{k-1}$ and $w\k-w←k$ will use offset~$w←k$, for $1<k<n$; curves between direction $w←n-w←{n-1}$ and slope~1 (actually slope~$\infty$ after skewing) will use offset~$w←n$. In even-numbered octants, the directions are actually $w←k-w\k$ instead of $w\k-w←k$, because the offsets have been listed in reverse order. Each offset $w←k$ is represented by skewed coordinates $(u←k-v←k,v←k)$, where $(u←k,v←k)$ is the representation of $w←k$ after it has been rotated into a first-octant disguise. @ The top-level data structure of a pen polygon is a 10-word node containing a reference count followed by pointers to the eight pen lists, followed by an indication of the pen's range of values. If |p|~points to such a node, and if the offset list for, say, the fourth octant has entries $w←0$, $w←1$, \dots, $w←n$,~$w←{n+1}$, then |info(p+fourth←octant)| will equal~$n$, and |link(p+fourth←octant)| will point to the offset node containing~$w←0$. Memory location |p+fourth←octant| is said to be the {\sl header\/} of the pen-offset list for the fourth octant. Since this is an even-numbered octant, $w←0$ is the offset that goes with the fifth octant, and $w←{n+1}$ goes with the third. The elements of the offset list themselves are doubly linked 3-word nodes, containing coordinates in their |x←coord| and |y←coord| fields. The two link fields are called |link| and |knil|; if |w|~points to the node for~$w←k$, then |link(w)| and |knil(w)| point respectively to the nodes for $w\k$ and~$w←{k-1}$. If |h| is the list header, |link(h)| points to the node for~$w←0$ and |knil(link(h))| to the node for~$w←{n+1}$. The tenth word of a pen header node contains the maximum absolute value of an $x$ or $y$ coordinate among all of the unskewed pen offsets. The |link| field of a pen header node should be |null| if and only if the pen has no offsets. @d pen←node←size=10 @d coord←node←size=3 @d max←offset(#)==mem[#+9].sc @ The |print←pen| subroutine illustrates these conventions by reconstructing the vertices of a polygon from \MF's complicated internal offset representation. @<Declare subroutines for printing expressions@>= procedure print←pen(@!p:pointer;@!s:str←number;@!nuline:boolean); var @!nothing←printed:boolean; {has there been any action yet?} @!k:1..8; {octant number} @!h:pointer; {offset list head} @!m,@!n:integer; {offset indices} @!w,@!ww:pointer; {pointers that traverse the offset list} begin print←diagnostic("Pen polygon",s,nuline); nothing←printed:=true; print←ln; for k:=1 to 8 do begin octant:=octant←code[k]; h:=p+octant; n:=info(h); w:=link(h); if not odd(k) then w:=knil(w); {in even octants, start at $w←{n+1}$} for m:=1 to n+1 do begin if odd(k) then ww:=link(w)@+else ww:=knil(w); if (x←coord(ww)<>x←coord(w))or(y←coord(ww)<>y←coord(w)) then @<Print the unskewed and unrotated coordinates of node |ww|@>; w:=ww; end; end; if nothing←printed then begin w:=link(p+first←octant); print←two(x←coord(w)+y←coord(w),y←coord(w)); end; print←nl(" .. cycle"); end←diagnostic(true); end; @ @<Print the unskewed and unrotated coordinates of node |ww|@>= begin if nothing←printed then nothing←printed:=false else print←nl(" .. "); print←two←true(x←coord(ww),y←coord(ww)); end @ A null pen polygon, which has just one vertex $(0,0)$, is predeclared for error recovery. It doesn't need a proper reference count, because the |toss←pen| procedure below will never delete it from memory. @<Initialize table entries...@>= ref←count(null←pen):=null; link(null←pen):=null;@/ info(null←pen+1):=1; link(null←pen+1):=null←coords; for k:=null←pen+2 to null←pen+8 do mem[k]:=mem[null←pen+1]; max←offset(null←pen):=0;@/ link(null←coords):=null←coords; knil(null←coords):=null←coords;@/ x←coord(null←coords):=0; y←coord(null←coords):=0; @ Here's a trivial subroutine that inserts a copy of an offset on the |link| side of its clone in the doubly linked list. @p procedure dup←offset(@!w:pointer); var @!r:pointer; {the new node} begin r:=get←node(coord←node←size); x←coord(r):=x←coord(w); y←coord(r):=y←coord(w); link(r):=link(w); knil(link(w)):=r; knil(r):=w; link(w):=r; end; @ The following algorithm is somewhat more interesting: It converts a knot list for a cyclic path into a pen polygon, ignoring everything but the |x←coord|, |y←coord|, and |link| fields. If the given path vertices do not define a convex polygon, an error message is issued and the null pen is returned. @p function make←pen(@!h:pointer):pointer; label done,done1,not←found,found; var @!o,@!oo,@!k:small←number; {octant numbers---old, new, and current} @!p:pointer; {top-level node for the new pen} @!q,@!r,@!s,@!w,@!hh:pointer; {for list manipulation} @!n:integer; {offset counter} @!dx,@!dy:scaled; {polygon direction} @!mc:scaled; {the largest coordinate} begin @<Stamp all nodes with an octant code, compute the maximum offset, and set |hh| to the node that begins the first octant; |goto not←found| if there's a problem@>; if mc>=fraction←one-half←unit then goto not←found; p:=get←node(pen←node←size); q:=hh; max←offset(p):=mc; ref←count(p):=null; if link(q)<>q then link(p):=null+1; for k:=1 to 8 do @<Construct the offset list for the |k|th octant@>; goto found; not←found:p:=null←pen; @<Complain about a bad pen path@>; found: if internal[tracing←pens]>0 then print←pen(p," (newly created)",true); make←pen:=p; end; @ @<Complain about a bad pen path@>= if mc>=fraction←one-half←unit then begin print←err("Pen too large"); @.Pen too large@> help2("The cycle you specified has a coordinate of 4095.5 or more.")@/ ("So I've replaced it by the trivial path `(0,0)..cycle'.");@/ end else begin print←err("Pen cycle must be convex"); @.Pen cycle must be convex@> help3("The cycle you specified either has consecutive equal points")@/ ("or turns right or turns through more than 360 degrees.")@/ ("So I've replaced it by the trivial path `(0,0)..cycle'.");@/ end; put←get←error @ There should be exactly one node whose octant number is less than its predecessor in the cycle; that is node~|hh|. The loop here will terminate in all cases, but the proof is somewhat tricky: If there are at least two distinct $y$~coordinates in the cycle, we will have |o>4| and |o<=4| at different points of the cycle. Otherwise there are at least two distinct $x$~coordinates, and we will have |o>2| somewhere, |o<=2| somewhere. @<Stamp all nodes...@>= q:=h; r:=link(q); mc:=abs(x←coord(h)); if q=r then begin hh:=h; right←type(h):=0; {this trick is explained below} if mc<abs(y←coord(h)) then mc:=abs(y←coord(h)); end else begin o:=0; hh:=null; loop@+ begin s:=link(r); if mc<abs(x←coord(r)) then mc:=abs(x←coord(r)); if mc<abs(y←coord(r)) then mc:=abs(y←coord(r)); dx:=x←coord(r)-x←coord(q); dy:=y←coord(r)-y←coord(q); if dx=0 then if dy=0 then goto not←found; {double point} if ab←vs←cd(dx,y←coord(s)-y←coord(r),dy,x←coord(s)-x←coord(r))<0 then goto not←found; {right turn} @<Determine the octant code for direction |(dx,dy)|@>; right←type(q):=octant; oo:=octant←number[octant]; if o>oo then begin if hh<>null then goto not←found; {$>360↑\circ$} hh:=q; end; o:=oo; if (q=h)and(hh<>null) then goto done; q:=r; r:=s; end; done:end @ We want the octant for |(-dx,-dy)| to be exactly opposite the octant for |(dx,dy)|. @<Determine the octant code for direction |(dx,dy)|@>= if dx>0 then octant:=first←octant else if dx=0 then if dy>0 then octant:=first←octant@+else octant:=first←octant+negate←x else begin negate(dx); octant:=first←octant+negate←x; end; if dy<0 then begin negate(dy); octant:=octant+negate←y; end else if dy=0 then if octant>first←octant then octant:=first←octant+negate←x+negate←y; if dx<dy then octant:=octant+switch←x←and←y @ Now |q| points to the node that the present octant shares with the previous octant, and |right←type(q)| is the octant code during which |q|~should advance. We have set |right←type(q)=0| in the special case that |q| should never advance (because the pen is degenerate). The number of offsets |n| must be smaller than |max←quarterword|, because the |fill←envelope| routine stores |n+1| in the |right←type| field of a knot node. @<Construct the offset list...@>= begin octant:=octant←code[k]; n:=0; h:=p+octant; loop@+ begin r:=get←node(coord←node←size); skew(x←coord(q),y←coord(q),octant); x←coord(r):=cur←x; y←coord(r):=cur←y; if n=0 then link(h):=r else @<Link node |r| to the previous node@>; w:=r; if right←type(q)<>octant then goto done1; q:=link(q); incr(n); end; done1: @<Finish linking the offset nodes, and duplicate the borderline offset nodes if necessary@>; if n>=max←quarterword then overflow("pen polygon size",max←quarterword); @:METAFONT capacity exceeded pen polygon size}{\quad pen polygon size@> info(h):=n; end @ Now |w| points to the node that was inserted most recently, and |k| is the current octant number. @<Link node |r| to the previous node@>= if odd(k) then begin link(w):=r; knil(r):=w; end else begin knil(w):=r; link(r):=w; end @ We have inserted |n+1| nodes; it remains to duplicate the nodes at the ends, if slopes 0 and~$\infty$ aren't already represented. At the end of this section the total number of offset nodes should be |n+2| (since we call them $w←0$, $w←1$, \dots,~$w←{n+1}$). @<Finish linking the offset nodes, and duplicate...@>= r:=link(h); if odd(k) then begin link(w):=r; knil(r):=w; end else begin knil(w):=r; link(r):=w; link(h):=w; r:=w; end; if (y←coord(r)<>y←coord(link(r)))or(n=0) then begin dup←offset(r); incr(n); end; r:=knil(r); if x←coord(r)<>x←coord(knil(r)) then dup←offset(r) else decr(n) @ Conversely, |make←path| goes back from a pen to a cyclic path that might have generated it. The structure of this subroutine is essentially the same as |print←pen|. @p @t\4@>@<Declare the function called |trivial←knot|@>@; function make←path(@!pen←head:pointer):pointer; var @!p:pointer; {the most recently copied knot} @!k:1..8; {octant number} @!h:pointer; {offset list head} @!m,@!n:integer; {offset indices} @!w,@!ww:pointer; {pointers that traverse the offset list} begin p:=temp←head; for k:=1 to 8 do begin octant:=octant←code[k]; h:=pen←head+octant; n:=info(h); w:=link(h); if not odd(k) then w:=knil(w); {in even octants, start at $w←{n+1}$} for m:=1 to n+1 do begin if odd(k) then ww:=link(w)@+else ww:=knil(w); if (x←coord(ww)<>x←coord(w))or(y←coord(ww)<>y←coord(w)) then @<Copy the unskewed and unrotated coordinates of node |ww|@>; w:=ww; end; end; if p=temp←head then begin w:=link(pen←head+first←octant); p:=trivial←knot(x←coord(w)+y←coord(w),y←coord(w)); link(temp←head):=p; end; link(p):=link(temp←head); make←path:=link(temp←head); end; @ @<Copy the unskewed and unrotated coordinates of node |ww|@>= begin unskew(x←coord(ww),y←coord(ww),octant); link(p):=trivial←knot(cur←x,cur←y); p:=link(p); end @ @<Declare the function called |trivial←knot|@>= function trivial←knot(@!x,@!y:scaled):pointer; var @!p:pointer; {a new knot for explicit coordinates |x| and |y|} begin p:=get←node(knot←node←size); left←type(p):=explicit; right←type(p):=explicit;@/ x←coord(p):=x; left←x(p):=x; right←x(p):=x;@/ y←coord(p):=y; left←y(p):=y; right←y(p):=y;@/ trivial←knot:=p; end; @ That which can be created can be destroyed. @d add←pen←ref(#)==incr(ref←count(#)) @d delete←pen←ref(#)==if ref←count(#)=null then toss←pen(#) else decr(ref←count(#)) @<Declare the recycling subroutines@>= procedure toss←pen(@!p:pointer); var @!k:1..8; {relative header locations} @!w,@!ww:pointer; {pointers to offset nodes} begin if p<>null←pen then begin for k:=1 to 8 do begin w:=link(p+k); repeat ww:=link(w); free←node(w,coord←node←size); w:=ww; until w=link(p+k); end; free←node(p,pen←node←size); end; end; @ The |find←offset| procedure sets |(cur←x,cur←y)| to the offset associated with a given direction~|(x,y)| and a given pen~|p|. If |x=y=0|, the result is |(0,0)|. If two different offsets apply, one of them is chosen arbitrarily. @p procedure find←offset(@!x,@!y:scaled; @!p:pointer); label done,exit; var @!octant:first←octant..sixth←octant; {octant code for |(x,y)|} @!s:-1..+1; {sign of the octant} @!n:integer; {number of offsets remaining} @!h,@!w,@!ww:pointer; {list traversal registers} begin @<Compute the octant code; skew and rotate the coordinates |(x,y)|@>; if odd(octant←number[octant]) then s:=-1@+else s:=+1; h:=p+octant; w:=link(link(h)); ww:=link(w); n:=info(h); while n>1 do begin if ab←vs←cd(x,y←coord(ww)-y←coord(w),@| y,x←coord(ww)-x←coord(w))<>s then goto done; w:=ww; ww:=link(w); decr(n); end; done:unskew(x←coord(w),y←coord(w),octant); exit:end; @ @<Compute the octant code; skew and rotate the coordinates |(x,y)|@>= if x>0 then octant:=first←octant else if x=0 then if y<=0 then if y=0 then begin cur←x:=0; cur←y:=0; return; end else octant:=first←octant+negate←x else octant:=first←octant else begin x:=-x; if y=0 then octant:=first←octant+negate←x+negate←y else octant:=first←octant+negate←x; end; if y<0 then begin octant:=octant+negate←y; y:=-y; end; if x>=y then x:=x-y else begin octant:=octant+switch←x←and←y; x:=y-x; y:=y-x; end @* \[24] Filling an envelope. We are about to reach the culmination of \MF's digital plotting routines: Almost all of the previous algorithms will be brought of bear on \MF's most difficult task, which is to fill the envelope of a given cyclic path with respect to a given pen polygon. But we still must complete some of the preparatory work before taking such a big plunge. @ Given a pointer |c| to a nonempty list of cubics, and a pointer~|h| to the header information of a pen polygon segment, the |offset←prep| routine changes the list into cubics that are associated with particular pen offsets. Namely, the cubic between |p| and~|q| should be associated with the |k|th offset when |right←type(p)=k|. List |c| is actually part of a cycle spec, so it terminates at the first node whose |right←type| is |endpoint|. The cubics all have monotone-nondecreasing $x'(t)$ and $y'(t)$. @p @t\4@>@<Declare subroutines needed by |offset←prep|@>@; procedure offset←prep(@!c,@!h:pointer); label done,not←found; var @!n:halfword; {the number of pen offsets} @!p,@!q,@!r,@!lh,@!ww:pointer; {for list manipulation} @!k:halfword; {the current offset index} @!w:pointer; {a pointer to offset $w←k$} @<Other local variables for |offset←prep|@>@; begin p:=c; n:=info(h); lh:=link(h); {now |lh| points to $w←0$} while right←type(p)<>endpoint do begin q:=link(p); @<Split the cubic between |p| and |q|, if necessary, into cubics associated with single offsets, after which |q| should point to the end of the final such cubic@>; @<Advance |p| to node |q|, removing any ``dead'' cubics that might have been introduced by the splitting process@>; end; end; @ @<Advance |p| to node |q|, removing any ``dead'' cubics...@>= repeat r:=link(p); if x←coord(p)=right←x(p) then if y←coord(p)=right←y(p) then if x←coord(p)=left←x(r) then if y←coord(p)=left←y(r) then if x←coord(p)=x←coord(r) then if y←coord(p)=y←coord(r) then begin remove←cubic(p); if r=q then q:=p; r:=p; end; p:=r; until p=q @ The splitting process uses a subroutine like |split←cubic|, but (for ``bulletproof'' operation) we check to make sure that the resulting (skewed) coordinates satisfy $\Delta x\G0$ and $\Delta y\G0$ after splitting; |make←spec| has made sure that these relations hold before splitting. @<Declare subroutines needed by |offset←prep|@>= procedure split←for←offset(@!p:pointer;@!t:fraction); var @!q:pointer; {the successor of |p|} @!r:pointer; {the new node} begin q:=link(p); split←cubic(p,t,x←coord(q),y←coord(q)); r:=link(p); if y←coord(r)<y←coord(p) then y←coord(r):=y←coord(p) else if y←coord(r)>y←coord(q) then y←coord(r):=y←coord(q); if x←coord(r)<x←coord(p) then x←coord(r):=x←coord(p) else if x←coord(r)>x←coord(q) then x←coord(r):=x←coord(q); end; @ If the pen polygon has |n| offsets, and if $w←k=(u←k,v←k)$ is the $k$th of these, the $k$th pen slope is defined by the formula $$s←k={v\k-v←k\over u\k-u←k},\qquad\hbox{for $0<k<n$}.$$ In odd-numbered octants, the numerator and denominator of this fraction will be positive; in even-numbered octants they will both be negative. Furthermore we always have $0=s←0<s←1<\cdots<s←n=\infty$. The goal of |offset←prep| is to find an offset index~|k| to associate with each cubic, such that the slope $s(t)$ of the cubic satisfies $$s←{k-1}<=s(t)<=s←k\qquad\hbox{for $0<=t<=1$.}\eqno(*)$$ We may have to split a cubic into as many as $2n-1$ pieces before each piece corresponds to a unique offset. @<Split the cubic between |p| and |q|, if necessary, into cubics...@>= if n<=1 then right←type(p):=1 {this case is easy} else begin @<Prepare for derivative computations; |goto not←found| if the current cubic is dead@>; @<Find the initial slope, |dy/dx|@>; if dx=0 then @<Handle the special case of infinite slope@> else begin @<Find the index |k| such that $s←{k-1}\L\\{dy}/\\{dx}<s←k$@>; @<Complete the offset splitting process@>; end; not←found: end @ The slope of a cubic $B(z←0,z←1,z←2,z←3;t)=\bigl(x(t),y(t)\bigr)$ can be calculated from the quadratic polynomials ${1\over3}x'(t)=B(x←1-x←0,x←2-x←1,x←3-x←2;t)$ and ${1\over3}y'(t)=B(y←1-y←0,y←2-y←1,y←3-y←2;t)$. Since we may be calculating slopes from several cubics split from the current one, it is desirable to do these calculations without losing too much precision. ``Scaled up'' values of the derivatives, which will be less tainted by accumulated errors than derivatives found from the cubics themselves, are maintained in local variables |x0|, |x1|, and |x2|, representing $X←0=2↑l(x←1-x←0)$, $X←1=2↑l(x←2-x←1)$, and $X←2=2↑l(x←3-x←2)$; similarly |y0|, |y1|, and~|y2| represent $Y←0=2↑l(y←1-y←0)$, $Y←1=2↑l(y←2-y←1)$, and $Y←2=2↑l(y←3-y←2)$. To test whether the slope of the cubic is $>=s$ or $<=s$, we will test the sign of the quadratic ${1\over3}2↑l\bigl(y'(t)-sx'(t)\bigr)$ if $s<=1$, or ${1\over3}2↑l\bigl(y'(t)/s-x'(t)\bigr)$ if $s>1$. @<Other local variables for |offset←prep|@>= @!x0,@!x1,@!x2,@!y0,@!y1,@!y2:integer; {representatives of derivatives} @!t0,@!t1,@!t2:integer; {coefficients of polynomial for slope testing} @!du,@!dv,@!dx,@!dy:integer; {for slopes of the pen and the curve} @!max←coef:integer; {used while scaling} @!x0a,@!x1a,@!x2a,@!y0a,@!y1a,@!y2a:integer; {intermediate values} @!t:fraction; {where the derivative passes through zero} @!s:fraction; {slope or reciprocal slope} @ @<Prepare for derivative computations...@>= x0:=right←x(p)-x←coord(p); {should be |>=0|} x2:=x←coord(q)-left←x(q); {likewise} x1:=left←x(q)-right←x(p); {but this might be negative} y0:=right←y(p)-y←coord(p); y2:=y←coord(q)-left←y(q); y1:=left←y(q)-right←y(p); max←coef:=abs(x0); {we take |abs| just to make sure} if abs(x1)>max←coef then max←coef:=abs(x1); if abs(x2)>max←coef then max←coef:=abs(x2); if abs(y0)>max←coef then max←coef:=abs(y0); if abs(y1)>max←coef then max←coef:=abs(y1); if abs(y2)>max←coef then max←coef:=abs(y2); if max←coef=0 then goto not←found; while max←coef<fraction←one do begin double(max←coef); double(x0); double(x1); double(x2); double(y0); double(y1); double(y2); end @ Let us first solve a special case of the problem: Suppose we know an index~$k$ such that either (i)~$s(t)\G s←{k-1}$ for all~$t$ and $s(0)<s←k$, or (ii)~$s(t)\L s←k$ for all~$t$ and $s(0)>s←{k-1}$. Then, in a sense, we're halfway done, since one of the two inequalities in $(*)$ is satisfied, and the other couldn't be satisfied for any other value of~|k|. The |fin←offset←prep| subroutine solves the stated subproblem. It has a boolean parameter called |rising| that is |true| in case~(i), |false| in case~(ii). When |rising=false|, parameters |x0| through |y2| represent the negative of the derivative of the cubic following |p|; otherwise they represent the actual derivative. The |w| parameter should point to offset~$w←k$. @<Declare subroutines needed by |offset←prep|@>= procedure fin←offset←prep(@!p:pointer;@!k:halfword;@!w:pointer; @!x0,@!x1,@!x2,@!y0,@!y1,@!y2:integer;@!rising:boolean;@!n:integer); label exit; var @!q,@!ww:pointer; {for list manipulation} @!du,@!dv:scaled; {for slope calculation} @!t0,@!t1,@!t2:integer; {test coefficients} @!t:fraction; {place where the derivative passes a critical slope} @!s:fraction; {slope or reciprocal slope} @!v:integer; {intermediate value for updating |x0..y2|} begin loop begin q:=link(p); right←type(p):=k; if rising then if k=n then return else ww:=link(w) {a pointer to $w\k$} else if k=1 then return else ww:=knil(w); {a pointer to $w←{k-1}$} @<Compute test coefficients |(t0,t1,t2)| for $s(t)$ versus $s←k$ or $s←{k-1}$@>; t:=crossing←point(t0,t1,t2); if t>=fraction←one then return; @<Split the cubic at $t$, and split off another cubic if the derivative crosses back@>; if rising then incr(k)@+else decr(k); w:=ww; end; exit:end; @ @<Compute test coefficients |(t0,t1,t2)| for $s(t)$ versus...@>= du:=x←coord(ww)-x←coord(w); dv:=y←coord(ww)-y←coord(w); if abs(du)>=abs(dv) then {$s←{k\pm1}<=1$} begin s:=make←fraction(dv,du); t0:=take←fraction(x0,s)-y0; t1:=take←fraction(x1,s)-y1; t2:=take←fraction(x2,s)-y2; end else begin s:=make←fraction(du,dv); t0:=x0-take←fraction(y0,s); t1:=x1-take←fraction(y1,s); t2:=x2-take←fraction(y2,s); end @ The curve has crossed $s←k$ or $s←{k-1}$; its initial segment satisfies $(*)$, and it might cross again and return towards $s←k$, yielding another solution of $(*)$. @<Split the cubic at $t$, and split off another...@>= begin split←for←offset(p,t); right←type(p):=k; p:=link(p);@/ v:=t←of←the←way(x0)(x1); x1:=t←of←the←way(x1)(x2); x0:=t←of←the←way(v)(x1);@/ v:=t←of←the←way(y0)(y1); y1:=t←of←the←way(y1)(y2); y0:=t←of←the←way(v)(y1);@/ t1:=t←of←the←way(t1)(t2); if t1>0 then t1:=0; {without rounding error, |t1| would be |<=0|} t:=crossing←point(0,-t1,-t2); if t<fraction←one then begin split←for←offset(p,t); right←type(link(p)):=k;@/ v:=t←of←the←way(x1)(x2); x1:=t←of←the←way(x0)(x1); x2:=t←of←the←way(x1)(v);@/ v:=t←of←the←way(y1)(y2); y1:=t←of←the←way(y0)(y1); y2:=t←of←the←way(y1)(v); end; end @ Now we must consider the general problem of |offset←prep|, when nothing is known about a given cubic. We start by finding its slope $s(0)$ in the vicinity of |t=0|. If $z'(t)=0$, the given cubic is numerically unstable, since the slope direction is probably being influenced primarily by rounding errors. A user who specifies such cuspy curves should expect to generate rather wild results. The present code tries its best to believe the existing data, as if no rounding errors were present. @ @<Find the initial slope, |dy/dx|@>= dx:=x0; dy:=y0; if dx=0 then if dy=0 then begin dx:=x1; dy:=y1; if dx=0 then if dy=0 then begin dx:=x2; dy:=y2; end; end @ The next step is to bracket the initial slope between consecutive slopes of the pen polygon. The most important invariant relation in the following loop is that |dy/dx>=@t$s←{k-1}$@>|. @<Find the index |k| such that $s←{k-1}\L\\{dy}/\\{dx}<s←k$@>= k:=1; w:=link(lh); loop@+ begin if k=n then goto done; ww:=link(w); if ab←vs←cd(dy,abs(x←coord(ww)-x←coord(w)),@| dx,abs(y←coord(ww)-y←coord(w)))>=0 then begin incr(k); w:=ww; end else goto done; end; done: @ Finally we want to reduce the general problem to situations that |fin←offset←prep| can handle. If |k=1|, we already are in the desired situation. Otherwise we can split the cubic into at most three parts with respect to $s←{k-1}$, and apply |fin←offset←prep| to each part. @<Complete the offset splitting process@>= if k=1 then t:=fraction←one+1 else begin ww:=knil(w); @<Compute test coeff...@>; t:=crossing←point(-t0,-t1,-t2); end; if t>=fraction←one then fin←offset←prep(p,k,w,x0,x1,x2,y0,y1,y2,true,n) else begin split←for←offset(p,t); r:=link(p);@/ x1a:=t←of←the←way(x0)(x1); x1:=t←of←the←way(x1)(x2); x2a:=t←of←the←way(x1a)(x1);@/ y1a:=t←of←the←way(y0)(y1); y1:=t←of←the←way(y1)(y2); y2a:=t←of←the←way(y1a)(y1);@/ fin←offset←prep(p,k,w,x0,x1a,x2a,y0,y1a,y2a,true,n); x0:=x2a; y0:=y2a; t1:=t←of←the←way(t1)(t2); if t1<0 then t1:=0; t:=crossing←point(0,t1,t2); if t<fraction←one then @<Split off another |rising| cubic for |fin←offset←prep|@>; fin←offset←prep(r,k-1,ww,-x0,-x1,-x2,-y0,-y1,-y2,false,n); end @ @<Split off another |rising| cubic for |fin←offset←prep|@>= begin split←for←offset(r,t);@/ x1a:=t←of←the←way(x1)(x2); x1:=t←of←the←way(x0)(x1); x0a:=t←of←the←way(x1)(x1a);@/ y1a:=t←of←the←way(y1)(y2); y1:=t←of←the←way(y0)(y1); y0a:=t←of←the←way(y1)(y1a);@/ fin←offset←prep(link(r),k,w,x0a,x1a,x2,y0a,y1a,y2,true,n); x2:=x0a; y2:=y0a; end @ @<Handle the special case of infinite slope@>= fin←offset←prep(p,n,knil(knil(lh)),-x0,-x1,-x2,-y0,-y1,-y2,false,n) @ OK, it's time now for the biggie. The |fill←envelope| routine generalizes |fill←spec| to polygonal envelopes. Its outer structure is essentially the same as before, except that octants with no cubics do contribute to the envelope. @p @t\4@>@<Declare the procedure called |skew←line←edges|@>@; @t\4@>@<Declare the procedure called |dual←moves|@>@; procedure fill←envelope(@!spec←head:pointer); label done, done1; var @!p,@!q,@!r,@!s:pointer; {for list traversal} @!h:pointer; {head of pen offset list for current octant} @!www:pointer; {a pen offset of temporary interest} @<Other local variables for |fill←envelope|@>@; begin if internal[tracing←edges]>0 then begin←edge←tracing; p:=spec←head; {we assume that |left←type(spec←head)=endpoint|} repeat octant:=left←octant(p); h:=cur←pen+octant; @<Set variable |q| to the node at the end of the current octant@>; @<Determine the envelope's starting and ending lattice points |(m0,n0)| and |(m1,n1)|@>; offset←prep(p,h); {this may clobber node~|q|, if it becomes ``dead''} @<Set variable |q| to the node at the end of the current octant@>; @<Make the envelope moves for the current octant and insert them in the pixel data@>; p:=link(q); until p=spec←head; if internal[tracing←edges]>0 then end←edge←tracing; toss←knot←list(spec←head); end; @ In even-numbered octants we have reflected the coordinates an odd number of times, hence clockwise and counterclockwise are reversed; this means that the envelope is being formed in a ``dual'' manner. For the time being, let's concentrate on odd-numbered octants, since they're easier to understand. After we have coded the program for odd-numbered octants, the changes needed to dualize it will not be so mysterious. It is convenient to assume that we enter an odd-numbered octant with an |axis| transition (where the skewed slope is zero) and leave at a |diagonal| one (where the skewed slope is infinite). Then all of the offset points $z(t)+w(t)$ will lie in a rectangle whose lower left and upper right corners are the initial and final offset points. If this assumption doesn't hold we can implicitly change the curve so that it does. For example, if the entering transition is diagonal, we can draw a straight line from $z←0+w←{n+1}$ to $z←0+w←0$ and continue as if the curve were moving rightward. The effect of this on the envelope is simply to ``doubly color'' the region enveloped by a section of the pen that goes from $w←0$ to $w←1$ to $\cdots$ to $w←{n+1}$ to~$w←0$. The additional straight line at the beginning (and a similar one at the end, where it may be necessary to go from $z←1+w←{n+1}$ to $z←1+w←0$) can be drawn by the |line←edges| routine; we are thereby saved from the embarrassment that these lines travel backwards from the current octant direction. Once we have established the assumption that the curve goes from $z←0+w←0$ to $z←1+w←{n+1}$, any further retrograde moves that might occur within the octant can be essentially ignored; we merely need to keep track of the rightmost edge in each row, in order to compute the envelope. Envelope moves consist of offset cubics intermixed with straight line segments. We record them in a separate |env←move| array, which is something like |move| but it keeps track of the rightmost position of the envelope in each row. @<Glob...@>= @!env←move:array[0..move←size] of integer; @ @<Determine the envelope's starting and ending...@>= w:=link(h);@+if left←transition(p)=diagonal then w:=knil(w); @!stat if internal[tracing←edges]>unity then @<Print a line of diagnostic info to introduce this octant@>; tats@;@/ ww:=link(h); www:=ww; {starting and ending offsets} if odd(octant←number[octant]) then www:=knil(www)@+else ww:=knil(ww); if w<>ww then skew←line←edges(p,w,ww); end←round(x←coord(p)+x←coord(ww),y←coord(p)+y←coord(ww)); m0:=m1; n0:=n1; d0:=d1;@/ end←round(x←coord(q)+x←coord(www),y←coord(q)+y←coord(www)); if n1-n0>=move←size then overflow("move table size",move←size) @:METAFONT capacity exceeded move table size}{\quad move table size@> @ @<Print a line of diagnostic info to introduce this octant@>= begin print←nl("@@ Octant "); print(octant←dir[octant]); @:]]]\AT!←Octant}{\.{\AT! Octant...}@> print(" ("); print←int(info(h)); print(" offset"); if info(h)<>1 then print←char("s"); print("), from "); print←two←true(x←coord(p)+x←coord(w),y←coord(p)+y←coord(w)); ww:=link(h);@+if right←transition(q)=diagonal then ww:=knil(ww); print(" to "); print←two←true(x←coord(q)+x←coord(ww),y←coord(q)+y←coord(ww)); end @ A slight variation of the |line←edges| procedure comes in handy when we must draw the retrograde lines for nonstandard entry and exit conditions. @<Declare the procedure called |skew←line←edges|@>= procedure skew←line←edges(@!p,@!w,@!ww:pointer); var @!x0,@!y0,@!x1,@!y1:scaled; {from and to} begin if (x←coord(w)<>x←coord(ww))or(y←coord(w)<>y←coord(ww)) then begin x0:=x←coord(p)+x←coord(w); y0:=y←coord(p)+y←coord(w);@/ x1:=x←coord(p)+x←coord(ww); y1:=y←coord(p)+y←coord(ww);@/ unskew(x0,y0,octant); {unskew and unrotate the coordinates} x0:=cur←x; y0:=cur←y;@/ unskew(x1,y1,octant);@/ @!stat if internal[tracing←edges]>unity then begin print←nl("@@ retrograde line from "); @:]]]\AT!←retro←}{\.{\AT! retrograde line...}@> @.retrograde line...@> print←two(x0,y0); print(" to "); print←two(cur←x,cur←y); print←nl(""); end;@+tats@;@/ line←edges(x0,y0,cur←x,cur←y); {then draw a straight line} end; end; @ The envelope calculations require more local variables than we needed in the simpler case of |fill←spec|. At critical points in the computation, |w| will point to offset $w←k$; |m| and |n| will record the current lattice positions. The values of |move←ptr| after the initial and before the final offset adjustments are stored in |smooth←bot| and |smooth←top|, respectively. @<Other local variables for |fill←envelope|@>= @!m,@!n:integer; {current lattice position} @!mm0,@!mm1:integer; {skewed equivalents of |m0| and |m1|} @!k:integer; {current offset number} @!w,@!ww:pointer; {pointers to the current offset and its neighbor} @!smooth←bot,@!smooth←top:0..move←size; {boundaries of smoothing} @!xx,@!yy,@!xp,@!yp,@!delx,@!dely,@!tx,@!ty:scaled; {registers for coordinate calculations} @ @<Make the envelope moves for the current octant...@>= if odd(octant←number[octant]) then begin @<Initialize for ordinary envelope moves@>; r:=p; right←type(q):=info(h)+1; loop@+ begin if r=q then smooth←top:=move←ptr; while right←type(r)<>k do @<Insert a line segment to approach the correct offset@>; if r=p then smooth←bot:=move←ptr; if r=q then goto done; move[move←ptr]:=1; n:=move←ptr; s:=link(r);@/ make←moves(x←coord(r)+x←coord(w),right←x(r)+x←coord(w), left←x(s)+x←coord(w),x←coord(s)+x←coord(w),@| y←coord(r)+y←coord(w)+half←unit,right←y(r)+y←coord(w)+half←unit, left←y(s)+y←coord(w)+half←unit,y←coord(s)+y←coord(w)+half←unit,@| xy←corr[octant],y←corr[octant]); @<Transfer moves from the |move| array to |env←move|@>; r:=s; end; done: @<Insert the new envelope moves in the pixel data@>; end else dual←moves(h,p,q); right←type(q):=endpoint @ @<Initialize for ordinary envelope moves@>= k:=0; w:=link(h); ww:=knil(w); mm0:=floor←unscaled(x←coord(p)+x←coord(w)-xy←corr[octant]); mm1:=floor←unscaled(x←coord(q)+x←coord(ww)-xy←corr[octant]); for n:=0 to n1-n0 do env←move[n]:=mm0; env←move[n1-n0]:=mm1; move←ptr:=0; m:=mm0 @ At this point |n| holds the value of |move←ptr| that was current when |make←moves| began to record its moves. @<Transfer moves from the |move| array to |env←move|@>= repeat m:=m+move[n]-1; if m>env←move[n] then env←move[n]:=m; incr(n); until n>move←ptr @ Retrograde lines (when |k| decreases) do not need to be recorded in |env←move| because their edges are not the furthest right in any row. @<Insert a line segment to approach the correct offset@>= begin xx:=x←coord(r)+x←coord(w); yy:=y←coord(r)+y←coord(w)+half←unit; @!stat if internal[tracing←edges]>unity then begin print←nl("@@ transition line "); print←int(k); print(", from "); @:]]]\AT!←trans←}{\.{\AT! transition line...}@> @.transition line...@> print←two←true(xx,yy-half←unit); end;@+tats@;@/ if right←type(r)>k then begin incr(k); w:=link(w); xp:=x←coord(r)+x←coord(w); yp:=y←coord(r)+y←coord(w)+half←unit; if yp<>yy then @<Record a line segment from |(xx,yy)| to |(xp,yp)| in |env←move|@>; end else begin decr(k); w:=knil(w); xp:=x←coord(r)+x←coord(w); yp:=y←coord(r)+y←coord(w)+half←unit; end; stat if internal[tracing←edges]>unity then begin print(" to "); print←two←true(xp,yp-half←unit); print←nl(""); end;@+tats@;@/ m:=floor←unscaled(xp-xy←corr[octant]); move←ptr:=floor←unscaled(yp-y←corr[octant])-n0; if m>env←move[move←ptr] then env←move[move←ptr]:=m; end @ In this step we have |xp>=xx| and |yp>=yy|. @<Record a line segment from |(xx,yy)| to |(xp,yp)| in |env←move|@>= begin ty:=floor←scaled(yy-y←corr[octant]); dely:=yp-yy; yy:=yy-ty; ty:=yp-y←corr[octant]-ty; if ty>=unity then begin delx:=xp-xx; yy:=unity-yy; loop@+ begin tx:=take←fraction(delx,make←fraction(yy,dely)); if ab←vs←cd(tx,dely,delx,yy)+xy←corr[octant]>0 then decr(tx); m:=floor←unscaled(xx+tx); if m>env←move[move←ptr] then env←move[move←ptr]:=m; ty:=ty-unity; if ty<unity then goto done1; yy:=yy+unity; incr(move←ptr); end; done1:end; end @ @<Insert the new envelope moves in the pixel data@>= debug if (m<>mm1)or(move←ptr<>n1-n0) then confusion("1");@+gubed@;@/ move[0]:=d0+env←move[0]-mm0; for n:=1 to move←ptr do move[n]:=env←move[n]-env←move[n-1]+1; move[move←ptr]:=move[move←ptr]-d1; if internal[smoothing]>0 then smooth←moves(smooth←bot,smooth←top); move←to←edges(m0,n0,m1,n1); if right←transition(q)=axis then begin w:=link(h); skew←line←edges(q,knil(w),w); end @ OK, now we've done it all in the odd-octant case; the only thing remaining is to repeat the same ideas, upside down and/or backwards. The following code has been split off as a subprocedure of |fill←envelope|, because some \PASCAL\ compilers cannot handle procedures as large as |fill←envelope| would otherwise be. @<Declare the procedure called |dual←moves|@>= procedure dual←moves(@!h,@!p,@!q:pointer); label done,done1; var @!r,@!s:pointer; {for list traversal} @<Other local variables for |fill←envelope|@>@; begin @<Initialize for dual envelope moves@>; r:=p; {recall that |right←type(q)=endpoint=0| now} loop@+ begin if r=q then smooth←top:=move←ptr; while right←type(r)<>k do @<Insert a line segment dually to approach the correct offset@>; if r=p then smooth←bot:=move←ptr; if r=q then goto done; move[move←ptr]:=1; n:=move←ptr; s:=link(r);@/ make←moves(x←coord(r)+x←coord(w),right←x(r)+x←coord(w), left←x(s)+x←coord(w),x←coord(s)+x←coord(w),@| y←coord(r)+y←coord(w)+half←unit,right←y(r)+y←coord(w)+half←unit, left←y(s)+y←coord(w)+half←unit,y←coord(s)+y←coord(w)+half←unit,@| xy←corr[octant],y←corr[octant]); @<Transfer moves dually from the |move| array to |env←move|@>; r:=s; end; done:@<Insert the new envelope moves dually in the pixel data@>; end; @ In the dual case the normal situation is to arrive with a |diagonal| transition and to leave at the |axis|. The leftmost edge in each row is relevant instead of the rightmost one. @<Initialize for dual envelope moves@>= k:=info(h)+1; ww:=link(h); w:=knil(ww);@/ mm0:=floor←unscaled(x←coord(p)+x←coord(w)-xy←corr[octant]); mm1:=floor←unscaled(x←coord(q)+x←coord(ww)-xy←corr[octant]); for n:=1 to n1-n0+1 do env←move[n]:=mm1; env←move[0]:=mm0; move←ptr:=0; m:=mm0 @ @<Transfer moves dually from the |move| array to |env←move|@>= repeat if m<env←move[n] then env←move[n]:=m; m:=m+move[n]-1; incr(n); until n>move←ptr @ Dual retrograde lines occur when |k| increases; the edges of such lines are not the furthest left in any row. @<Insert a line segment dually to approach the correct offset@>= begin xx:=x←coord(r)+x←coord(w); yy:=y←coord(r)+y←coord(w)+half←unit; @!stat if internal[tracing←edges]>unity then begin print←nl("@@ transition line "); print←int(k); print(", from "); @:]]]\AT!←trans←}{\.{\AT! transition line...}@> @.transition line...@> print←two←true(xx,yy-half←unit); end;@+tats@;@/ if right←type(r)<k then begin decr(k); w:=knil(w); xp:=x←coord(r)+x←coord(w); yp:=y←coord(r)+y←coord(w)+half←unit; if yp<>yy then @<Record a line segment from |(xx,yy)| to |(xp,yp)| dually in |env←move|@>; end else begin incr(k); w:=link(w); xp:=x←coord(r)+x←coord(w); yp:=y←coord(r)+y←coord(w)+half←unit; end; stat if internal[tracing←edges]>unity then begin print(" to "); print←two←true(xp,yp-half←unit); print←nl(""); end;@+tats@;@/ m:=floor←unscaled(xp-xy←corr[octant]); move←ptr:=floor←unscaled(yp-y←corr[octant])-n0; if m<env←move[move←ptr] then env←move[move←ptr]:=m; end @ Again, |xp>=xx| and |yp>=yy|; but this time we are interested in the {\sl smallest\/} |m| that belongs to a given |move←ptr| position, instead of the largest~|m|. @<Record a line segment from |(xx,yy)| to |(xp,yp)| dually in |env←move|@>= begin ty:=floor←scaled(yy-y←corr[octant]); dely:=yp-yy; yy:=yy-ty; ty:=yp-y←corr[octant]-ty; if ty>=unity then begin delx:=xp-xx; yy:=unity-yy; loop@+ begin if m<env←move[move←ptr] then env←move[move←ptr]:=m; tx:=take←fraction(delx,make←fraction(yy,dely)); if ab←vs←cd(tx,dely,delx,yy)+xy←corr[octant]>0 then decr(tx); m:=floor←unscaled(xx+tx); ty:=ty-unity; incr(move←ptr); if ty<unity then goto done1; yy:=yy+unity; end; done1: if m<env←move[move←ptr] then env←move[move←ptr]:=m; end; end @ Since |env←move| contains minimum values instead of maximum values, the finishing-up process is slightly different in the dual case. @<Insert the new envelope moves dually in the pixel data@>= debug if (m<>mm1)or(move←ptr<>n1-n0) then confusion("2");@+gubed@;@/ move[0]:=d0+env←move[1]-mm0; for n:=1 to move←ptr do move[n]:=env←move[n+1]-env←move[n]+1; move[move←ptr]:=move[move←ptr]-d1; if internal[smoothing]>0 then smooth←moves(smooth←bot,smooth←top); move←to←edges(m0,n0,m1,n1); if right←transition(q)=diagonal then begin w:=link(h); skew←line←edges(q,w,knil(w)); end @* \[25] Elliptical pens. To get the envelope of a cyclic path with respect to an ellipse, \MF\ calculates the envelope with respect to a polygonal approximation to the ellipse, using an approach due to John Hobby [to appear]. @↑Hobby, John Douglas@> This has two important advantages over trying to obtain the ``exact'' envelope: \yskip\textindent{1)}It gives better results, because the polygon has been designed to counteract problems that arise from digitization; the polygon includes sub-pixel corrections to an exact ellipse that make the results essentially independent of where the path falls on the raster. For example, the exact envelope with respect to a pen of diameter~1 blackens a pixel if and only if the path intersects a circle of diameter~1 inscribed in that pixel; the resulting pattern has ``blots'' when the path is travelling diagonally in unfortunate raster positions. A much better result is obtained when pixels are blackened only when the path intersects an inscribed {\sl diamond\/} of diameter~1. Such a diamond is precisely the polygon that \MF\ uses in the special case of a circle whose diameter is~1. \yskip\textindent{2)}Polygonal envelopes of cubic splines are cubic splines, hence it isn't necessary to introduce completely different routines. By contrast, exact envelopes of cubic splines with respect to circles are complicated curves, more difficult to plot than cubics. @ Hobby's construction involves some interesting number theory. If $u$ and~$v$ are relatively prime integers, we divide the set of integer points $(m,n)$ into equivalence classes by saying that $(m,n)$ belongs to class $um+vn$. Then any two integer points that lie on a line of slope $-u/v$ belong to the same class, because such points have the form $(m+tv,n-tu)$. Neighboring lines of slope $-u/v$ that go through integer points are separated by distance $1/\psqrt{u↑2+v↑2}$ from each other, and these lines are perpendicular to lines of slope~$v/u$. If we start at the origin and travel a distance $k/\psqrt{u↑2+v↑2}$ in direction $(u,v)$, we reach the line of slope~$-u/v$ whose points belong to class~$k$. For example, let $u=2$ and $v=3$. Then the points $(0,0)$, $(3,-2)$, $\ldots$ belong to class~0; the points $(-1,1)$, $(2,-1)$, $\ldots$ belong to class~1; and the distance between these two lines is $1/\sqrt{13}$. The point $(2,3)$ itself belongs to class~13, hence its distance from the origin is $13/\sqrt{13}=\sqrt{13}$ (which we already knew). Suppose we wish to plot envelopes with respect to polygons with integer vertices. Then the best polygon for curves that travel in direction $(v,-u)$ will contain the points of class~$k$ such that $k/\psqrt{u↑2+v↑2}$ is as close as possible to~$d$, where $d$ is the maximum distance of the given ellipse from the line $ux+vy=0$. The |fillin| correction assumes that a diagonal line has an apparent thickness $2f\cdot\min(\vert u\vert,\vert v\vert)/\psqrt{u↑2+v↑2}$ greater than what would be obtained with truly square pixels. (If a white pixel at an exterior corner is assumed to have apparent darkness $f←1$ and a black pixel at an interior corner is assumed to have apparent darkness $1-f←2$, then $f=f←1-f←2$ is the |fillin| parameter.) Under this assumption we want to choose $k$ so that $\bigl(k+2f\cdot\min(\vert u\vert,\vert v\vert)\bigr)\big/\psqrt{u↑2+v↑2}$ is as close as possible to $d$. Integer coordinates for the vertices work nicely because the thickness of the envelope at any given slope is independent of the position of the path with respect to the raster. It turns out, in fact, that the same property holds for polygons whose vertices have coordinates that are integer multiples of~$1\over2$, because ellipses are symmetric about the origin. It's convenient to double all dimensions and require the resulting polygon to have vertices with integer coordinates. For example, to get a circle of {\sl diameter}~$r$, we shall compute integer coordinates for a circle of {\sl radius}~$r$. The circle of radius~$r$ will want to be represented by a polygon that contains the boundary points $(0,\pm r)$ and~$(\pm r,0)$; later we will divide everything by~2 and get a polygon with $(0,\pm{1\over2}r)$ and $(\pm{1\over2}r,0)$ on its boundary. @ In practice the important slopes are those having small values of $u$ and~$v$; these make regular patterns in which our eyes quickly spot irregularities. For example, horizontal and vertical lines (when $u=0$ and $\vert v\vert=1$, or $\vert u\vert=1$ and $v=0$) are the most important; diagonal lines (when $\vert u\vert=\vert v\vert=1$) are next; and then come lines with slope $\pm2$ or $\pm1/2$. The nicest way to generate all rational directions having small numerators and denominators is to generalize the Stern-Peirce tree [cf.~{\sl The Art of Computer Programming\/ \bf2}, exercise 4.5.3--40] @↑Peirce, Charles Santiago Sanders@> @↑Stern, Moriz Abraham@> to a ``Stern-Peirce wreath'' as follows: Begin with four nodes arranged in a circle, containing the respective directions $(u,v)=(1,0)$, $(0,1)$, $(-1,0)$, and~$(0,-1)$. Then between pairs of consecutive terms $(u,v)$ and $(u',v')$ of the wreath, insert the direction $(u+u',v+v')$; continue doing this until some stopping criterion is fulfilled. It is not difficult to verify that, regardless of the stopping criterion, consecutive directions $(u,v)$ and $(u',v')$ of this wreath will always satisfy the relation $uv'-u'v=1$. Such pairs of directions have a nice property with respect to the equivalence classes described above. Let $l$ be a line of equivalent integer points $(m+tv,n-tu)$ with respect to~$(u,v)$, and let $l'$ be a line of equivalent integer points $(m'+tv',n'-tu')$ with respect to~$(u',v')$. Then $l$ and~$l'$ intersect in an integer point $(m'',n'')$, because the determinant of the linear equations for intersection is $uv'-u'v=1$. Notice that the class number of $(m'',n'')$ with respect to $(u+u',v+v')$ is the sum of its class numbers with respect to $(u,v)$ and~$(u',v')$. Moreover, consecutive points on~$l$ and~$l'$ belong to classes that differ by exactly~1 with respect to $(u+u',v+v')$. This leads to a nice algorithm in which we construct a polygon having ``correct'' class numbers for as many small-integer directions $(u,v)$ as possible: Assuming that lines $l$ and~$l'$ contain points of the correct class for $(u,v)$ and~$(u',v')$, respectively, we determine the intersection $(m'',n'')$ and compute its class with respect to $(u+u',v+v')$. If the class is too large to be the best approximation, we move back the proper number of steps from $(m'',n'')$ toward smaller class numbers on both $l$ and~$l'$, unless this requires moving to points that are no longer in the polygon; in this we arrive at two points that determine a line~$l''$ having the appropriate class. The process continues recursively, until it cannot proceed without removing the last remaining point from the class for $(u,v)$ or the class for $(u',v')$. @ The |make←ellipse| subroutine produces a pointer to a cyclic path whose vertices define a polygon suitable for envelopes. The control points on this path will be ignored; in fact, the fields in knot nodes that are usually reserved for control points are occupied by other data that helps |make←ellipse| compute the desired polygon. Parameters |major←axis| and |minor←axis| define the axes of the ellipse; and parameter |theta| is an angle by which the ellipse is rotated counterclockwise. If |theta=0|, the ellipse has the equation $(x/a)↑2+(y/b)↑2=1$, where |a=major←axis/2| and |b=minor←axis/2|. In general, the points of the ellipse are generated in the complex plane by the formula $e↑{i\theta}(a\cos t+ib\sin t)$, as $t$~ranges over all angles. Notice that if |major←axis=minor←axis=d|, we obtain a circle of diameter~|d|, regardless of the value of |theta|. The method sketched above is used to produce the elliptical polygon, except that the main work is done only in the halfplane obtained from the three starting directions $(0,-1)$, $(1,0)$,~$(0,1)$. Since the ellipse has circular symmetry, we use the fact that the last half of the polygon is simply the negative of the first half. Furthermore, we need to compute only one quarter of the polygon if the ellipse has axis symmetry. @p function make←ellipse(@!major←axis,@!minor←axis:scaled; @!theta:angle):pointer; label done,done1,found; var @!p,@!q,@!r,@!s:pointer; {for list manipulation} @!h:pointer; {head of the constructed knot list} @!alpha,@!beta,@!gamma,@!delta:integer; {special points} @!c,@!d:integer; {class numbers} @!u,@!v:integer; {directions} @!symmetric:boolean; {should the result be symmetric about the axes?} begin @<Initialize the ellipse data structure by beginning with directions $(0,-1)$, $(1,0)$, $(0,1)$@>; @<Interpolate new vertices in the ellipse data structure until improvement is impossible@>; if symmetric then @<Complete the half ellipse by reflecting the quarter already computed@>; @<Complete the ellipse by copying the negative of the half already computed@>; make←ellipse:=h; end; @ A special data structure is used only with |make←ellipse|: The |right←x|, |left←x|, |right←y|, and |left←y| fields of knot nodes are renamed |right←u|, |left←v|, |right←class|, and |left←length|, in order to store information that simplifies the necessary computations. If |p| and |q| are consecutive knots in this data structure, the |x←coord| and |y←coord| fields of |p| and~|q| contain current vertices of the polygon; their values are integer multiples of |half←unit|. Both of these vertices belong to equivalence class |right←class(p)| with respect to the direction $\bigl($|right←u(p),left←v(q)|$\bigr)$. The number of points of this class on the line from vertex~|p| to vertex~|q| is |1+left←length(q)|. In particular, |left←length(q)=0| means that |x←coord(p)=x←coord(q)| and |y←coord(p)=y←coord(q)|; such duplicate vertices will be discarded during the course of the algorithm. The contents of |right←u(p)| and |left←v(q)| are integer multiples of |half←unit|, just like the coordinate fields. Hence, for example, the point $\bigl($|x←coord(p)-left←v(q),y←coord(p)+right←u(q)|$\bigr)$ also belongs to class number |right←class(p)|. This point is one step closer to the vertex in node~|q|; it equals that vertex if and only if |left←length(q)=1|. The |left←type| and |right←type| fields are not used, but |link| has its normal meaning. To start the process, we create four nodes for the three directions $(0,-1)$, $(1,0)$, and $(0,1)$. The corresponding vertices are $(-\alpha,-\beta)$, $(\gamma,-\beta)$, $(\gamma,\beta)$, and $(\alpha,\beta)$, where $(\alpha,\beta)$ is a half-integer approximation to where the ellipse rises highest above the $x$-axis, and where $\gamma$ is a half-integer approximation to the maximum $x$-coordinate of the ellipse. The fourth of these nodes is not actually calculated if the ellipse has axis symmetry. @d right←u==right←x {|u| value for a pen edge} @d left←v==left←x {|v| value for a pen edge} @d right←class==right←y {equivalence class number of a pen edge} @d left←length==left←y {length of a pen edge} @<Initialize the ellipse data structure...@>= @<Calculate integers $\alpha$, $\beta$, $\gamma$ for the vertex coordinates@>; p:=get←node(knot←node←size); q:=get←node(knot←node←size); r:=get←node(knot←node←size); if symmetric then s:=null@+else s:=get←node(knot←node←size); h:=p; link(p):=q; link(q):=r; link(r):=s; {|s=null| or |link(s)=null|} @<Revise the values of $\alpha$, $\beta$, $\gamma$, if necessary, so that degenerate lines of length zero will not be obtained@>; x←coord(p):=-alpha*half←unit; y←coord(p):=-beta*half←unit; x←coord(q):=gamma*half←unit;@/ y←coord(q):=y←coord(p); x←coord(r):=x←coord(q);@/ right←u(p):=0; left←v(q):=-half←unit;@/ right←u(q):=half←unit; left←v(r):=0;@/ right←u(r):=0; right←class(p):=beta; right←class(q):=gamma; right←class(r):=beta;@/ left←length(q):=gamma+alpha; if symmetric then begin y←coord(r):=0; left←length(r):=beta; end else begin y←coord(r):=-y←coord(p); left←length(r):=beta+beta;@/ x←coord(s):=-x←coord(p); y←coord(s):=y←coord(r);@/ left←v(s):=half←unit; left←length(s):=gamma-alpha; end @ One of the important invariants of the pen data structure is that the points are distinct. We may need to correct the pen specification in order to avoid this. (The result of \&{pencircle} will always be at least one pixel wide and one pixel tall, although \&{makepen} is capable of producing smaller pens.) @<Revise the values of $\alpha$, $\beta$, $\gamma$, if necessary...@>= if beta=0 then beta:=1; if gamma=0 then gamma:=1; if gamma<=abs(alpha) then if alpha>0 then alpha:=gamma-1 else alpha:=1-gamma @ If $a$ and $b$ are the semi-major and semi-minor axes, the given ellipse rises highest above the $y$-axis at the point $\bigl((a↑2-b↑2)\sin\theta\cos\theta/\rho\bigr)+i\rho$, where $\rho=\sqrt{(a\sin\theta)↑2+(b\cos\theta)↑2}$. It reaches furthest to the right of~the $x$-axis at the point $\sigma+i(a↑2-b↑2)\sin\theta\cos\theta/\sigma$, where $\sigma=\sqrt{(a\cos\theta)↑2+(b\sin\theta)↑2}$. @<Calculate integers $\alpha$, $\beta$, $\gamma$...@>= if (major←axis=minor←axis)or(theta mod ninety←deg=0) then begin symmetric:=true; alpha:=0; if odd(theta div ninety←deg) then begin beta:=major←axis; gamma:=minor←axis; n←sin:=fraction←one; n←cos:=0; {|n←sin| and |n←cos| are used later} end else begin beta:=minor←axis; gamma:=major←axis; end; {|n←sin| and |n←cos| aren't needed in this case} end else begin symmetric:=false; n←sin←cos(theta); {set up $|n←sin|=\sin\theta$ and $|n←cos|=\cos\theta$} gamma:=take←fraction(major←axis,n←sin); delta:=take←fraction(minor←axis,n←cos); beta:=pyth←add(gamma,delta); alpha:=take←fraction(take←fraction(major←axis, make←fraction(gamma,beta)),n←cos)@| -take←fraction(take←fraction(minor←axis, make←fraction(delta,beta)),n←sin); alpha:=(alpha+half←unit) div unity; gamma:=pyth←add(take←fraction(major←axis,n←cos), take←fraction(minor←axis,n←sin)); end; beta:=(beta+half←unit) div unity; gamma:=(gamma+half←unit) div unity @ Now |p|, |q|, and |r| march through the list, always representing three consecutive vertices and two consecutive slope directions. When a new slope is interpolated, we back up slightly, until further refinement is impossible; then we march forward again. The somewhat magical operations performed in this part of the algorithm are justified by the theory sketched earlier. Complications arise only from the need to keep zero-length lines out of the final data structure. @<Interpolate new vertices in the ellipse data structure...@>= loop@+ begin u:=right←u(p)+right←u(q); v:=left←v(q)+left←v(r); c:=right←class(p)+right←class(q);@/ @<Compute the distance |d| from class~0 to the edge of the ellipse in direction |(u,v)|, times $\psqrt{u↑2+v↑2}$, rounded to the nearest integer@>; delta:=c-d; {we want to move |delta| steps back from the intersection vertex~|q|} if delta>0 then begin if delta>left←length(r) then delta:=left←length(r); if delta>=left←length(q) then @<Remove the line from |p| to |q|, and adjust vertex~|q| to introduce a new line@> else @<Insert a new line for direction |(u,v)| between |p| and~|q|@>; end else p:=q; @<Move to the next remaining triple |(p,q,r)|, removing and skipping past zero-length lines that might be present; |goto done| if all triples have been processed@>; end; done: @ The appearance of a zero-length line means that we should advance |p| past it. We must not try to straddle a missing direction, because the algorithm works only on consecutive pairs of directions. @<Move to the next remaining triple |(p,q,r)|...@>= loop@+ begin q:=link(p); if q=null then goto done; if left←length(q)=0 then begin link(p):=link(q); right←class(p):=right←class(q); right←u(p):=right←u(q); free←node(q,knot←node←size); end else begin r:=link(q); if r=null then goto done; if left←length(r)=0 then begin link(p):=r; free←node(q,knot←node←size); p:=r; end else goto found; end; end; found: @ The `\&{div} 8' near the end of this step comes from the fact that |delta| is scaled by~$2↑{15}$ and $d$~by~$2↑{16}$, while |take←fraction| removes a scale factor of~$2↑{28}$. We also make sure that $d\G\max(\vert u\vert,\vert v\vert)$, so that the pen will always include a circular pen of diameter~1 as a subset; then it won't be possible to get disconnected path envelopes. @<Compute the distance |d| from class~0 to the edge of the ellipse...@>= delta:=pyth←add(u,v); if major←axis=minor←axis then d:=major←axis {circles are easy} else begin if theta=0 then begin alpha:=u; beta:=v; end else begin alpha:=take←fraction(u,n←cos)+take←fraction(v,n←sin); beta:=take←fraction(v,n←cos)-take←fraction(u,n←sin); end; alpha:=make←fraction(alpha,delta); beta:=make←fraction(beta,delta); d:=pyth←add(take←fraction(major←axis,alpha), take←fraction(minor←axis,beta)); end; d:=take←fraction(d,delta); alpha:=abs(u); beta:=abs(v); if alpha<beta then begin delta:=alpha; alpha:=beta; beta:=delta; end; {now $\alpha=\max(\vert u\vert,\vert v\vert)$, $\beta=\min(\vert u\vert,\vert v\vert)$} if internal[fillin]<>0 then d:=d-take←fraction(internal[fillin],beta+beta); d:=(d+4) div 8; alpha:=alpha div half←unit; if d<alpha then d:=alpha @ At this point there's a line of length |<=delta| from vertex~|p| to vertex~|q|, orthogonal to direction $\bigl($|right←u(p),left←v(q)|$\bigr)$; and there's a line of length |>=delta| from vertex~|q| to to vertex~|r|, orthogonal to direction $\bigl($|right←u(q),left←v(r)|$\bigr)$. The best line to direction $(u,v)$ should replace the line from |p| to~|q|; this new line will have the same length as the old. @<Remove the line from |p| to |q|...@>= begin delta:=left←length(q);@/ right←class(p):=c-delta; right←u(p):=u; left←v(q):=v;@/ x←coord(q):=x←coord(q)-delta*left←v(r); y←coord(q):=y←coord(q)+delta*right←u(q);@/ left←length(r):=left←length(r)-delta; end @ Here is the main case, now that we have dealt with the exception: We insert a new line of length |delta| for direction |(u,v)|, decreasing each of the adjacent lines by |delta| steps. @<Insert a new line for direction |(u,v)| between |p| and~|q|@>= begin s:=get←node(knot←node←size); link(p):=s; link(s):=q;@/ x←coord(s):=x←coord(q)+delta*left←v(q); y←coord(s):=y←coord(q)-delta*right←u(p);@/ x←coord(q):=x←coord(q)-delta*left←v(r); y←coord(q):=y←coord(q)+delta*right←u(q);@/ left←v(s):=left←v(q); right←u(s):=u; left←v(q):=v;@/ right←class(s):=c-delta;@/ left←length(s):=left←length(q)-delta; left←length(q):=delta; left←length(r):=left←length(r)-delta; end @ Only the coordinates need to be copied, not the class numbers and other stuff. @<Complete the half ellipse...@>= begin s:=null; q:=h; loop@+ begin r:=get←node(knot←node←size); link(r):=s; s:=r;@/ x←coord(s):=x←coord(q); y←coord(s):=-y←coord(q); if q=p then goto done1; q:=link(q); if y←coord(q)=0 then goto done1; end; done1: link(p):=s; beta:=-y←coord(h); while y←coord(p)<>beta do p:=link(p); q:=link(p); end @ Now we use a somewhat tricky fact: The pointer |q| will be null if and only if the line for the final direction $(0,1)$ has been removed. If that line still survives, it should be combined with a possibly surviving line in the initial direction $(0,-1)$. @<Complete the ellipse by copying...@>= if q<>null then begin if right←u(h)=0 then begin p:=h; h:=link(h); free←node(p,knot←node←size);@/ x←coord(q):=-x←coord(h); end; p:=q; end else q:=p; r:=link(h); {now |p=q|, |x←coord(p)=-x←coord(h)|, |y←coord(p)=-y←coord(h)|} repeat s:=get←node(knot←node←size); link(p):=s; p:=s;@/ x←coord(p):=-x←coord(r); y←coord(p):=-y←coord(r); r:=link(r); until r=q; link(p):=h @* \[26] Direction and intersection times. A path of length $n$ is defined parametrically by functions $x(t)$ and $y(t)$, for |0<=t<=n|; we can regard $t$ as the ``time'' at which the path reaches the point $\bigl(x(t),y(t)\bigr)$. In this section of the program we shall consider operations that determine special times associated with given paths: the first time that a path travels in a given direction, and a pair of times at which two paths cross each other. @ Let's start with the easier task. The function |find←direction←time| is given a direction |(x,y)| and a path starting at~|h|. If the path never travels in direction |(x,y)|, the direction time will be~|-1|; otherwise it will be nonnegative. Certain anomalous cases can arise: If |(x,y)=(0,0)|, so that the given direction is undefined, the direction time will be~0. If $\bigl(x'(t), y'(t)\bigr)=(0,0)$, so that the path direction is undefined, it will be assumed to match any given direction at time~|t|. The routine solves this problem in nondegenerate cases by rotating the path and the given direction so that |(x,y)=(1,0)|; i.e., the main task will be to find when a given path first travels ``due east.'' @p function find←direction←time(@!x,@!y:scaled;@!h:pointer):scaled; label exit,found,not←found,done; var @!max:scaled; {$\max\bigl(\vert x\vert,\vert y\vert\bigr)$} @!p,@!q:pointer; {for list traversal} @!n:scaled; {the direction time at knot |p|} @!tt:scaled; {the direction time within a cubic} @<Other local variables for |find←direction←time|@>@; begin @<Normalize the given direction for better accuracy; but |return| with zero result if it's zero@>; n:=0; p:=h; loop@+ begin if right←type(p)=endpoint then goto not←found; q:=link(p); @<Rotate the cubic between |p| and |q|; then |goto found| if the rotated cubic travels due east at some time |tt|; but |goto not←found| if an entire cyclic path has been traversed@>; p:=q; n:=n+unity; end; not←found: find←direction←time:=-unity; return; found: find←direction←time:=n+tt; exit:end; @ @<Normalize the given direction for better accuracy...@>= if abs(x)<abs(y) then begin x:=make←fraction(x,abs(y)); if y>0 then y:=fraction←one@+else y:=-fraction←one; end else if x=0 then begin find←direction←time:=0; return; end else begin y:=make←fraction(y,abs(x)); if x>0 then x:=fraction←one@+else x:=-fraction←one; end @ Since we're interested in the tangent directions, we work with the derivative $3B'(x←0,x←1,x←2,x←3;t)=B(x←1-x←0,x←2-x←1,x←3-x←2;t)$ instead of $B(x←0,x←1,x←2,x←3;t)$ itself. The derived coefficients are also scaled up in order to achieve better accuracy. The given path may turn abruptly at a knot, and it might pass the critical tangent direction at such a time. Therefore we remember the direction |phi| in which the previous rotated cubic was traveling. (The value of |phi| will be undefined on the first cubic, i.e., when |n=0|.) @<Rotate the cubic between |p| and |q|; then...@>= tt:=0; @<Set local variables |x1,x2,x3| and |y1,y2,y3| to multiples of the control points of the rotated derivatives@>; if y1=0 then if x1>=0 then goto found; if n>0 then begin @<Exit to |found| if an eastward direction occurs at knot |p|@>; if p=h then goto not←found; end; if (x3<>0)or(y3<>0) then phi:=n←arg(x3,y3); @<Exit to |found| if the curve whose derivatives are specified by |x1,x2,x3,y1,y2,y3| travels eastward at some time~|tt|@> @ @<Other local variables for |find←direction←time|@>= @!x1,@!x2,@!x3,@!y1,@!y2,@!y3:scaled; {multiples of rotated derivatives} @!theta,@!phi:angle; {angles of exit and entry at a knot} @!t:fraction; {temp storage} @ @<Set local variables |x1,x2,x3| and |y1,y2,y3| to multiples...@>= x1:=right←x(p)-x←coord(p); x2:=left←x(q)-right←x(p); x3:=x←coord(q)-left←x(q);@/ y1:=right←y(p)-y←coord(p); y2:=left←y(q)-right←y(p); y3:=y←coord(q)-left←y(q);@/ max:=abs(x1); if abs(x2)>max then max:=abs(x2); if abs(x3)>max then max:=abs(x3); if abs(y1)>max then max:=abs(y1); if abs(y2)>max then max:=abs(y2); if abs(y3)>max then max:=abs(y3); if max=0 then goto found; while max<fraction←half do begin double(max); double(x1); double(x2); double(x3); double(y1); double(y2); double(y3); end; t:=x1; x1:=take←fraction(x1,x)+take←fraction(y1,y); y1:=take←fraction(y1,x)-take←fraction(t,y);@/ t:=x2; x2:=take←fraction(x2,x)+take←fraction(y2,y); y2:=take←fraction(y2,x)-take←fraction(t,y);@/ t:=x3; x3:=take←fraction(x3,x)+take←fraction(y3,y); y3:=take←fraction(y3,x)-take←fraction(t,y) @ @<Exit to |found| if an eastward direction occurs at knot |p|@>= theta:=n←arg(x1,y1); if theta>=0 then if phi<=0 then if phi>=theta-one←eighty←deg then goto found; if theta<=0 then if phi>=0 then if phi<=theta+one←eighty←deg then goto found @ In this step we want to use the |crossing←point| routine to find the roots of the quadratic equation $B(y←1,y←2,y←3;t)=0$. Several complications arise: If the quadratic equation has a double root, the curve never crosses zero, and |crossing←point| will find nothing; this case occurs iff $y←1y←3=y←2↑2$ and $y←1y←2<0$. If the quadratic equation has simple roots, or only one root, we may have to negate it so that $B(y←1,y←2,y←3;t)$ crosses from positive to negative at its first root. And finally, we need to do special things if $B(y←1,y←2,y←3;t)$ is identically zero. @ @<Exit to |found| if the curve whose derivatives are specified by...@>= if x1<0 then if x2<0 then if x3<0 then goto done; if ab←vs←cd(y1,y3,y2,y2)=0 then @<Handle the test for eastward directions when $y←1y←3=y←2↑2$; either |goto found| or |goto done|@>; if y1<=0 then if y1<0 then begin y1:=-y1; y2:=-y2; y3:=-y3; end else if y2>0 then begin y2:=-y2; y3:=-y3; end; @<Check the places where $B(y←1,y←2,y←3;t)=0$ to see if $B(x←1,x←2,x←3;t)>=0$@>; done: @ The quadratic polynomial $B(y←1,y←2,y←3;t)$ begins |>=0| and has at most two roots, because we know that it isn't identically zero. It must be admitted that the |crossing←point| routine is not perfectly accurate; rounding errors might cause it to find a root when $y←1y←3>y←2↑2$, or to miss the roots when $y←1y←3<y←2↑2$. The rotation process is itself subject to rounding errors. Yet this code optimistically tries to do the right thing. @d we←found←it==begin tt:=(t+@'4000) div @'10000; goto found; end @<Check the places where $B(y←1,y←2,y←3;t)=0$...@>= t:=crossing←point(y1,y2,y3); if t>fraction←one then goto done; y2:=t←of←the←way(y2)(y3); x1:=t←of←the←way(x1)(x2); x2:=t←of←the←way(x2)(x3); x1:=t←of←the←way(x1)(x2); if x1>=0 then we←found←it; if y2>0 then y2:=0; tt:=t; t:=crossing←point(0,-y2,-y3); if t>fraction←one then goto done; x1:=t←of←the←way(x1)(x2); x2:=t←of←the←way(x2)(x3); if t←of←the←way(x1)(x2)>=0 then begin t:=t←of←the←way(tt)(fraction←one); we←found←it; end @ @<Handle the test for eastward directions when $y←1y←3=y←2↑2$; either |goto found| or |goto done|@>= begin if ab←vs←cd(y1,y2,0,0)<0 then begin t:=make←fraction(y1,y1-y2); x1:=t←of←the←way(x1)(x2); x2:=t←of←the←way(x2)(x3); if t←of←the←way(x1)(x2)>=0 then we←found←it; end else if y3=0 then if y1=0 then @<Exit to |found| if the derivative $B(x←1,x←2,x←3;t)$ becomes |>=0|@> else if x3>=0 then begin tt:=unity; goto found; end; goto done; end @ At this point we know that the derivative of |y(t)| is identically zero, and that |x1<0|; but either |x2>=0| or |x3>=0|, so there's some hope of traveling east. @<Exit to |found| if the derivative $B(x←1,x←2,x←3;t)$ becomes |>=0|...@>= begin t:=crossing←point(-x1,-x2,-x3); if t<=fraction←one then we←found←it; if ab←vs←cd(x1,x3,x2,x2)<=0 then begin t:=make←fraction(x1,x1-x2); we←found←it; end; end @ The intersection of two cubics can be found by an interesting variant of the general bisection scheme described in the introduction to |make←moves|.\ Given $w(t)=B(w←0,w←1,w←2,w←3;t)$ and $z(t)=B(z←0,z←1,z←2,z←3;t)$, we wish to find a pair of times $(t←1,t←2)$ such that $w(t←1)=z(t←2)$, if an intersection exists. First we find the smallest rectangle that encloses the points $\{w←0,w←1,w←2,w←3\}$ and check that it overlaps the smallest rectangle that encloses $\{z←0,z←1,z←2,z←3\}$; if not, the cubics certainly don't intersect. But if the rectangles do overlap, we bisect the intervals, getting new cubics $w'$ and~$w''$, $z'$~and~$z''$; the intersection routine first tries for an intersection between $w'$ and~$z'$, then (if unsuccessful) between $w'$ and~$z''$, then (if still unsuccessful) between $w''$ and~$z'$, finally (if thrice unsuccessful) between $w''$ and~$z''$. After $l$~successful levels of bisection we will have determined the intersection times $t←1$ and~$t←2$ to $l$~bits of accuracy. \def\submin{←{\rm min}} \def\submax{←{\rm max}} As before, it is better to work with the numbers $W←k=2↑l(w←k-w←{k-1})$ and $Z←k=2↑l(z←k-z←{k-1})$ rather than the coefficients $w←k$ and $z←k$ themselves. We also need one other quantity, $\Delta=2↑l(w←0-z←0)$, to determine when the enclosing rectangles overlap. Here's why: The $x$-coordinates of~$w(t)$ are between $u\submin$ and $u\submax$, and the $x$-coordinates of~$z(t)$ are between $x\submin$ and $x\submax$, if we write $w←k=(u←k,v←k)$ and $z←k=(x←k,y←k)$ and $u\submin= \min(u←0,u←1,u←2,u←3)$, etc. These intervals of $x$-coordinates overlap if and only if $u\submin\L x\submax$ and $x\submin\L u\submax$. Letting $$U\submin=\min(0,U←1,U←1+U←2,U←1+U←2+U←3),\; U\submax=\max(0,U←1,U←1+U←2,U←1+U←2+U←3),$$ we have $u\submin=2↑lu←0+U\submin$, etc.; the condition for overlap reduces to $$X\submin-U\submax\L 2↑l(u←0-x←0)\L X\submax-U\submin.$$ Thus we want to maintain the quantity $2↑l(u←0-x←0)$; similarly, the quantity $2↑l(v←0-y←0)$ accounts for the $y$-coordinates. The coordinates of $\Delta=2↑l(w←0-z←0)$ must stay bounded as $l$ increases, because of the overlap condition; i.e., we know that $X\submin$, $X\submax$, and their relatives are bounded, hence $X\submax- U\submin$ and $X\submin-U\submax$ are bounded. @ Incidentally, if the given cubics intersect more than once, the process just sketched will not necessarily find the lexicographically smallest pair $(t←1,t←2)$. The solution actually obtained will be smallest in ``shuffled order''; i.e., if $t←1=(.a←1a←2\ldots a←{16})←2$ and $t←2=(.b←1b←2\ldots b←{16})←2$, then we will minimize $a←1b←1a←2b←2\ldots a←{16}b←{16}$, not $a←1a←2\ldots a←{16}b←1b←2\ldots b←{16}$. Shuffled order agrees with lexicographic order if all pairs of solutions $(t←1,t←2)$ and $(t←1',t←2')$ have the property that $t←1<t←1'$ iff $t←2<t←2'$; but in general, lexicographic order can be quite different, and the bisection algorithm would be substantially less efficient if it were constrained by lexicographic order. For example, suppose that an overlap has been found for $l=3$ and $(t←1,t←2)= (.101,.011)$ in binary, but that no overlap is produced by either of the alternatives $(.1010,.0110)$, $(.1010,.0111)$ at level~4. Then there is probably an intersection in one of the subintervals $(.1011,.011x)$; but lexicographic order would require us to explore $(.1010,.1xxx)$ and $(.1011,.00xx)$ and $(.1011,.010x)$ first. We wouldn't want to store all of the subdivision data for the second path, so the subdivisions would have to be regenerated many times. Such inefficiencies would be associated with every `1' in the binary representation of~$t←1$. @ The subdivision process introduces rounding errors, hence we need to make a more liberal test for overlap. It is not hard to show that the computed values of $U←i$ differ from the truth by at most~$l$, on level~$l$, hence $U\submin$ and $U\submax$ will be at most $3l$ in error. If $\beta$ is an upper bound on the absolute error in the computed components of $\Delta=(|delx|,|dely|)$ on level~$l$, we will replace the test `$X\submin-U\submax\L|delx|$' by the more liberal test `$X\submin-U\submax\L|delx|+|tol|$', where $|tol|=6l+\beta$. More accuracy is obtained if we try the algorithm first with |tol=0|; the more liberal tolerance is used only if an exact approach fails. It is convenient to do this double-take by letting `3' in the preceding paragraph be a parameter, which is first 0, then 3. @<Glob...@>= @!tol←step:0..6; {either 0 or 3, usually} @ We shall use an explicit stack to implement the recursive bisection method described above. In fact, the |bisect←stack| array is available for this purpose. It will contain numerous 5-word packets like $(U←1,U←2,U←3,U\submin,U\submax)$, as well as 20-word packets comprising the 5-word packets for $U$, $V$, $X$, and~$Y$. The following macros define the allocation of stack positions to the quantities needed for bisection-intersection. @d stack←1(#)==bisect←stack[#] {$U←1$, $V←1$, $X←1$, or $Y←1$} @d stack←2(#)==bisect←stack[#+1] {$U←2$, $V←2$, $X←2$, or $Y←2$} @d stack←3(#)==bisect←stack[#+2] {$U←3$, $V←3$, $X←3$, or $Y←3$} @d stack←min(#)==bisect←stack[#+3] {$U\submin$, $V\submin$, $X\submin$, or $Y\submin$} @d stack←max(#)==bisect←stack[#+4] {$U\submax$, $V\submax$, $X\submax$, or $Y\submax$} @d int←packets=20 {number of words to represent $U←k$, $V←k$, $X←k$, and $Y←k$} @# @d u←packet(#)==#-5 @d v←packet(#)==#-10 @d x←packet(#)==#-15 @d y←packet(#)==#-20 @d l←packets==bisect←ptr-int←packets @d r←packets==bisect←ptr @d ul←packet==u←packet(l←packets) {base of $U'←k$ variables} @d vl←packet==v←packet(l←packets) {base of $V'←k$ variables} @d xl←packet==x←packet(l←packets) {base of $X'←k$ variables} @d yl←packet==y←packet(l←packets) {base of $Y'←k$ variables} @d ur←packet==u←packet(r←packets) {base of $U''←k$ variables} @d vr←packet==v←packet(r←packets) {base of $V''←k$ variables} @d xr←packet==x←packet(r←packets) {base of $X''←k$ variables} @d yr←packet==y←packet(r←packets) {base of $Y''←k$ variables} @# @d u1l==stack←1(ul←packet) {$U'←1$} @d u2l==stack←2(ul←packet) {$U'←2$} @d u3l==stack←3(ul←packet) {$U'←3$} @d v1l==stack←1(vl←packet) {$V'←1$} @d v2l==stack←2(vl←packet) {$V'←2$} @d v3l==stack←3(vl←packet) {$V'←3$} @d x1l==stack←1(xl←packet) {$X'←1$} @d x2l==stack←2(xl←packet) {$X'←2$} @d x3l==stack←3(xl←packet) {$X'←3$} @d y1l==stack←1(yl←packet) {$Y'←1$} @d y2l==stack←2(yl←packet) {$Y'←2$} @d y3l==stack←3(yl←packet) {$Y'←3$} @d u1r==stack←1(ur←packet) {$U''←1$} @d u2r==stack←2(ur←packet) {$U''←2$} @d u3r==stack←3(ur←packet) {$U''←3$} @d v1r==stack←1(vr←packet) {$V''←1$} @d v2r==stack←2(vr←packet) {$V''←2$} @d v3r==stack←3(vr←packet) {$V''←3$} @d x1r==stack←1(xr←packet) {$X''←1$} @d x2r==stack←2(xr←packet) {$X''←2$} @d x3r==stack←3(xr←packet) {$X''←3$} @d y1r==stack←1(yr←packet) {$Y''←1$} @d y2r==stack←2(yr←packet) {$Y''←2$} @d y3r==stack←3(yr←packet) {$Y''←3$} @# @d stack←dx==bisect←stack[bisect←ptr] {stacked value of |delx|} @d stack←dy==bisect←stack[bisect←ptr+1] {stacked value of |dely|} @d stack←tol==bisect←stack[bisect←ptr+2] {stacked value of |tol|} @d stack←uv==bisect←stack[bisect←ptr+3] {stacked value of |uv|} @d stack←xy==bisect←stack[bisect←ptr+4] {stacked value of |xy|} @d int←increment=int←packets+int←packets+5 {number of stack words per level} @<Check the ``constant''...@>= if int←packets+17*int←increment>bistack←size then bad:=32; @ Computation of the min and max is a tedious but fairly fast sequence of instructions; exactly four comparisons are made in each branch. @d set←min←max(#)== if stack←1(#)<0 then if stack←3(#)>=0 then begin if stack←2(#)<0 then stack←min(#):=stack←1(#)+stack←2(#) else stack←min(#):=stack←1(#); stack←max(#):=stack←1(#)+stack←2(#)+stack←3(#); if stack←max(#)<0 then stack←max(#):=0; end else begin stack←min(#):=stack←1(#)+stack←2(#)+stack←3(#); if stack←min(#)>stack←1(#) then stack←min(#):=stack←1(#); stack←max(#):=stack←1(#)+stack←2(#); if stack←max(#)<0 then stack←max(#):=0; end else if stack←3(#)<=0 then begin if stack←2(#)>0 then stack←max(#):=stack←1(#)+stack←2(#) else stack←max(#):=stack←1(#); stack←min(#):=stack←1(#)+stack←2(#)+stack←3(#); if stack←min(#)>0 then stack←min(#):=0; end else begin stack←max(#):=stack←1(#)+stack←2(#)+stack←3(#); if stack←max(#)<stack←1(#) then stack←max(#):=stack←1(#); stack←min(#):=stack←1(#)+stack←2(#); if stack←min(#)>0 then stack←min(#):=0; end @ It's convenient to keep the current values of $l$, $t←1$, and $t←2$ in the integer form $2↑l+2↑lt←1$ and $2↑l+2↑lt←2$. The |cubic←intersection| routine uses global variables |cur←t| and |cur←tt| for this purpose; after successful completion, |cur←t| and |cur←tt| will contain |unity| plus the |scaled| values of $t←1$ and $t←2$. The values of |cur←t| and |cur←tt| will be set to zero if |cubic←intersection| finds no intersection. The routine gives up if it has backtracked more than 5000 times (otherwise there are cases where several minutes of fruitless computation would be possible). @d max←patience=5000 @<Glob...@>= @!cur←t,@!cur←tt:integer; {controls and results of |cubic←intersection|} @!time←to←go:integer; {this many backtracks before giving up} @ The given cubics $B(w←0,w←1,w←2,w←3;t)$ and $B(z←0,z←1,z←2,z←3;t)$ are specified in adjacent knot nodes |(p,link(p))| and |(pp,link(pp))|, respectively. @p procedure cubic←intersection(@!p,@!pp:pointer); label continue, not←found, exit; var @!q,@!qq:pointer; {|link(p)|, |link(pp)|} begin time←to←go:=max←patience; @<Initialize for intersections at level zero@>; loop@+ begin continue: if delx-tol<=stack←max(x←packet(xy))-stack←min(u←packet(uv)) then if delx+tol>=stack←min(x←packet(xy))-stack←max(u←packet(uv)) then if dely-tol<=stack←max(y←packet(xy))-stack←min(v←packet(uv)) then if dely+tol>=stack←min(y←packet(xy))-stack←max(v←packet(uv)) then begin if cur←t>=two then {we've done 17 bisections} begin cur←t:=half(cur←t+1); cur←tt:=half(cur←tt+1); return; end; @<Subdivide for a new level of intersection@>; goto continue; end; if time←to←go>0 then decr(time←to←go) else begin cur←t:=0; cur←tt:=0; return; end; @<Advance to the next pair |(cur←t,cur←tt)|@>; end; exit:end; @ The following variables are global, although they are used only by |cubic←intersection|, because it is necessary on some machines to split |cubic←intersection| up into two procedures. @<Glob...@>= @!delx,@!dely:integer; {the components of $\Delta=2↑l(w←0-z←0)$} @!tol:integer; {bound on the uncertainly in the overlap test} @!uv,@!xy:0..bistack←size; {pointers to the current packets of interest} @!three←l:integer; {|tol←step| times the bisection level} @ We shall assume that the coordinates are sufficiently non-extreme that integer overflow will not occur. @<Initialize for intersections at level zero@>= q:=link(p); qq:=link(pp); bisect←ptr:=int←packets;@/ u1r:=right←x(p)-x←coord(p); u2r:=left←x(q)-right←x(p); u3r:=x←coord(q)-left←x(q); set←min←max(ur←packet);@/ v1r:=right←y(p)-y←coord(p); v2r:=left←y(q)-right←y(p); v3r:=y←coord(q)-left←y(q); set←min←max(vr←packet);@/ x1r:=right←x(pp)-x←coord(pp); x2r:=left←x(qq)-right←x(pp); x3r:=x←coord(qq)-left←x(qq); set←min←max(xr←packet);@/ y1r:=right←y(pp)-y←coord(pp); y2r:=left←y(qq)-right←y(pp); y3r:=y←coord(qq)-left←y(qq); set←min←max(yr←packet);@/ delx:=x←coord(p)-x←coord(pp); dely:=y←coord(p)-y←coord(pp);@/ tol:=0; uv:=r←packets; xy:=r←packets; three←l:=0; cur←t:=1; cur←tt:=1 @ @<Subdivide for a new level of intersection@>= stack←dx:=delx; stack←dy:=dely; stack←tol:=tol; stack←uv:=uv; stack←xy:=xy; bisect←ptr:=bisect←ptr+int←increment;@/ double(cur←t); double(cur←tt);@/ u1l:=stack←1(u←packet(uv)); u3r:=stack←3(u←packet(uv)); u2l:=half(u1l+stack←2(u←packet(uv))); u2r:=half(u3r+stack←2(u←packet(uv))); u3l:=half(u2l+u2r); u1r:=u3l; set←min←max(ul←packet); set←min←max(ur←packet);@/ v1l:=stack←1(v←packet(uv)); v3r:=stack←3(v←packet(uv)); v2l:=half(v1l+stack←2(v←packet(uv))); v2r:=half(v3r+stack←2(v←packet(uv))); v3l:=half(v2l+v2r); v1r:=v3l; set←min←max(vl←packet); set←min←max(vr←packet);@/ x1l:=stack←1(x←packet(xy)); x3r:=stack←3(x←packet(xy)); x2l:=half(x1l+stack←2(x←packet(xy))); x2r:=half(x3r+stack←2(x←packet(xy))); x3l:=half(x2l+x2r); x1r:=x3l; set←min←max(xl←packet); set←min←max(xr←packet);@/ y1l:=stack←1(y←packet(xy)); y3r:=stack←3(y←packet(xy)); y2l:=half(y1l+stack←2(y←packet(xy))); y2r:=half(y3r+stack←2(y←packet(xy))); y3l:=half(y2l+y2r); y1r:=y3l; set←min←max(yl←packet); set←min←max(yr←packet);@/ uv:=l←packets; xy:=l←packets; double(delx); double(dely);@/ tol:=tol-three←l+tol←step; double(tol); three←l:=three←l+tol←step @ @<Advance to the next pair |(cur←t,cur←tt)|@>= not←found: if odd(cur←tt) then if odd(cur←t) then @<Descend to the previous level and |goto not←found|@> else begin incr(cur←t); delx:=delx+stack←1(u←packet(uv))+stack←2(u←packet(uv)) +stack←3(u←packet(uv)); dely:=dely+stack←1(v←packet(uv))+stack←2(v←packet(uv)) +stack←3(v←packet(uv)); uv:=uv+int←packets; {switch from |l←packet| to |r←packet|} decr(cur←tt); xy:=xy-int←packets; {switch from |r←packet| to |l←packet|} delx:=delx+stack←1(x←packet(xy))+stack←2(x←packet(xy)) +stack←3(x←packet(xy)); dely:=dely+stack←1(y←packet(xy))+stack←2(y←packet(xy)) +stack←3(y←packet(xy)); end else begin incr(cur←tt); tol:=tol+three←l; delx:=delx-stack←1(x←packet(xy))-stack←2(x←packet(xy)) -stack←3(x←packet(xy)); dely:=dely-stack←1(y←packet(xy))-stack←2(y←packet(xy)) -stack←3(y←packet(xy)); xy:=xy+int←packets; {switch from |l←packet| to |r←packet|} end @ @<Descend to the previous level...@>= begin cur←t:=half(cur←t); cur←tt:=half(cur←tt); if cur←t=0 then return; bisect←ptr:=bisect←ptr-int←increment; delx:=stack←dx; dely:=stack←dy; tol:=stack←tol; uv:=stack←uv; xy:=stack←xy;@/ goto not←found; end @ The |path←intersection| procedure is much simpler (and less interesting). It invokes |cubic←intersection| in lexicographic order until finding a pair of cubics that intersect. The final intersection times are placed in |cur←t| and~|cur←tt|. @p procedure path←intersection(@!h,@!hh:pointer); label exit; var @!p,@!pp:pointer; {link registers that traverse the given paths} @!n,@!nn:integer; {integer parts of intersection times, minus |unity|} begin @<Change one-point paths into dead cycles@>; tol←step:=0; repeat n:=-unity; p:=h; repeat if right←type(p)<>endpoint then begin nn:=-unity; pp:=hh; repeat if right←type(pp)<>endpoint then begin cubic←intersection(p,pp); if cur←t>0 then begin cur←t:=cur←t+n; cur←tt:=cur←tt+nn; return; end; end; nn:=nn+unity; pp:=link(pp); until pp=hh; end; n:=n+unity; p:=link(p); until p=h; tol←step:=tol←step+3; until tol←step>3; cur←t:=-unity; cur←tt:=-unity; exit:end; @ @<Change one-point paths...@>= if right←type(h)=endpoint then begin right←x(h):=x←coord(h); left←x(h):=x←coord(h); right←y(h):=y←coord(h); left←y(h):=y←coord(h); right←type(h):=explicit; end; if right←type(hh)=endpoint then begin right←x(hh):=x←coord(hh); left←x(hh):=x←coord(hh); right←y(hh):=y←coord(hh); left←y(hh):=y←coord(hh); right←type(hh):=explicit; end; @* \[27] Online graphic output. \MF\ displays images on the user's screen by means of a few primitive operations that are defined below. These operations have deliberately been kept simple so that they can be implemented without great difficulty on a wide variety of machines. Since \PASCAL\ has no traditional standards for graphic output, some system-dependent code needs to be written in order to support this aspect of \MF; but the necessary routines are usually quite easy to write. @↑system dependencies@> In fact, there are exactly four such routines: \yskip\hang |init←screen| does whatever initialization is necessary to support the other operations; it is a boolean function that returns |false| if graphic output cannot be supported (e.g., if the other three routines have not been written, or if the user doesn't have the right kind of terminal). \yskip\hang |blank←rectangle| updates a buffer area in memory so that all pixels in a specified rectangle will be set to the background color. \yskip\hang |paint←row| assigns values to specified pixels in a row of the buffer just mentioned, based on ``transition'' indices explained below. \yskip\hang |update←screen| displays the current screen buffer; the effects of |blank←rectangle| and |paint←row| commands may or may not become visible until the next |update←screen| operation is performed. (Thus, |update←screen| is analogous to |update←terminal|.) \yskip\noindent The \PASCAL\ code here is a minimum version of |init←screen| and |update←screen|, usable on \MF\ installations that don't support screen output. If |init←screen| is changed to return |true| instead of false, the other routines will simply log the fact that they have been called; they won't really display anything. The standard test routines for \MF\ use this log information to check that \MF\ is working properly, but the |wlog| instructions should be removed from production versions of \MF. @p function init←screen:boolean; begin init←screen:=false; end; @# procedure update←screen; {will be called only if |init←screen| returns |true|} begin @!init wlog←ln('Calling UPDATESCREEN');@+tini {for testing only} end; @ The user's screen is assumed to be a rectangular area, |screen←width| pixels wide and |screen←depth| pixels deep. The pixel in the upper left corner is said to be in column~0 of row~0; the pixel in the lower right corner is said to be in column |screen←width-1| of row |screen←depth-1|. Notice that row numbers increase from top to bottom, contrary to \MF's other coordinates. Each pixel is assumed to have two states, referred to in this documentation as |black| and |white|. The background color is called |white| and the other color is called |black|; but any two distinct pixel values can actually be used. For example, the author developed \MF\ on a system for which |white| was black and |black| was bright green. @d white=0 {background pixels} @d black=1 {visible pixels} @<Types...@>= @!screen←row=0..screen←depth; {a row number on the screen} @!screen←col=0..screen←width; {a column number on the screen} @!trans←spec=array[screen←col] of screen←col; {a transition spec, see below} @!pixel←color=white..black; {specifies one of the two pixel values} @ We'll illustrate the |blank←rectangle| and |paint←row| operations by pretending to declare a screen buffer called |screen←pixel|. This code is actually commented out, but it does specify the intended effects. @<Glob...@>= @{@!screen←pixel:array[screen←row,screen←col] of pixel←color;@+@} @ The |blank←rectangle| routine simply whitens all pixels that lie in columns |left←col| through |right←col-1|, inclusive, of rows |top←row| through |bot←row-1|, inclusive, given four parameters that satisfy the relations $$\hbox{|0<=left←col<=right←col<=screen←width|,\quad |0<=top←row<=bot←row<=screen←depth|.}$$ If |left←col=right←col| or |top←row=bot←row|, nothing happens. The commented-out code in the following procedure is for illustrative purposes only. @↑system dependencies@> @p procedure blank←rectangle(@!left←col,@!right←col:screen←col; @!top←row,@!bot←row:screen←row); var @!r:screen←row; @!c:screen←col; begin @{@+for r:=top←row to bot←row-1 do for c:=left←col to right←col-1 do screen←pixel[r,c]:=white;@+@}@/ @!init wlog←cr; {this will be done only after |init←screen=true|} wlog←ln('Calling BLANKRECTANGLE(',left←col:1,',', right←col:1,',',top←row:1,',',bot←row:1,')');@+tini end; @ The real work of screen display is done by |paint←row|. But it's not hard work, because the operation affects only one of the screen rows, and it affects only a contiguous set of columns in that row. There are four parameters: |r|~(the row), |b|~(the initial color), |a|~(the array of transition specifications), and |n|~(the number of transitions). The elements of~|a| will satisfy $$0\L a[0]<a[1]<\cdots<a[n]\L |screen←width|;$$ the value of |r| will satisfy |0<=r<screen←depth|; and |n| will be positive. The general idea is to paint blocks of pixels in alternate colors; the precise details are best conveyed by means of a \PASCAL\ program (see the commented-out code below). @↑system dependencies@> @p procedure paint←row(@!r:screen←row;@!b:pixel←color;var @!a:trans←spec; @!n:screen←col); var @!k:screen←col; {an index into |a|} @!c:screen←col; {an index into |screen←pixel|} begin @{ k:=0; c:=a[0]; repeat incr(k); repeat screen←pixel[r,c]:=b; incr(c); until c=a[k]; b:=black-b; {$|black|\swap|white|$} until k=n;@+@}@/ @!init wlog('Calling PAINTROW(',r:1,',',b:1,';'); {this is done only after |init←screen=true|} for k:=0 to n do begin wlog(a[k]:1); if k<>n then wlog(','); end; wlog←ln(')');@+tini end; @ The remainder of \MF's screen routines are system-independent calls on the four primitives just defined. First we have a global boolean variable that tells if |init←screen| has been called, and another one that tells if |init←screen| has given a |true| response. @<Glob...@>= @!screen←started:boolean; {have the screen primitives been initialized?} @!screen←OK:boolean; {is it legitimate to call |blank←rectangle|, |paint←row|, and |update←screen|?} @ @d start←screen==begin if not screen←started then begin screen←OK:=init←screen; screen←started:=true; end; end @<Set init...@>= screen←started:=false; screen←OK:=false; @ \MF\ provides the user with 16 ``window'' areas on the screen, in each of which it is possible to produce independent displays. It should be noted that \MF's windows aren't really independent ``clickable'' entities in the sense of multi-window graphic workstations; \MF\ simply maps them into subsets of a single screen image that is controlled by |init←screen|, |blank←rectangle|, |paint←row|, and |update←screen| as described above. Implementations of \MF\ on a multi-window workstation probably therefore make use of only two windows in the other sense: one for the terminal output and another for the screen with \MF's 16 areas. Henceforth we shall use the term window only in \MF's sense. @<Types...@>= @!window←number=0..15; @ A user doesn't have to use any of the 16 windows. But when a window is ``opened,'' it is allocated to a specific rectangular portion of the screen and to a specific rectangle with respect to \MF's coordinates. The relevant data is stored in global arrays |window←open|, |left←col|, |right←col|, |top←row|, |bot←row|, |m←window|, and |n←window|. The |window←open| array is boolean, and its significance is obvious. The |left←col|, \dots, |bot←row| arrays contain screen coordinates that can be used to blank the entire window with |blank←rectangle|. And the other two arrays just mentioned handle the conversion between actual coordinates and screen coordinates: \MF's pixel in column~$m$ of row~$n$ will appear in screen column |m←window+m| and in screen row |n←window-n|, provided that these lie inside the boundaries of the window. Another array |window←time| holds the number of times this window has been updated. @<Glob...@>= @!window←open:array[window←number] of boolean; {has this window been opened?} @!left←col:array[window←number] of screen←col; {leftmost column position on screen} @!right←col:array[window←number] of screen←col; {rightmost column position, plus~1} @!top←row:array[window←number] of screen←row; {topmost row position on screen} @!bot←row:array[window←number] of screen←row; {bottommost row position, plus~1} @!m←window:array[window←number] of integer; {offset between user and screen columns} @!n←window:array[window←number] of integer; {offset between user and screen rows} @!window←time:array[window←number] of integer; {it has been updated this often} @ @<Set init...@>= for k:=0 to 15 do begin window←open[k]:=false; window←time[k]:=0; end; @ Opening a window isn't like opening a file, because you can open it as often as you like, and you never have to close it again. The idea is simply to define special points on the current screen display. Overlapping window specifications may cause complex effects that can be understood only by scrutinizing \MF's display algorithms; thus it has been left undefined in the \MF\ user manual, although the behavior @:METAFONTbook}{\sl The {\logos METAFONT\/}book@> is in fact predictable. Here is a subroutine that implements the command `\&{openwindow}~|k| \&{from}~$(\\{r0},\\{c0})$ \&{to}~$(\\{r1},\\{c1})$ \&{at}~$(x,y)$'. @p procedure open←a←window(@!k:window←number;@!r0,@!c0,@!r1,@!c1:scaled; @!x,@!y:scaled); var @!m,@!n:integer; {pixel coordinates} begin @<Adjust the coordinates |(r0,c0)| and |(r1,c1)| so that they lie in the proper range@>; window←open[k]:=true; incr(window←time[k]);@/ left←col[k]:=c0; right←col[k]:=c1; top←row[k]:=r0; bot←row[k]:=r1;@/ @<Compute the offsets between screen coordinates and actual coordinates@>; start←screen; if screen←OK then begin blank←rectangle(c0,c1,r0,r1); update←screen; end; end; @ A window whose coordinates don't fit the existing screen size will be truncated until they do. @<Adjust the coordinates |(r0,c0)| and |(r1,c1)|...@>= if r0<0 then r0:=0@+else r0:=round←unscaled(r0); r1:=round←unscaled(r1); if r1>screen←depth then r1:=screen←depth; if r1<r0 then if r0>screen←depth then r0:=r1@+else r1:=r0; if c0<0 then c0:=0@+else c0:=round←unscaled(c0); c1:=round←unscaled(c1); if c1>screen←width then c1:=screen←width; if c1<c0 then if c0>screen←width then c0:=c1@+else c1:=c0 @ Three sets of coordinates are rampant, and they must be kept straight! (i)~\MF's main coordinates refer to the edges between pixels. (ii)~\MF's pixel coordinates (within edge structures) say that the pixel bounded by $(m,n)$, $(m,n+1)$, $(m+1,n)$, and~$(m+1,n+1)$ is in pixel row number~$n$ and pixel column number~$m$. (iii)~Screen coordinates, on the other hand, have rows numbered in increasing order from top to bottom, as mentioned above. @↑coordinates, explained@> The program here first computes integers $m$ and $n$ such that pixel column~$m$ of pixel row~$n$ will be at the upper left corner of the window. Hence pixel column |m-c0| of pixel row |n+r0| will be at the upper left corner of the screen. @<Compute the offsets between screen coordinates and actual coordinates@>= m:=round←unscaled(x); n:=round←unscaled(y)-1;@/ m←window[k]:=c0-m; n←window[k]:=r0+n @ Now here comes \MF's most complicated operation related to window display: Given the number~|k| of an open window, the pixels of positive weight in |cur←edges|will be shown as |black| in the window; all other pixels will be shown as |white|. @p procedure disp←edges(@!k:window←number); label done,found; var @!p,@!q:pointer; {for list manipulation} @!already←there:boolean; {is a previous incarnation in the window?} @!r:integer; {row number} @<Other local variables for |disp←edges|@>@; begin if screen←OK then if left←col[k]<right←col[k] then if top←row[k]<bot←row[k] then begin already←there:=false; if last←window(cur←edges)=k then if last←window←time(cur←edges)=window←time[k] then already←there:=true; if not already←there then blank←rectangle(left←col[k],right←col[k],top←row[k],bot←row[k]); @<Initialize for the display computations@>; p:=link(cur←edges); r:=n←window[k]-(n←min(cur←edges)-zero←field); while (p<>cur←edges)and(r>=top←row[k]) do begin if r<bot←row[k] then @<Display the pixels of edge row |p| in screen row |r|@>; p:=link(p); decr(r); end; update←screen; incr(window←time[k]); last←window(cur←edges):=k; last←window←time(cur←edges):=window←time[k]; end; end; @ Since it takes some work to display a row, we try to avoid recomputation whenever we can. @<Display the pixels of edge row |p| in screen row |r|@>= begin if unsorted(p)>void then sort←edges(p) else if unsorted(p)=void then if already←there then goto done; unsorted(p):=void; {this time we'll paint, but maybe not next time} @<Set up the parameters needed for |paint←row|; but |goto done| if no painting is needed after all@>; paint←row(r,b,row←transition,n); done: end @ The transition-specification parameter to |paint←row| is always the same array. @<Glob...@>= @!row←transition:trans←spec; {an array of |black|/|white| transitions} @ The job remaining is to go through the list |sorted(p)|, unpacking the |info| fields into |m| and weight, then making |black| the pixels whose accumulated weight~|w| is positive. @<Other local variables for |disp←edges|@>= @!n:screen←col; {the highest active index in |row←transition|} @!w,@!ww:integer; {old and new accumulated weights} @!b:pixel←color; {status of first pixel in the row transitions} @!m,@!mm:integer; {old and new screen column positions} @!d:integer; {edge-and-weight without |min←halfword| compensation} @!m←adjustment:integer; {conversion between edge and screen coordinates} @!right←edge:integer; {largest edge-and-weight that could affect the window} @!min←col:screen←col; {the smallest screen column number in the window} @ Some precomputed constants make the display calculations faster. @<Initialize for the display computations@>= m←adjustment:=m←window[k]-m←offset(cur←edges);@/ right←edge:=8*(right←col[k]-m←adjustment);@/ min←col:=left←col[k] @ @<Set up the parameters needed for |paint←row|...@>= n:=0; ww:=0; m:=-1; w:=0; q:=sorted(p); row←transition[0]:=min←col; loop@+ begin if q=sentinel then d:=right←edge else d:=ho(info(q)); mm:=(d div 8)+m←adjustment; if mm<>m then begin @<Record a possible transition in column |m|@>; m:=mm; w:=ww; end; if d>=right←edge then goto found; ww:=ww+(d mod 8)-zero←w; q:=link(q); end; found:@<Wind up the |paint←row| parameter calculation by inserting the final transition; |goto done| if no painting is needed@>; @ Now |m| is a screen column |<right←col[k]|. @<Record a possible transition in column |m|@>= if w<=0 then begin if ww>0 then if m>min←col then begin if n=0 then if already←there then begin b:=white; incr(n); end else b:=black else incr(n); row←transition[n]:=m; end; end else if ww<=0 then if m>min←col then begin if n=0 then b:=black; incr(n); row←transition[n]:=m; end @ If the entire row is |white| in the window area, we can omit painting it when |already←there| is false, since it has already been blanked out in that case. When the following code is invoked, |row←transition[n]| will be strictly less than |right←col[k]|. @<Wind up the |paint←row|...@>= if already←there or(ww>0) then begin if n=0 then if ww>0 then b:=black else b:=white; incr(n); row←transition[n]:=right←col[k]; end else if n=0 then goto done @* \[28] Dynamic linear equations. \MF\ users define variables implicitly by stating equations that should be satisfied; the computer is supposed to be smart enough to solve those equations. And indeed, the computer tries valiantly to do so, by distinguishing five different types of numeric values: \smallskip\hang |type(p)=known| is the nice case, when |value(p)| is the |scaled| value of the variable whose address is~|p|. \smallskip\hang |type(p)=dependent| means that |value(p)| is not present, but |dep←list(p)| points to a {\sl dependency list\/} that expresses the value of variable~|p| as a |scaled| number plus a sum of independent variables with |fraction| coefficients. \smallskip\hang |type(p)=independent| means that |value(p)=m>=0|; each dependent variable that refers to this one is actually referring to the future value of this variable times~$2↑m$. (Usually |value(p)=0|, but higher degrees of scaling are sometimes needed to keep the coefficients in dependency lists from getting too large. The value of~|m| will always be even.) \smallskip\hang |type(p)=numeric←type| means that variable |p| hasn't appeared in an equation before, but it has been explicitly declared to be numeric. \smallskip\hang |type(p)=undefined| means that variable |p| hasn't appeared before. \smallskip\noindent We have actually discussed these five types in the reverse order of their history during a computation: Once |known|, a variable never again becomes |dependent|; once |dependent|, it almost never again becomes |independent|; once |independent|, it never again becomes |numeric←type|; and once |numeric←type|, it never again becomes |undefined| (except of course when the user specifically decides to scrap the old value and start again). A backward step may, however, take place: Sometimes a |dependent| variable becomes |independent| again, when one of the independent variables it depends on is reverting to |undefined|. @ But how are dependency lists represented? Simple; the linear combination $\alpha←1v←1+\cdots+\alpha←kv←k+\beta$ appears in |k+1| value nodes. If |q=dep←list(p)| points to this list, and if |k>0|, then |value(q)= @t$\alpha←1$@>| (which is a |fraction|); |info(q)| points to the location of $\alpha←1$; and |link(p)| points to the dependency list $\alpha←2v←2+\cdots+\alpha←kv←k+\beta$. On the other hand if |k=0|, then |value(q)=@t$\beta$@>| (which is |scaled|) and |info(q)=null|. The independent variables $v←1$, \dots,~$v←k$ have been sorted so that they appear in decreasing order of their locations in~|mem|. The |link| field in the node that contains the constant term $\beta$ is called the {\sl final link\/} of the dependency list. \MF\ maintains a doubly-linked master list of all dependency lists, in terms of a permanently allocated node in |mem| called |dep←head|. If there are no dependencies, we have |link(dep←head)=dep←head| and |prev←dep(dep←head)=dep←head|; otherwise |link(dep←head)| points to the first dependent variable, say~|p|, and |prev←dep(p)=dep←head|. We have |type(p)=dependent|, and |dep←list(p)| points to its dependency list. If the final link of that dependency list occurs in location~|q|, then |link(q)| points to the next dependent variable (say~|r|); and |prev←dep(r)=q|, etc. @d dep←list(#)==link(value←loc(#)) {half of the |value| field in a |dependent| variable} @d prev←dep(#)==info(value←loc(#)) {the other half; makes a doubly linked list} @d dep←node←size=2 {the number of words per dependency node} @<Initialize table entries...@>= link(dep←head):=dep←head; prev←dep(dep←head):=dep←head; info(dep←head):=null; dep←list(dep←head):=null; @ Actually the description above contains a little white lie. There's another kind of variable called |proto←dependent|, which is just like a |dependent| one except that the $\alpha$ coefficients in its dependency list are |scaled| instead of being fractions. Proto-dependency lists are mixed with dependency lists in the nodes reachable from |dep←head|. @ Here is a procedure that prints a dependency list in symbolic form. The second parameter should be either |dependent| or |proto←dependent|, to indicate the scaling of the coefficients. @<Declare subroutines for printing expressions@>= procedure print←dependency(@!p:pointer;@!t:small←number); label exit; var @!v:integer; {a coefficient} @!pp,@!q:pointer; {for list manipulation} begin pp:=p; loop@+ begin v:=abs(value(p)); q:=info(p); if q=null then {the constant term} begin if (v<>0)or(p=pp) then begin if value(p)>0 then if p<>pp then print←char("+"); print←scaled(value(p)); end; return; end; @<Print the coefficient, unless it's $\pm1.0$@>; if type(q)<>independent then confusion("dep"); @:this can't happen dep}{\quad dep@> print←variable←name(q); v:=value(q); while v>0 do begin print("*4"); v:=v-2; end; p:=link(p); end; exit:end; @ @<Print the coefficient, unless it's $\pm1.0$@>= if value(p)<0 then print←char("-") else if p<>pp then print←char("+"); if t=dependent then v:=round←fraction(v); if v<>unity then print←scaled(v) @ The maximum absolute value of a coefficient in a given dependency list is returned by the following simple function. @p function max←coef(@!p:pointer):fraction; var @!x:fraction; {the maximum so far} begin x:=0; while info(p)<>null do begin if abs(value(p))>x then x:=abs(value(p)); p:=link(p); end; max←coef:=x; end; @ One of the main operations needed on dependency lists is to add a multiple of one list to the other; we call this |p←plus←fq|, where |p| and~|q| point to dependency lists and |f| is a fraction. If the coefficient of any independent variable becomes |coef←bound| or more, in absolute value, this procedure changes the type of that variable to `|independent←needing←fix|', and sets the global variable |fix←needed| to~|true|. The value of $|coef←bound|=\mu$ is chosen so that $\mu↑2+\mu<8$; this means that the numbers we deal with won't get too large. (Instead of the ``optimum'' $\mu=(\sqrt{33}-1)/2\approx 2.3723$, the safer value 7/3 is taken as the threshold.) The changes mentioned in the preceding paragraph are actually done only if the global variable |watch←coefs| is |true|. But it usually is; in fact, it is |false| only when \MF\ is making a dependency list that will soon be equated to zero. Several procedures that act on dependency lists, including |p←plus←fq|, set the global variable |dep←final| to the final (constant term) node of the dependency list that they produce. @d coef←bound==@'4525252525 {|fraction| approximation to 7/3} @d independent←needing←fix=0 @<Glob...@>= @!fix←needed:boolean; {does at least one |independent| variable need scaling?} @!watch←coefs:boolean; {should we scale coefficients that exceed |coef←bound|?} @!dep←final:pointer; {location of the constant term and final link} @ @<Set init...@>= fix←needed:=false; watch←coefs:=true; @ The |p←plus←fq| procedure has a fourth parameter, |t|, that should be set to |proto←dependent| if |p| is a proto-dependency list. In this case |f| will be |scaled|, not a |fraction|. Similarly, the fifth parameter~|tt| should be |proto←dependent| if |q| is a proto-dependency list. List |q| is unchanged by the operation; but list |p| is totally destroyed. The final link of the dependency list or proto-dependency list returned by |p←plus←fq| is the same as the original final link of~|p|. Indeed, the constant term of the result will be located in the same |mem| location as the original constant term of~|p|. Coefficients of the result are assumed to be zero if they are less than a certain threshold. This compensates for inevitable rounding errors, and tends to make more variables `|known|'. The threshold is approximately $10↑{-5}$ in the case of normal dependency lists, $10↑{-4}$ for proto-dependencies. @d fraction←threshold=2685 {a |fraction| coefficient less than this is zeroed} @d half←fraction←threshold=1342 {half of |fraction←threshold|} @d scaled←threshold=8 {a |scaled| coefficient less than this is zeroed} @d half←scaled←threshold=4 {half of |scaled←threshold|} @<Declare basic dependency-list subroutines@>= function p←plus←fq(@!p:pointer;@!f:integer;@!q:pointer; @!t,@!tt:small←number):pointer; label done; var @!pp,@!qq:pointer; {|info(p)| and |info(q)|, respectively} @!r,@!s:pointer; {for list manipulation} @!threshold:integer; {defines a neighborhood of zero} @!v:integer; {temporary register} begin if t=dependent then threshold:=fraction←threshold else threshold:=scaled←threshold; r:=temp←head; pp:=info(p); qq:=info(q); loop if pp=qq then if pp=null then goto done else @<Contribute a term from |p|, plus |f| times the corresponding term from |q|@> else if pp<qq then @<Contribute a term from |q|, multiplied by~|f|@> else begin link(r):=p; r:=p; p:=link(p); pp:=info(p); end; done: if t=dependent then value(p):=slow←add(value(p),take←fraction(value(q),f)) else value(p):=slow←add(value(p),take←scaled(value(q),f)); link(r):=p; dep←final:=p; p←plus←fq:=link(temp←head); end; @ @<Contribute a term from |p|, plus |f|...@>= begin if tt=dependent then v:=value(p)+take←fraction(f,value(q)) else v:=value(p)+take←scaled(f,value(q)); value(p):=v; s:=p; p:=link(p); if abs(v)<threshold then free←node(s,dep←node←size) else begin if abs(v)>=coef←bound then if watch←coefs then begin type(qq):=independent←needing←fix; fix←needed:=true; end; link(r):=s; r:=s; end; pp:=info(p); q:=link(q); qq:=info(q); end @ @<Contribute a term from |q|, multiplied by~|f|@>= begin if tt=dependent then v:=take←fraction(f,value(q)) else v:=take←scaled(f,value(q)); if abs(v)>half(threshold) then begin s:=get←node(dep←node←size); info(s):=qq; value(s):=v; if abs(v)>=coef←bound then if watch←coefs then begin type(qq):=independent←needing←fix; fix←needed:=true; end; link(r):=s; r:=s; end; q:=link(q); qq:=info(q); end @ It is convenient to have another subroutine for the special case of |p←plus←fq| when |f=1.0|. In this routine lists |p| and |q| are both of the same type~|t| (either |dependent| or |proto←dependent|). @p function p←plus←q(@!p:pointer;@!q:pointer;@!t:small←number):pointer; label done; var @!pp,@!qq:pointer; {|info(p)| and |info(q)|, respectively} @!r,@!s:pointer; {for list manipulation} @!threshold:integer; {defines a neighborhood of zero} @!v:integer; {temporary register} begin if t=dependent then threshold:=fraction←threshold else threshold:=scaled←threshold; r:=temp←head; pp:=info(p); qq:=info(q); loop if pp=qq then if pp=null then goto done else @<Contribute a term from |p|, plus the corresponding term from |q|@> else if pp<qq then begin s:=get←node(dep←node←size); info(s):=qq; value(s):=value(q); q:=link(q); qq:=info(q); link(r):=s; r:=s; end else begin link(r):=p; r:=p; p:=link(p); pp:=info(p); end; done: value(p):=slow←add(value(p),value(q)); link(r):=p; dep←final:=p; p←plus←q:=link(temp←head); end; @ @<Contribute a term from |p|, plus the...@>= begin v:=value(p)+value(q); value(p):=v; s:=p; p:=link(p); pp:=info(p); if abs(v)<threshold then free←node(s,dep←node←size) else begin if abs(v)>=coef←bound then if watch←coefs then begin type(qq):=independent←needing←fix; fix←needed:=true; end; link(r):=s; r:=s; end; q:=link(q); qq:=info(q); end @ A somewhat simpler routine will multiply a dependency list by a given constant~|v|. The constant is either a |fraction| less than |fraction←one|, or it is |scaled|. In the latter case we might be forced to convert a dependency list to a proto-dependency list. Parameters |t0| and |t1| are the list types before and after; they should agree unless |t0=dependent| and |t1=proto←dependent| and |v←is←scaled=true|. @p function p←times←v(@!p:pointer;@!v:integer; @!t0,@!t1:small←number;@!v←is←scaled:boolean):pointer; var @!r,@!s:pointer; {for list manipulation} @!w:integer; {tentative coefficient} @!threshold:integer; @!scaling←down:boolean; begin if t0<>t1 then scaling←down:=true@+else scaling←down:=not v←is←scaled; if t1=dependent then threshold:=half←fraction←threshold else threshold:=half←scaled←threshold; r:=temp←head; while info(p)<>null do begin if scaling←down then w:=take←fraction(v,value(p)) else w:=take←scaled(v,value(p)); if abs(w)<=threshold then begin s:=link(p); free←node(p,dep←node←size); p:=s; end else begin if abs(w)>=coef←bound then begin fix←needed:=true; type(info(p)):=independent←needing←fix; end; link(r):=p; r:=p; value(p):=w; p:=link(p); end; end; link(r):=p; if v←is←scaled then value(p):=take←scaled(value(p),v) else value(p):=take←fraction(value(p),v); p←times←v:=link(temp←head); end; @ Similarly, we sometimes need to divide a dependency list by a given |scaled| constant. @<Declare basic dependency-list subroutines@>= function p←over←v(@!p:pointer;@!v:scaled; @!t0,@!t1:small←number):pointer; var @!r,@!s:pointer; {for list manipulation} @!w:integer; {tentative coefficient} @!threshold:integer; @!scaling←down:boolean; begin if t0<>t1 then scaling←down:=true@+else scaling←down:=false; if t1=dependent then threshold:=half←fraction←threshold else threshold:=half←scaled←threshold; r:=temp←head; while info(p)<>null do begin if scaling←down then if abs(v)<@'2000000 then w:=make←scaled(value(p),v*@'10000) else w:=make←scaled(round←fraction(value(p)),v) else w:=make←scaled(value(p),v); if abs(w)<=threshold then begin s:=link(p); free←node(p,dep←node←size); p:=s; end else begin if abs(w)>=coef←bound then begin fix←needed:=true; type(info(p)):=independent←needing←fix; end; link(r):=p; r:=p; value(p):=w; p:=link(p); end; end; link(r):=p; value(p):=make←scaled(value(p),v); p←over←v:=link(temp←head); end; @ Here's another utility routine for dependency lists. When an independent variable becomes dependent, we want to remove it from all existing dependencies. The |p←with←x←becoming←q| function computes the dependency list of~|p| after variable~|x| has been replaced by~|q|. This procedure has basically the same calling conventions as |p←plus←fq|: List~|q| is unchanged; list~|p| is destroyed; the constant node and the final link are inherited from~|p|; and the fourth parameter tells whether or not |p| is |proto←dependent|. However, the global variable |dep←final| is not altered if |x| does not occur in list~|p|. @p function p←with←x←becoming←q(@!p,@!x,@!q:pointer;@!t:small←number):pointer; var @!r,@!s:pointer; {for list manipulation} @!v:integer; {coefficient of |x|} begin s:=p; r:=temp←head; while info(s)>x do begin r:=s; s:=link(s); end; if info(s)<>x then p←with←x←becoming←q:=p else begin link(temp←head):=p; link(r):=link(s); v:=value(s); free←node(s,dep←node←size); p←with←x←becoming←q:=p←plus←fq(link(temp←head),v,q,t,dependent); end; end; @ Here's a simple procedure that reports an error when a variable has just received a known value that's out of the required range. @<Declare basic dependency-list subroutines@>= procedure val←too←big(@!x:scaled); begin if internal[warning←check]>0 then begin print←err("Value is too large ("); print←scaled(x); print←char(")"); @.Value is too large@> help4("The equation I just processed has given some variable")@/ ("a value of 4096 or more. Continue and I'll try to cope")@/ ("with that big value; but it might be dangerous.")@/ ("(Set warningcheck:=0 to suppress this message.)"); error; end; end; @ When a dependent variable becomes known, the following routine removes its dependency list. Here |p| points to the variable, and |q| points to the dependency list (which is one node long). @<Declare basic dependency-list subroutines@>= procedure make←known(@!p,@!q:pointer); var @!t:dependent..proto←dependent; {the previous type} begin prev←dep(link(q)):=prev←dep(p); link(prev←dep(p)):=link(q); t:=type(p); type(p):=known; value(p):=value(q); free←node(q,dep←node←size); if abs(value(p))>=fraction←one then val←too←big(value(p)); if internal[tracing←equations]>0 then if interesting(p) then begin begin←diagnostic; print←nl("#### "); @:]]]\#\#\#\#←}{\.{\#\#\#\#}@> print←variable←name(p); print←char("="); print←scaled(value(p)); end←diagnostic(false); end; if cur←exp=p then if cur←type=t then begin cur←type:=known; cur←exp:=value(p); free←node(p,value←node←size); end; end; @ The |fix←dependencies| routine is called into action when |fix←needed| has been triggered. The program keeps a list~|s| of independent variables whose coefficients must be divided by~4. In unusual cases, this fixup process might reduce one or more coefficients to zero, so that a variable will become known more or less by default. @<Declare basic dependency-list subroutines@>= procedure fix←dependencies; label done; var @!p,@!q,@!r,@!s,@!t:pointer; {list manipulation registers} @!x:pointer; {an independent variable} begin r:=link(dep←head); s:=null; while r<>dep←head do begin t:=r; @<Run through the dependency list for variable |t|, fixing all nodes, and ending with final link~|q|@>; r:=link(q); if q=dep←list(t) then make←known(t,q); end; while s<>null do begin p:=link(s); x:=info(s); free←avail(s); s:=p; type(x):=independent; value(x):=value(x)+2; end; fix←needed:=false; end; @ @d independent←being←fixed=1 {this variable already appears in |s|} @<Run through the dependency list for variable |t|...@>= r:=value←loc(t); {|link(r)=dep←list(t)|} loop@+ begin q:=link(r); x:=info(q); if x=null then goto done; if type(x)<=independent←being←fixed then begin if type(x)<independent←being←fixed then begin p:=get←avail; link(p):=s; s:=p; info(s):=x; type(x):=independent←being←fixed; end; value(q):=value(q) div 4; if value(q)=0 then begin link(r):=link(q); free←node(q,dep←node←size); q:=r; end; end; r:=q; end; done: @ The |new←dep| routine installs a dependency list~|p| into the value node~|q|, linking it into the list of all known dependencies. We assume that |dep←final| points to the final node of list~|p|. @p procedure new←dep(@!q,@!p:pointer); var @!r:pointer; {what used to be the first dependency} begin dep←list(q):=p; prev←dep(q):=dep←head; r:=link(dep←head); link(dep←final):=r; prev←dep(r):=dep←final; link(dep←head):=q; end; @ Here is one of the ways a dependency list gets started. The |const←dependency| routine produces a list that has nothing but a constant term. @p function const←dependency(@!v:scaled):pointer; begin dep←final:=get←node(dep←node←size); value(dep←final):=v; info(dep←final):=null; const←dependency:=dep←final; end; @ And here's a more interesting way to start a dependency list: The parameter to |single←dependency| is the location of an independent variable~|x|, and the result is the dependency list `|x+0|'. In the unlikely event that the given independent variable has been doubled so often that we can't refer to it with a nonzero coefficient, |single←dependency| returns the simple list `0'. This case can be recognized by testing that the returned list pointer is equal to |dep←final|. @p function single←dependency(@!p:pointer):pointer; var @!q:pointer; {the new dependency list} begin if value(p)>28 then single←dependency:=const←dependency(0) else begin q:=get←node(dep←node←size); value(q):=two←to←the[28-value(p)]; info(q):=p;@/ link(q):=const←dependency(0); single←dependency:=q; end; end; @ We sometimes need to make an exact copy of a dependency list. @p function copy←dep←list(@!p:pointer):pointer; label done; var @!q:pointer; {the new dependency list} begin q:=get←node(dep←node←size); dep←final:=q; loop@+ begin info(dep←final):=info(p); value(dep←final):=value(p); if info(dep←final)=null then goto done; link(dep←final):=get←node(dep←node←size); dep←final:=link(dep←final); p:=link(p); end; done:copy←dep←list:=q; end; @ But how do variables normally become known? Ah, now we get to the heart of the equation-solving mechanism. The |linear←eq| procedure is given a |dependent| or |proto←dependent| list,~|p|, in which at least one independent variable appears. It equates this list to zero, by choosing an independent variable with the largest coefficient and making it dependent on the others. The newly dependent variable is eliminated from all current dependencies, thereby possibly making other dependent variables known. The given list |p| is, of course, totally destroyed by all this processing. @p procedure linear←eq(@!p:pointer;@!t:small←number); var @!q,@!r,@!s:pointer; {for link manipulation} @!x:pointer; {the variable that loses its independence} @!n:integer; {the number of times |x| had been quartered} @!v:integer; {the coefficient of |x| in list |p|} @!prev←r:pointer; {lags one step behind |r|} @!final←node:pointer; {the constant term of the new dependency list} @!w:integer; {a tentative coefficient} begin @<Find a node |q| in list |p| whose coefficient |v| is largest@>; x:=info(q); n:=value(x);@/ @<Divide list |p| by |-v|, removing node |q|@>; if internal[tracing←equations]>0 then @<Display the new dependency@>; @<Simplify all existing dependencies by substituting for |x|@>; @<Change variable |x| from |independent| to |dependent| or |known|@>; if fix←needed then fix←dependencies; end; @ @<Find a node |q| in list |p| whose coefficient |v| is largest@>= q:=p; r:=link(p); v:=value(q); while info(r)<>null do begin if abs(value(r))>abs(v) then begin q:=r; v:=value(r); end; r:=link(r); end @ Here we want to change the coefficients from |scaled| to |fraction|, except in the constant term. In the common case of a trivial equation like `\.{x=3.14}', we will have |v=-fraction←one|, |q=p|, and |t=dependent|. @<Divide list |p| by |-v|, removing node |q|@>= s:=temp←head; link(s):=p; r:=p; repeat if r=q then begin link(s):=link(r); free←node(r,dep←node←size); end else begin w:=make←fraction(value(r),v); if abs(w)<=half←fraction←threshold then begin link(s):=link(r); free←node(r,dep←node←size); end else begin value(r):=-w; s:=r; end; end; r:=link(s); until info(r)=null; if t=proto←dependent then value(r):=-make←scaled(value(r),v) else if v<>-fraction←one then value(r):=-make←fraction(value(r),v); final←node:=r; p:=link(temp←head) @ @<Display the new dependency@>= if interesting(x) then begin begin←diagnostic; print←nl("## "); print←variable←name(x); @:]]]\#\#←}{\.{\#\#}@> w:=n; while w>0 do begin print("*4"); w:=w-2; end; print←char("="); print←dependency(p,dependent); end←diagnostic(false); end @ @<Simplify all existing dependencies by substituting for |x|@>= prev←r:=dep←head; r:=link(dep←head); while r<>dep←head do begin s:=dep←list(r); q:=p←with←x←becoming←q(s,x,p,type(r)); if info(q)=null then make←known(r,q) else begin dep←list(r):=q; repeat q:=link(q); until info(q)=null; prev←r:=q; end; r:=link(prev←r); end @ @<Change variable |x| from |independent| to |dependent| or |known|@>= if n>0 then @<Divide list |p| by $2↑n$@>; if info(p)=null then begin type(x):=known; value(x):=value(p); if abs(value(x))>=fraction←one then val←too←big(value(x)); free←node(p,dep←node←size); if cur←exp=x then if cur←type=independent then begin cur←exp:=value(x); cur←type:=known; free←node(x,value←node←size); end; end else begin type(x):=dependent; dep←final:=final←node; new←dep(x,p); if cur←exp=x then if cur←type=independent then cur←type:=dependent; end @ @<Divide list |p| by $2↑n$@>= begin s:=temp←head; link(temp←head):=p; r:=p; repeat if n>30 then w:=0 else w:=value(r) div two←to←the[n]; if (abs(w)<=half←fraction←threshold)and(info(r)<>null) then begin link(s):=link(r); free←node(r,dep←node←size); end else begin value(r):=w; s:=r; end; r:=link(s); until info(s)=null; p:=link(temp←head); end @ The |check←mem| procedure, which is used only when \MF\ is being debugged, makes sure that the current dependency lists are well formed. @<Check the list of linear dependencies@>= q:=dep←head; p:=link(q); while p<>dep←head do begin if prev←dep(p)<>q then begin print←nl("Bad PREVDEP at "); print←int(p); @.Bad PREVDEP...@> end; p:=dep←list(p); r:=hi←mem←min; repeat if info(p)>=r then begin print←nl("Out of order at "); print←int(p); @.Out of order...@> end; r:=info(p); q:=p; p:=link(q); until r=null; end @* \[29] Dynamic nonlinear equations. Variables of numeric type are maintained by the general scheme of independent, dependent, and known values that we have just studied; and the components of pair and transform variables are handled in the same way. But \MF\ also has five other types of values: \&{boolean}, \&{string}, \&{pen}, \&{path}, and \&{picture}; what about them? Equations are allowed between nonlinear quantities, but only in a simple form. Two variables that haven't yet been assigned values are either equal to each other, or they're not. Before a boolean variable has received a value, its type is |unknown←boolean|; similarly, there are variables whose type is |unknown←string|, |unknown←pen|, |unknown←path|, and |unknown←picture|. In such cases the value is either |null| (which means that no other variables are equivalent to this one), or it points to another variable of the same undefined type. The pointers in the latter case form a cycle of nodes, which we shall call a ``ring.'' Rings of undefined variables may include capsules, which arise as intermediate results within expressions or as \&{expr} parameters to macros. When one member of a ring receives a value, the same value is given to all the other members. In the case of paths and pictures, this implies making separate copies of a potentially large data structure; users should restrain their enthusiasm for such generality, unless they have lots and lots of memory space. @ The following procedure is called when a capsule node is being added to a ring (e.g., when an unknown variable is mentioned in an expression). @p function new←ring←entry(@!p:pointer):pointer; var q:pointer; {the new capsule node} begin q:=get←node(value←node←size); name←type(q):=capsule; type(q):=type(p); if value(p)=null then value(q):=p@+else value(q):=value(p); value(p):=q; new←ring←entry:=q; end; @ Conversely, we might delete a capsule or a variable before it becomes known. The following procedure simply detaches a quantity from its ring, without recycling the storage. @<Declare the recycling subroutines@>= procedure ring←delete(@!p:pointer); var @!q:pointer; begin q:=value(p); if q<>null then if q<>p then begin while value(q)<>p do q:=value(q); value(q):=value(p); end; end; @ Eventually there might be an equation that assigns values to all of the variables in a ring. The |nonlinear←eq| subroutine does the necessary propagation of values. If the parameter |flush←p| is |true|, node |p| itself needn't receive a value, because it will soon be recycled. @p procedure nonlinear←eq(@!v:integer;@!p:pointer;@!flush←p:boolean); var @!t:small←number; {the type of ring |p|} @!q,@!r:pointer; {link manipulation registers} begin t:=type(p)-unknown←tag; q:=value(p); if flush←p then type(p):=vacuous@+else p:=q; repeat r:=value(q); type(q):=t; case t of boolean←type: value(q):=v; string←type: begin value(q):=v; add←str←ref(v); end; pen←type: begin value(q):=v; add←pen←ref(v); end; path←type: value(q):=copy←path(v); picture←type: value(q):=copy←edges(v); end; {there ain't no more cases} q:=r; until q=p; end; @ If two members of rings are equated, and if they have the same type, the |ring←merge| procedure is called on to make them equivalent. @p procedure ring←merge(@!p,@!q:pointer); label exit; var @!r:pointer; {traverses one list} begin r:=value(p); while r<>p do begin if r=q then begin @<Exclaim about a redundant equation@>; return; end; r:=value(r); end; r:=value(p); value(p):=value(q); value(q):=r; exit:end; @ @<Exclaim about a redundant equation@>= begin print←err("Redundant equation");@/ @.Redundant equation@> help2("I already knew that this equation was true.")@/ ("But perhaps no harm has been done; let's continue.");@/ put←get←error; end @* \[30] Introduction to the syntactic routines. Let's pause a moment now and try to look at the Big Picture. The \MF\ program consists of three main parts: syntactic routines, semantic routines, and output routines. The chief purpose of the syntactic routines is to deliver the user's input to the semantic routines, while parsing expressions and locating operators and operands. The semantic routines act as an interpreter responding to these operators, which may be regarded as commands. And the output routines are periodically called on to produce compact font descriptions that can be used for typesetting or for making interim proof drawings. We have discussed the basic data structures and many of the details of semantic operations, so we are good and ready to plunge into the part of \MF\ that actually controls the activities. Our current goal is to come to grips with the |get←next| procedure, which is the keystone of \MF's input mechanism. Each call of |get←next| sets the value of three variables |cur←cmd|, |cur←mod|, and |cur←sym|, representing the next input token. $$\vbox{\halign{#\hfil\cr \hbox{|cur←cmd| denotes a command code from the long list of codes given earlier;}\cr \hbox{|cur←mod| denotes a modifier of the command code;}\cr \hbox{|cur←sym| is the hash address of the symbolic token that was just scanned,}\cr \hbox{\qquad or zero in the case of a numeric or string or capsule token.}\cr}}$$ Underlying this external behavior of |get←next| is all the machinery necessary to convert from character files to tokens. At a given time we may be only partially finished with the reading of several files (for which \&{input} was specified), and partially finished with the expansion of some user-defined macros and/or some macro parameters, and partially finished reading some text that the user has inserted online, and so on. When reading a character file, the characters must be converted to tokens; comments and blank spaces must be removed, numeric and string tokens must be evaluated. To handle these situations, which might all be present simultaneously, \MF\ uses various stacks that hold information about the incomplete activities, and there is a finite state control for each level of the input mechanism. These stacks record the current state of an implicitly recursive process, but the |get←next| procedure is not recursive. @<Glob...@>= @!cur←cmd: eight←bits; {current command set by |get←next|} @!cur←mod: integer; {operand of current command} @!cur←sym: halfword; {hash address of current symbol} @ The |print←cmd←mod| routine prints a symbolic interpretation of a command code and its modifier. It consists of a rather tedious sequence of print commands, and most of it is essentially an inverse to the |primitive| routine that enters a \MF\ primitive into |hash| and |eqtb|. Therefore almost all of this procedure appears elsewhere in the program, together with the corresponding |primitive| calls. @<Declare the procedure called |print←cmd←mod|@>= procedure print←cmd←mod(@!c,@!m:integer); begin case c of @t\4@>@<Cases of |print←cmd←mod| for symbolic printing of primitives@>@/ othercases print("[unknown command code!]") endcases; end; @ Here is a procedure that displays a given command in braces, in the user's transcript file. @d show←cur←cmd←mod==show←cmd←mod(cur←cmd,cur←mod) @p procedure show←cmd←mod(@!c,@!m:integer); begin begin←diagnostic; print←nl("{"); print←cmd←mod(c,m); print←char("}"); end←diagnostic(false); end; @* \[31] Input stacks and states. The state of \MF's input mechanism appears in the input stack, whose entries are records with five fields, called |index|, |start|, |loc|, |limit|, and |name|. The top element of this stack is maintained in a global variable for which no subscripting needs to be done; the other elements of the stack appear in an array. Hence the stack is declared thus: @<Types...@>= @!in←state←record = record @!index←field: quarterword; @!start←field,@!loc←field, @!limit←field, @!name←field: halfword; end; @ @<Glob...@>= @!input←stack : array[0..stack←size] of in←state←record; @!input←ptr : 0..stack←size; {first unused location of |input←stack|} @!max←in←stack: 0..stack←size; {largest value of |input←ptr| when pushing} @!cur←input : in←state←record; {the ``top'' input state} @ We've already defined the special variable |@!loc==cur←input.loc←field| in our discussion of basic input-output routines. The other components of |cur←input| are defined in the same way: @d index==cur←input.index←field {reference for buffer information} @d start==cur←input.start←field {starting position in |buffer|} @d limit==cur←input.limit←field {end of current line in |buffer|} @d name==cur←input.name←field {name of the current file} @ Let's look more closely now at the five control variables (|index|,~|start|,~|loc|,~|limit|,~|name|), assuming that \MF\ is reading a line of characters that have been input from some file or from the user's terminal. There is an array called |buffer| that acts as a stack of all lines of characters that are currently being read from files, including all lines on subsidiary levels of the input stack that are not yet completed. We will return to the other lines when we are finished with the present input file. (Incidentally, on a machine with byte-oriented addressing, it would be appropriate to combine |buffer| with the |str←pool| array, letting the buffer entries grow downward from the top of the string pool and checking that these two tables don't bump into each other.) The line we are currently working on begins in position |start| of the buffer; the next character we are about to read is |buffer[loc]|; and |limit| is the location of the last character present. We always have |loc<=limit|. For convenience, |buffer[limit]| has been set to |"%"|, so that the end of a line is easily sensed. The |name| variable is a string number that designates the name of the current file, if we are reading a text file. It is 0 if we are reading from the terminal for normal input, or 1 if we are executing a \&{readstring} command, or 2 if we are reading a string that was moved into the buffer by \&{scantokens}. @ Additional information about the current line is available via the |index| variable, which counts how many lines of characters are present in the buffer below the current level. We have |index=0| when reading from the terminal and prompting the user for each line; then if the user types, e.g., `\.{input font}', we will have |index=1| while reading the file \.{font.mf}. However, it does not follow that |index| is the same as the input stack pointer, since many of the levels on the input stack may come from token lists. The global variable |in←open| is equal to the |index| value of the highest non-token-list level. Thus, the number of partially read lines in the buffer is |in←open+1|, and we have |in←open=index| when we are not reading a token list. If we are not currently reading from the terminal, we are reading from the file variable |input←file[index]|. We use the notation |terminal←input| as a convenient abbreviation for |name=0|, and |cur←file| as an abbreviation for |input←file[index]|. The global variable |line| contains the line number in the topmost open file, for use in error messages. If we are not reading from the terminal, |line←stack[index]| holds the line number for the enclosing level, so that |line| can be restored when the current file has been read. If more information about the input state is needed, it can be included in small arrays like those shown here. For example, the current page or segment number in the input file might be put into a variable |@!page|, maintained for enclosing levels in `\ignorespaces|@!page←stack:array[1..max←in←open] of integer|\unskip' by analogy with |line←stack|. @↑system dependencies@> @d terminal←input==(name=0) {are we reading from the terminal?} @d cur←file==input←file[index] {the current |alpha←file| variable} @<Glob...@>= @!in←open : 0..max←in←open; {the number of lines in the buffer, less one} @!input←file : array[1..max←in←open] of alpha←file; @!line : integer; {current line number in the current source file} @!line←stack : array[1..max←in←open] of integer; @ However, all this discussion about input state really applies only to the case that we are inputting from a file. There is another important case, namely when we are currently getting input from a token list. In this case |index>max←in←open|, and the conventions about the other state variables are different: \yskip\hang|loc| is a pointer to the current node in the token list, i.e., the node that will be read next. If |loc=null|, the token list has been fully read. \yskip\hang|start| points to the first node of the token list; this node may or may not contain a reference count, depending on the type of token list involved. \yskip\hang|token←type|, which takes the place of |index| in the discussion above, is a code number that explains what kind of token list is being scanned. \yskip\hang|name| points to the |eqtb| address of the control sequence being expanded, if the current token list is a macro not defined by \&{vardef}. Macros defined by \&{vardef} have |name=null|; their name can be deduced by looking at their first two parameters. \yskip\hang|param←start|, which takes the place of |limit|, tells where the parameters of the current macro or loop text begin in the |param←stack|. \yskip\noindent The |token←type| can take several values, depending on where the current token list came from: \yskip \indent|forever←text|, if the token list being scanned is the body of a \&{forever} loop; \indent|loop←text|, if the token list being scanned is the body of a \&{for} or \&{forsuffixes} loop; \indent|parameter|, if a \&{text} or \&{suffix} parameter is being scanned; \indent|backed←up|, if the token list being scanned has been inserted as `to be read again'. \indent|inserted|, if the token list being scanned has been inserted as part of error recovery; \indent|macro|, if the expansion of a user-defined symbolic token is being scanned. \yskip\noindent The token list begins with a reference count if and only if |token←type= macro|. @↑reference counts@> @d token←type==index {type of current token list} @d token←state==(index>max←in←open) {are we scanning a token list?} @d file←state==(index<=max←in←open) {are we scanning a file line?} @d param←start==limit {base of macro parameters in |param←stack|} @d forever←text=max←in←open+1 {|token←type| code for loop texts} @d loop←text=max←in←open+2 {|token←type| code for loop texts} @d parameter=max←in←open+3 {|token←type| code for parameter texts} @d backed←up=max←in←open+4 {|token←type| code for texts to be reread} @d inserted=max←in←open+5 {|token←type| code for inserted texts} @d macro=max←in←open+6 {|token←type| code for macro replacement texts} @ The |param←stack| is an auxiliary array used to hold pointers to the token lists for parameters at the current level and subsidiary levels of input. This stack grows at a different rate from the others. @<Glob...@>= @!param←stack:array [0..param←size] of pointer; {token list pointers for parameters} @!param←ptr:0..param←size; {first unused entry in |param←stack|} @!max←param←stack:integer; {largest value of |param←ptr|} @ Thus, the ``current input state'' can be very complicated indeed; there can be many levels and each level can arise in a variety of ways. The |show←context| procedure, which is used by \MF's error-reporting routine to print out the current input state on all levels down to the most recent line of characters from an input file, illustrates most of these conventions. The global variable |file←ptr| contains the lowest level that was displayed by this procedure. @<Glob...@>= @!file←ptr:0..stack←size; {shallowest level shown by |show←context|} @ The status at each level is indicated by printing two lines, where the first line indicates what was read so far and the second line shows what remains to be read. The context is cropped, if necessary, so that the first line contains at most |half←error←line| characters, and the second contains at most |error←line|. Non-current input levels whose |token←type| is `|backed←up|' are shown only if they have not been fully read. @p procedure show←context; {prints where the scanner is} label done; var @!old←setting:0..max←selector; {saved |selector| setting} @<Local variables for formatting calculations@>@/ begin file←ptr:=input←ptr; input←stack[file←ptr]:=cur←input; {store current state} loop@+begin cur←input:=input←stack[file←ptr]; {enter into the context} @<Display the current context@>; if file←state then if (name>2) or (file←ptr=0) then goto done; decr(file←ptr); end; done: cur←input:=input←stack[input←ptr]; {restore original state} end; @ @<Display the current context@>= if (file←ptr=input←ptr) or file←state or (token←type<>backed←up) or (loc<>null) then {we omit backed-up token lists that have already been read} begin tally:=0; {get ready to count characters} old←setting:=selector; if file←state then begin @<Print location of current line@>; @<Pseudoprint the line@>; end else begin @<Print type of token list@>; @<Pseudoprint the token list@>; end; selector:=old←setting; {stop pseudoprinting} @<Print two lines using the tricky pseudoprinted information@>; end @ This routine should be changed, if necessary, to give the best possible indication of where the current line resides in the input file. For example, on some systems it is best to print both a page and line number. @↑system dependencies@> @<Print location of current line@>= if name<=1 then if terminal←input and(file←ptr=0) then print←nl("<*>") else print←nl("<insert>") else if name=2 then print←nl("<scantokens>") else begin print←nl("l."); print←int(line); end; print←char(" ") @ @<Print type of token list@>= case token←type of forever←text: print←nl("<forever> "); loop←text: @<Print the current loop value@>; parameter: print←nl("<argument> "); backed←up: if loc=null then print←nl("<recently read> ") else print←nl("<to be read again> "); inserted: print←nl("<inserted text> "); macro: begin print←ln; if name<>null then print(text(name)) else @<Print the name of a \&{vardef}'d macro@>; print("->"); end; othercases print←nl("?") {this should never happen} @.?@> endcases @ The parameter that corresponds to a loop text is either a token list (in the case of \&{forsuffixes}) or a ``capsule'' (in the case of \&{for}). We'll discuss capsules later; for now, all we need to know is that the |link| field in a capsule parameter is |void| and that |print←exp(p,0)| displays the value of capsule~|p| in abbreviated form. @<Print the current loop value@>= begin print←nl("<for("); p:=param←stack[param←start]; if p<>null then if link(p)=void then print←exp(p,0) {we're in a \&{for} loop} else show←token←list(p,null,20,tally); print(")> "); end @ The first two parameters of a macro defined by \&{vardef} will be token lists representing the macro's prefix and ``at point.'' By putting these together, we get the macro's full name. @<Print the name of a \&{vardef}'d macro@>= begin p:=param←stack[param←start]; if p=null then show←token←list(param←stack[param←start+1],null,20,tally) else begin q:=p; while link(q)<>null do q:=link(q); link(q):=param←stack[param←start+1]; show←token←list(p,null,20,tally); link(q):=null; end; end @ Now it is necessary to explain a little trick. We don't want to store a long string that corresponds to a token list, because that string might take up lots of memory; and we are printing during a time when an error message is being given, so we dare not do anything that might overflow one of \MF's tables. So `pseudoprinting' is the answer: We enter a mode of printing that stores characters into a buffer of length |error←line|, where character $k+1$ is placed into \hbox{|trick←buf[k mod error←line]|} if |k<trick←count|, otherwise character |k| is dropped. Initially we set |tally:=0| and |trick←count:=1000000|; then when we reach the point where transition from line 1 to line 2 should occur, we set |first←count:=tally| and |trick←count:=@tmax@>(error←line, tally+1+error←line-half←error←line)|. At the end of the pseudoprinting, the values of |first←count|, |tally|, and |trick←count| give us all the information we need to print the two lines, and all of the necessary text is in |trick←buf|. Namely, let |l| be the length of the descriptive information that appears on the first line. The length of the context information gathered for that line is |k=first←count|, and the length of the context information gathered for line~2 is $m=\min(|tally|, |trick←count|)-k$. If |l+k<=h|, where |h=half←error←line|, we print |trick←buf[0..k-1]| after the descriptive information on line~1, and set |n:=l+k|; here |n| is the length of line~1. If $l+k>h$, some cropping is necessary, so we set |n:=h| and print `\.{...}' followed by $$\hbox{|trick←buf[(l+k-h+3)..k-1]|,}$$ where subscripts of |trick←buf| are circular modulo |error←line|. The second line consists of |n|~spaces followed by |trick←buf[k..(k+m-1)]|, unless |n+m>error←line|; in the latter case, further cropping is done. This is easier to program than to explain. @<Local variables for formatting...@>= @!i:0..buf←size; {index into |buffer|} @!l:integer; {length of descriptive information on line 1} @!m:integer; {context information gathered for line 2} @!n:0..error←line; {length of line 1} @!p: integer; {starting or ending place in |trick←buf|} @!q: integer; {temporary index} @ The following code sets up the print routines so that they will gather the desired information. @d begin←pseudoprint== begin l:=tally; tally:=0; selector:=pseudo; trick←count:=1000000; end @d set←trick←count== begin first←count:=tally; trick←count:=tally+1+error←line-half←error←line; if trick←count<error←line then trick←count:=error←line; end @ And the following code uses the information after it has been gathered. @<Print two lines using the tricky pseudoprinted information@>= if trick←count=1000000 then set←trick←count; {|set←trick←count| must be performed} if tally<trick←count then m:=tally-first←count else m:=trick←count-first←count; {context on line 2} if l+first←count<=half←error←line then begin p:=0; n:=l+first←count; end else begin print("..."); p:=l+first←count-half←error←line+3; n:=half←error←line; end; for q:=p to first←count-1 do print←char(trick←buf[q mod error←line]); print←ln; for q:=1 to n do print←char(" "); {print |n| spaces to begin line~2} if m+n<=error←line then p:=first←count+m else p:=first←count+(error←line-n-3); for q:=first←count to p-1 do print←char(trick←buf[q mod error←line]); if m+n>error←line then print("...") @ But the trick is distracting us from our current goal, which is to understand the input state. So let's concentrate on the data structures that are being pseudoprinted as we finish up the |show←context| procedure. @<Pseudoprint the line@>= begin←pseudoprint; if limit>0 then for i:=start to limit-1 do begin if i=loc then set←trick←count; print(buffer[i]); end @ @<Pseudoprint the token list@>= begin←pseudoprint; if token←type<>macro then show←token←list(start,loc,100000,0) else show←macro(start,loc,100000) @ Here is the missing piece of |show←token←list| that is activated when the token beginning line~2 is about to be shown: @<Do magic computation@>=set←trick←count @* \[32] Maintaining the input stacks. The following subroutines change the input status in commonly needed ways. First comes |push←input|, which stores the current state and creates a new level (having, initially, the same properties as the old). @d push←input==@t@> {enter a new input level, save the old} begin if input←ptr>max←in←stack then begin max←in←stack:=input←ptr; if input←ptr=stack←size then overflow("input stack size",stack←size); @:METAFONT capacity exceeded input stack size}{\quad input stack size@> end; input←stack[input←ptr]:=cur←input; {stack the record} incr(input←ptr); end @ And of course what goes up must come down. @d pop←input==@t@> {leave an input level, re-enter the old} begin decr(input←ptr); cur←input:=input←stack[input←ptr]; end @ Here is a procedure that starts a new level of token-list input, given a token list |p| and its type |t|. If |t=macro|, the calling routine should set |name|, reset~|loc|, and increase the macro's reference count. @d back←list(#)==begin←token←list(#,backed←up) {backs up a simple token list} @p procedure begin←token←list(@!p:pointer;@!t:quarterword); begin push←input; start:=p; token←type:=t; param←start:=param←ptr; loc:=p; end; @ When a token list has been fully scanned, the following computations should be done as we leave that level of input. @↑inner loop@> @p procedure end←token←list; {leave a token-list input level} label done; var @!p:pointer; {temporary register} begin if token←type>=backed←up then {token list to be deleted} if token←type<=inserted then begin flush←token←list(start); goto done; end else delete←mac←ref(start); {update reference count} while param←ptr>param←start do {parameters must be flushed} begin decr(param←ptr); p:=param←stack[param←ptr]; if p<>null then if link(p)=void then {it's an \&{expr} parameter} begin recycle←value(p); free←node(p,value←node←size); end else flush←token←list(p); {it's a \&{suffix} or \&{text} parameter} end; done: pop←input; check←interrupt; end; @ The contents of |cur←cmd,cur←mod,cur←sym| are placed into an equivalent token by the |cur←tok| routine. @↑inner loop@> @p @t\4@>@<Declare the procedure called |make←exp←copy|@>@;@/ function cur←tok:pointer; var @!p:pointer; {a new token node} @!save←type:small←number; {|cur←type| to be restored} @!save←exp:integer; {|cur←exp| to be restored} begin if cur←sym=0 then if cur←cmd=capsule←token then begin save←type:=cur←type; save←exp:=cur←exp; make←exp←copy(cur←mod); p:=stash←cur←exp; link(p):=null; cur←type:=save←type; cur←exp:=save←exp; end else begin p:=get←node(token←node←size); value(p):=cur←mod; name←type(p):=token; if cur←cmd=numeric←token then type(p):=known else type(p):=string←type; end else begin fast←get←avail(p); info(p):=cur←sym; end; cur←tok:=p; end; @ Sometimes \MF\ has read too far and wants to ``unscan'' what it has seen. The |back←input| procedure takes care of this by putting the token just scanned back into the input stream, ready to be read again. If |cur←sym<>0|, the values of |cur←cmd| and |cur←mod| are irrelevant. @p procedure back←input; {undoes one token of input} var @!p:pointer; {a token list of length one} @!s:0..param←size; {value of |param←start| on the current level} begin p:=cur←tok; while token←state and(loc=null) do end←token←list; {conserve stack space} back←list(p); end; @ The |back←error| routine is used when we want to restore or replace an offending token just before issuing an error message. We disable interrupts during the call of |back←input| so that the help message won't be lost. @p procedure back←error; {back up one token and call |error|} begin OK←to←interrupt:=false; back←input; OK←to←interrupt:=true; error; end; @# procedure ins←error; {back up one inserted token and call |error|} begin OK←to←interrupt:=false; back←input; token←type:=inserted; OK←to←interrupt:=true; error; end; @ The |begin←file←reading| procedure starts a new level of input for lines of characters to be read from a file, or as an insertion from the terminal. It does not take care of opening the file, nor does it set |loc| or |limit| or |line|. @↑system dependencies@> @p procedure begin←file←reading; begin if in←open=max←in←open then overflow("text input levels",max←in←open); @:METAFONT capacity exceeded text input levels}{\quad text input levels@> if first=buf←size then overflow("buffer size",buf←size); @:METAFONT capacity exceeded buffer size}{\quad buffer size@> incr(in←open); push←input; index:=in←open; line←stack[index]:=line; start:=first; name:=0; {|terminal←input| is now |true|} end; @ Conversely, the variables must be downdated when such a level of input is finished: @p procedure end←file←reading; begin first:=start; line:=line←stack[index]; if index<>in←open then confusion("endinput"); @:this can't happen endinput}{\quad endinput@> if name>2 then a←close(cur←file); {forget it} pop←input; decr(in←open); end; @ In order to keep the stack from overflowing during a long sequence of inserted `\.{show}' commands, the following routine removes completed error-inserted lines from memory. @p procedure clear←for←error←prompt; begin while file←state and terminal←input and@| (input←ptr>0)and(loc=limit) do end←file←reading; print←ln; clear←terminal; end; @ To get \MF's whole input mechanism going, we perform the following actions. @<Initialize the input routines@>= begin input←ptr:=0; max←in←stack:=0; in←open:=0; max←buf←stack:=0; param←ptr:=0; max←param←stack:=0; first:=1; start:=1; index:=0; line:=0; name:=0; force←eof:=false; if not init←terminal then goto final←end; limit:=last; first:=last+1; {|init←terminal| has set |loc| and |last|} end; @* \[33] Getting the next token. The heart of \MF's input mechanism is the |get←next| procedure, which we shall develop in the next few sections of the program. Perhaps we shouldn't actually call it the ``heart,'' however; it really acts as \MF's eyes and mouth, reading the source files and gobbling them up. And it also helps \MF\ to regurgitate stored token lists that are to be processed again. The main duty of |get←next| is to input one token and to set |cur←cmd| and |cur←mod| to that token's command code and modifier. Furthermore, if the input token is a symbolic token, that token's |hash| address is stored in |cur←sym|; otherwise |cur←sym| is set to zero. Underlying this simple description is a certain amount of complexity because of all the cases that need to be handled, as explained above. However, the inner loop of |get←next| is reasonably short and fast. @ Before getting into |get←next|, we need to consider a mechanism by which \MF\ helps keep errors from propagating too far. Whenever the program goes into a mode where it keeps calling |get←next| repeatedly until a certain condition is met, it sets |scanner←status| to some value other than |normal|. Then if an input file ends, or if an `\&{outer}' symbol appears, an appropriate error recovery will be possible. The global variable |warning←info| helps in this error recovery by providing additional information. For example, |warning←info| might indicate the name of a macro whose replacement text is being scanned. @d normal=0 {|scanner←status| at ``quiet times''} @d skipping=1 {|scanner←status| when false conditional text is being skipped} @d flushing=2 {|scanner←status| when junk after a statement is being ignored} @d absorbing=3 {|scanner←status| when a \&{text} parameter is being scanned} @d var←defining=4 {|scanner←status| when a \&{vardef} is being scanned} @d op←defining=5 {|scanner←status| when a macro \&{def} is being scanned} @d loop←defining=6 {|scanner←status| when a \&{for} loop is being scanned} @<Glob...@>= @!scanner←status:normal..loop←defining; {are we scanning at high speed?} @!warning←info:integer; {if so, what else do we need to know, in case an error occurs during the scanning?} @ @<Initialize the input routines@>= scanner←status:=normal; @ The following subroutine is called when an `\&{outer}' symbolic token has been scanned or when the end of a file has been reached. These two cases are distinguished by |cur←sym|, which is zero at the end of a file. @p function check←outer←validity:boolean; var @!p:pointer; {points to inserted token list} begin if scanner←status=normal then check←outer←validity:=true else begin deletions←allowed:=false; @<Back up an outer symbolic token so that it can be reread@>; if scanner←status>skipping then @<Tell the user what has run away and try to recover@> else begin print←err("Incomplete if; all text was ignored after line "); @.Incomplete if...@> print←int(warning←info);@/ help3("A forbidden `outer' token occurred in skipped text.")@/ ("This kind of error happens when you say `if...' and forget")@/ ("the matching `fi'. I've inserted a `fi'; this might work."); if cur←sym=0 then help←line[2]:=@| "The file ended while I was skipping conditional text."; cur←sym:=frozen←fi; ins←error; end; deletions←allowed:=true; check←outer←validity:=false; end; end; @ @<Back up an outer symbolic token so that it can be reread@>= if cur←sym<>0 then begin p:=get←avail; info(p):=cur←sym; back←list(p); {prepare to read the symbolic token again} end @ @<Tell the user what has run away...@>= begin runaway; {print the definition-so-far} if cur←sym=0 then print←err("File ended") @.File ended while scanning...@> else begin print←err("Forbidden token found"); @.Forbidden token found...@> end; print(" while scanning "); help4("I suspect you have forgotten an `enddef',")@/ ("causing me to read past where you wanted me to stop.")@/ ("I'll try to recover; but if the error is serious,")@/ ("you'd better type `E' or `X' now and fix your file.");@/ case scanner←status of @t\4@>@<Complete the error message, and set |cur←sym| to a token that might help recover from the error@>@; end; {there are no other cases} ins←error; end @ As we consider various kinds of errors, it is also appropriate to change the first line of the help message just given; |help←line[3]| points to the string that might be changed. @<Complete the error message,...@>= flushing: begin print("to the end of the statement"); help←line[3]:="A previous error seems to have propagated,"; cur←sym:=frozen←semicolon; end; absorbing: begin print("a text argument"); help←line[3]:="It seems that a right delimiter was left out,"; if warning←info=0 then cur←sym:=frozen←end←group else begin cur←sym:=frozen←right←delimiter; equiv(frozen←right←delimiter):=warning←info; end; end; var←defining, op←defining: begin print("the definition of "); if scanner←status=op←defining then print(text(warning←info)) else print←variable←name(warning←info); cur←sym:=frozen←end←def; end; loop←defining: begin print("the text of a "); print(text(warning←info)); print(" loop"); help←line[3]:="I suspect you have forgotten an `endfor',"; cur←sym:=frozen←end←for; end; @ The |runaway| procedure displays the first part of the text that occurred when \MF\ began its special |scanner←status|, if that text has been saved. @<Declare the procedure called |runaway|@>= procedure runaway; begin if scanner←status>flushing then begin print←nl("Runaway "); case scanner←status of absorbing: print("text?"); var←defining,op←defining: print("definition?"); loop←defining: print("loop?"); end; {there are no other cases} print←ln; show←token←list(link(temp←head),null,error←line-10,0); end; end; @ We need to mention a procedure that may be called by |get←next|. @p procedure@?firm←up←the←line; forward; @ And now we're ready to take the plunge into |get←next| itself. @d switch=25 {a label in |get←next|} @d start←numeric←token=85 {another} @d start←decimal←token=86 {and another} @d fin←numeric←token=87 {and still another, although |goto| is considered harmful} @p procedure get←next; {sets |cur←cmd|, |cur←mod|, |cur←sym| to next token} @↑inner loop@> label restart, {go here to get the next input token} exit, {go here when the next input token has been got} found, {go here when the end of a symbolic token has been found} switch, {go here to branch on the class of an input character} start←numeric←token,start←decimal←token,fin←numeric←token,done; {go here at crucial stages when scanning a number} var @!k:0..buf←size; {an index into |buffer|} @!c:ASCII←code; {the current character in the buffer} @!class:ASCII←code; {its class number} @!n,@!f:integer; {registers for decimal-to-binary conversion} begin restart: cur←sym:=0; if file←state then @<Input from external file; |goto restart| if no input found, or |return| if a non-symbolic token is found@> else @<Input from token list; |goto restart| if end of list or if a parameter needs to be expanded, or |return| if a non-symbolic token is found@>; @<Finish getting the symbolic token in |cur←sym|; |goto restart| if it is illegal@>; exit:end; @ When a symbolic token is declared to be `\&{outer}', its command code is increased by |outer←tag|. @↑inner loop@> @<Finish getting the symbolic token in |cur←sym|...@>= cur←cmd:=eq←type(cur←sym); cur←mod:=equiv(cur←sym); if cur←cmd>=outer←tag then if check←outer←validity then cur←cmd:=cur←cmd-outer←tag else goto restart @ A percent sign appears in |buffer[limit]|; this makes it unnecessary to have a special test for end-of-line. @↑inner loop@> @<Input from external file;...@>= begin switch: c:=buffer[loc]; incr(loc); class:=char←class[c]; case class of digit←class: goto start←numeric←token; period←class: begin class:=char←class[buffer[loc]]; if class>period←class then goto switch else if class<period←class then {|class=digit←class|} begin n:=0; goto start←decimal←token; end; @:. }{\..\ token@> end; space←class: goto switch; percent←class: begin @<Move to next line of file, or |goto restart| if there is no next line@>; check←interrupt; goto switch; end; string←class: @<Get a string token and |return|@>; isolated←classes: begin k:=loc-1; goto found; end; invalid←class: @<Decry the invalid character and |goto restart|@>; othercases do←nothing {letters, etc.} endcases;@/ k:=loc-1; while char←class[buffer[loc]]=class do incr(loc); goto found; start←numeric←token:@<Get the integer part |n| of a numeric token; set |f:=0| and |goto fin←numeric←token| if there is no decimal point@>; start←decimal←token:@<Get the fraction part |f| of a numeric token@>; fin←numeric←token:@<Pack the numeric and fraction parts of a numeric token and |return|@>; found: cur←sym:=id←lookup(k,loc-k); end @ We go to |restart| instead of to |switch|, because |state| might equal |token←list| after the error has been dealt with (cf.\ |clear←for←error←prompt|). @<Decry the invalid...@>= begin print←err("Text line contains an invalid character"); @.Text line contains...@> help2("A funny symbol that I can't read has just been input.")@/ ("Continue, and I'll forget that it ever happened.");@/ deletions←allowed:=false; error; deletions←allowed:=true; goto restart; end @ @<Get a string token and |return|@>= begin if buffer[loc]="""" then cur←mod:="" else begin k:=loc; buffer[limit+1]:=""""; repeat incr(loc); until buffer[loc]=""""; if loc>limit then @<Decry the missing string delimiter and |goto restart|@>; if loc=k+1 then cur←mod:=buffer[k] else begin str←room(loc-k); repeat append←char(buffer[k]); incr(k); until k=loc; cur←mod:=make←string; end; end; incr(loc); cur←cmd:=string←token; return; end @ We go to |restart| after this error message, instead of to |switch|, because the |clear←for←error←prompt| routine might have reinstated |token←state| after |error| has finished. @<Decry the missing string delimiter and |goto restart|@>= begin loc:=limit; {the next character to be read on this line will be |"%"|} print←err("Incomplete string token has been flushed"); @.Incomplete string token...@> help3("Strings should finish on the same line as they began.")@/ ("I've deleted the partial string; you might want to")@/ ("insert another by typing, e.g., `I""new string""'.");@/ deletions←allowed:=false; error; deletions←allowed:=true; goto restart; end @ @<Get the integer part |n| of a numeric token...@>= n:=c-"0"; while char←class[buffer[loc]]=digit←class do begin if n<4096 then n:=10*n+buffer[loc]-"0"; incr(loc); end; if buffer[loc]="." then if char←class[buffer[loc+1]]=digit←class then goto done; f:=0; goto fin←numeric←token; done: incr(loc) @ @<Get the fraction part |f| of a numeric token@>= k:=0; repeat if k<17 then {digits for |k>=17| cannot affect the result} begin dig[k]:=buffer[loc]-"0"; incr(k); end; incr(loc); until char←class[buffer[loc]]<>digit←class; f:=round←decimals(k); if f=unity then begin incr(n); f:=0; end @ @<Pack the numeric and fraction parts of a numeric token and |return|@>= if n<4096 then cur←mod:=n*unity+f else begin print←err("Enormous number has been reduced"); @.Enormous number...@> help2("I can't handle numbers bigger than about 4095.99998;")@/ ("so I've changed your constant to that maximum amount.");@/ deletions←allowed:=false; error; deletions←allowed:=true; cur←mod:=@'1777777777; end; cur←cmd:=numeric←token; return @ Let's consider now what happens when |get←next| is looking at a token list. @↑inner loop@> @<Input from token list;...@>= if loc>=hi←mem←min then {one-word token} begin cur←sym:=info(loc); loc:=link(loc); {move to next} if cur←sym>=expr←base then if cur←sym>=suffix←base then @<Insert a suffix or text parameter and |goto restart|@> else begin cur←cmd:=capsule←token; cur←mod:=param←stack[param←start+cur←sym-(expr←base)]; cur←sym:=0; return; end; end else if loc>null then @<Get a stored numeric or string or capsule token and |return|@> else begin {we are done with this token list} end←token←list; goto restart; {resume previous level} end @ @<Insert a suffix or text parameter...@>= begin if cur←sym>=text←base then cur←sym:=cur←sym-param←size; {|param←size=text←base-suffix←base|} begin←token←list(param←stack[param←start+cur←sym-(suffix←base)],parameter); goto restart; end @ @<Get a stored numeric or string or capsule token...@>= begin if name←type(loc)=token then begin cur←mod:=value(loc); if type(loc)=known then cur←cmd:=numeric←token else begin cur←cmd:=string←token; add←str←ref(cur←mod); end; end else begin cur←mod:=loc; cur←cmd:=capsule←token; end; loc:=link(loc); return; end @ All of the easy branches of |get←next| have now been taken care of. There is one more branch. @<Move to next line of file, or |goto restart|...@>= if name>2 then @<Read next line of file into |buffer|, or |goto restart| if the file has ended@> else begin if input←ptr>0 then {text was inserted during error recovery or by \&{scantokens}} begin end←file←reading; goto restart; {resume previous level} end; if selector<log←only then open←log←file; if interaction>nonstop←mode then begin if limit=start then {previous line was empty} print←nl("(Please type a command or say `end')"); @.Please type...@> print←ln; first:=start; prompt←input("*"); {input on-line into |buffer|} @.*@> limit:=last; buffer[limit]:="%"; first:=limit+1; loc:=start; end else fatal←error("*** (job aborted, no legal end found)"); @.job aborted@> {nonstop mode, which is intended for overnight batch processing, never waits for on-line input} end @ The global variable |force←eof| is normally |false|; it is set |true| by an \&{endinput} command. @<Glob...@>= @!force←eof:boolean; {should the next \&{input} be aborted early?} @ @<Read next line of file into |buffer|, or |goto restart| if the file has ended@>= begin incr(line); first:=start; if not force←eof then begin if input←ln(cur←file,true) then {not end of file} firm←up←the←line {this sets |limit|} else force←eof:=true; end; if force←eof then begin print←char(")"); force←eof:=false; update←terminal; {show user that file has been read} end←file←reading; {resume previous level} if check←outer←validity then goto restart@+else goto restart; end; buffer[limit]:="%"; first:=limit+1; loc:=start; {ready to read} end @ If the user has set the |pausing| parameter to some positive value, and if nonstop mode has not been selected, each line of input is displayed on the terminal and the transcript file, followed by `\.{=>}'. \MF\ waits for a response. If the response is null (i.e., if nothing is typed except perhaps a few blank spaces), the original line is accepted as it stands; otherwise the line typed is used instead of the line in the file. @p procedure firm←up←the←line; var @!k:0..buf←size; {an index into |buffer|} begin limit:=last; if internal[pausing]>0 then if interaction>nonstop←mode then begin wake←up←terminal; print←ln; if start<limit then for k:=start to limit-1 do print(buffer[k]); first:=limit; prompt←input("=>"); {wait for user response} @.=>@> if last>first then begin for k:=first to last-1 do {move line down in buffer} buffer[k+start-first]:=buffer[k]; limit:=start+last-first; end; end; end; @* \[34] Scanning macro definitions. \MF\ has a variety of ways to tuck tokens away into token lists for later use: Macros can be defined with \&{def}, \&{vardef}, \&{primarydef}, etc.; repeatable code can be defined with \&{for}, \&{forever}, \&{forsuffixes}. All such operations are handled by the routines in this part of the program. The modifier part of each command code is zero for the ``ending delimiters'' like \&{enddef} and \&{endfor}. @d start←def=1 {command modifier for \&{def}} @d var←def=2 {command modifier for \&{vardef}} @d end←def=0 {command modifier for \&{enddef}} @d start←forever=1 {command modifier for \&{forever}} @d end←for=0 {command modifier for \&{endfor}} @<Put each...@>= primitive("def",macro←def,start←def);@/ @!@:def←}{\&{def} primitive@> primitive("vardef",macro←def,var←def);@/ @!@:var←def←}{\&{vardef} primitive@> primitive("primarydef",macro←def,secondary←primary←macro);@/ @!@:primary←def←}{\&{primarydef} primitive@> primitive("secondarydef",macro←def,tertiary←secondary←macro);@/ @!@:secondary←def←}{\&{secondarydef} primitive@> primitive("tertiarydef",macro←def,expression←tertiary←macro);@/ @!@:tertiary←def←}{\&{tertiarydef} primitive@> primitive("enddef",macro←def,end←def); eqtb[frozen←end←def]:=eqtb[cur←sym];@/ @!@:end←def←}{\&{enddef} primitive@> @# primitive("for",iteration,expr←base);@/ @!@:for←}{\&{for} primitive@> primitive("forsuffixes",iteration,suffix←base);@/ @!@:for←suffixes←}{\&{forsuffixes} primitive@> primitive("forever",iteration,start←forever);@/ @!@:forever←}{\&{forever} primitive@> primitive("endfor",iteration,end←for); eqtb[frozen←end←for]:=eqtb[cur←sym];@/ @!@:end←for←}{\&{endfor} primitive@> @ @<Cases of |print←cmd...@>= macro←def:if m<=var←def then if m=start←def then print("def") else if m<start←def then print("enddef") else print("vardef") else if m=secondary←primary←macro then print("primarydef") else if m=tertiary←secondary←macro then print("secondarydef") else print("tertiarydef"); iteration: if m<=start←forever then if m=start←forever then print("forever")@+else print("endfor") else if m=expr←base then print("for")@+else print("forsuffixes"); @ Different macro-absorbing operations have different syntaxes, but they also have a lot in common. There is a list of special symbols that are to be replaced by parameter tokens; there is a special command code that ends the definition; the quotation conventions are identical. Therefore it makes sense to have most of the work done by a single subroutine. That subroutine is called |scan←toks|. The first parameter to |scan←toks| is the command code that will terminate scanning (either |macro←def|, |loop←repeat|, or |iteration|). The second parameter, |subst←list|, points to a (possibly empty) list of two-word nodes whose |info| and |value| fields specify symbol tokens before and after replacement. The list will be returned to free storage by |scan←toks|. The third parameter is simply appended to the token list that is built. And the final parameter tells how many of the special operations \.{\#\AT!}, \.{\AT!}, and \.{\AT!\#} are to be replaced by suffix parameters. When such parameters are present, they are called \.{(SUFFIX0)}, \.{(SUFFIX1)}, and \.{(SUFFIX2)}. @p function scan←toks(@!terminator:command←code; @!subst←list,@!tail←end:pointer;@!suffix←count:small←number):pointer; label done,found; var @!p:pointer; {tail of the token list being built} @!q:pointer; {temporary for link management} @!balance:integer; {left delimiters minus right delimiters} begin p:=temp←head; balance:=1; link(temp←head):=null; loop@+ begin get←next; if cur←sym>0 then begin @<Substitute for |cur←sym|, if it's on the |subst←list|@>; if cur←cmd=terminator then @<Adjust the balance; |goto done| if it's zero@> else if cur←cmd=macro←special then @<Handle quoted symbols, \.{\#\AT!}, \.{\AT!}, or \.{\AT!\#}@>; end; link(p):=cur←tok; p:=link(p); end; done: link(p):=tail←end; flush←node←list(subst←list); scan←toks:=link(temp←head); end; @ @<Substitute for |cur←sym|...@>= begin q:=subst←list; while q<>null do begin if info(q)=cur←sym then begin cur←sym:=value(q); cur←cmd:=relax; goto found; end; q:=link(q); end; found:end @ @<Adjust the balance; |goto done| if it's zero@>= if cur←mod>0 then incr(balance) else begin decr(balance); if balance=0 then goto done; end @ Four commands are intended to be used only within macro texts: \&{quote}, \.{\#\AT!}, \.{\AT!}, and \.{\AT!\#}. They are variants of a single command code called |macro←special|. @d quote=0 {|macro←special| modifier for \&{quote}} @d macro←prefix=1 {|macro←special| modifier for \.{\#\AT!}} @d macro←at=2 {|macro←special| modifier for \.{\AT!}} @d macro←suffix=3 {|macro←special| modifier for \.{\AT!\#}} @<Put each...@>= primitive("quote",macro←special,quote);@/ @!@:quote←}{\&{quote} primitive@> primitive("#@@",macro←special,macro←prefix);@/ @!@:]]]\#\AT!←}{\.{\#\AT!} primitive@> primitive("@@",macro←special,macro←at);@/ @!@:]]]\AT!←}{\.{\AT!} primitive@> primitive("@@#",macro←special,macro←suffix);@/ @!@:]]]\AT!\#←}{\.{\AT!\#} primitive@> @ @<Cases of |print←cmd...@>= macro←special: case m of macro←prefix: print("#@@"); macro←at: print←char("@@"); macro←suffix: print("@@#"); othercases print("quote") endcases; @ @<Handle quoted...@>= begin if cur←mod=quote then get←next else if cur←mod<=suffix←count then cur←sym:=suffix←base-1+cur←mod; end @ Here is a routine that's used whenever a token will be redefined. If the user's token is unredefinable, the `|frozen←inaccessible|' token is substituted; the latter is redefinable but essentially impossible to use, hence \MF's tables won't get fouled up. @p procedure get←symbol; {sets |cur←sym| to a safe symbol} label restart; begin restart: get←next; if (cur←sym=0)or(cur←sym>frozen←inaccessible) then begin print←err("Missing symbolic token inserted"); @.Missing symbolic token...@> help3("Sorry: You can't redefine a number, string, or expr.")@/ ("I've inserted an inaccessible symbol so that your")@/ ("definition will be completed without mixing me up too badly."); if cur←sym>0 then help←line[2]:="Sorry: You can't redefine my error-recovery tokens." else if cur←cmd=string←token then delete←str←ref(cur←mod); cur←sym:=frozen←inaccessible; ins←error; goto restart; end; end; @ Before we actually redefine a symbolic token, we need to clear away its former value, if it was a variable. The following stronger version of |get←symbol| does that. @p procedure get←clear←symbol; begin get←symbol; clear←symbol(cur←sym,false); end; @ Here's another little subroutine; it checks that an equals sign or assignment sign comes along at the proper place in a macro definition. @p procedure check←equals; begin if cur←cmd<>equals then if cur←cmd<>assignment then begin missing←err("=");@/ @.Missing `='@> help5("The next thing in this `def' should have been `=',")@/ ("because I've already looked at the definition heading.")@/ ("But don't worry; I'll pretend that an equals sign")@/ ("was present. Everything from here to `enddef'")@/ ("will be the replacement text of this macro."); back←error; end; end; @ A \&{primarydef}, \&{secondarydef}, or \&{tertiarydef} is rather easily handled now that we have |scan←toks|. In this case there are two parameters, which will be \.{EXPR0} and \.{EXPR1} (i.e., |expr←base| and |expr←base+1|). @p procedure make←op←def; var @!m:command←code; {the type of definition} @!p,@!q,@!r:pointer; {for list manipulation} begin m:=cur←mod;@/ get←symbol; q:=get←node(token←node←size); info(q):=cur←sym; value(q):=expr←base;@/ get←clear←symbol; warning←info:=cur←sym;@/ get←symbol; p:=get←node(token←node←size); info(p):=cur←sym; value(p):=expr←base+1; link(p):=q;@/ get←next; check←equals;@/ scanner←status:=op←defining; q:=get←avail; ref←count(q):=null; r:=get←avail; link(q):=r; info(r):=general←macro; link(r):=scan←toks(macro←def,p,null,0); scanner←status:=normal; eq←type(warning←info):=m; equiv(warning←info):=q; get←x←next; end; @ Parameters to macros are introduced by the keywords \&{expr}, \&{suffix}, \&{text}, \&{primary}, \&{secondary}, and \&{tertiary}. @<Put each...@>= primitive("expr",param←type,expr←base);@/ @!@:expr←}{\&{expr} primitive@> primitive("suffix",param←type,suffix←base);@/ @!@:suffix←}{\&{suffix} primitive@> primitive("text",param←type,text←base);@/ @!@:text←}{\&{text} primitive@> primitive("primary",param←type,primary←macro);@/ @!@:primary←}{\&{primary} primitive@> primitive("secondary",param←type,secondary←macro);@/ @!@:secondary←}{\&{secondary} primitive@> primitive("tertiary",param←type,tertiary←macro);@/ @!@:tertiary←}{\&{tertiary} primitive@> @ @<Cases of |print←cmd...@>= param←type:if m>=expr←base then if m=expr←base then print("expr") else if m=suffix←base then print("suffix") else print("text") else if m<secondary←macro then print("primary") else if m=secondary←macro then print("secondary") else print("tertiary"); @ Let's turn next to the more complex processing associated with \&{def} and \&{vardef}. When the following procedure is called, |cur←mod| should be either |start←def| or |var←def|. @p @t\4@>@<Declare the procedure called |check←delimiter|@>@; @t\4@>@<Declare the function called |scan←declared←variable|@>@; procedure scan←def; var @!m:start←def..var←def; {the type of definition} @!n:0..3; {the number of special suffix parameters} @!k:0..param←size; {the total number of parameters} @!c:general←macro..text←macro; {the kind of macro we're defining} @!r:pointer; {parameter-substitution list} @!q:pointer; {tail of the macro token list} @!p:pointer; {temporary storage} @!base:halfword; {|expr←base|, |suffix←base|, or |text←base|} @!l←delim,@!r←delim:pointer; {matching delimiters} begin m:=cur←mod; c:=general←macro; link(temp←head):=null;@/ q:=get←avail; ref←count(q):=null; r:=null;@/ @<Scan the token or variable to be defined; set |n|, |scanner←status|, and |warning←info|@>; k:=n; if cur←cmd=left←delimiter then @<Absorb delimited parameters, putting them into lists |q| and |r|@>; if cur←cmd=param←type then @<Absorb undelimited parameters, putting them into list |r|@>; check←equals; p:=get←avail; info(p):=c; link(q):=p; @<Attach the replacement text to the tail of node |p|@>; scanner←status:=normal; get←x←next; end; @ We don't put `|frozen←end←group|' into the replacement text of a \&{vardef}, because the user may want to redefine `\.{endgroup}'. @<Attach the replacement text to the tail of node |p|@>= if m=start←def then link(p):=scan←toks(macro←def,r,null,n) else begin q:=get←avail; info(q):=bg←loc; link(p):=q; p:=get←avail; info(p):=eg←loc; link(q):=scan←toks(macro←def,r,p,n); end; if warning←info=bad←vardef then flush←token←list(value(bad←vardef)) @ @<Glob...@>= @!bg←loc,@!eg←loc:1..hash←end; {hash addresses of `\.{begingroup}' and `\.{endgroup}'} @ @<Scan the token or variable to be defined;...@>= if m=start←def then begin get←clear←symbol; warning←info:=cur←sym; get←next; scanner←status:=op←defining; n:=0; eq←type(warning←info):=defined←macro; equiv(warning←info):=q; end else begin p:=scan←declared←variable; flush←variable(equiv(info(p)),link(p),true); warning←info:=find←variable(p); flush←list(p); if warning←info=null then @<Change to `\.{a bad variable}'@>; scanner←status:=var←defining; n:=2; if cur←cmd=macro←special then if cur←mod=macro←suffix then {\.{\AT!\#}} begin n:=3; get←next; end; type(warning←info):=unsuffixed←macro-2+n; value(warning←info):=q; end {|suffixed←macro=unsuffixed←macro+1|} @ @<Change to `\.{a bad variable}'@>= begin print←err("This variable already starts with a macro"); @.This variable already...@> help2("After `vardef a' you can't say `vardef a.b'.")@/ ("So I'll have to discard this definition."); error; warning←info:=bad←vardef; end @ @<Initialize table entries...@>= name←type(bad←vardef):=root; link(bad←vardef):=frozen←bad←vardef; equiv(frozen←bad←vardef):=bad←vardef; eq←type(frozen←bad←vardef):=tag←token; @ @<Absorb delimited parameters, putting them into lists |q| and |r|@>= repeat l←delim:=cur←sym; r←delim:=cur←mod; get←next; if (cur←cmd=param←type)and(cur←mod>=expr←base) then base:=cur←mod else begin print←err("Missing parameter type; `expr' will be assumed"); @.Missing parameter type@> help1("You should've had `expr' or `suffix' or `text' here."); back←error; base:=expr←base; end; @<Absorb parameter tokens for type |base|@>; check←delimiter(l←delim,r←delim); get←next; until cur←cmd<>left←delimiter @ @<Absorb parameter tokens for type |base|@>= repeat link(q):=get←avail; q:=link(q); info(q):=base+k;@/ get←symbol; p:=get←node(token←node←size); value(p):=base+k; info(p):=cur←sym; if k=param←size then overflow("parameter stack size",param←size); @:METAFONT capacity exceeded parameter stack size}{\quad parameter stack size@> incr(k); link(p):=r; r:=p; get←next; until cur←cmd<>comma @ @<Absorb undelimited parameters, putting them into list |r|@>= begin p:=get←node(token←node←size); if cur←mod<expr←base then begin c:=cur←mod; value(p):=expr←base+k; end else begin value(p):=cur←mod+k; if cur←mod=expr←base then c:=expr←macro else if cur←mod=suffix←base then c:=suffix←macro else c:=text←macro; end; if k=param←size then overflow("parameter stack size",param←size); incr(k); get←symbol; info(p):=cur←sym; link(p):=r; r:=p; get←next; if c=expr←macro then if cur←cmd=of←token then begin c:=of←macro; p:=get←node(token←node←size); if k=param←size then overflow("parameter stack size",param←size); value(p):=expr←base+k; get←symbol; info(p):=cur←sym; link(p):=r; r:=p; get←next; end; end @* \[35] Expanding the next token. Only a few command codes |<min←command| can possibly be returned by |get←next|; in increasing order, they are |if←test|, |fi←or←else|, |input|, |iteration|, |repeat←loop|, |exit←test|, |relax|, |expand←after|, |scan←tokens|, and |defined←macro|. \MF\ usually gets the next token of input by saying |get←x←next|. This is like |get←next| except that it keeps getting more tokens until finding |cur←cmd>=min←command|. In other words, |get←x←next| expands macros and removes conditionals or iterations or input instructions that might be present. It follows that |get←x←next| might invoke itself recursively. In fact, there is massive recursion, since macro expansion can involve the scanning of arbitrarily complex expressions, which in turn involve macro expansion and conditionals, etc. @↑recursion@> Therefore it's necessary to declare a whole bunch of |forward| procedures at this point, and to insert some other procedures that will be invoked by |get←x←next|. @p procedure@?scan←primary; forward;@t\2@> procedure@?scan←secondary; forward;@t\2@> procedure@?scan←tertiary; forward;@t\2@> procedure@?scan←expression; forward;@t\2@> procedure@?scan←suffix; forward;@t\2@>@/ @t\4@>@<Declare the procedure called |macro←call|@>@;@/ procedure@?get←boolean; forward;@t\2@> procedure@?pass←text; forward;@t\2@> procedure@?conditional; forward;@t\2@> procedure@?start←input; forward;@t\2@> procedure@?begin←iteration; forward;@t\2@> procedure@?resume←iteration; forward;@t\2@> procedure@?stop←iteration; forward;@t\2@> @ An auxiliary subroutine called |expand| is used by |get←x←next| when it has to do exotic expansion commands. @p procedure expand; var @!p:pointer; {for list manipulation} @!k:integer; {something that we hope is |<=buf←size|} @!j:pool←pointer; {index into |str←pool|} begin if internal[tracing←commands]>unity then if cur←cmd<>defined←macro then show←cur←cmd←mod; case cur←cmd of if←test:conditional; {this procedure is discussed in Part 36 below} fi←or←else:@<Terminate the current conditional and skip to \&{fi}@>; input:@<Initiate or terminate input from a file@>; iteration:if cur←mod=end←for then @<Scold the user for having an extra \&{endfor}@> else begin←iteration; {this procedure is discussed in Part 37 below} repeat←loop: @<Repeat a loop@>; exit←test: @<Exit a loop if the proper time has come@>; relax: do←nothing; expand←after: @<Expand the token after the next token@>; scan←tokens: @<Put a string into the input buffer@>; defined←macro:macro←call(cur←mod,null,cur←sym); end; {there are no other cases} end; @ @<Scold the user...@>= begin print←err("Extra `endfor'"); @.Extra `endfor'@> help2("I'm not currently working on a for loop,")@/ ("so I had better not try to end anything.");@/ error; end @ The processing of \&{input} involves the |start←input| subroutine, which will be declared later; the processing of \&{endinput} is trivial. @<Put each...@>= primitive("input",input,0);@/ @!@:input←}{\&{input} primitive@> primitive("endinput",input,1);@/ @!@:end←input←}{\&{endinput} primitive@> @ @<Cases of |print←cmd←mod|...@>= input: if m=0 then print("input")@+else print("endinput"); @ @<Initiate or terminate input...@>= if cur←mod>0 then force←eof:=true else start←input @ We'll discuss the complicated parts of loop operations later. For now it suffices to know that there's a global variable called |loop←ptr| that will be |null| if no loop is in progress. @<Repeat a loop@>= begin while token←state and(loc=null) do end←token←list; {conserve stack space} if loop←ptr=null then begin print←err("Lost loop"); @.Lost loop@> help2("I'm confused; after exiting from a loop, I still seem")@/ ("to want to repeat it. I'll try to forget the problem.");@/ error; end else resume←iteration; {this procedure is in Part 37 below} end @ @<Exit a loop if the proper time has come@>= begin get←boolean; if internal[tracing←commands]>unity then show←cmd←mod(nullary,cur←exp); if cur←exp=true←code then if loop←ptr=null then begin print←err("No loop is in progress"); @.No loop is in progress@> help1("Why say `exitif' when there's nothing to exit from?"); if cur←cmd=semicolon then error@+else back←error; end else @<Exit prematurely from an iteration@> else if cur←cmd<>semicolon then begin missing←err(";");@/ @.Missing `;'@> help2("After `exitif <boolean exp>' I expect to see a semicolon.")@/ ("I shall pretend that one was there."); back←error; end; end @ Here we use the fact that |forever←text| is the only |token←type| that is less than |loop←text|. @<Exit prematurely...@>= begin p:=null; repeat if file←state then end←file←reading else begin if token←type<=loop←text then p:=start; end←token←list; end; until p<>null; if p<>info(loop←ptr) then fatal←error("*** (loop confusion)"); @.loop confusion@> stop←iteration; {this procedure is in Part 37 below} end @ @<Expand the token after the next token@>= begin get←next; p:=cur←tok; get←next; if cur←cmd<min←command then expand else back←input; back←list(p); end @ @<Put a string into the input buffer@>= begin get←x←next; scan←primary; if cur←type<>string←type then begin disp←err(null,"Not a string"); @.Not a string@> help2("I'm going to flush this expression, since")@/ ("scantokens should be followed by a known string."); put←get←flush←error(0); end else begin back←input; if length(cur←exp)>0 then @<Pretend we're reading a new one-line file@>; end; end @ @<Pretend we're reading a new one-line file@>= begin begin←file←reading; name:=2; k:=first+length(cur←exp); if k>=max←buf←stack then begin if k>=buf←size then begin max←buf←stack:=buf←size; overflow("buffer size",buf←size); @:METAFONT capacity exceeded buffer size}{\quad buffer size@> end; max←buf←stack:=k+1; end; j:=str←start[cur←exp]; limit:=k; while first<limit do begin buffer[first]:=str←pool[j]; incr(j); incr(first); end; buffer[limit]:="%"; first:=limit+1; loc:=start; flush←cur←exp(0); end @ Here finally is |get←x←next|. The expression scanning routines to be considered later communicate via the global quantities |cur←type| and |cur←exp|; we must be very careful to save and restore these quantities while macros are being expanded. @↑inner loop@> @p procedure get←x←next; var @!save←exp:pointer; {a capsule to save |cur←type| and |cur←exp|} begin get←next; if cur←cmd<min←command then begin save←exp:=stash←cur←exp; repeat if cur←cmd=defined←macro then macro←call(cur←mod,null,cur←sym) else expand; get←next; until cur←cmd>=min←command; unstash←cur←exp(save←exp); {that restores |cur←type| and |cur←exp|} end; end; @ Now let's consider the |macro←call| procedure, which is used to start up all user-defined macros. Since the arguments to a macro might be expressions, |macro←call| is recursive. @↑recursion@> The first parameter to |macro←call| points to the reference count of the token list that defines the macro. The second parameter contains any arguments that have already been parsed (see below). The third parameter points to the symbolic token that names the macro. If the third parameter is |null|, the macro was defined by \&{vardef}, so its name can be reconstructed from the prefix and ``at'' arguments found within the second parameter. What is this second parameter? It's simply a linked list of one-word items, whose |info| fields point to the arguments. In other words, if |arg←list=null|, no arguments have been scanned yet; otherwise |info(arg←list)| points to the first scanned argument, and |link(arg←list)| points to the list of further arguments (if any). Arguments of type \&{expr} are so-called capsules, which we will discuss later when we concentrate on expressions; they can be recognized easily because their |link| field is |void|. Arguments of type \&{suffix} and \&{text} are token lists without reference counts. @ After argument scanning is complete, the arguments are moved to the |param←stack|. (They can't be put on that stack any sooner, because the stack is growing and shrinking in unpredictable ways as more arguments are being acquired.) Then the macro body is fed to the scanner; i.e., the replacement text of the macro is placed at the top of the \MF's input stack, so that |get←next| will proceed to read it next. @<Declare the procedure called |macro←call|@>= @t\4@>@<Declare the procedure called |print←macro←name|@>@; @t\4@>@<Declare the procedure called |print←arg|@>@; @t\4@>@<Declare the procedure called |scan←text←arg|@>@; procedure macro←call(@!def←ref,@!arg←list,@!macro←name:pointer); {invokes a user-defined control sequence} label found; var @!r:pointer; {current node in the macro's token list} @!p,@!q:pointer; {for list manipulation} @!n:integer; {the number of arguments} @!l←delim,@!r←delim:pointer; {a delimiter pair} @!tail:pointer; {tail of the argument list} begin r:=link(def←ref); add←mac←ref(def←ref); if arg←list=null then n:=0 else @<Determine the number |n| of arguments already supplied, and set |tail| to the tail of |arg←list|@>; if internal[tracing←macros]>0 then @<Show the text of the macro being expanded, and the existing arguments@>; @<Scan the remaining arguments, if any; set |r| to the first token of the replacement text@>; @<Feed the arguments and replacement text to the scanner@>; end; @ @<Show the text of the macro...@>= begin begin←diagnostic; print←ln; print←macro←name(arg←list,macro←name); if n=3 then print("@@#"); {indicate a suffixed macro} show←macro(def←ref,null,100000); if arg←list<>null then begin n:=0; p:=arg←list; repeat q:=info(p); print←arg(q,n,0); incr(n); p:=link(p); until p=null; end; end←diagnostic(false); end @ @<Declare the procedure called |print←macro←name|@>= procedure print←macro←name(@!a,@!n:pointer); var @!p,@!q:pointer; {they traverse the first part of |a|} begin if n<>null then print(text(n)) else begin p:=info(a); if p=null then print(text(info(info(link(a))))) else begin q:=p; while link(q)<>null do q:=link(q); link(q):=info(link(a)); show←token←list(p,null,1000,0); link(q):=null; end; end; end; @ @<Declare the procedure called |print←arg|@>= procedure print←arg(@!q:pointer;@!n:integer;@!b:pointer); begin if link(q)=void then print←nl("(EXPR") else if (b<text←base)and(b<>text←macro) then print←nl("(SUFFIX") else print←nl("(TEXT"); print←int(n); print(")<-"); if link(q)=void then print←exp(q,1) else show←token←list(q,null,1000,0); end; @ @<Determine the number |n| of arguments already supplied...@>= begin n:=1; tail:=arg←list; while link(tail)<>null do begin incr(n); tail:=link(tail); end; end @ @<Scan the remaining arguments, if any; set |r|...@>= cur←cmd:=comma+1; {anything |<>comma| will do} while info(r)>=expr←base do begin @<Scan the delimited argument represented by |info(r)|@>; r:=link(r); end; if cur←cmd=comma then begin print←err("Too many arguments to "); @.Too many arguments...@> print←macro←name(arg←list,macro←name); print←char(";"); print←nl(" Missing `"); print(text(r←delim)); @.Missing `)'...@> print("' has been inserted"); help3("I'm going to assume that the comma I just read was a")@/ ("right delimiter, and then I'll begin expanding the macro.")@/ ("You might want to delete some tokens before continuing."); error; end; if info(r)<>general←macro then @<Scan undelimited argument(s)@>; r:=link(r) @ At this point, the reader will find it advisable to review the explanation of token list format that was presented earlier, paying special attention to the conventions that apply only at the beginning of a macro's token list. On the other hand, the reader will have to take the expression-parsing aspects of the following program on faith; we will explain |cur←type| and |cur←exp| later. (Several things in this program depend on each other, and it's necessary to jump into the circle somewhere.) @<Scan the delimited argument represented by |info(r)|@>= if cur←cmd<>comma then begin get←x←next; if cur←cmd<>left←delimiter then begin print←err("Missing argument to "); @.Missing argument...@> print←macro←name(arg←list,macro←name); help3("That macro has more parameters than you thought.")@/ ("I'll continue by pretending that each missing argument")@/ ("is either zero or null."); if info(r)>=suffix←base then begin cur←exp:=null; cur←type:=token←list; end else begin cur←exp:=0; cur←type:=known; end; back←error; cur←cmd:=right←delimiter; goto found; end; l←delim:=cur←sym; r←delim:=cur←mod; end; @<Scan the argument represented by |info(r)|@>; if cur←cmd<>comma then @<Check that the proper right delimiter was present@>; found: @<Append the current expression to |arg←list|@> @ @<Check that the proper right delim...@>= if (cur←cmd<>right←delimiter)or(cur←mod<>l←delim) then if info(link(r))>=expr←base then begin missing←err(","); @.Missing `,'@> help3("I've finished reading a macro argument and am about to")@/ ("read another; the arguments weren't delimited correctly.")@/ ("You might want to delete some tokens before continuing."); back←error; cur←cmd:=comma; end else begin missing←err(text(r←delim)); @.Missing `)'@> help2("I've gotten to the end of the macro parameter list.")@/ ("You might want to delete some tokens before continuing."); back←error; end @ A \&{suffix} or \&{text} parameter will be have been scanned as a token list pointed to by |cur←exp|, in which case we will have |cur←type=token←list|. @<Append the current expression to |arg←list|@>= begin p:=get←avail; if cur←type=token←list then info(p):=cur←exp else info(p):=stash←cur←exp; if internal[tracing←macros]>0 then begin begin←diagnostic; print←arg(info(p),n,info(r)); end←diagnostic(false); end; if arg←list=null then arg←list:=p else link(tail):=p; tail:=p; incr(n); end @ @<Scan the argument represented by |info(r)|@>= if info(r)>=text←base then scan←text←arg(l←delim,r←delim) else begin get←x←next; if info(r)>=suffix←base then scan←suffix else scan←expression; end @ The parameters to |scan←text←arg| are either a pair of delimiters or zero; the latter case is for undelimited text arguments, which end with the first semicolon or \&{endgroup} or \&{end} that is not contained in a group. @<Declare the procedure called |scan←text←arg|@>= procedure scan←text←arg(@!l←delim,@!r←delim:pointer); label done; var @!balance:integer; {excess of |l←delim| over |r←delim|} @!p:pointer; {list tail} begin warning←info:=l←delim; scanner←status:=absorbing; p:=temp←head; balance:=1; link(temp←head):=null; loop@+ begin get←next; if l←delim=0 then @<Adjust the balance for an undelimited argument; |goto done| if done@> else @<Adjust the balance for a delimited argument; |goto done| if done@>; link(p):=cur←tok; p:=link(p); end; done: cur←exp:=link(temp←head); cur←type:=token←list; scanner←status:=normal; end; @ @<Adjust the balance for a delimited argument...@>= begin if cur←cmd=right←delimiter then begin if cur←mod=l←delim then begin decr(balance); if balance=0 then goto done; end; end else if cur←cmd=left←delimiter then if cur←mod=r←delim then incr(balance); end @ @<Adjust the balance for an undelimited...@>= begin if end←of←statement then {|cur←cmd=semicolon|, |end←group|, or |stop|} begin if balance=1 then goto done else if cur←cmd=end←group then decr(balance); end else if cur←cmd=begin←group then incr(balance); end @ @<Scan undelimited argument(s)@>= begin if info(r)<text←macro then begin get←x←next; if info(r)<>suffix←macro then if (cur←cmd=equals)or(cur←cmd=assignment) then get←x←next; end; case info(r) of primary←macro:scan←primary; secondary←macro:scan←secondary; tertiary←macro:scan←tertiary; expr←macro:scan←expression; of←macro:@<Scan an expression followed by `\&{of} $\langle$primary$\rangle$'@>; suffix←macro:@<Scan a suffix with optional delimiters@>; text←macro:scan←text←arg(0,0); end; {there are no other cases} back←input; @<Append the current expression to |arg←list|@>; end @ @<Scan an expression followed by `\&{of} $\langle$primary$\rangle$'@>= begin scan←expression; p:=get←avail; info(p):=stash←cur←exp; if internal[tracing←macros]>0 then begin begin←diagnostic; print←arg(info(p),n,0); end←diagnostic(false); end; if arg←list=null then arg←list:=p@+else link(tail):=p; tail:=p;incr(n); if cur←cmd<>of←token then begin missing←err("of"); print(" for "); @.Missing `of'@> print←macro←name(arg←list,macro←name); help1("I've got the first argument; will look now for the other."); back←error; end; get←x←next; scan←primary; end @ @<Scan a suffix with optional delimiters@>= begin if cur←cmd<>left←delimiter then l←delim:=null else begin l←delim:=cur←sym; r←delim:=cur←mod; get←x←next; end; scan←suffix; if l←delim<>null then begin if(cur←cmd<>right←delimiter)or(cur←mod<>l←delim) then begin missing←err(text(r←delim)); @.Missing `)'@> help2("I've gotten to the end of the macro parameter list.")@/ ("You might want to delete some tokens before continuing."); back←error; end; get←x←next; end; end @ Before we put a new token list on the input stack, it is wise to clean off all token lists that have recently been depleted. Then a user macro that ends with a call to itself will not require unbounded stack space. @<Feed the arguments and replacement text to the scanner@>= while token←state and(loc=null) do end←token←list; {conserve stack space} if param←ptr+n>max←param←stack then begin max←param←stack:=param←ptr+n; if max←param←stack>param←size then overflow("parameter stack size",param←size); @:METAFONT capacity exceeded parameter stack size}{\quad parameter stack size@> end; begin←token←list(def←ref,macro); name:=macro←name; loc:=r; if n>0 then begin p:=arg←list; repeat param←stack[param←ptr]:=info(p); incr(param←ptr); p:=link(p); until p=null; flush←list(arg←list); end @ It's sometimes necessary to put a single argument onto |param←stack|. The |stack←argument| subroutine does this. @p procedure stack←argument(@!p:pointer); begin if param←ptr=max←param←stack then begin incr(max←param←stack); if max←param←stack>param←size then overflow("parameter stack size",param←size); @:METAFONT capacity exceeded parameter stack size}{\quad parameter stack size@> end; param←stack[param←ptr]:=p; incr(param←ptr); end; @* \[36] Conditional processing. Let's consider now the way \&{if} commands are handled. Conditions can be inside conditions, and this nesting has a stack that is independent of other stacks. Four global variables represent the top of the condition stack: |cond←ptr| points to pushed-down entries, if~any; |cur←if| tells whether we are processing \&{if} or \&{elseif}; |if←limit| specifies the largest code of a |fi←or←else| command that is syntactically legal; and |if←line| is the line number at which the current conditional began. If no conditions are currently in progress, the condition stack has the special state |cond←ptr=null|, |if←limit=normal|, |cur←if=0|, |if←line=0|. Otherwise |cond←ptr| points to a two-word node; the |type|, |name←type|, and |link| fields of the first word contain |if←limit|, |cur←if|, and |cond←ptr| at the next level, and the second word contains the corresponding |if←line|. @d if←node←size=2 {number of words in stack entry for conditionals} @d if←line←field(#)==mem[#+1].int @d if←code=1 {code for \&{if} being evaluated} @d fi←code=2 {code for \&{fi}} @d else←code=3 {code for \&{else}} @d else←if←code=4 {code for \&{elseif}} @<Glob...@>= @!cond←ptr:pointer; {top of the condition stack} @!if←limit:normal..else←if←code; {upper bound on |fi←or←else| codes} @!cur←if:small←number; {type of conditional being worked on} @!if←line:integer; {line where that conditional began} @ @<Set init...@>= cond←ptr:=null; if←limit:=normal; cur←if:=0; if←line:=0; @ @<Put each...@>= primitive("if",if←test,if←code);@/ @!@:if←}{\&{if} primitive@> primitive("fi",fi←or←else,fi←code); eqtb[frozen←fi]:=eqtb[cur←sym];@/ @!@:fi←}{\&{fi} primitive@> primitive("else",fi←or←else,else←code);@/ @!@:else←}{\&{else} primitive@> primitive("elseif",fi←or←else,else←if←code);@/ @!@:else←if←}{\&{elseif} primitive@> @ @<Cases of |print←cmd←mod|...@>= if←test,fi←or←else: case m of if←code:print("if"); fi←code:print("fi"); else←code:print("else"); othercases print("elseif") endcases; @ Here is a procedure that ignores text until coming to an \&{elseif}, \&{else}, or \&{fi} at level zero of $\&{if}\ldots\&{fi}$ nesting. After it has acted, |cur←mod| will indicate the token that was found. \MF's smallest two command codes are |if←test| and |fi←or←else|; this makes the skipping process a bit simpler. @p procedure pass←text; label done; var l:integer; begin scanner←status:=skipping; l:=0; warning←info:=line; loop@+ begin get←next; if cur←cmd<=fi←or←else then if cur←cmd<fi←or←else then incr(l) else begin if l=0 then goto done; if cur←mod=fi←code then decr(l); end else @<Decrease the string reference count, if the current token is a string@>; end; done: scanner←status:=normal; end; @ @<Decrease the string reference count...@>= if cur←cmd=string←token then delete←str←ref(cur←mod) @ When we begin to process a new \&{if}, we set |if←limit:=if←code|; then if \&{elseif} or \&{else} or \&{fi} occurs before the current \&{if} condition has been evaluated, a colon will be inserted. A construction like `\.{if fi}' would otherwise get \MF\ confused. @<Push the condition stack@>= begin p:=get←node(if←node←size); link(p):=cond←ptr; type(p):=if←limit; name←type(p):=cur←if; if←line←field(p):=if←line; cond←ptr:=p; if←limit:=if←code; if←line:=line; cur←if:=if←code; end @ @<Pop the condition stack@>= begin p:=cond←ptr; if←line:=if←line←field(p); cur←if:=name←type(p); if←limit:=type(p); cond←ptr:=link(p); free←node(p,if←node←size); end @ Here's a procedure that changes the |if←limit| code corresponding to a given value of |cond←ptr|. @p procedure change←if←limit(@!l:small←number;@!p:pointer); label exit; var q:pointer; begin if p=cond←ptr then if←limit:=l {that's the easy case} else begin q:=cond←ptr; loop@+ begin if q=null then confusion("if"); @:this can't happen if}{\quad if@> if link(q)=p then begin type(q):=l; return; end; q:=link(q); end; end; exit:end; @ The user is supposed to put colons into the proper parts of conditional statements. Therefore, \MF\ has to check for their presence. @p procedure check←colon; begin if cur←cmd<>colon then begin missing←err(":");@/ @.Missing `:'@> help2("There should've been a colon after the condition.")@/ ("I shall pretend that one was there.");@; back←error; end; end; @ A condition is started when the |get←x←next| procedure encounters an |if←test| command; in that case |get←x←next| calls |conditional|, which is a recursive procedure. @↑recursion@> @p procedure conditional; label exit,done,reswitch,found; var @!save←cond←ptr:pointer; {|cond←ptr| corresponding to this conditional} @!new←if←limit:fi←code..else←if←code; {future value of |if←limit|} @!p:pointer; {temporary register} begin @<Push the condition stack@>;@+save←cond←ptr:=cond←ptr; reswitch: get←boolean; new←if←limit:=else←if←code; if internal[tracing←commands]>unity then @<Display the boolean value of |cur←exp|@>; found: check←colon; if cur←exp=true←code then begin change←if←limit(new←if←limit,save←cond←ptr); return; {wait for \&{elseif}, \&{else}, or \&{fi}} end; @<Skip to \&{elseif} or \&{else} or \&{fi}, then |goto done|@>; done: cur←if:=cur←mod; if←line:=line; if cur←mod=fi←code then @<Pop the condition stack@> else if cur←mod=else←if←code then goto reswitch else begin cur←exp:=true←code; new←if←limit:=fi←code; get←x←next; goto found; end; exit:end; @ In a construction like `\&{if} \&{if} \&{true}: $0=1$: \\{foo} \&{else}: \\{bar} \&{fi}', the first \&{else} that we come to after learning that the \&{if} is false is not the \&{else} we're looking for. Hence the following curious logic is needed. @<Skip to \&{elseif}...@>= loop@+ begin pass←text; if cond←ptr=save←cond←ptr then goto done else if cur←mod=fi←code then @<Pop the condition stack@>; end @ @<Display the boolean value...@>= begin begin←diagnostic; if cur←exp=true←code then print("{true}")@+else print("{false}"); end←diagnostic(false); end @ The processing of conditionals is complete except for the following code, which is actually part of |get←x←next|. It comes into play when \&{elseif}, \&{else}, or \&{fi} is scanned. @<Terminate the current conditional and skip to \&{fi}@>= if cur←mod>if←limit then if if←limit=if←code then {condition not yet evaluated} begin missing←err(":"); @.Missing `:'@> back←input; cur←sym:=frozen←colon; ins←error; end else begin print←err("Extra "); print←cmd←mod(fi←or←else,cur←mod); @.Extra else@> @.Extra elseif@> @.Extra fi@> help1("I'm ignoring this; it doesn't match any if."); error; end else begin while cur←mod<>fi←code do pass←text; {skip to \&{fi}} @<Pop the condition stack@>; end @* \[37] Iterations. To bring our treatment of |get←x←next| to a close, we need to consider what \MF\ does when it sees \&{for}, \&{forsuffixes}, and \&{forever}. There's a global variable |loop←ptr| that keeps track of the \&{for} loops that are currently active. If |loop←ptr=null|, no loops are in progress; otherwise |info(loop←ptr)| points to the iterative text of the current (innermost) loop, and |link(loop←ptr)| points to the data for any other loops that enclose the current one. A loop-control node also has two other fields, called |loop←type| and |loop←list|, whose contents depend on the type of loop: \yskip\indent|loop←type(loop←ptr)=null| means that |loop←list(loop←ptr)| points to a list of one-word nodes whose |info| fields point to the remaining argument values of a suffix list and expression list. \yskip\indent|loop←type(loop←ptr)=void| means that the current loop is `\&{forever}'. \yskip\indent|loop←type(loop←ptr)=p>void| means that |value(p)|, |step←size(p)|, and |final←value(p)| contain the data for an arithmetic progression. \yskip\noindent In the latter case, |p| points to a ``progression node'' whose first word is not used. (No value could be stored there because the link field of words in the dynamic memory area cannot be arbitrary.) @d loop←list←loc(#)==#+1 {where the |loop←list| field resides} @d loop←type(#)==info(loop←list←loc(#)) {the type of \&{for} loop} @d loop←list(#)==link(loop←list←loc(#)) {the remaining list elements} @d loop←node←size=2 {the number of words in a loop control node} @d progression←node←size=4 {the number of words in a progression node} @d step←size(#)==mem[#+2].sc {the step size in an arithmetic progression} @d final←value(#)==mem[#+3].sc {the final value in an arithmetic progression} @<Glob...@>= @!loop←ptr:pointer; {top of the loop-control-node stack} @ @<Set init...@>= loop←ptr:=null; @ If the expressions that define an arithmetic progression in a \&{for} loop don't have known numeric values, the |bad←for| subroutine screams at the user. @p procedure bad←for(@!s:str←number); begin disp←err(null,"Improper "); {show the bad expression above the message} @.Improper...replaced by 0@> print(s); print(" has been replaced by 0"); help4("When you say `for x=a step b until c',")@/ ("the initial value `a' and the step size `b'")@/ ("and the final value `c' must have known numeric values.")@/ ("I'm zeroing this one. Proceed, with fingers crossed."); put←get←flush←error(0); end; @ Here's what \MF\ does when \&{for}, \&{forsuffixes}, or \&{forever} has just been scanned. (This code requires slight familiarity with expression-parsing routines that we have not yet discussed; but it seems to belong in the present part of the program, even though the author didn't write it until later. The reader may wish to come back to it.) @p procedure begin←iteration; label continue,done,found; var @!m:halfword; {|expr←base| (\&{for}) or |suffix←base| (\&{forsuffixes})} @!n:halfword; {hash address of the current symbol} @!p,@!q,@!s,@!pp:pointer; {link manipulation registers} begin m:=cur←mod; n:=cur←sym; s:=get←node(loop←node←size); if m=start←forever then begin loop←type(s):=void; p:=null; get←x←next; goto found; end; get←symbol; p:=get←node(token←node←size); info(p):=cur←sym; value(p):=m;@/ get←x←next; if (cur←cmd<>equals)and(cur←cmd<>assignment) then begin missing←err("=");@/ @.Missing `='@> help3("The next thing in this loop should have been `=' or `:='.")@/ ("But don't worry; I'll pretend that an equals sign")@/ ("was present, and I'll look for the values next.");@/ back←error; end; @<Scan the values to be used in the loop@>; found:@<Check for the presence of a colon@>; @<Scan the loop text and put it on the loop control stack@>; resume←iteration; end; @ @<Check for the presence of a colon@>= if cur←cmd<>colon then begin missing←err(":");@/ @.Missing `:'@> help3("The next thing in this loop should have been a `:'.")@/ ("So I'll pretend that a colon was present;")@/ ("everything from here to `endfor' will be iterated."); back←error; end @ We append a special |frozen←repeat←loop| token in place of the `\&{endfor}' at the end of the loop. This will come through \MF's scanner at the proper time to cause the loop to be repeated. (If the user tries some shenanigan like `\&{for} $\ldots$ \&{let} \&{endfor}', he will be foiled by the |get←symbol| routine, which keeps frozen tokens unchanged. Furthermore the |frozen←repeat←loop| is an \&{outer} token, so it won't be lost accidentally.) @ @<Scan the loop text...@>= q:=get←avail; info(q):=frozen←repeat←loop; scanner←status:=loop←defining; warning←info:=n; info(s):=scan←toks(iteration,p,q,0); scanner←status:=normal;@/ link(s):=loop←ptr; loop←ptr:=s @ @<Initialize table...@>= eq←type(frozen←repeat←loop):=repeat←loop+outer←tag; text(frozen←repeat←loop):=" ENDFOR"; @ The loop text is inserted into \MF's scanning apparatus by the |resume←iteration| routine. @p procedure resume←iteration; label not←found,exit; var @!p,@!q:pointer; {link registers} begin p:=loop←type(loop←ptr); if p>void then {|p| points to a progression node} begin cur←exp:=value(p); if @<The arithmetic progression has ended@> then goto not←found; cur←type:=known; q:=stash←cur←exp; {make |q| an \&{expr} argument} value(p):=cur←exp+step←size(p); {set |value(p)| for the next iteration} end else if p<void then begin p:=loop←list(loop←ptr); if p=null then goto not←found; loop←list(loop←ptr):=link(p); q:=info(p); free←avail(p); end else begin begin←token←list(info(loop←ptr),forever←text); return; end; begin←token←list(info(loop←ptr),loop←text); stack←argument(q); if internal[tracing←commands]>unity then @<Trace the start of a loop@>; return; not←found:stop←iteration; exit:end; @ @<The arithmetic progression has ended@>= ((step←size(p)>0)and(cur←exp>final←value(p)))or@| ((step←size(p)<0)and(cur←exp<final←value(p))) @ @<Trace the start of a loop@>= begin begin←diagnostic; print←nl("{loop value="); @.loop value=n@> if (q<>null)and(link(q)=void) then print←exp(q,1) else show←token←list(q,null,50,0); print←char("}"); end←diagnostic(false); end @ A level of loop control disappears when |resume←iteration| has decided not to resume, or when an \&{exitif} construction has removed the loop text from the input stack. @p procedure stop←iteration; var @!p,@!q:pointer; {the usual} begin p:=loop←type(loop←ptr); if p>void then free←node(p,progression←node←size) else if p<void then begin q:=loop←list(loop←ptr); while q<>null do begin p:=info(q); if p<>null then if link(p)=void then {it's an \&{expr} parameter} begin recycle←value(p); free←node(p,value←node←size); end else flush←token←list(p); {it's a \&{suffix} or \&{text} parameter} p:=q; q:=link(q); free←avail(p); end; end; p:=loop←ptr; loop←ptr:=link(p); flush←token←list(info(p)); free←node(p,loop←node←size); end; @ Now that we know all about loop control, we can finish up the missing portion of |begin←iteration| and we'll be done. The following code is performed after the `\.=' has been scanned in a \&{for} construction (if |m=expr←base|) or a \&{forsuffixes} construction (if |m=suffix←base|). @<Scan the values to be used in the loop@>= loop←type(s):=null; q:=loop←list←loc(s); link(q):=null; {|link(q)=loop←list(s)|} repeat get←x←next; if m<>expr←base then scan←suffix else begin if cur←cmd>=colon then if cur←cmd<=comma then goto continue; scan←expression; if cur←cmd=step←token then if q=loop←list←loc(s) then @<Prepare for step-until construction and |goto done|@>; cur←exp:=stash←cur←exp; end; link(q):=get←avail; q:=link(q); info(q):=cur←exp; cur←type:=vacuous; continue: until cur←cmd<>comma; done: @ @<Prepare for step-until construction and |goto done|@>= begin if cur←type<>known then bad←for("initial value"); pp:=get←node(progression←node←size); value(pp):=cur←exp;@/ get←x←next; scan←expression; if cur←type<>known then bad←for("step size"); step←size(pp):=cur←exp; if cur←cmd<>until←token then begin missing←err("until");@/ @.Missing `until'@> help2("I assume you meant to say `until' after `step'.")@/ ("So I'll look for the final value and colon next."); back←error; end; get←x←next; scan←expression; if cur←type<>known then bad←for("final value"); final←value(pp):=cur←exp; loop←type(s):=pp; goto done; end @* \[38] File names. It's time now to fret about file names. Besides the fact that different operating systems treat files in different ways, we must cope with the fact that completely different naming conventions are used by different groups of people. The following programs show what is required for one particular operating system; similar routines for other systems are not difficult to devise. @↑system dependencies@> \MF\ assumes that a file name has three parts: the name proper; its ``extension''; and a ``file area'' where it is found in an external file system. The extension of an input file is assumed to be `\.{.mf}' unless otherwise specified; it is `\.{.log}' on the transcript file that records each run of \MF; it is `\.{.tfm}' on the font metric files that describe characters in the fonts \MF\ creates; it is `\.{.gf}' on the output files that specify generic font information; and it is `\.{.base}' on the base files written by \.{INIMF} to initialize \MF. The file area can be arbitrary on input files, but files are usually output to the user's current area. If an input file cannot be found on the specified area, \MF\ will look for it on a special system area; this special area is intended for commonly used input files. Simple uses of \MF\ refer only to file names that have no explicit extension or area. For example, a person usually says `\.{input} \.{cmr10}' instead of `\.{input} \.{cmr10.new}'. Simple file names are best, because they make the \MF\ source files portable; whenever a file name consists entirely of letters and digits, it should be treated in the same way by all implementations of \MF. However, users need the ability to refer to other files in their environment, especially when responding to error messages concerning unopenable files; therefore we want to let them use the syntax that appears in their favorite operating system. @ \MF\ uses the same conventions that have proved to be satisfactory for \TeX. In order to isolate the system-dependent aspects of file names, the @↑system dependencies@> system-independent parts of \MF\ make use of three system-dependent procedures that are called |begin←name|, |more←name|, and |end←name|. In essence, if the user-specified characters of the file name are $c←1\ldots c←n$, the system-independent driver program does the operations $$|begin←name|;\,|more←name|(c←1);\,\ldots\,;|more←name|(c←n); \,|end←name|.$$ These three procedures communicate with each other via global variables. Afterwards the file name will appear in the string pool as three strings called |cur←name|\penalty10000\hskip-.05em, |cur←area|, and |cur←ext|; the latter two are null (i.e., |""|), unless they were explicitly specified by the user. Actually the situation is slightly more complicated, because \MF\ needs to know when the file name ends. The |more←name| routine is a function (with side effects) that returns |true| on the calls |more←name|$(c←1)$, \dots, |more←name|$(c←{n-1})$. The final call |more←name|$(c←n)$ returns |false|; or, it returns |true| and $c←n$ is the last character on the current input line. In other words, |more←name| is supposed to return |true| unless it is sure that the file name has been completely scanned; and |end←name| is supposed to be able to finish the assembly of |cur←name|, |cur←area|, and |cur←ext| regardless of whether $|more←name|(c←n)$ returned |true| or |false|. @<Glob...@>= @!cur←name:str←number; {name of file just scanned} @!cur←area:str←number; {file area just scanned, or \.{""}} @!cur←ext:str←number; {file extension just scanned, or \.{""}} @ The file names we shall deal with for illustrative purposes have the following structure: If the name contains `\.>' or `\.:', the file area consists of all characters up to and including the final such character; otherwise the file area is null. If the remaining file name contains `\..', the file extension consists of all such characters from the first remaining `\..' to the end, otherwise the file extension is null. @↑system dependencies@> We can scan such file names easily by using two global variables that keep track of the occurrences of area and extension delimiters: @<Glob...@>= @!area←delimiter:pool←pointer; {the most recent `\.>' or `\.:', if any} @!ext←delimiter:pool←pointer; {the relevant `\..', if any} @ Input files that can't be found in the user's area may appear in a standard system area called |MF←area|. This system area name will, of course, vary from place to place. @↑system dependencies@> @d MF←area=="MFinputs:" @.MFinputs@> @ Here now is the first of the system-dependent routines for file name scanning. @↑system dependencies@> @p procedure begin←name; begin area←delimiter:=0; ext←delimiter:=0; end; @ And here's the second. @↑system dependencies@> @p function more←name(@!c:ASCII←code):boolean; begin if c=" " then more←name:=false else begin if (c=">")or(c=":") then begin area←delimiter:=pool←ptr; ext←delimiter:=0; end else if (c=".")and(ext←delimiter=0) then ext←delimiter:=pool←ptr; str←room(1); append←char(c); {contribute |c| to the current string} more←name:=true; end; end; @ The third. @↑system dependencies@> @p procedure end←name; begin if str←ptr+3>max←str←ptr then begin if str←ptr+3>max←strings then overflow("number of strings",max←strings-init←str←ptr); @:METAFONT capacity exceeded number of strings}{\quad number of strings@> max←str←ptr:=str←ptr+3; end; if area←delimiter=0 then cur←area:="" else begin cur←area:=str←ptr; incr(str←ptr); str←start[str←ptr]:=area←delimiter+1; end; if ext←delimiter=0 then begin cur←ext:=""; cur←name:=make←string; end else begin cur←name:=str←ptr; incr(str←ptr); str←start[str←ptr]:=ext←delimiter; cur←ext:=make←string; end; end; @ Conversely, here is a routine that takes three strings and prints a file name that might have produced them. (The routine is system dependent, because some operating systems put the file area last instead of first.) @↑system dependencies@> @<Basic printing...@>= procedure print←file←name(@!n,@!a,@!e:integer); begin print(a); print(n); print(e); end; @ Another system-dependent routine is needed to convert three \MF\ strings to the |name←of←file| value that is used to open files. The present code allows both lowercase and uppercase letters in the file name. @↑system dependencies@> @d append←to←name(#)==begin c:=#; incr(k); if k<=file←name←size then name←of←file[k]:=xchr[c]; end @p procedure pack←file←name(@!n,@!a,@!e:str←number); var @!k:integer; {number of positions filled in |name←of←file|} @!c: ASCII←code; {character being packed} @!j:pool←pointer; {index into |str←pool|} begin k:=0; for j:=str←start[a] to str←start[a+1]-1 do append←to←name(str←pool[j]); for j:=str←start[n] to str←start[n+1]-1 do append←to←name(str←pool[j]); for j:=str←start[e] to str←start[e+1]-1 do append←to←name(str←pool[j]); if k<=file←name←size then name←length:=k@+else name←length:=file←name←size; for k:=name←length+1 to file←name←size do name←of←file[k]:=' '; end; @ A messier routine is also needed, since base file names must be scanned before \MF's string mechanism has been initialized. We shall use the global variable |MF←base←default| to supply the text for default system areas and extensions related to base files. @↑system dependencies@> @d base←default←length=18 {length of the |MF←base←default| string} @d base←area←length=8 {length of its area part} @d base←ext←length=5 {length of its `\.{.base}' part} @<Glob...@>= @!MF←base←default:packed array[1..base←default←length] of char; @ @<Set init...@>= MF←base←default:='MFbases:plain.base'; @.MFbases@> @.plain@> @↑system dependencies@> @ @<Check the ``constant'' values for consistency@>= if base←default←length>file←name←size then bad:=41; @ Here is the messy routine that was just mentioned. It sets |name←of←file| from the first |n| characters of |MF←base←default|, followed by |buffer[a..b]|, followed by the last |base←ext←length| characters of |MF←base←default|. We dare not give error messages here, since \MF\ calls this routine before the |error| routine is ready to roll. Instead, we simply drop excess characters, since the error will be detected in another way when a strange file name isn't found. @↑system dependencies@> @p procedure pack←buffered←name(@!n:small←number;@!a,@!b:integer); var @!k:integer; {number of positions filled in |name←of←file|} @!c: ASCII←code; {character being packed} @!j:integer; {index into |buffer| or |MF←base←default|} begin if n+b-a+1+base←ext←length>file←name←size then b:=a+file←name←size-n-1-base←ext←length; k:=0; for j:=1 to n do append←to←name(xord[MF←base←default[j]]); for j:=a to b do append←to←name(buffer[j]); for j:=base←default←length-base←ext←length+1 to base←default←length do append←to←name(xord[MF←base←default[j]]); if k<=file←name←size then name←length:=k@+else name←length:=file←name←size; for k:=name←length+1 to file←name←size do name←of←file[k]:=' '; end; @ Here is the only place we use |pack←buffered←name|. This part of the program becomes active when a ``virgin'' \MF\ is trying to get going, just after the preliminary initialization, or when the user is substituting another base file by typing `\.\&' after the initial `\.{**}' prompt. The buffer contains the first line of input in |buffer[loc..(last-1)]|, where |loc<last| and |buffer[loc]<>" "|. @<Declare the function called |open←base←file|@>= function open←base←file:boolean; label found,exit; var @!j:0..buf←size; {the first space after the file name} begin j:=loc; if buffer[loc]="&" then begin incr(loc); j:=loc; buffer[last]:=" "; while buffer[j]<>" " do incr(j); pack←buffered←name(0,loc,j-1); {try first without the system file area} if w←open←in(base←file) then goto found; pack←buffered←name(base←area←length,loc,j-1); {now try the system base file area} if w←open←in(base←file) then goto found; wake←up←terminal; wterm←ln('Sorry, I can''t find that base;',' will try PLAIN.'); @.Sorry, I can't find...@> update←terminal; end; {now pull out all the stops: try for the system \.{plain} file} pack←buffered←name(base←default←length-base←ext←length,1,0); if not w←open←in(base←file) then begin wake←up←terminal; wterm←ln('I can''t find the PLAIN base file!'); @.I can't find PLAIN...@> @.plain@> open←base←file:=false; return; end; found:loc:=j; open←base←file:=true; exit:end; @ Operating systems often make it possible to determine the exact name (and possible version number) of a file that has been opened. The following routine, which simply makes a \MF\ string from the value of |name←of←file|, should ideally be changed to deduce the full name of file~|f|, which is the file most recently opened, if it is possible to do this in a \PASCAL\ program. @↑system dependencies@> This routine might be called after string memory has overflowed, hence we dare not use `|str←room|'. @p function make←name←string:str←number; var @!k:1..file←name←size; {index into |name←of←file|} begin if (pool←ptr+name←length>pool←size)or(str←ptr=max←strings) then make←name←string:="?" else begin for k:=1 to name←length do append←char(xord[name←of←file[k]]); make←name←string:=make←string; end; end; function a←make←name←string(var @!f:alpha←file):str←number; begin a←make←name←string:=make←name←string; end; function b←make←name←string(var @!f:byte←file):str←number; begin b←make←name←string:=make←name←string; end; function w←make←name←string(var @!f:word←file):str←number; begin w←make←name←string:=make←name←string; end; @ Now let's consider the routines by which \MF\ deals with file names in a system-independent manner. First comes a procedure that looks for a file name in the input by taking the information from the input buffer. (We can't use |get←next|, because the conversion to tokens would destroy necessary information.) This procedure doesn't allow semicolons or percent signs to be part of file names, because of other conventions of \MF. The manual doesn't use semicolons or percents immediately after file names, but some users no doubt will find it natural to do so; therefore system-dependent changes to allow such characters in file names should probably be made with reluctance, and only when an entire file name that includes special characters is ``quoted'' somehow. @↑system dependencies@> @p procedure scan←file←name; label done; begin begin←name; while buffer[loc]=" " do incr(loc); loop@+begin if (buffer[loc]=";")or(buffer[loc]="%") then goto done; if not more←name(buffer[loc]) then goto done; incr(loc); end; done: end←name; end; @ The global variable |job←name| contains the file name that was first \&{input} by the user. This name is extended by `\.{log}' and `\.{gf}' and `\.{base}' and `\.{tfm}' in order to make the names of \MF's output files. @<Glob...@>= @!job←name:str←number; {principal file name} @!log←name:str←number; {full name of the log file} @ Initially |job←name=0|; it becomes nonzero as soon as the true name is known. We have |job←name=0| if and only if the `\.{log}' file has not been opened, except of course for a short time just after |job←name| has become nonzero. @<Initialize the output...@>=job←name:=0; @ Here is a routine that manufactures the output file names, assuming that |job←name<>0|. It ignores and changes the current settings of |cur←area| and |cur←ext|. @d pack←cur←name==pack←file←name(cur←name,cur←area,cur←ext) @p procedure pack←job←name(@!s:str←number); {|s = ".log"|, |".gf"|, or |".base"|} begin cur←area:=""; cur←ext:=s; cur←name:=job←name; pack←cur←name; end; @ Actually the main output file extension is usually something like |".300gf"| instead of just |".gf"|; the additional number indicates the resolution in pixels per inch, based on the setting of |hppp| when the file is opened. @<Glob...@>= @!gf←ext:str←number; {default extension for the output file} @ If some trouble arises when \MF\ tries to open a file, the following routine calls upon the user to supply another file name. Parameter~|s| is used in the error message to identify the type of file; parameter~|e| is the default extension if none is given. Upon exit from the routine, variables |cur←name|, |cur←area|, |cur←ext|, and |name←of←file| are ready for another attempt at file opening. @p procedure prompt←file←name(@!s,@!e:str←number); label done; var @!k:0..buf←size; {index into |buffer|} begin if interaction=scroll←mode then wake←up←terminal; if s="input file name" then print←err("I can't find file `") @.I can't find file x@> else print←err("I can't write on file `"); @.I can't write on file x@> print←file←name(cur←name,cur←area,cur←ext); print("'."); if e=".mf" then show←context; print←nl("Please type another "); print(s); @.Please type...@> if interaction<scroll←mode then fatal←error("*** (job aborted, file error in nonstop mode)"); @.job aborted, file error...@> clear←terminal; prompt←input(": "); @<Scan file name in the buffer@>; if cur←ext="" then cur←ext:=e; pack←cur←name; end; @ @<Scan file name in the buffer@>= begin begin←name; k:=first; while (buffer[k]=" ")and(k<last) do incr(k); loop@+ begin if k=last then goto done; if not more←name(buffer[k]) then goto done; incr(k); end; done:end←name; end @ The |open←log←file| routine is used to open the transcript file and to help it catch up to what has previously been printed on the terminal. @p procedure open←log←file; var @!old←setting:0..max←selector; {previous |selector| setting} @!k:0..buf←size; {index into |months| and |buffer|} @!l:0..buf←size; {end of first input line} @!m:integer; {the current month} @!months:packed array [1..36] of char; {abbreviations of month names} begin old←setting:=selector; if job←name=0 then job←name:="mfput"; pack←job←name(".log"); while not a←open←out(log←file) do @<Try to get a different log file name@>; log←name:=a←make←name←string(log←file); selector:=log←only; @<Print the banner line, including the date and time@>; input←stack[input←ptr]:=cur←input; {make sure bottom level is in memory} print←nl("**"); @.**@> l:=input←stack[0].limit←field-1; {last position of first line} for k:=1 to l do print(buffer[k]); print←ln; {now the transcript file contains the first line of input} selector:=old←setting+2; {|log←only| or |term←and←log|} end; @ Sometimes |open←log←file| is called at awkward moments when \MF\ is unable to print error messages or even to |show←context|. Therefore the program is careful not to call |prompt←file←name| if a fatal error could result. Incidentally, the program always refers to the log file as a `\.{transcript file}', because some systems cannot use the extension `\.{.log}' for this file. @<Try to get a different log file name@>= begin if interaction<scroll←mode then {bypass |fatal←error|} begin print←err("I can't write on file `"); @.I can't write on file x@> print←file←name(cur←name,cur←area,cur←ext); print("'.");@/ job←name:=0; history:=fatal←error←stop; jump←out; end; {abort the program without a log file} prompt←file←name("transcript file name",".log"); end @ @<Print the banner...@>= begin wlog(banner); print(base←ident); print(" "); print←int(round←unscaled(internal[day])); print←char(" "); months:='JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'; m:=round←unscaled(internal[month]); for k:=3*m-2 to 3*m do wlog(months[k]); print←char(" "); print←int(round←unscaled(internal[year])); print←char(" "); m:=round←unscaled(internal[time]); print←dd(m div 60); print←char(":"); print←dd(m mod 60); end @ Here's an example of how these conventions are used. We shall use the macro |set←output←file←name| when it is time to crank up the output file. @d set←output←file←name== begin if job←name=0 then open←log←file; pack←job←name(gf←ext); while not b←open←out(gf←file) do prompt←file←name("file name for output",gf←ext); output←file←name:=b←make←name←string(gf←file); end @<Glob...@>= @!gf←file: byte←file; {the generic font output goes here} @!output←file←name: str←number; {full name of the output file} @ @<Initialize the output...@>=output←file←name:=0; @ Let's turn now to the procedure that is used to initiate file reading when an `\.{input}' command is being processed. @p procedure start←input; {\MF\ will \.{input} something} label done; begin @<Put the desired file name in |(cur←name,cur←ext,cur←area)|@>; if cur←ext="" then cur←ext:=".mf"; pack←cur←name; loop@+ begin begin←file←reading; {set up |cur←file| and new level of input} if a←open←in(cur←file) then goto done; pack←file←name(cur←name,MF←area,cur←ext); if a←open←in(cur←file) then goto done; end←file←reading; {remove the level that didn't work} prompt←file←name("input file name",".mf"); end; done: name:=a←make←name←string(cur←file); str←ref[cur←name]:=max←str←ref; if job←name=0 then begin job←name:=cur←name; open←log←file; end; {|open←log←file| doesn't |show←context|, so |limit| and |loc| needn't be set to meaningful values yet} if term←offset+length(name)>max←print←line-2 then print←ln else if (term←offset>0)or(file←offset>0) then print←char(" "); print←char("("); print(name); update←terminal; if name=str←ptr-1 then {we can conserve string pool space now} begin flush←string(name); name:=cur←name; end; @<Read the first line of the new file@>; end; @ Here we have to remember to tell the |input←ln| routine not to start with a |get|. If the file is empty, it is considered to contain a single blank line. @↑system dependencies@> @<Read the first line...@>= begin if not input←ln(cur←file,false) then do←nothing; firm←up←the←line; buffer[limit]:="%"; first:=limit+1; loc:=start; line:=1; end @ @<Put the desired file name in |(cur←name,cur←ext,cur←area)|@>= while token←state and(loc=null) do end←token←list; if token←state then begin print←err("File names can't appear within macros"); @.File names can't...@> help3("Sorry...I've converted what follows to tokens,")@/ ("possibly garbaging the name you gave.")@/ ("Please delete the tokens and insert the name again.");@/ error; end; if file←state then scan←file←name else begin cur←name:=""; cur←ext:=""; cur←area:=""; end @* \[39] Introduction to the parsing routines. We come now to the central nervous system that sparks many of \MF's activities. By evaluating expressions, from their primary constituents to ever larger subexpressions, \MF\ builds the structures that ultimately define fonts. Four mutually recursive subroutines are involved in this process: We call them $$\hbox{|scan←primary|, |scan←secondary|, |scan←tertiary|, and |scan←expression|.}$$ @↑recursion@> Each of them is parameterless and begins with the first token to be scanned already represented in |cur←cmd|, |cur←mod|, and |cur←sym|. After execution, the value of the primary or secondary or tertiary or expression that was found will appear in the global variables |cur←type| and |cur←exp|. The token following the expression will be represented in |cur←cmd|, |cur←mod|, and |cur←sym|. Technically speaking, the parsing algorithms are ``LL(1),'' more or less; backup mechanisms have been added in order to provide reasonable error recovery. @<Glob...@>= @!cur←type:small←number; {the type of the expression just found} @!cur←exp:integer; {the value of the expression just found} @ @<Set init...@>= cur←exp:=0; @ Many different kinds of expressions are possible, so it is wise to have precise descriptions of what |cur←type| and |cur←exp| mean in all cases: \smallskip\hang |cur←type=vacuous| means that this expression didn't turn out to have a value at all, because it arose from a \&{begingroup}$\,\ldots\,$\&{endgroup} construction in which there was no expression before the \&{endgroup}. In this case |cur←exp| has some irrelevant value. \smallskip\hang |cur←type=boolean←type| means that |cur←exp| is either |true←code| or |false←code|. \smallskip\hang |cur←type=unknown←boolean| means that |cur←exp| points to a capsule node that is in the ring of variables equivalent to at least one undefined boolean variable. \smallskip\hang |cur←type=string←type| means that |cur←exp| is a string number (i.e., an integer in the range |0<=cur←exp<str←ptr|). That string's reference count includes this particular reference. \smallskip\hang |cur←type=unknown←string| means that |cur←exp| points to a capsule node that is in the ring of variables equivalent to at least one undefined string variable. \smallskip\hang |cur←type=pen←type| means that |cur←exp| points to a pen header node. This node contains a reference count, which takes account of this particular reference. \smallskip\hang |cur←type=unknown←pen| means that |cur←exp| points to a capsule node that is in the ring of variables equivalent to at least one undefined pen variable. \smallskip\hang |cur←type=future←pen| means that |cur←exp| points to a knot list that should eventually be made into a pen. Nobody else points to this particular knot list. The |future←pen| option occurs only as an output of |scan←primary| and |scan←secondary|, not as an output of |scan←tertiary| or |scan←expression|. \smallskip\hang |cur←type=path←type| means that |cur←exp| points to a the first node of a path; nobody else points to this particular path. The control points of the path will have been chosen. \smallskip\hang |cur←type=unknown←path| means that |cur←exp| points to a capsule node that is in the ring of variables equivalent to at least one undefined path variable. \smallskip\hang |cur←type=picture←type| means that |cur←exp| points to an edges header node. Nobody else points to this particular set of edges. \smallskip\hang |cur←type=unknown←picture| means that |cur←exp| points to a capsule node that is in the ring of variables equivalent to at least one undefined picture variable. \smallskip\hang |cur←type=transform←type| means that |cur←exp| points to a capsule node whose type is |transform←type|. The |value| part of this capsule points to a transform node that contains six numeric values, each of which is |independent|, |dependent|, |proto←dependent|, or |known|. \smallskip\hang |cur←type=pair←type| means that |cur←exp| points to a capsule node whose type is |pair←type|. The |value| part of this capsule points to a pair node that contains two numeric values, each of which is |independent|, |dependent|, |proto←dependent|, or |known|. \smallskip\hang |cur←type=known| means that |cur←exp| is a |scaled| value. \smallskip\hang |cur←type=dependent| means that |cur←exp| points to a capsule node whose type is |dependent|. The |dep←list| field in this capsule points to the associated dependency list. \smallskip\hang |cur←type=proto←dependent| means that |cur←exp| points to a capsule node whose type is |proto←dependent|. The |dep←list| field in this capsule points to the associated dependency list. \smallskip\hang |cur←type=independent| means that |cur←exp| points to a capsule node whose type is |independent|. This somewhat unusual case can arise, for example, in the expression `$x+\&{begingroup}\,\&{string}\,x; 0\,\&{endgroup}$'. \smallskip\hang |cur←type=token←list| means that |cur←exp| points to a linked list of tokens. This case arises only on the left-hand side of an assignment (`\.{:=}') operation, under very special circumstances. \smallskip\noindent The possible settings of |cur←type| have been listed here in increasing numerical order. Notice that |cur←type| will never be |numeric←type| or |suffixed←macro| or |unsuffixed←macro|, although variables of those types are allowed. Conversely, \MF\ has no variables of type |vacuous| or |token←list|. @ Capsules are two-word nodes that have a similar meaning to |cur←type| and |cur←exp|. Such nodes have |name←type=capsule| and |link<=void|; and their |type| field is one of the possibilities for |cur←type| listed above. The |value| field of a capsule is, in most cases, the value that corresponds to its |type|, as |cur←exp| corresponds to |cur←type|. However, when |cur←exp| would point to a capsule, no extra layer of indirection is present; the |value| field is what would have been called |value(cur←exp)| if it had not been encapsulated. Furthermore, if the type is |dependent| or |proto←dependent|, the |value| field of a capsule is replaced by |dep←list| and |prev←dep| fields, since dependency lists in capsules are always part of the general |dep←list| structure. The |get←x←next| routine is careful not to change the values of |cur←type| and |cur←exp| when it gets an expanded token. However, |get←x←next| might call a macro, which might parse an expression, which might execute lots of commands in a group; hence it's possible that |cur←type| might change from, say, |unknown←boolean| to |boolean←type|, or from |dependent| to |known| or |independent|, during the time |get←x←next| is called. The programs below are careful to stash sensitive intermediate results in capsules, so that \MF's generality doesn't cause trouble. Here is a procedure that illustrates these conventions. It takes the contents of |(cur←type,cur←exp)| and stashes them away in a capsule. It is not used when |cur←type=token←list|. After the operation, |cur←type=vacuous|; hence there is no need to copy path lists or to update reference counts, etc. The special link |void| is put on the capsule returned by |stash←cur←exp|, because this procedure is used to store macro parameters that must be easily distinguishable from token lists. @<Declare the stashing/unstashing routines@>= function stash←cur←exp:pointer; var @!p:pointer; {the capsule that will be returned} begin case cur←type of unknown←types,transform←type,pair←type,dependent,proto←dependent, independent:p:=cur←exp; othercases begin p:=get←node(value←node←size); name←type(p):=capsule; type(p):=cur←type; value(p):=cur←exp; end endcases;@/ cur←type:=vacuous; link(p):=void; stash←cur←exp:=p; end; @ The inverse of |stash←cur←exp| is the following procedure, which deletes an unnecessary capsule and puts its contents into |cur←type| and |cur←exp|. The program steps of \MF\ can be divided into two categories: those in which |cur←type| and |cur←exp| are ``alive'' and those in which they are ``dead,'' in the sense that |cur←type| and |cur←exp| contain relevant information or not. It's important not to ignore them when they're alive, and it's important not to pay attention to them when they're dead. There's also an intermediate category: If |cur←type=vacuous|, then |cur←exp| is irrelevant, hence we can proceed without caring if |cur←type| and |cur←exp| are alive or dead. In such cases we say that |cur←type| and |cur←exp| are {\sl dormant}. It is permissible to call |get←x←next| only when they are alive or dormant. The \\{stash} procedure above assumes that |cur←type| and |cur←exp| are alive or dormant. The \\{unstash} procedure assumes that they are dead or dormant; it resuscitates them. @<Declare the stashing/unstashing...@>= procedure unstash←cur←exp(@!p:pointer); begin cur←type:=type(p); case cur←type of unknown←types,transform←type,pair←type,dependent,proto←dependent, independent: cur←exp:=p; othercases begin cur←exp:=value(p); free←node(p,value←node←size); end endcases;@/ end; @ The following procedure prints the values of expressions in an abbreviated format. If its first parameter |p| is null, the value of |(cur←type,cur←exp)| is displayed; otherwise |p| should be a capsule containing the desired value. The second parameter controls the amount of output. If it is~0, dependency lists will be abbreviated to `\.{linearform}' unless they consist of a single term. If it is greater than~1, complicated structures (pens, pictures, and paths) will be displayed in full. @<Declare subroutines for printing expressions@>= @t\4@>@<Declare the procedure called |print←dp|@>@; @t\4@>@<Declare the stashing/unstashing routines@>@; procedure print←exp(@!p:pointer;@!verbosity:small←number); var @!restore←cur←exp:boolean; {should |cur←exp| be restored?} @!t:small←number; {the type of the expression} @!v:integer; {the value of the expression} @!q:pointer; {a big node being displayed} begin if p<>null then restore←cur←exp:=false else begin p:=stash←cur←exp; restore←cur←exp:=true; end; t:=type(p); if t<dependent then v:=value(p)@+else if t<independent then v:=dep←list(p); @<Print an abbreviated value of |v| with format depending on |t|@>; if restore←cur←exp then unstash←cur←exp(p); end; @ @<Print an abbreviated value of |v| with format depending on |t|@>= case t of vacuous:print("vacuous"); boolean←type:if v=true←code then print("true")@+else print("false"); unknown←types,numeric←type:@<Display a variable that's been declared but not defined@>; string←type:begin print←char(""""); slow←print(v); print←char(""""); end; pen←type,future←pen,path←type,picture←type:@<Display a complex type@>; transform←type,pair←type:if v=null then print←type(t) else @<Display a big node@>; known:print←scaled(v); dependent,proto←dependent:print←dp(t,v,verbosity); independent:print←variable←name(p); othercases confusion("exp") @:this can't happen exp}{\quad exp@> endcases @ @<Display a big node@>= begin print←char("("); q:=v+big←node←size[t]; repeat if type(v)=known then print←scaled(value(v)) else if type(v)=independent then print←variable←name(v) else print←dp(type(v),dep←list(v),verbosity); v:=v+2; if v<>q then print←char(","); until v=q; print←char(")"); end @ Values of type \&{picture}, \&{path}, and \&{pen} are displayed verbosely in the log file only, unless the user has given a positive value to \\{tracingonline}. @<Display a complex type@>= if verbosity<=1 then print←type(t) else begin if selector=term←and←log then if internal[tracing←online]<=0 then begin selector:=term←only; print←type(t); print(" (see the transcript file)"); selector:=term←and←log; end; case t of pen←type:print←pen(v,"",false); future←pen:print←path(v," (future pen)",false); path←type:print←path(v,"",false); picture←type:begin cur←edges:=v; print←edges("",false,0,0); end; end; {there are no other cases} end @ @<Declare the procedure called |print←dp|@>= procedure print←dp(@!t:small←number;@!p:pointer;@!verbosity:small←number); var @!q:pointer; {the node following |p|} begin q:=link(p); if (info(q)=null) or (verbosity>0) then print←dependency(p,t) else print("linearform"); end; @ The displayed name of a variable in a ring will not be a capsule unless the ring consists entirely of capsules. @<Display a variable that's been declared but not defined@>= begin print←type(t); if v<>null then begin print←char(" "); while (name←type(v)=capsule) and (v<>p) do v:=value(v); print←variable←name(v); end; end @ When errors are detected during parsing, it is often helpful to display an expression just above the error message, using |exp←err| or |disp←err| instead of |print←err|. @d exp←err(#)==disp←err(null,#) {displays the current expression} @<Declare subroutines for printing expressions@>= procedure disp←err(@!p:pointer;@!s:str←number); begin if interaction=error←stop←mode then wake←up←terminal; print←nl(">> "); @.>>@> print←exp(p,1); {``medium verbose'' printing of the expression} if s<>"" then begin print←nl("! "); print(s); @.!@> end; end; @ If |cur←type| and |cur←exp| contain relevant information that should be recycled, we will use the following procedure, which changes |cur←type| to |known| and stores a given value in |cur←exp|. We can think of |cur←type| and |cur←exp| as either alive or dormant after this has been done, because |cur←exp| will not contain a pointer value. @<Declare the procedure called |flush←cur←exp|@>= procedure flush←cur←exp(@!v:scaled); begin case cur←type of unknown←types,transform←type,pair←type,@|dependent,proto←dependent,independent: begin recycle←value(cur←exp); free←node(cur←exp,value←node←size); end; pen←type: delete←pen←ref(cur←exp); string←type:delete←str←ref(cur←exp); future←pen,path←type: toss←knot←list(cur←exp); picture←type:toss←edges(cur←exp); othercases do←nothing endcases;@/ cur←type:=known; cur←exp:=v; end; @ There's a much more general procedure that is capable of releasing the storage associated with any two-word value packet. @<Declare the recycling subroutines@>= procedure recycle←value(@!p:pointer); label done; var @!t:small←number; {a type code} @!v:integer; {a value} @!vv:integer; {another value} @!q,@!r,@!s,@!pp:pointer; {link manipulation registers} begin t:=type(p); if t<dependent then v:=value(p); case t of undefined,vacuous,boolean←type,known,numeric←type:do←nothing; unknown←types:ring←delete(p); string←type:delete←str←ref(v); pen←type:delete←pen←ref(v); path←type,future←pen:toss←knot←list(v); picture←type:toss←edges(v); pair←type,transform←type:@<Recycle a big node@>; dependent,proto←dependent:@<Recycle a dependency list@>; independent:@<Recycle an independent variable@>; token←list,structured:confusion("recycle"); @:this can't happen recycle}{\quad recycle@> unsuffixed←macro,suffixed←macro:delete←mac←ref(value(p)); end; {there are no other cases} type(p):=undefined; end; @ @<Recycle a big node@>= if v<>null then begin q:=v+big←node←size[t]; repeat q:=q-2; recycle←value(q); until q=v; free←node(v,big←node←size[t]); end @ @<Recycle a dependency list@>= begin q:=dep←list(p); while info(q)<>null do q:=link(q); link(prev←dep(p)):=link(q); prev←dep(link(q)):=prev←dep(p); link(q):=null; flush←node←list(dep←list(p)); end @ When an independent variable disappears, it simply fades away, unless something depends on it. In the latter case, a dependent variable whose coeffcient of dependence is maximal will take its place. For example, suppose that variable $x$ is being recycled, and that the only variables depending on~$x$ are $y=2x+a$ and $z=x+b$. In this case we want to make $y$ independent and $z=.5y-.5a+b$; no other variables will depend on~$y$. If $\\{tracingequations}>0$ in this situation, we will print `\.{\#\#\# -2x=-y+a}'. There's a slight complication, however: An independent variable $x$ can occur both in dependency lists and in proto-dependency lists. This makes it necessary to be careful when deciding which coefficient is maximal. Furthermore, this complication is not so slight when a proto-dependent variable is chosen to become independent. For example, suppose that $y=2x+100a$ is proto-dependent while $z=x+b$ is dependent; then we must change $z=.5y-50a+b$ to a proto-dependency, because of the large coefficient `50'. In order to deal with these complications without wasting too much time, we shall link together the occurrences of~$x$ among all the linear dependencies, maintaining separate lists for the dependent and proto-dependent cases. @<Recycle an independent variable@>= begin max←c[dependent]:=0; max←c[proto←dependent]:=0;@/ max←link[dependent]:=null; max←link[proto←dependent]:=null;@/ q:=link(dep←head); while q<>dep←head do begin s:=value←loc(q); {now |link(s)=dep←list(q)|} loop@+ begin r:=link(s); if info(r)=null then goto done; if info(r)<>p then s:=r else begin t:=type(q); link(s):=link(r); info(r):=q; if abs(value(r))>max←c[t] then @<Record a new maximum coefficient of type |t|@> else begin link(r):=max←link[t]; max←link[t]:=r; end; end; end; done: q:=link(r); end; if (max←c[dependent]>0)or(max←c[proto←dependent]>0) then @<Choose a dependent variable to take the place of the disappearing independent variable, and change all remaining dependencies accordingly@>; end @ The code for independency removal makes use of three two-word arrays. @<Glob...@>= @!max←c:array[dependent..proto←dependent] of integer; {max coefficient magnitude} @!max←ptr:array[dependent..proto←dependent] of pointer; {where |p| occurs with |max←c|} @!max←link:array[dependent..proto←dependent] of pointer; {other occurrences of |p|} @ @<Record a new maximum coefficient...@>= begin if max←c[t]>0 then begin link(max←ptr[t]):=max←link[t]; max←link[t]:=max←ptr[t]; end; max←c[t]:=abs(value(r)); max←ptr[t]:=r; end @ @<Choose a dependent...@>= begin if (max←c[dependent]>=fraction←one)or@| (max←c[dependent] div @'10000 >= max←c[proto←dependent]) then t:=dependent else t:=proto←dependent; @<Determine the dependency list |s| to substitute for the independent variable~|p|@>; t:=dependent+proto←dependent-t; {complement |t|} if max←c[t]>0 then {we need to pick up an unchosen dependency} begin link(max←ptr[t]):=max←link[t]; max←link[t]:=max←ptr[t]; end; if t<>dependent then @<Substitute new dependencies in place of |p|@> else @<Substitute new proto-dependencies in place of |p|@>; flush←node←list(s); if fix←needed then fix←dependencies; check←arith; end @ Let |r=max←ptr[t]|. At this point we have $|value|(r)=\pm|max←c|[t]$, and |info(r)| points to the dependent variable~|pp| of type~|t| from whose dependency list we have removed node~|r|. We must reinsert node~|r| into the dependency list, with coefficient $-1.0$, and with |pp| as the new independent variable. @<Determine the dep...@>= r:=max←ptr[t]; pp:=info(r); v:=value(r); if t=dependent then value(r):=-fraction←one@+else value(r):=-unity; q:=value←loc(pp); s:=link(q); {|s=dep←list(pp)|} while info(s)>pp do begin q:=s; s:=link(q); end; link(q):=r; link(r):=s; while info(s)<>null do s:=link(s); q:=link(s); link(s):=null; s:=dep←list(pp); prev←dep(q):=prev←dep(pp); link(prev←dep(pp)):=q; type(pp):=independent; value(pp):=0; if cur←exp=pp then if cur←type=t then cur←type:=independent; if internal[tracing←equations]>0 then @<Show the transformed dependency@> @ Now $(-v)$ times the formerly independent variable~|p| is being replaced by the dependency list~|s|. @<Show the transformed...@>= if interesting(p) then begin begin←diagnostic; print←nl("### "); if v>0 then print←char("-"); if t=dependent then vv:=round←fraction(max←c[dependent]) else vv:=max←c[proto←dependent]; if vv<>unity then print←scaled(vv); print←variable←name(p); while value(p)>0 do begin print("*4"); value(p):=value(p)-2; end; if t=dependent then print←char("=")@+else print(" = "); print←dependency(s,t); end←diagnostic(false); end @ Finally, there are dependent and proto-dependent variables whose dependency lists must be brought up to date. @<Substitute new dependencies...@>= for t:=dependent to proto←dependent do begin r:=max←link[t]; while r<>null do begin q:=info(r); dep←list(q):=p←plus←fq(dep←list(q),@| make←fraction(value(r),-v),s,t,dependent); if dep←list(q)=dep←final then make←known(q,dep←final); q:=r; r:=link(r); free←node(q,dep←node←size); end; end @ @<Substitute new proto...@>= for t:=dependent to proto←dependent do begin r:=max←link[t]; while r<>null do begin q:=info(r); if t=dependent then {for safety's sake, we change |q| to |proto←dependent|} begin if cur←exp=q then if cur←type=dependent then cur←type:=proto←dependent; dep←list(q):=p←over←v(dep←list(q),unity,dependent,proto←dependent); type(q):=proto←dependent; value(r):=round←fraction(value(r)); end; dep←list(q):=p←plus←fq(dep←list(q),@| make←scaled(value(r),-v),s,proto←dependent,proto←dependent); if dep←list(q)=dep←final then make←known(q,dep←final); q:=r; r:=link(r); free←node(q,dep←node←size); end; end @ Here are some routines that provide handy combinations of actions that are often needed during error recovery. For example, `|flush←error|' flushes the current expression, replaces it by a given value, and calls |error|. Errors often are detected after an extra token has already been scanned. The `\\{put\←get}' routines put that token back before calling |error|; then they get it back again. (Or perhaps they get another token, if the user has changed things.) @<Declare the procedure called |flush←cur←exp|@>= procedure flush←error(@!v:scaled); begin error; flush←cur←exp(v); end; @# procedure@?back←error; forward;@t\2@>@/ procedure@?get←x←next; forward;@t\2@>@/ @# procedure put←get←error; begin back←error; get←x←next; end; @# procedure put←get←flush←error(@!v:scaled); begin put←get←error; flush←cur←exp(v); end; @ A global variable called |var←flag| is set to a special command code just before \MF\ calls |scan←expression|, if the expression should be treated as a variable when this command code immediately follows. For example, |var←flag| is set to |assignment| at the beginning of a statement, because we want to know the {\sl location\/} of a variable at the left of `\.{:=}', not the {\sl value\/} of that variable. The |scan←expression| subroutine calls |scan←tertiary|, which calls |scan←secondary|, which calls |scan←primary|, which sets |var←flag:=0|. In this way each of the scanning routines ``knows'' when it has been called with a special |var←flag|, but |var←flag| is usually zero. A variable preceding a command that equals |var←flag| is converted to a token list rather than a value. Furthermore, an `\.{=}' sign following an expression with |var←flag=assignment| is not considered to be a relation that produces boolean expressions. @<Glob...@>= @!var←flag:0..max←command←code; {command that wants a variable} @ @<Set init...@>= var←flag:=0; @* \[40] Parsing primary expressions. The first parsing routine, |scan←primary|, is also the most complicated one, since it involves so many different cases. But each case---with one exception---is fairly simple by itself. When |scan←primary| begins, the first token of the primary to be scanned should already appear in |cur←cmd|, |cur←mod|, and |cur←sym|. The values of |cur←type| and |cur←exp| should be either dead or dormant, as explained earlier. If |cur←cmd| is not between |min←primary←command| and |max←primary←command|, inclusive, a syntax error will be signalled. @<Declare the basic parsing subroutines@>= procedure scan←primary; label restart, done, done1, done2; var @!p,@!q,@!r:pointer; {for list manipulation} @!c:quarterword; {a primitive operation code} @!my←var←flag:0..max←command←code; {initial value of |my←var←flag|} @!l←delim,@!r←delim:pointer; {hash addresses of a delimiter pair} @<Other local variables for |scan←primary|@>@; begin my←var←flag:=var←flag; var←flag:=0; restart:check←arith; @<Supply diagnostic information, if requested@>; case cur←cmd of left←delimiter:@<Scan a delimited primary@>; begin←group:@<Scan a grouped primary@>; string←token:@<Scan a string constant@>; numeric←token:@<Scan a primary that starts with a numeric token@>; nullary:@<Scan a nullary operation@>; unary,type←name,cycle,plus←or←minus:@<Scan a unary operation@>; primary←binary:@<Scan a binary operation with `\&{of}' between its operands@>; str←op:@<Convert a suffix to a string@>; internal←quantity:@<Scan an internal numeric quantity@>; capsule←token:make←exp←copy(cur←mod); tag←token:@<Scan a variable primary; |goto restart| if it turns out to be a macro@>; othercases begin bad←exp("A primary"); goto restart; @.A primary expression...@> end endcases;@/ get←x←next; {the routines |goto done| if they don't want this} done: if cur←cmd=left←bracket then if cur←type>=known then @<Scan a mediation construction@>; end; @ Errors at the beginning of expressions are flagged by |bad←exp|. @p procedure bad←exp(@!s:str←number); var save←flag:0..max←command←code; begin print←err(s); print(" expression can't begin with `"); print←cmd←mod(cur←cmd,cur←mod); print←char("'"); help4("I'm afraid I need some sort of value in order to continue,")@/ ("so I've tentatively inserted `0'. You may want to")@/ ("delete this zero and insert something else;")@/ ("see Chapter 27 of The METAFONTbook for an example."); @:METAFONTbook}{\sl The {\logos METAFONT\/}book@> back←input; cur←sym:=0; cur←cmd:=numeric←token; cur←mod:=0; ins←error;@/ save←flag:=var←flag; var←flag:=0; get←x←next; var←flag:=save←flag; end; @ @<Supply diagnostic information, if requested@>= debug if panicking then check←mem(false);@+gubed@;@/ if interrupt<>0 then if OK←to←interrupt then begin back←input; check←interrupt; get←x←next; end @ @<Scan a delimited primary@>= begin l←delim:=cur←sym; r←delim:=cur←mod; get←x←next; scan←expression; if (cur←cmd=comma) and (cur←type>=known) then @<Scan the second of a pair of numerics@> else check←delimiter(l←delim,r←delim); end @ The |stash←in| subroutine puts the current (numeric) expression into a field within a ``big node.'' @p procedure stash←in(@!p:pointer); var @!q:pointer; {temporary register} begin type(p):=cur←type; if cur←type=known then value(p):=cur←exp else begin if cur←type=independent then @<Stash an independent |cur←exp| into a big node@> else begin mem[value←loc(p)]:=mem[value←loc(cur←exp)]; {|dep←list(p):=dep←list(cur←exp)| and |prev←dep(p):=prev←dep(cur←exp)|} link(prev←dep(p)):=p; end; free←node(cur←exp,value←node←size); end; cur←type:=vacuous; end; @ In rare cases the current expression can become |independent|. There may be many dependency lists pointing to such an independent capsule, so we can't simply move it into place within a big node. Instead, we copy it, then recycle it. @ @<Stash an independent |cur←exp|...@>= begin q:=single←dependency(cur←exp); if q=dep←final then begin type(p):=known; value(p):=0; free←node(q,dep←node←size); end else begin type(p):=dependent; new←dep(p,q); end; recycle←value(cur←exp); end @ @<Scan the second of a pair of numerics@>= begin p:=get←node(value←node←size); type(p):=pair←type; name←type(p):=capsule; init←big←node(p); q:=value(p); stash←in(x←part←loc(q));@/ get←x←next; scan←expression; if cur←type<known then begin exp←err("Nonnumeric ypart has been replaced by 0"); @.Nonnumeric...replaced by 0@> help4("I thought you were giving me a pair `(x,y)'; but")@/ ("after finding a nice xpart `x' I found a ypart `y'")@/ ("that isn't of numeric type. So I've changed y to zero.")@/ ("(The y that I didn't like appears above the error message.)"); put←get←flush←error(0); end; stash←in(y←part←loc(q)); check←delimiter(l←delim,r←delim); cur←type:=pair←type; cur←exp:=p; end @ The local variable |group←line| keeps track of the line where a \&{begingroup} command occurred; this will be useful in an error message if the group doesn't actually end. @<Other local variables for |scan←primary|@>= @!group←line:integer; {where a group began} @ @<Scan a grouped primary@>= begin group←line:=line; if internal[tracing←commands]>0 then show←cur←cmd←mod; save←boundary←item(p); repeat do←statement; {ends with |cur←cmd>=semicolon|} until cur←cmd<>semicolon; if cur←cmd<>end←group then begin print←err("A group begun on line "); @.A group...never ended@> print←int(group←line); print(" never ended"); help2("I saw a `begingroup' back there that hasn't been matched")@/ ("by `endgroup'. So I've inserted `endgroup' now."); back←error; cur←cmd:=end←group; end; unsave; {this might change |cur←type|, if independent variables are recycled} if internal[tracing←commands]>0 then show←cur←cmd←mod; end @ @<Scan a string constant@>= begin cur←type:=string←type; cur←exp:=cur←mod; end @ Later we'll come to procedures that perform actual operations like addition, square root, and so on; our purpose now is to do the parsing. But we might as well mention those future procedures now, so that the suspense won't be too bad: \smallskip |do←nullary(c)| does primitive operations that have no operands (e.g., `\&{true}' or `\&{pencircle}'); \smallskip |do←unary(c)| applies a primitive operation to the current expression; \smallskip |do←binary(p,c)| applies a primitive operation to the capsule~|p| and the current expression. @<Scan a nullary operation@>=do←nullary(cur←mod) @ @<Scan a unary operation@>= begin c:=cur←mod; get←x←next; scan←primary; do←unary(c); goto done; end @ A numeric token might be a primary by itself, or it might be the numerator of a fraction composed solely of numeric tokens, or it might multiply the primary that follows (provided that the primary doesn't begin with a plus sign or a minus sign). The code here uses the facts that |max←primary←command=plus←or←minus| and |max←primary←command-1=numeric←token|. If a fraction is found that is less than unity, we try to retain higher precision when we use it in scalar multiplication. @<Other local variables for |scan←primary|@>= @!num,@!denom:scaled; {for primaries that are fractions, like `1/2'} @ @<Scan a primary that starts with a numeric token@>= begin cur←exp:=cur←mod; cur←type:=known; get←x←next; if cur←cmd<>slash then begin num:=0; denom:=0; end else begin get←x←next; if cur←cmd<>numeric←token then begin back←input; cur←cmd:=slash; cur←mod:=over; cur←sym:=frozen←slash; goto done; end; num:=cur←exp; denom:=cur←mod; if denom=0 then @<Protest division by zero@> else cur←exp:=make←scaled(num,denom); check←arith; get←x←next; end; if cur←cmd>=min←primary←command then if cur←cmd<numeric←token then {in particular, |cur←cmd<>plus←or←minus|} begin p:=stash←cur←exp; scan←primary; if (abs(num)>=abs(denom))or(cur←type<pair←type) then do←binary(p,times) else begin frac←mult(num,denom); free←node(p,value←node←size); end; end; goto done; end @ @<Protest division...@>= begin print←err("Division by zero"); @.Division by zero@> help1("I'll pretend that you meant to divide by 1."); error; end @ @<Scan a binary operation with `\&{of}' between its operands@>= begin c:=cur←mod; get←x←next; scan←expression; if cur←cmd<>of←token then begin missing←err("of"); print(" for "); print←cmd←mod(primary←binary,c); @.Missing `of'@> help1("I've got the first argument; will look now for the other."); back←error; end; p:=stash←cur←exp; get←x←next; scan←primary; do←binary(p,c); goto done; end @ @<Convert a suffix to a string@>= begin get←x←next; scan←suffix; old←setting:=selector; selector:=new←string; show←token←list(cur←exp,null,100000,0); flush←token←list(cur←exp); cur←exp:=make←string; selector:=old←setting; cur←type:=string←type; goto done; end @ If an internal quantity appears all by itself on the left of an assignment, we return a token list of length one, containing the address of the internal quantity plus |hash←end|. (This accords with the conventions of the save stack, as described earlier.) @<Scan an internal...@>= begin q:=cur←mod; if my←var←flag=assignment then begin get←x←next; if cur←cmd=assignment then begin cur←exp:=get←avail; info(cur←exp):=q+hash←end; cur←type:=token←list; goto done; end; back←input; end; cur←type:=known; cur←exp:=internal[q]; end @ The most difficult part of |scan←primary| has been saved for last, since it was necessary to build up some confidence first. We can now face the task of scanning a variable. As we scan a variable, we build a token list containing the relevant names and subscript values, simultaneously following along in the ``collective'' structure to see if we are actually dealing with a macro instead of a value. The local variables |pre←head| and |post←head| will point to the beginning of the prefix and suffix lists; |tail| will point to the end of the list that is currently growing. Another local variable, |tt|, contains partial information about the declared type of the variable-so-far. If |tt>=unsuffixed←macro|, the relation |tt=type(q)| will always hold. If |tt=undefined|, the routine doesn't bother to update its information about type. And if |undefined<tt<unsuffixed←macro|, the precise value of |tt| isn't critical. @ @<Other local variables for |scan←primary|@>= @!pre←head,@!post←head,@!tail:pointer; {prefix and suffix list variables} @!tt:small←number; {approximation to the type of the variable-so-far} @!t:pointer; {a token} @!macro←ref:pointer; {reference count for a suffixed macro} @ @<Scan a variable primary...@>= begin fast←get←avail(pre←head); tail:=pre←head; post←head:=null; tt:=vacuous; loop@+ begin t:=cur←tok; link(tail):=t; if tt<>undefined then begin @<Find the approximate type |tt| and corresponding~|q|@>; if tt>=unsuffixed←macro then @<Either begin an unsuffixed macro call or prepare for a suffixed one@>; end; get←x←next; tail:=t; if cur←cmd=left←bracket then @<Scan for a subscript; replace |cur←cmd| by |numeric←token| if found@>; if cur←cmd>max←suffix←token then goto done1; if cur←cmd<min←suffix←token then goto done1; end; {now |cur←cmd| is |internal←quantity|, |tag←token|, or |numeric←token|} done1:@<Handle unusual cases that masquerade as variables, and |goto restart| or |goto done| if appropriate; otherwise make a copy of the variable and |goto done|@>; end @ @<Either begin an unsuffixed macro call or...@>= begin link(tail):=null; if tt>unsuffixed←macro then {|tt=suffixed←macro|} begin post←head:=get←avail; tail:=post←head; link(tail):=t;@/ tt:=undefined; macro←ref:=value(q); add←mac←ref(macro←ref); end else @<Set up unsuffixed macro call and |goto restart|@>; end @ @<Scan for a subscript; replace |cur←cmd| by |numeric←token| if found@>= begin get←x←next; scan←expression; if cur←cmd<>right←bracket then @<Put the left bracket and the expression back to be rescanned@> else begin if cur←type<>known then bad←subscript; cur←cmd:=numeric←token; cur←mod:=cur←exp; cur←sym:=0; end; end @ The left bracket that we thought was introducing a subscript might have actually been the left bracket in a mediation construction like `\.{x[a,b]}'. So we don't issue an error message at this point; but we do want to back up so as to avoid any embarrassment about our incorrect assumption. @<Put the left bracket and the expression back to be rescanned@>= begin back←input; {that was the token following the current expression} back←expr; cur←cmd:=left←bracket; cur←mod:=0; cur←sym:=frozen←left←bracket; end @ Here's a routine that puts the current expression back to be read again. @p procedure back←expr; var @!p:pointer; {capsule token} begin p:=stash←cur←exp; link(p):=null; back←list(p); end; @ Unknown subscripts lead to the following error message. @p procedure bad←subscript; begin exp←err("Improper subscript has been replaced by zero"); @.Improper subscript...@> help3("A bracketed subscript must have a known numeric value;")@/ ("unfortunately, what I found was the value that appears just")@/ ("above this error message. So I'll try a zero subscript."); flush←error(0); end; @ Every time we call |get←x←next|, there's a chance that the variable we've been looking at will disappear. Thus, we cannot safely keep |q| pointing into the variable structure; we need to start searching from the root each time. @<Find the approximate type |tt| and corresponding~|q|@>= @↑inner loop@> begin p:=link(pre←head); q:=info(p); tt:=undefined; if eq←type(q) mod outer←tag=tag←token then begin q:=equiv(q); if q=null then goto done2; loop@+ begin p:=link(p); if p=null then begin tt:=type(q); goto done2; end; if type(q)<>structured then goto done2; q:=link(attr←head(q)); {the |collective←subscript| attribute} if p>=hi←mem←min then {it's not a subscript} begin repeat q:=link(q); until attr←loc(q)>=info(p); if attr←loc(q)>info(p) then goto done2; end; end; end; done2:end @ How do things stand now? Well, we have scanned an entire variable name, including possible subscripts and/or attributes; |cur←cmd|, |cur←mod|, and |cur←sym| represent the token that follows. If |post←head=null|, a token list for this variable name starts at |link(pre←head)|, with all subscripts evaluated. But if |post←head<>null|, the variable turned out to be a suffixed macro; |pre←head| is the head of the prefix list, while |post←head| is the head of a token list containing both `\.{\AT!}' and the suffix. Our immediate problem is to see if this variable still exists. (Variable structures can change drastically whenever we call |get←x←next|; users aren't supposed to do this, but the fact that it is possible means that we must be cautious.) The following procedure prints an error message when a variable unexpectedly disappears. Its help message isn't quite right for our present purposes, but we'll be able to fix that up. @p procedure obliterated(@!q:pointer); begin print←err("Variable "); show←token←list(q,null,1000,0); print(" has been obliterated"); @.Variable...obliterated@> help5("It seems you did a nasty thing---probably by accident,")@/ ("but nevertheless you nearly hornswoggled me...")@/ ("While I was evaluating the right-hand side of this")@/ ("command, something happened, and the left-hand side")@/ ("is no longer a variable! So I won't change anything."); end; @ If the variable does exist, we also need to check for a few other special cases before deciding that a plain old ordinary variable has, indeed, been scanned. @<Handle unusual cases that masquerade as variables...@>= if post←head<>null then @<Set up suffixed macro call and |goto restart|@>; q:=link(pre←head); free←avail(pre←head); if cur←cmd=my←var←flag then begin cur←type:=token←list; cur←exp:=q; goto done; end; p:=find←variable(q); if p<>null then make←exp←copy(p) else begin obliterated(q);@/ help←line[2]:="While I was evaluating the suffix of this variable,"; help←line[1]:="something was redefined, and it's no longer a variable!"; help←line[0]:="In order to get back on my feet, I've inserted `0' instead."; put←get←flush←error(0); end; flush←node←list(q); goto done @ The only complication associated with macro calling is that the prefix and ``at'' parameters must be packaged in an appropriate list of lists. @<Set up unsuffixed macro call and |goto restart|@>= begin p:=get←avail; info(pre←head):=link(pre←head); link(pre←head):=p; info(p):=t; macro←call(value(q),pre←head,null); get←x←next; goto restart; end @ If the ``variable'' that turned out to be a suffixed macro no longer exists, we don't care, because we have reserved a pointer (|macro←ref|) to its token list. @<Set up suffixed macro call and |goto restart|@>= begin back←input; p:=get←avail; q:=link(post←head); info(pre←head):=link(pre←head); link(pre←head):=post←head; info(post←head):=q; link(post←head):=p; info(p):=link(q); link(q):=null; macro←call(macro←ref,pre←head,null); decr(ref←count(macro←ref)); get←x←next; goto restart; end @ Our remaining job is simply to make a copy of the value that has been found. Some cases are harder than others, but complexity arises solely because of the multiplicity of possible cases. @<Declare the procedure called |make←exp←copy|@>= @t\4@>@<Declare subroutines needed by |make←exp←copy|@>@; procedure make←exp←copy(@!p:pointer); label restart; var @!q,@!r,@!t:pointer; {registers for list manipulation} begin restart: cur←type:=type(p); case cur←type of vacuous,boolean←type,known:cur←exp:=value(p); unknown←types:cur←exp:=new←ring←entry(p); string←type:begin cur←exp:=value(p); add←str←ref(cur←exp); end; pen←type:begin cur←exp:=value(p); add←pen←ref(cur←exp); end; picture←type:cur←exp:=copy←edges(value(p)); path←type,future←pen:cur←exp:=copy←path(value(p)); transform←type,pair←type:@<Copy the big node |p|@>; dependent,proto←dependent:encapsulate(copy←dep←list(dep←list(p))); numeric←type:begin type(p):=independent; value(p):=0; goto restart; end; independent: begin q:=single←dependency(p); if q=dep←final then begin cur←type:=known; cur←exp:=0; free←node(q,value←node←size); end else begin cur←type:=dependent; encapsulate(q); end; end; othercases confusion("copy") @:this can't happen copy}{\quad copy@> endcases; end; @ The |encapsulate| subroutine assumes that |dep←final| is the tail of dependency list~|p|. @<Declare subroutines needed by |make←exp←copy|@>= procedure encapsulate(@!p:pointer); begin cur←exp:=get←node(value←node←size); type(cur←exp):=cur←type; name←type(cur←exp):=capsule; new←dep(cur←exp,p); end; @ The most tedious case arises when the user refers to a \&{pair} or \&{transform} variable; we must copy several fields, each of which can be |independent|, |dependent|, |proto←dependent|, or |known|. @<Copy the big node |p|@>= begin if value(p)=null then init←big←node(p); t:=get←node(value←node←size); name←type(t):=capsule; type(t):=cur←type; init←big←node(t);@/ q:=value(p)+big←node←size[cur←type]; r:=value(t)+big←node←size[cur←type]; repeat q:=q-2; r:=r-2; install(r,q); until q=value(p); cur←exp:=t; end @ The |install| procedure copies a numeric field~|q| into field~|r| of a big node that will be part of a capsule. @<Declare subroutines needed by |make←exp←copy|@>= procedure install(@!r,@!q:pointer); var p:pointer; {temporary register} begin if type(q)=known then begin value(r):=value(q); type(r):=known; end else if type(q)=independent then begin p:=single←dependency(q); if p=dep←final then begin type(r):=known; value(r):=0; free←node(p,value←node←size); end else begin type(r):=dependent; new←dep(r,p); end; end else begin type(r):=type(q); new←dep(r,copy←dep←list(dep←list(q))); end; end; @ Expressions of the form `\.{a[b,c]}' are converted into `\.{b+a*(c-b)}', without checking the types of \.b~or~\.c. @<Scan a mediation...@>= begin p:=stash←cur←exp; get←x←next; scan←expression; if cur←cmd<>comma then begin @<Put the left bracket and the expression back...@>; unstash←cur←exp(p); end else begin q:=stash←cur←exp; get←x←next; scan←expression; if cur←cmd<>right←bracket then begin missing←err("]");@/ @.Missing `]'@> help3("I've scanned an expression of the form `a[b,c',")@/ ("so a right bracket should have come next.")@/ ("I shall pretend that one was there.");@/ back←error; end; r:=stash←cur←exp; make←exp←copy(q);@/ do←binary(r,minus); do←binary(p,times); do←binary(q,plus); get←x←next; end; end @ Here is a comparatively simple routine that is used to scan the \&{suffix} parameters of a macro. @<Declare the basic parsing subroutines@>= procedure scan←suffix; label done; var @!h,@!t:pointer; {head and tail of the list being built} @!p:pointer; {temporary register} begin h:=get←avail; t:=h; loop@+ begin if cur←cmd=left←bracket then @<Scan a bracketed subscript and set |cur←cmd:=numeric←token|@>; if cur←cmd=numeric←token then p:=new←num←tok(cur←mod) else if (cur←cmd=tag←token)or(cur←cmd=internal←quantity) then begin p:=get←avail; info(p):=cur←sym; end else goto done; link(t):=p; t:=p; get←x←next; end; done: cur←exp:=link(h); free←avail(h); cur←type:=token←list; end; @ @<Scan a bracketed subscript and set |cur←cmd:=numeric←token|@>= begin get←x←next; scan←expression; if cur←type<>known then bad←subscript; if cur←cmd<>right←bracket then begin missing←err("]");@/ @.Missing `]'@> help3("I've seen a `[' and a subscript value, in a suffix,")@/ ("so a right bracket should have come next.")@/ ("I shall pretend that one was there.");@/ back←error; end; cur←cmd:=numeric←token; cur←mod:=cur←exp; end @* \[41] Parsing secondary and higher expressions. After the complications of |scan←primary|, the |scan←secondary| routine is refreshingly simple. It's not trivial, but the operations are relatively straightforward; the main difficulty is, again, that expressions and data structures might change drastically every time we call |get←x←next|, so a cautious approach is mandatory. For example, a macro defined by \&{primarydef} might have disappeared by the time its second argument has been scanned; we solve this by increasing the reference count of its token list, so that the macro can be called even after it has been clobbered. @<Declare the basic parsing subroutines@>= procedure scan←secondary; label restart,continue; var @!p,@!q,@!r:pointer; {for list manipulation} @!c,@!d:halfword; {operation codes or modifiers} @!mac←name:pointer; {token defined with \&{primarydef}} begin restart:if(cur←cmd<min←primary←command)or@| (cur←cmd>max←primary←command) then bad←exp("A secondary"); @.A secondary expression...@> scan←primary; continue: if cur←cmd<=max←secondary←command then if cur←cmd>=min←secondary←command then begin p:=stash←cur←exp; c:=cur←mod; d:=cur←cmd; if d=secondary←primary←macro then begin mac←name:=cur←sym; add←mac←ref(c); end; get←x←next; scan←primary; if d<>secondary←primary←macro then do←binary(p,c) else begin back←input; binary←mac(p,c,mac←name); decr(ref←count(c)); get←x←next; goto restart; end; goto continue; end; end; @ The following procedure calls a macro that has two parameters, |p| and |cur←exp|. @p procedure binary←mac(@!p,@!c,@!n:pointer); var @!q,@!r:pointer; {nodes in the parameter list} begin q:=get←avail; r:=get←avail; link(q):=r;@/ info(q):=p; info(r):=stash←cur←exp;@/ macro←call(c,q,n); end; @ The next procedure, |scan←tertiary|, is pretty much the same deal. @<Declare the basic parsing subroutines@>= procedure scan←tertiary; label restart,continue; var @!p:pointer; {for list manipulation} @!c,@!d:halfword; {operation codes or modifiers} @!mac←name:pointer; {token defined with \&{secondarydef}} begin restart:if(cur←cmd<min←primary←command)or@| (cur←cmd>max←primary←command) then bad←exp("A tertiary"); @.A tertiary expression...@> scan←secondary; if cur←type=future←pen then materialize←pen; continue: if cur←cmd<=max←tertiary←command then if cur←cmd>=min←tertiary←command then begin p:=stash←cur←exp; c:=cur←mod; d:=cur←cmd; if d=tertiary←secondary←macro then begin mac←name:=cur←sym; add←mac←ref(c); end; get←x←next; scan←secondary; if d<>tertiary←secondary←macro then do←binary(p,c) else begin back←input; binary←mac(p,c,mac←name); decr(ref←count(c)); get←x←next; goto restart; end; goto continue; end; end; @ A |future←pen| becomes a full-fledged pen here. @p procedure materialize←pen; label common←ending; var @!a←minus←b,@!a←plus←b,@!major←axis,@!minor←axis:scaled; {ellipse variables} @!theta:angle; {amount by which the ellipse has been rotated} @!p:pointer; {path traverser} @!q:pointer; {the knot list to be made into a pen} begin q:=cur←exp; if left←type(q)=endpoint then begin print←err("Pen path must be a cycle"); @.Pen path must be a cycle@> help2("I can't make a pen from the given path.")@/ ("So I've replaced it by the trivial path `(0,0)..cycle'."); put←get←error; cur←exp:=null←pen; goto common←ending; end else if left←type(q)=open then @<Change node |q| to a path for an elliptical pen@>; cur←exp:=make←pen(q); common←ending: toss←knot←list(q); cur←type:=pen←type; end; @ We placed the three points $(0,0)$, $(1,0)$, $(0,1)$ into a \&{pencircle}, and they have now been transformed to $(u,v)$, $(A+u,B+v)$, $(C+u,D+v)$; this gives us enough information to deduce the transformation $(x,y)\mapsto(Ax+Cy+u,Bx+Dy+v)$. Given ($A,B,C,D)$ we can always find $(a,b,\theta,\phi)$ such that $$\eqalign{A&=a\cos\phi\cos\theta-b\sin\phi\sin\theta;\cr B&=a\cos\phi\sin\theta+b\sin\phi\cos\theta;\cr C&=-a\sin\phi\cos\theta-b\cos\phi\sin\theta;\cr D&=-a\sin\phi\sin\theta+b\cos\phi\cos\theta.\cr}$$ In this notation, the unit circle $(\cos t,\sin t)$ is transformed into $$\bigl(a\cos(\phi+t)\cos\theta-b\sin(\phi+t)\sin\theta,\; a\cos(\phi+t)\sin\theta+b\sin(\phi+t)\cos\theta\bigr)\;+\;(u,v),$$ which is an ellipse with semi-axes~$(a,b)$, rotated by~$\theta$ and shifted by~$(u,v)$. To solve the stated equations, we note that it is necessary and sufficient to solve $$\eqalign{A-D&=(a-b)\cos(\theta-\phi),\cr B+C&=(a-b)\sin(\theta-\phi),\cr} \qquad \eqalign{A+D&=(a+b)\cos(\theta+\phi),\cr B-C&=(a+b)\sin(\theta+\phi);\cr}$$ and it is easy to find $a-b$, $a+b$, $\theta-\phi$, and $\theta+\phi$ from these formulas. The code below uses |(txx,tyx,txy,tyy,tx,ty)| to stand for $(A,B,C,D,u,v)$. @<Change node |q|...@>= begin tx:=x←coord(q); ty:=y←coord(q); txx:=left←x(q)-tx; tyx:=left←y(q)-ty; txy:=right←x(q)-tx; tyy:=right←y(q)-ty; a←minus←b:=pyth←add(txx-tyy,tyx+txy); a←plus←b:=pyth←add(txx+tyy,tyx-txy); major←axis:=half(a←minus←b+a←plus←b); minor←axis:=half(abs(a←plus←b-a←minus←b)); if major←axis=minor←axis then theta:=0 {circle} else theta:=half(n←arg(txx-tyy,tyx+txy)+n←arg(txx+tyy,tyx-txy)); free←node(q,knot←node←size); q:=make←ellipse(major←axis,minor←axis,theta); if (tx<>0)or(ty<>0) then @<Shift the coordinates of path |q|@>; end @ @<Shift the coordinates of path |q|@>= begin p:=q; repeat x←coord(p):=x←coord(p)+tx; y←coord(p):=y←coord(p)+ty; p:=link(p); until p=q; end @ Finally we reach the deepest level in our quartet of parsing routines. This one is much like the others; but it has an extra complication from the construction of paths, which materialize here. @d continue←path=25 {a label inside of |scan←expression|} @d finish←path=26 {another} @<Declare the basic parsing subroutines@>= procedure scan←expression; label restart,done,continue,continue←path,finish←path,exit; var @!p,@!q,@!r,@!pp,@!qq:pointer; {for list manipulation} @!c,@!d:halfword; {operation codes or modifiers} @!my←var←flag:0..max←command←code; {initial value of |var←flag|} @!mac←name:pointer; {token defined with \&{tertiarydef}} @!cycle←hit:boolean; {did a path expression just end with `\&{cycle}'?} @!x,@!y:scaled; {explicit coordinates or tension at a path join} @!t:endpoint..open; {knot type following a path join} begin my←var←flag:=var←flag; restart:if(cur←cmd<min←primary←command)or@| (cur←cmd>max←primary←command) then bad←exp("An"); @.An expression...@> scan←tertiary; continue: if cur←cmd<=max←expression←command then if cur←cmd>=min←expression←command then if (cur←cmd<>equals)or(my←var←flag<>assignment) then begin p:=stash←cur←exp; c:=cur←mod; d:=cur←cmd; if d=expression←tertiary←macro then begin mac←name:=cur←sym; add←mac←ref(c); end; if (d<ampersand)or((d=ampersand)and@| ((type(p)=pair←type)or(type(p)=path←type))) then @<Scan a path construction operation; but |return| if |p| has the wrong type@> else begin get←x←next; scan←tertiary; if d<>expression←tertiary←macro then do←binary(p,c) else begin back←input; binary←mac(p,c,mac←name); decr(ref←count(c)); get←x←next; goto restart; end; end; goto continue; end; exit:end; @ The reader should review the data structure conventions for paths before hoping to understand the next part of this code. @<Scan a path construction operation...@>= begin cycle←hit:=false; @<Convert the left operand, |p|, into a partial path ending at~|q|; but |return| if |p| doesn't have a suitable type@>; continue←path: @<Determine the path join parameters; but |goto finish←path| if there's only a direction specifier@>; if cur←cmd=cycle then @<Get ready to close a cycle@> else begin scan←tertiary; @<Convert the right operand, |cur←exp|, into a partial path from |pp| to~|qq|@>; end; @<Join the partial paths and reset |p| and |q| to the head and tail of the result@>; if cur←cmd>=min←expression←command then if cur←cmd<=ampersand then if not cycle←hit then goto continue←path; finish←path: @<Choose control points for the path and put the result into |cur←exp|@>; end @ @<Convert the left operand, |p|, into a partial path ending at~|q|...@>= begin unstash←cur←exp(p); if cur←type=pair←type then p:=new←knot else if cur←type=path←type then p:=cur←exp else return; q:=p; while link(q)<>p do q:=link(q); if left←type(p)<>endpoint then {open up a cycle} begin r:=copy←knot(p); link(q):=r; q:=r; end; left←type(p):=open; right←type(q):=open; end @ A pair of numeric values is changed into a knot node for a one-point path when \MF\ discovers that the pair is part of a path. @p@t\4@>@<Declare the procedure called |known←pair|@>@; function new←knot:pointer; {convert a pair to a knot with two endpoints} var @!q:pointer; {the new node} begin q:=get←node(knot←node←size); left←type(q):=endpoint; right←type(q):=endpoint; link(q):=q;@/ known←pair; x←coord(q):=cur←x; y←coord(q):=cur←y; new←knot:=q; end; @ The |known←pair| subroutine sets |cur←x| and |cur←y| to the components of the current expression, assuming that the current expression is a pair of known numerics. Unknown components are zeroed, and the current expression is flushed. @<Declare the procedure called |known←pair|@>= procedure known←pair; var @!p:pointer; {the pair node} begin if cur←type<>pair←type then begin exp←err("Undefined coordinates have been replaced by (0,0)"); @.Undefined coordinates...@> help5("I need x and y numbers for this part of the path.")@/ ("The value I found (see above) was no good;")@/ ("so I'll try to keep going by using zero instead.")@/ ("(Chapter 27 of The METAFONTbook explains that")@/ @:METAFONTbook}{\sl The {\logos METAFONT\/}book@> ("you might want to type `I ???' now.)"); put←get←flush←error(0); cur←x:=0; cur←y:=0; end else begin p:=value(cur←exp); @<Make sure that both |x| and |y| parts of |p| are known; copy them into |cur←x| and |cur←y|@>; flush←cur←exp(0); end; end; @ @<Make sure that both |x| and |y| parts of |p| are known...@>= if type(x←part←loc(p))=known then cur←x:=value(x←part←loc(p)) else begin disp←err(x←part←loc(p), "Undefined x coordinate has been replaced by 0"); @.Undefined coordinates...@> help5("I need a `known' x value for this part of the path.")@/ ("The value I found (see above) was no good;")@/ ("so I'll try to keep going by using zero instead.")@/ ("(Chapter 27 of The METAFONTbook explains that")@/ @:METAFONTbook}{\sl The {\logos METAFONT\/}book@> ("you might want to type `I ???' now.)"); put←get←error; recycle←value(x←part←loc(p)); cur←x:=0; end; if type(y←part←loc(p))=known then cur←y:=value(y←part←loc(p)) else begin disp←err(y←part←loc(p), "Undefined y coordinate has been replaced by 0"); help5("I need a `known' y value for this part of the path.")@/ ("The value I found (see above) was no good;")@/ ("so I'll try to keep going by using zero instead.")@/ ("(Chapter 27 of The METAFONTbook explains that")@/ ("you might want to type `I ???' now.)"); put←get←error; recycle←value(y←part←loc(p)); cur←y:=0; end @ At this point |cur←cmd| is either |ampersand|, |left←brace|, or |path←join|. @<Determine the path join parameters...@>= if cur←cmd=left←brace then @<Put the pre-join direction information into node |q|@>; d:=cur←cmd; if d=path←join then @<Determine the tension and/or control points@> else if d<>ampersand then goto finish←path; get←x←next; if cur←cmd=left←brace then @<Put the post-join direction information into |x| and |t|@> else if right←type(q)<>explicit then begin t:=open; x:=0; end @ The |scan←direction| subroutine looks at the directional information that is enclosed in braces, and also scans ahead to the following character. A type code is returned, either |open| (if the direction was $(0,0)$), or |curl| (if the direction was a curl of known value |cur←exp|), or |given| (if the direction is given by the |angle| value that now appears in |cur←exp|). There's nothing difficult about this subroutine, but the program is rather lengthy because a variety of potential errors need to be nipped in the bud. @p function scan←direction:small←number; var @!t:given..open; {the type of information found} @!x:scaled; {an |x| coordinate} begin get←x←next; if cur←cmd=curl←command then @<Scan a curl specification@> else @<Scan a given direction@>; if cur←cmd<>right←brace then begin missing←err("}");@/ @.Missing `\char`\}'@> help3("I've scanned a direction spec for part of a path,")@/ ("so a right brace should have come next.")@/ ("I shall pretend that one was there.");@/ back←error; end; get←x←next; scan←direction:=t; end; @ @<Scan a curl specification@>= begin get←x←next; scan←expression; if (cur←type<>known)or(cur←exp<0) then begin exp←err("Improper curl has been replaced by 1"); @.Improper curl@> help1("A curl must be a known, nonnegative number."); put←get←flush←error(unity); end; t:=curl; end @ @<Scan a given direction@>= begin scan←expression; if cur←type>pair←type then @<Get given directions separated by commas@> else known←pair; if (cur←x=0)and(cur←y=0) then t:=open else begin t:=given; cur←exp:=n←arg(cur←x,cur←y); end; end @ @<Get given directions separated by commas@>= begin if cur←type<>known then begin exp←err("Undefined x coordinate has been replaced by 0"); @.Undefined coordinates...@> help5("I need a `known' x value for this part of the path.")@/ ("The value I found (see above) was no good;")@/ ("so I'll try to keep going by using zero instead.")@/ ("(Chapter 27 of The METAFONTbook explains that")@/ @:METAFONTbook}{\sl The {\logos METAFONT\/}book@> ("you might want to type `I ???' now.)"); put←get←flush←error(0); end; x:=cur←exp; if cur←cmd<>comma then begin missing←err(",");@/ @.Missing `,'@> help2("I've got the x coordinate of a path direction;")@/ ("will look for the y coordinate next."); back←error; end; get←x←next; scan←expression; if cur←type<>known then begin exp←err("Undefined y coordinate has been replaced by 0"); help5("I need a `known' y value for this part of the path.")@/ ("The value I found (see above) was no good;")@/ ("so I'll try to keep going by using zero instead.")@/ ("(Chapter 27 of The METAFONTbook explains that")@/ ("you might want to type `I ???' now.)"); put←get←flush←error(0); end; cur←y:=cur←exp; cur←x:=x; end @ At this point |right←type(q)| is usually |open|, but it may have been set to some other value by a previous splicing operation. We must maintain the value of |right←type(q)| in unusual cases such as `\.{..z1\{z2\}\&\{z3\}z1\{0,0\}..}'. @<Put the pre-join...@>= begin t:=scan←direction; if t<>open then begin right←type(q):=t; right←given(q):=cur←exp; if left←type(q)=open then begin left←type(q):=t; left←given(q):=cur←exp; end; {note that |left←given(q)=left←curl(q)|} end; end @ Since |left←tension| and |left←y| share the same position in knot nodes, and since |left←given| is similarly equivalent to |left←x|, we use |x| and |y| to hold the given direction and tension information when there are no explicit control points. @<Put the post-join...@>= begin t:=scan←direction; if right←type(q)<>explicit then x:=cur←exp else t:=explicit; {the direction information is superfluous} end @ @<Determine the tension and/or...@>= begin get←x←next; if cur←cmd=tension then @<Set explicit tensions@> else if cur←cmd=controls then @<Set explicit control points@> else begin right←tension(q):=unity; y:=unity; back←input; {default tension} goto done; end; if cur←cmd<>path←join then begin missing←err("..");@/ @.Missing `..'@> help1("A path join command should end with two dots."); back←error; end; done:end @ @<Set explicit tensions@>= begin get←x←next; y:=cur←cmd; if cur←cmd=at←least then get←x←next; scan←primary; @<Make sure that the current expression is a valid tension setting@>; if y=at←least then negate(cur←exp); right←tension(q):=cur←exp; if cur←cmd=and←command then begin get←x←next; y:=cur←cmd; if cur←cmd=at←least then get←x←next; scan←primary; @<Make sure that the current expression is a valid tension setting@>; if y=at←least then negate(cur←exp); end; y:=cur←exp; end @ @d min←tension==three←quarter←unit @<Make sure that the current expression is a valid tension setting@>= if (cur←type<>known)or(cur←exp<min←tension) then begin exp←err("Improper tension has been set to 1"); @.Improper tension@> help1("The expression above should have been a number > 3/4."); put←get←flush←error(unity); end @ @<Set explicit control points@>= begin right←type(q):=explicit; t:=explicit; get←x←next; scan←primary;@/ known←pair; right←x(q):=cur←x; right←y(q):=cur←y; if cur←cmd<>and←command then begin x:=right←x(q); y:=right←y(q); end else begin get←x←next; scan←primary;@/ known←pair; x:=cur←x; y:=cur←y; end; end @ @<Convert the right operand, |cur←exp|, into a partial path...@>= begin if cur←type<>path←type then pp:=new←knot else pp:=cur←exp; qq:=pp; while link(qq)<>pp do qq:=link(qq); if left←type(pp)<>endpoint then {open up a cycle} begin r:=copy←knot(pp); link(qq):=r; qq:=r; end; left←type(pp):=open; right←type(qq):=open; end @ If a person tries to define an entire path by saying `\.{(x,y)\&cycle}', we silently change the specification to `\.{(x,y)..cycle}', since a cycle shouldn't have length zero. @<Get ready to close a cycle@>= begin cycle←hit:=true; get←x←next; pp:=p; qq:=p; if d=ampersand then if p=q then begin d:=path←join; right←tension(q):=unity; y:=unity; end; end @ @<Join the partial paths and reset |p| and |q|...@>= begin if d=ampersand then if (x←coord(q)<>x←coord(pp))or(y←coord(q)<>y←coord(pp)) then begin print←err("Paths don't touch; `&' will be changed to `..'"); @.Paths don't touch@> help3("When you join paths `p&q', the ending point of p")@/ ("must be exactly equal to the starting point of q.")@/ ("So I'm going to pretend that you said `p..q' instead."); put←get←error; d:=path←join; right←tension(q):=unity; y:=unity; end; @<Plug an opening in |right←type(pp)|, if possible@>; if d=ampersand then @<Splice independent paths together@> else begin @<Plug an opening in |right←type(q)|, if possible@>; link(q):=pp; left←y(pp):=y; if t<>open then begin left←x(pp):=x; left←type(pp):=t; end; end; q:=qq; end @ @<Plug an opening in |right←type(q)|...@>= if right←type(q)=open then if (left←type(q)=curl)or(left←type(q)=given) then begin right←type(q):=left←type(q); right←given(q):=left←given(q); end @ @<Plug an opening in |right←type(pp)|...@>= if right←type(pp)=open then if (t=curl)or(t=given) then begin right←type(pp):=t; right←given(pp):=x; end @ @<Splice independent paths together@>= begin if left←type(q)=open then if right←type(q)=open then begin left←type(q):=curl; left←curl(q):=unity; end; if right←type(pp)=open then if t=open then begin right←type(pp):=curl; right←curl(pp):=unity; end; right←type(q):=right←type(pp); link(q):=link(pp);@/ right←x(q):=right←x(pp); right←y(q):=right←y(pp); free←node(pp,knot←node←size); if qq=pp then qq:=q; end @ @<Choose control points for the path...@>= if cycle←hit then begin if d=ampersand then p:=q; end else begin left←type(p):=endpoint; if right←type(p)=open then begin right←type(p):=curl; right←curl(p):=unity; end; right←type(q):=endpoint; if left←type(q)=open then begin left←type(q):=curl; left←curl(q):=unity; end; link(q):=p; end; make←choices(p); cur←type:=path←type; cur←exp:=p @ Finally, we sometimes need to scan an expression whose value is supposed to be either |true←code| or |false←code|. @<Declare the basic parsing subroutines@>= procedure get←boolean; begin get←x←next; scan←expression; if cur←type<>boolean←type then begin exp←err("Undefined condition will be treated as `false'"); @.Undefined condition...@> help2("The expression shown above should have had a definite")@/ ("true-or-false value. I'm changing it to `false'.");@/ put←get←flush←error(false←code); cur←type:=boolean←type; end; end; @* \[42] Doing the operations. The purpose of parsing is primarily to permit people to avoid piles of parentheses. But the real work is done after the structure of an expression has been recognized; that's when new expressions are generated. We turn now to the guts of \MF, which handles individual operators that have come through the parsing mechanism. We'll start with the easy ones that take no operands, then work our way up to operators with one and ultimately two arguments. In other words, we will write the three procedures |do←nullary|, |do←unary|, and |do←binary| that are invoked periodically by the expression scanners. First let's make sure that all of the primitive operators are in the hash table. Although |scan←primary| and its relatives made use of the \\{cmd} code for these operators, the \\{do} routines base everything on the \\{mod} code. For example, |do←binary| doesn't care whether the operation it performs is a |primary←binary| or |secondary←binary|, etc. @<Put each...@>= primitive("true",nullary,true←code);@/ @!@:true←}{\&{true} primitive@> primitive("false",nullary,false←code);@/ @!@:false←}{\&{false} primitive@> primitive("nullpicture",nullary,null←picture←code);@/ @!@:null←picture←}{\&{nullpicture} primitive@> primitive("nullpen",nullary,null←pen←code);@/ @!@:null←pen←}{\&{nullpen} primitive@> primitive("jobname",nullary,job←name←op);@/ @!@:job←name←}{\&{jobname} primitive@> primitive("readstring",nullary,read←string←op);@/ @!@:read←string←}{\&{readstring} primitive@> primitive("pencircle",nullary,pen←circle);@/ @!@:pen←circle←}{\&{pencircle} primitive@> primitive("normaldeviate",nullary,normal←deviate);@/ @!@:normal←deviate←}{\&{normaldeviate} primitive@> primitive("odd",unary,odd←op);@/ @!@:odd←}{\&{odd} primitive@> primitive("known",unary,known←op);@/ @!@:known←}{\&{known} primitive@> primitive("unknown",unary,unknown←op);@/ @!@:unknown←}{\&{unknown} primitive@> primitive("not",unary,not←op);@/ @!@:not←}{\&{not} primitive@> primitive("decimal",unary,decimal);@/ @!@:decimal←}{\&{decimal} primitive@> primitive("reverse",unary,reverse);@/ @!@:reverse←}{\&{reverse} primitive@> primitive("makepath",unary,make←path←op);@/ @!@:make←path←}{\&{makepath} primitive@> primitive("makepen",unary,make←pen←op);@/ @!@:make←pen←}{\&{makepen} primitive@> primitive("totalweight",unary,total←weight←op);@/ @!@:total←weight←}{\&{totalweight} primitive@> primitive("oct",unary,oct←op);@/ @!@:oct←}{\&{oct} primitive@> primitive("hex",unary,hex←op);@/ @!@:hex←}{\&{hex} primitive@> primitive("ASCII",unary,ASCII←op);@/ @!@:ASCII←}{\&{ASCII} primitive@> primitive("char",unary,char←op);@/ @!@:char←}{\&{char} primitive@> primitive("length",unary,length←op);@/ @!@:length←}{\&{length} primitive@> primitive("turningnumber",unary,turning←op);@/ @!@:turning←number←}{\&{turningnumber} primitive@> primitive("xpart",unary,x←part);@/ @!@:x←part←}{\&{xpart} primitive@> primitive("ypart",unary,y←part);@/ @!@:y←part←}{\&{ypart} primitive@> primitive("xxpart",unary,xx←part);@/ @!@:xx←part←}{\&{xxpart} primitive@> primitive("xypart",unary,xy←part);@/ @!@:xy←part←}{\&{xypart} primitive@> primitive("yxpart",unary,yx←part);@/ @!@:yx←part←}{\&{yxpart} primitive@> primitive("yypart",unary,yy←part);@/ @!@:yy←part←}{\&{yypart} primitive@> primitive("sqrt",unary,sqrt←op);@/ @!@:sqrt←}{\&{sqrt} primitive@> primitive("mexp",unary,m←exp←op);@/ @!@:m←exp←}{\&{mexp} primitive@> primitive("mlog",unary,m←log←op);@/ @!@:m←log←}{\&{mlog} primitive@> primitive("sind",unary,sin←d←op);@/ @!@:sin←d←}{\&{sind} primitive@> primitive("cosd",unary,cos←d←op);@/ @!@:cos←d←}{\&{cosd} primitive@> primitive("floor",unary,floor←op);@/ @!@:floor←}{\&{floor} primitive@> primitive("uniformdeviate",unary,uniform←deviate);@/ @!@:uniform←deviate←}{\&{uniformdeviate} primitive@> primitive("charexists",unary,char←exists←op);@/ @!@:char←exists←}{\&{charexists} primitive@> primitive("angle",unary,angle←op);@/ @!@:angle←}{\&{angle} primitive@> primitive("cycle",cycle,cycle←op);@/ @!@:cycle←}{\&{cycle} primitive@> primitive("+",plus←or←minus,plus);@/ @!@:+ }{\.{+} primitive@> primitive("-",plus←or←minus,minus);@/ @!@:- }{\.{-} primitive@> primitive("*",secondary←binary,times);@/ @!@:* }{\.{*} primitive@> primitive("/",slash,over); eqtb[frozen←slash]:=eqtb[cur←sym];@/ @!@:/ }{\.{/} primitive@> primitive("++",tertiary←binary,pythag←add);@/ @!@:++←}{\.{++} primitive@> primitive("+-+",tertiary←binary,pythag←sub);@/ @!@:+-+←}{\.{+-+} primitive@> primitive("and",and←command,and←op);@/ @!@:and←}{\&{and} primitive@> primitive("or",tertiary←binary,or←op);@/ @!@:or←}{\&{or} primitive@> primitive("<",expression←binary,less←than);@/ @!@:< }{\.{<} primitive@> primitive("<=",expression←binary,less←or←equal);@/ @!@:<=←}{\.{<=} primitive@> primitive(">",expression←binary,greater←than);@/ @!@:> }{\.{>} primitive@> primitive(">=",expression←binary,greater←or←equal);@/ @!@:>=←}{\.{>=} primitive@> primitive("=",equals,equal←to);@/ @!@:= }{\.{=} primitive@> primitive("<>",expression←binary,unequal←to);@/ @!@:<>←}{\.{<>} primitive@> primitive("substring",primary←binary,substring←of);@/ @!@:substring←}{\&{substring} primitive@> primitive("subpath",primary←binary,subpath←of);@/ @!@:subpath←}{\&{subpath} primitive@> primitive("directiontime",primary←binary,direction←time←of);@/ @!@:direction←time←}{\&{directiontime} primitive@> primitive("point",primary←binary,point←of);@/ @!@:point←}{\&{point} primitive@> primitive("precontrol",primary←binary,precontrol←of);@/ @!@:precontrol←}{\&{precontrol} primitive@> primitive("postcontrol",primary←binary,postcontrol←of);@/ @!@:postcontrol←}{\&{postcontrol} primitive@> primitive("penoffset",primary←binary,pen←offset←of);@/ @!@:pen←offset←}{\&{penoffset} primitive@> primitive("&",ampersand,concatenate);@/ @!@:!!!}{\.{\&} primitive@> primitive("rotated",secondary←binary,rotated←by);@/ @!@:rotated←}{\&{rotated} primitive@> primitive("slanted",secondary←binary,slanted←by);@/ @!@:slanted←}{\&{slanted} primitive@> primitive("scaled",secondary←binary,scaled←by);@/ @!@:scaled←}{\&{scaled} primitive@> primitive("shifted",secondary←binary,shifted←by);@/ @!@:shifted←}{\&{shifted} primitive@> primitive("transformed",secondary←binary,transformed←by);@/ @!@:transformed←}{\&{transformed} primitive@> primitive("xscaled",secondary←binary,x←scaled);@/ @!@:x←scaled←}{\&{xscaled} primitive@> primitive("yscaled",secondary←binary,y←scaled);@/ @!@:y←scaled←}{\&{yscaled} primitive@> primitive("zscaled",secondary←binary,z←scaled);@/ @!@:z←scaled←}{\&{zscaled} primitive@> primitive("intersectiontimes",tertiary←binary,intersect);@/ @!@:intersection←times←}{\&{intersectiontimes} primitive@> @ @<Cases of |print←cmd...@>= nullary,unary,primary←binary,secondary←binary,tertiary←binary, expression←binary,cycle,plus←or←minus,slash,ampersand,equals,and←command: print←op(m); @ OK, let's look at the simplest \\{do} procedure first. @p procedure do←nullary(@!c:quarterword); var @!k:integer; {all-purpose loop index} begin check←arith; if internal[tracing←commands]>two then show←cmd←mod(nullary,c); case c of true←code,false←code:begin cur←type:=boolean←type; cur←exp:=c; end; null←picture←code:begin cur←type:=picture←type; cur←exp:=get←node(edge←header←size); init←edges(cur←exp); end; null←pen←code:begin cur←type:=pen←type; cur←exp:=null←pen; end; normal←deviate:begin cur←type:=known; cur←exp:=norm←rand; end; pen←circle:@<Make a special knot node for \&{pencircle}@>; job←name←op: begin if job←name=0 then open←log←file; cur←type:=string←type; cur←exp:=job←name; end; read←string←op:@<Read a string from the terminal@>; end; {there are no other cases} check←arith; end; @ @<Make a special knot node for \&{pencircle}@>= begin cur←type:=future←pen; cur←exp:=get←node(knot←node←size); left←type(cur←exp):=open; right←type(cur←exp):=open; link(cur←exp):=cur←exp;@/ x←coord(cur←exp):=0; y←coord(cur←exp):=0;@/ left←x(cur←exp):=unity; left←y(cur←exp):=0;@/ right←x(cur←exp):=0; right←y(cur←exp):=unity;@/ end @ @<Read a string...@>= begin if interaction<=nonstop←mode then fatal←error("*** (cannot readstring in nonstop modes)"); begin←file←reading; name:=1; prompt←input(""); str←room(last-start); for k:=start to last-1 do append←char(buffer[k]); end←file←reading; cur←type:=string←type; cur←exp:=make←string; end @ Things get a bit more interesting when there's an operand. The operand to |do←unary| appears in |cur←type| and |cur←exp|. @p @t\4@>@<Declare unary action procedures@>@; procedure do←unary(@!c:quarterword); var @!p,@!q:pointer; {for list manipulation} @!x:integer; {a temporary register} begin check←arith; if internal[tracing←commands]>two then @<Trace the current unary operation@>; case c of plus:if cur←type<pair←type then if cur←type<>picture←type then bad←unary(plus); minus:@<Negate the current expression@>; @t\4@>@<Additional cases of unary operators@>@; end; {there are no other cases} check←arith; end; @ The |nice←pair| function returns |true| if both components of a pair are known. @<Declare unary action procedures@>= function nice←pair(@!p:integer;@!t:quarterword):boolean; label exit; begin if t=pair←type then begin p:=value(p); if type(x←part←loc(p))=known then if type(y←part←loc(p))=known then begin nice←pair:=true; return; end; end; nice←pair:=false; exit:end; @ @<Declare unary action...@>= procedure print←known←or←unknown←type(@!t:small←number;@!v:integer); begin print←char("("); if t<dependent then if t<>pair←type then print←type(t) else if nice←pair(v,pair←type) then print("pair") else print("unknown pair") else print("unknown numeric"); print←char(")"); end; @ @<Declare unary action...@>= procedure bad←unary(@!c:quarterword); begin exp←err("Not implemented: "); print←op(c); @.Not implemented...@> print←known←or←unknown←type(cur←type,cur←exp); help3("I'm afraid I don't know how to apply that operation to that")@/ ("particular type. Continue, and I'll simply return the")@/ ("argument (shown above) as the result of the operation."); put←get←error; end; @ @<Trace the current unary operation@>= begin begin←diagnostic; print←nl("{"); print←op(c); print←char("(");@/ print←exp(null,0); {show the operand, but not verbosely} print(")}"); end←diagnostic(false); end @ Negation is easy except when the current expression is of type |independent|, or when it is a pair with one or more |independent| components. It is tempting to argue that the negative of an independent variable is an independent variable, hence we don't have to do anything when negating it. The fallacy is that other dependent variables pointing to the current expression must change the sign of their coefficients if we make no change to the current expression. Instead, we work around the problem by copying the current expression and recycling it afterwards (cf.~the |stash←in| routine). @<Negate the current expression@>= case cur←type of pair←type,independent: begin q:=cur←exp; make←exp←copy(q); if cur←type=dependent then negate←dep←list(dep←list(cur←exp)) else if cur←type=pair←type then begin p:=value(cur←exp); if type(x←part←loc(p))=known then negate(value(x←part←loc(p))) else negate←dep←list(dep←list(x←part←loc(p))); if type(y←part←loc(p))=known then negate(value(y←part←loc(p))) else negate←dep←list(dep←list(y←part←loc(p))); end; {if |cur←type=known| then |cur←exp=0|} recycle←value(q); free←node(q,value←node←size); end; dependent,proto←dependent:negate←dep←list(dep←list(cur←exp)); known:negate(cur←exp); picture←type:negate←edges(cur←exp); othercases bad←unary(minus) endcases @ @<Declare unary action...@>= procedure negate←dep←list(@!p:pointer); label exit; begin loop@+begin negate(value(p)); if info(p)=null then return; p:=link(p); end; exit:end; @ @<Additional cases of unary operators@>= not←op: if cur←type<>boolean←type then bad←unary(not←op) else cur←exp:=true←code+false←code-cur←exp; @ @d three←sixty←units==23592960 {that's |360*unity|} @d boolean←reset(#)==if # then cur←exp:=true←code@+else cur←exp:=false←code @<Additional cases of unary operators@>= sqrt←op,m←exp←op,m←log←op,sin←d←op,cos←d←op,floor←op, uniform←deviate,odd←op,char←exists←op:@t@>@;@/ if cur←type<>known then bad←unary(c) else case c of sqrt←op:cur←exp:=square←rt(cur←exp); m←exp←op:cur←exp:=m←exp(cur←exp); m←log←op:cur←exp:=m←log(cur←exp); sin←d←op,cos←d←op:begin n←sin←cos((cur←exp mod three←sixty←units)*16); if c=sin←d←op then cur←exp:=round←fraction(n←sin) else cur←exp:=round←fraction(n←cos); end; floor←op:cur←exp:=floor←scaled(cur←exp); uniform←deviate:cur←exp:=unif←rand(cur←exp); odd←op: begin boolean←reset(odd(round←unscaled(cur←exp))); cur←type:=boolean←type; end; char←exists←op:@<Determine if a character has been shipped out@>; end; {there are no other cases} @ @<Additional cases of unary operators@>= angle←op:if nice←pair(cur←exp,cur←type) then begin p:=value(cur←exp); x:=n←arg(value(x←part←loc(p)),value(y←part←loc(p))); if x>=0 then flush←cur←exp((x+8)div 16) else flush←cur←exp(-((-x+8)div 16)); end else bad←unary(angle←op); @ If the current expression is a pair, but the context wants it to be a path, we call |pair←to←path|. @<Declare unary action...@>= procedure pair←to←path; begin cur←exp:=new←knot; cur←type:=path←type; end; @ @<Additional cases of unary operators@>= x←part,y←part:if (cur←type<=pair←type)and(cur←type>=transform←type) then take←part(c) else bad←unary(c); xx←part,xy←part,yx←part,yy←part: if cur←type=transform←type then take←part(c) else bad←unary(c); @ In the following procedure, |cur←exp| points to a capsule, which points to a big node. We want to delete all but one part of the big node. @<Declare unary action...@>= procedure take←part(@!c:quarterword); var @!p:pointer; {the big node} begin p:=value(cur←exp); value(temp←val):=p; type(temp←val):=cur←type; link(p):=temp←val; free←node(cur←exp,value←node←size); make←exp←copy(p+2*(c-x←part)); recycle←value(temp←val); end; @ @<Initialize table entries...@>= name←type(temp←val):=capsule; @ @<Additional cases of unary...@>= char←op: if cur←type<>known then bad←unary(char←op) else begin cur←exp:=round←unscaled(cur←exp) mod 128; cur←type:=string←type; if cur←exp<0 then cur←exp:=cur←exp+128; if length(cur←exp)<>1 then begin str←room(1); append←char(cur←exp); cur←exp:=make←string; end; end; decimal: if cur←type<>known then bad←unary(decimal) else begin old←setting:=selector; selector:=new←string; print←scaled(cur←exp); cur←exp:=make←string; selector:=old←setting; cur←type:=string←type; end; oct←op,hex←op,ASCII←op: if cur←type<>string←type then bad←unary(c) else str←to←num(c); @ @<Declare unary action...@>= procedure str←to←num(@!c:quarterword); {converts a string to a number} var @!n:integer; {accumulator} @!m:ASCII←code; {current character} @!k:pool←pointer; {index into |str←pool|} @!b:8..16; {radix of conversion} @!bad←char:boolean; {did the string contain an invalid digit?} begin if c=ASCII←op then if length(cur←exp)=0 then n:=-1 else n:=str←pool[str←start[cur←exp]] else begin if c=oct←op then b:=8@+else b:=16; n:=0; bad←char:=false; for k:=str←start[cur←exp] to str←start[cur←exp+1]-1 do begin m:=str←pool[k]; if (m>="0")and(m<="9") then m:=m-"0" else if (m>="A")and(m<="F") then m:=m-"A"+10 else if (m>="a")and(m<="f") then m:=m-"a"+10 else begin bad←char:=true; m:=0; end; if m>=b then begin bad←char:=true; m:=0; end; if n<32768 div b then n:=n*b+m@+else n:=32767; end; @<Give error messages if |bad←char| or |n>=4096|@>; end; flush←cur←exp(n*unity); end; @ @<Give error messages if |bad←char|...@>= if bad←char then begin exp←err("String contains illegal digits"); @.String contains illegal digits@> if c=oct←op then help1("I zeroed out characters that weren't in the range 0..7.") else help1("I zeroed out characters that weren't hex digits."); put←get←error; end; if n>4095 then begin print←err("Number too large ("); print←int(n); print←char(")"); @.Number too large@> help1("I have trouble with numbers greater than 4095; watch out."); put←get←error; end @ The length operation is somewhat unusual in that it applies to a variety of different types of operands. @<Additional cases of unary...@>= length←op: if cur←type=string←type then flush←cur←exp(length(cur←exp)*unity) else if cur←type=path←type then flush←cur←exp(path←length) else if cur←type=known then cur←exp:=abs(cur←exp) else if nice←pair(cur←exp,cur←type) then flush←cur←exp(pyth←add(value(x←part←loc(value(cur←exp))),@| value(y←part←loc(value(cur←exp))))) else bad←unary(c); @ @<Declare unary action...@>= function path←length:scaled; {computes the length of the current path} var @!n:scaled; {the path length so far} @!p:pointer; {traverser} begin p:=cur←exp; if left←type(p)=endpoint then n:=-unity@+else n:=0; repeat p:=link(p); n:=n+unity; until p=cur←exp; path←length:=n; end; @ The turning number is computed only with respect to null pens. A different pen might affect the turning number, in degenerate cases, because autorounding will produce a slightly different path, or because excessively large coordinates might be truncated. @<Additional cases of unary...@>= turning←op:if cur←type=pair←type then flush←cur←exp(0) else if cur←type<>path←type then bad←unary(turning←op) else if left←type(cur←exp)=endpoint then flush←cur←exp(0) {not a cyclic path} else begin cur←pen:=null←pen; cur←path←type:=contour←code; cur←exp:=make←spec(cur←exp, fraction←one-half←unit-1-el←gordo,0); flush←cur←exp(turning←number*unity); {convert to |scaled|} end; @ @d type←test←end== flush←cur←exp(true←code) else flush←cur←exp(false←code); cur←type:=boolean←type; end @d type←range←end(#)==(cur←type<=#) then type←test←end @d type←range(#)==begin if (cur←type>=#) and type←range←end @d type←test(#)==begin if cur←type=# then type←test←end @<Additional cases of unary operators@>= boolean←type: type←range(boolean←type)(unknown←boolean); string←type: type←range(string←type)(unknown←string); pen←type: type←range(pen←type)(future←pen); path←type: type←range(path←type)(unknown←path); picture←type: type←range(picture←type)(unknown←picture); transform←type,pair←type: type←test(c); numeric←type: type←range(known)(independent); known←op,unknown←op: test←known(c); @ @<Declare unary action procedures@>= procedure test←known(@!c:quarterword); label done; var @!b:true←code..false←code; {is the current expression known?} @!p,@!q:pointer; {locations in a big node} begin b:=false←code; case cur←type of vacuous,boolean←type,string←type,pen←type,future←pen,path←type,picture←type, known: b:=true←code; transform←type,pair←type:begin p:=value(cur←exp); q:=p+big←node←size[cur←type]; repeat q:=q-2; if type(q)<>known then goto done; until q=p; b:=true←code; done: end; othercases do←nothing endcases; if c=known←op then flush←cur←exp(b) else flush←cur←exp(true←code+false←code-b); cur←type:=boolean←type; end; @ @<Additional cases of unary operators@>= cycle←op: begin if cur←type<>path←type then flush←cur←exp(false←code) else if left←type(cur←exp)<>endpoint then flush←cur←exp(true←code) else flush←cur←exp(false←code); cur←type:=boolean←type; end; @ @<Additional cases of unary operators@>= make←pen←op: begin if cur←type=pair←type then pair←to←path; if cur←type=path←type then cur←type:=future←pen else bad←unary(make←pen←op); end; make←path←op: begin if cur←type=future←pen then materialize←pen; if cur←type<>pen←type then bad←unary(make←path←op) else begin flush←cur←exp(make←path(cur←exp)); cur←type:=path←type; end; end; total←weight←op: if cur←type<>picture←type then bad←unary(total←weight←op) else flush←cur←exp(total←weight(cur←exp)); reverse: if cur←type=path←type then begin p:=htap←ypoc(cur←exp); if right←type(p)=endpoint then p:=link(p); toss←knot←list(cur←exp); cur←exp:=p; end else if cur←type=pair←type then pair←to←path else bad←unary(reverse); @ Finally, we have the operations that combine a capsule~|p| with the current expression. @p @t\4@>@<Declare binary action procedures@>@; procedure do←binary(@!p:pointer;@!c:quarterword); label done,done1,exit; var @!q,@!r,@!rr:pointer; {for list manipulation} @!old←p,@!old←exp:pointer; {capsules to recycle} @!v:integer; {for numeric manipulation} begin check←arith; if internal[tracing←commands]>two then @<Trace the current binary operation@>; @<Sidestep |independent| cases@>; case c of plus,minus:@<Add or subtract the current expression from |p|@>; @t\4@>@<Additional cases of binary operators@>@; end; {there are no other cases} recycle←value(p); free←node(p,value←node←size); {|return| to avoid this} exit:check←arith; @<Recycle any sidestepped |independent| capsules@>; end; @ @<Declare binary action...@>= procedure bad←binary(@!p:pointer;@!c:quarterword); begin disp←err(p,""); exp←err("Not implemented: "); @.Not implemented...@> if c>=min←of then print←op(c); print←known←or←unknown←type(type(p),p); if c>=min←of then print("of")@+else print←op(c); print←known←or←unknown←type(cur←type,cur←exp);@/ help3("I'm afraid I don't know how to apply that operation to that")@/ ("combination of types. Continue, and I'll return the second")@/ ("argument (see above) as the result of the operation."); put←get←error; end; @ @<Trace the current binary operation@>= begin begin←diagnostic; print←nl("{("); print←exp(p,0); {show the operand, but not verbosely} print←char(")"); print←op(c); print←char("(");@/ print←exp(null,0); print(")}"); end←diagnostic(false); end @ Several of the binary operations are potentially complicated by the fact that |independent| values can sneak into capsules. For example, we've seen an instance of this difficulty in the unary operation of negation. In order to reduce the number of cases that need to be handled, we first change the two operands (if necessary) to rid them of |independent| components. The original operands are put into capsules called |old←p| and |old←exp|, which will be recycled after the binary operation has been safely carried out. @<Recycle any sidestepped |independent| capsules@>= if old←p<>null then begin recycle←value(old←p); free←node(old←p,value←node←size); end; if old←exp<>null then begin recycle←value(old←exp); free←node(old←exp,value←node←size); end @ @<Sidestep |independent| cases@>= case type(p) of transform←type,pair←type: old←p:=tarnished(p); independent: old←p:=void; othercases old←p:=null endcases; if old←p<>null then begin q:=stash←cur←exp; old←p:=p; make←exp←copy(old←p); p:=stash←cur←exp; unstash←cur←exp(q); end; case cur←type of transform←type,pair←type:old←exp:=tarnished(cur←exp); independent:old←exp:=void; othercases old←exp:=null endcases; if old←exp<>null then begin old←exp:=cur←exp; make←exp←copy(old←exp); end @ A big node is considered to be ``tarnished'' if it contains at least one independent component. Here's a simple subroutine that returns |null| if and only if its argument is not tarnished. @ @<Declare binary action...@>= function tarnished(@!p:pointer):pointer; label exit; var @!q:pointer; {beginning of the big node} @!r:pointer; {current position in the big node} begin q:=value(p); r:=q+big←node←size[type(p)]; repeat r:=r-2; if type(r)=independent then begin tarnished:=void; return; end; until r=q; tarnished:=null; exit:end; @ @<Add or subtract the current expression from |p|@>= if (cur←type<pair←type)or(type(p)<pair←type) then if (cur←type=picture←type)and(type(p)=picture←type) then begin if c=minus then negate←edges(cur←exp); cur←edges:=cur←exp; merge←edges(value(p)); end else bad←binary(p,c) else if cur←type=pair←type then if type(p)<>pair←type then bad←binary(p,c) else begin q:=value(p); r:=value(cur←exp); add←or←subtract(x←part←loc(q),x←part←loc(r),c); add←or←subtract(y←part←loc(q),y←part←loc(r),c); end else if type(p)=pair←type then bad←binary(p,c) else add←or←subtract(p,null,c) @ The first argument to |add←or←subtract| is the location of a value node in a capsule or pair node that will soon be recycled. The second argument is either a location within a pair or transform node of |cur←exp|, or it is null (which means that |cur←exp| itself should be the second argument). The third argument is either |plus| or |minus|. The sum or difference of the numeric quantities will replace the second operand. Arithmetic overflow may go undetected; users aren't supposed to be monkeying around with really big values. @<Declare binary action...@>= @t\4@>@<Declare the procedure called |dep←finish|@>@; procedure add←or←subtract(@!p,@!q:pointer;@!c:quarterword); label done,exit; var @!s,@!t:small←number; {operand types} @!r:pointer; {list traverser} @!v:integer; {second operand value} begin if q=null then begin t:=cur←type; if t<dependent then v:=cur←exp@+else v:=dep←list(cur←exp); end else begin t:=type(q); if t<dependent then v:=value(q)@+else v:=dep←list(q); end; if t=known then begin if c=minus then negate(v); if type(p)=known then begin v:=slow←add(value(p),v); if q=null then cur←exp:=v@+else value(q):=v; return; end; @<Add a known value to the constant term of |dep←list(p)|@>; end else begin if c=minus then negate←dep←list(v); @<Add operand |p| to the dependency list |v|@>; end; exit:end; @ @<Add a known value to the constant term of |dep←list(p)|@>= r:=dep←list(p); while info(r)<>null do r:=link(r); value(r):=slow←add(value(r),v); if q=null then begin q:=get←node(value←node←size); cur←exp:=q; cur←type:=type(p); name←type(q):=capsule; end; dep←list(q):=dep←list(p); type(q):=type(p); prev←dep(q):=prev←dep(p); link(prev←dep(p)):=q; type(p):=known; {this will keep the recycler from collecting non-garbage} @ We prefer |dependent| lists to |proto←dependent| ones, because it is nice to retain the extra accuracy of |fraction| coefficients. But we have to handle both kinds, and mixtures too. @<Add operand |p| to the dependency list |v|@>= if type(p)=known then @<Add the known |value(p)| to the constant term of |v|@> else begin s:=type(p); r:=dep←list(p); if t=dependent then begin if s=dependent then if max←coef(r)+max←coef(v)<coef←bound then begin v:=p←plus←q(v,r,dependent); goto done; end; {|fix←needed| will necessarily be false} t:=proto←dependent; v:=p←over←v(v,unity,dependent,proto←dependent); end; if s=proto←dependent then v:=p←plus←q(v,r,proto←dependent) else v:=p←plus←fq(v,unity,r,proto←dependent,dependent); done: @<Output the answer, |v| (which might have become |known|)@>; end @ @<Add the known |value(p)| to the constant term of |v|@>= begin while info(v)<>null do v:=link(v); value(v):=slow←add(value(p),value(v)); end @ @<Output the answer, |v| (which might have become |known|)@>= if q<>null then dep←finish(v,q,t) else begin cur←type:=t; dep←finish(v,null,t); end @ Here's the current situation: The dependency list |v| of type |t| should either be put into the current expression (if |q=null|) or into location |q| within a pair node (otherwise). The destination (|cur←exp| or |q|) formerly held a dependency list with the same final pointer as the list |v|. @<Declare the procedure called |dep←finish|@>= procedure dep←finish(@!v,@!q:pointer;@!t:small←number); var @!p:pointer; {the destination} @!vv:scaled; {the value, if it is |known|} begin if q=null then p:=cur←exp@+else p:=q; dep←list(p):=v; type(p):=t; if info(v)=null then begin vv:=value(v); if q=null then flush←cur←exp(vv) else begin recycle←value(p); type(q):=known; value(q):=vv; end; end else if q=null then cur←type:=t; if fix←needed then fix←dependencies; end; @ Let's turn now to the six basic relations of comparison. @<Additional cases of binary operators@>= less←than,less←or←equal,greater←than,greater←or←equal,equal←to,unequal←to: begin@t@>@; if (cur←type>pair←type)and(type(p)>pair←type) then add←or←subtract(p,null,minus) {|cur←exp:=(p)-cur←exp|} else if cur←type<>type(p) then begin bad←binary(p,c); goto done; end else if cur←type=string←type then flush←cur←exp(str←vs←str(value(p),cur←exp)) else if (cur←type=unknown←string)or(cur←type=unknown←boolean) then @<Check if unknowns have been equated@> else if (cur←type=pair←type)or(cur←type=transform←type) then @<Reduce comparison of big nodes to comparison of scalars@> else if cur←type=boolean←type then flush←cur←exp(cur←exp-value(p)) else begin bad←binary(p,c); goto done; end; @<Compare the current expression with zero@>; done: end; @ @<Compare the current expression with zero@>= if cur←type<>known then begin if cur←type<known then begin disp←err(p,""); help1("The quantities shown above have not been equated.")@/ end else help2("Oh dear. I can't decide if the expression above is positive,")@/ ("negative, or zero. So this comparison test won't be `true'."); exp←err("Unknown relation will be considered false"); @.Unknown relation...@> put←get←flush←error(false←code); end else case c of less←than: boolean←reset(cur←exp<0); less←or←equal: boolean←reset(cur←exp<=0); greater←than: boolean←reset(cur←exp>0); greater←or←equal: boolean←reset(cur←exp>=0); equal←to: boolean←reset(cur←exp=0); unequal←to: boolean←reset(cur←exp<>0); end; {there are no other cases} cur←type:=boolean←type @ When two unknown strings are in the same ring, we know that they are equal. Otherwise, we don't know whether they are equal or not, so we make no change. @<Check if unknowns have been equated@>= begin q:=value(cur←exp); while (q<>cur←exp)and(q<>p) do q:=value(q); if q=p then flush←cur←exp(0); end @ @<Reduce comparison of big nodes to comparison of scalars@>= begin q:=value(p); r:=value(cur←exp); rr:=r+big←node←size[cur←type]-2; loop@+ begin add←or←subtract(q,r,minus); if type(r)<>known then goto done1; if value(r)<>0 then goto done1; if r=rr then goto done1; q:=q+2; r:=r+2; end; done1:take←part(x←part+half(r-value(cur←exp))); end @ Here we use the sneaky fact that |and←op-false←code=or←op-true←code|. @<Additional cases of binary operators@>= and←op,or←op: if (type(p)<>boolean←type)or(cur←type<>boolean←type) then bad←binary(p,c) else if value(p)=c+false←code-and←op then cur←exp:=value(p); @ @<Additional cases of binary operators@>= times: if (cur←type<pair←type)or(type(p)<pair←type) then bad←binary(p,times) else if (cur←type=known)or(type(p)=known) then @<Multiply when at least one operand is known@> else if (nice←pair(p,type(p))and(cur←type>pair←type)) or(nice←pair(cur←exp,cur←type)and(type(p)>pair←type)) then begin hard←times(p); return; end else bad←binary(p,times); @ @<Multiply when at least one operand is known@>= begin if type(p)=known then begin v:=value(p); free←node(p,value←node←size); end else begin v:=cur←exp; unstash←cur←exp(p); end; if cur←type=known then cur←exp:=take←scaled(cur←exp,v) else if cur←type=pair←type then begin p:=value(cur←exp); dep←mult(x←part←loc(p),v,true); dep←mult(y←part←loc(p),v,true); end else dep←mult(null,v,true); return; end @ @<Declare binary action...@>= procedure dep←mult(@!p:pointer;@!v:integer;@!v←is←scaled:boolean); label exit; var @!q:pointer; {the dependency list being multiplied by |v|} @!s,@!t:small←number; {its type, before and after} begin if p=null then q:=cur←exp else if type(p)<>known then q:=p else begin if v←is←scaled then value(p):=take←scaled(value(p),v) else value(p):=take←fraction(value(p),v); return; end; t:=type(q); q:=dep←list(q); s:=t; if t=dependent then if v←is←scaled then if ab←vs←cd(max←coef(q),abs(v),coef←bound-1,unity)>=0 then t:=proto←dependent; q:=p←times←v(q,v,s,t,v←is←scaled); dep←finish(q,p,t); exit:end; @ Here is a routine that is similar to |times|; but it is invoked only internally, when |v| is a |fraction| whose magnitude is at most~1, and when |cur←type>=pair←type|. @p procedure frac←mult(@!n,@!d:scaled); {multiplies |cur←exp| by |n/d|} var @!p:pointer; {a pair node} @!old←exp:pointer; {a capsule to recycle} @!v:fraction; {|n/d|} begin if internal[tracing←commands]>two then @<Trace the fraction multiplication@>; case cur←type of transform←type,pair←type:old←exp:=tarnished(cur←exp); independent:old←exp:=void; othercases old←exp:=null endcases; if old←exp<>null then begin old←exp:=cur←exp; make←exp←copy(old←exp); end; v:=make←fraction(n,d); if cur←type=known then cur←exp:=take←fraction(cur←exp,v) else if cur←type=pair←type then begin p:=value(cur←exp); dep←mult(x←part←loc(p),v,false); dep←mult(y←part←loc(p),v,false); end else dep←mult(null,v,false); if old←exp<>null then begin recycle←value(old←exp); free←node(old←exp,value←node←size); end end; @ @<Trace the fraction multiplication@>= begin begin←diagnostic; print←nl("{("); print←scaled(n); print←char("/"); print←scaled(d); print(")*("); print←exp(null,0); print(")}"); end←diagnostic(false); end @ The |hard←times| routine multiplies a nice pair by a dependency list. @<Declare binary action procedures@>= procedure hard←times(@!p:pointer); var @!q:pointer; {a copy of the dependent variable |p|} @!r:pointer; {the big node for the nice pair} @!u,@!v:scaled; {the known values of the nice pair} begin if type(p)=pair←type then begin q:=stash←cur←exp; unstash←cur←exp(p); p:=q; end; {now |cur←type=pair←type|} r:=value(cur←exp); u:=value(x←part←loc(r)); v:=value(y←part←loc(r)); @<Move the dependent variable |p| into both parts of the pair node |r|@>; dep←mult(x←part←loc(r),u,true); dep←mult(y←part←loc(r),v,true); end; @ @<Move the dependent variable |p|...@>= type(y←part←loc(r)):=type(p); new←dep(y←part←loc(r),copy←dep←list(dep←list(p)));@/ type(x←part←loc(r)):=type(p); mem[value←loc(x←part←loc(r))]:=mem[value←loc(p)]; link(prev←dep(p)):=x←part←loc(r); free←node(p,value←node←size) @ @<Additional cases of binary operators@>= over: if (cur←type<>known)or(type(p)<pair←type) then bad←binary(p,over) else begin v:=cur←exp; unstash←cur←exp(p); if v=0 then @<Squeal about division by zero@> else begin if cur←type=known then cur←exp:=make←scaled(cur←exp,v) else if cur←type=pair←type then begin p:=value(cur←exp); dep←div(x←part←loc(p),v); dep←div(y←part←loc(p),v); end else dep←div(null,v); end; return; end; @ @<Declare binary action...@>= procedure dep←div(@!p:pointer;@!v:scaled); label exit; var @!q:pointer; {the dependency list being divided by |v|} @!s,@!t:small←number; {its type, before and after} begin if p=null then q:=cur←exp else if type(p)<>known then q:=p else begin value(p):=make←scaled(value(p),v); return; end; t:=type(q); q:=dep←list(q); s:=t; if t=dependent then if ab←vs←cd(max←coef(q),unity,coef←bound-1,abs(v))>=0 then t:=proto←dependent; q:=p←over←v(q,v,s,t); dep←finish(q,p,t); exit:end; @ @<Squeal about division by zero@>= begin exp←err("Division by zero"); @.Division by zero@> help2("You're trying to divide the quantity shown above the error")@/ ("message by zero. I'm going to divide it by one instead."); put←get←error; end @ @<Additional cases of binary operators@>= pythag←add,pythag←sub: if (cur←type=known)and(type(p)=known) then if c=pythag←add then cur←exp:=pyth←add(value(p),cur←exp) else cur←exp:=pyth←sub(value(p),cur←exp) else bad←binary(p,c); @ The next few sections of the program deal with affine transformations of coordinate data. @<Additional cases of binary operators@>= rotated←by,slanted←by,scaled←by,shifted←by,transformed←by, x←scaled,y←scaled,z←scaled: @t@>@;@/ if (type(p)=path←type)or(type(p)=future←pen)or(type(p)=pen←type) then begin path←trans(p,c); return; end else if (type(p)=pair←type)or(type(p)=transform←type) then big←trans(p,c) else if type(p)=picture←type then begin edges←trans(p,c); return; end else bad←binary(p,c); @ Let |c| be one of the eight transform operators. The procedure call |set←up←trans(c)| first changes |cur←exp| to a transform that corresponds to |c| and the original value of |cur←exp|. (In particular, |cur←exp| doesn't change at all if |c=transformed←by|.) Then, if all components of the resulting transform are |known|, they are moved to the global variables |txx|, |txy|, |tyx|, |tyy|, |tx|, |ty|; and |cur←exp| is changed to the known value zero. @<Declare binary action...@>= procedure set←up←trans(@!c:quarterword); label done,exit; var @!p,@!q,@!r:pointer; {list manipulation registers} begin if (c<>transformed←by)or(cur←type<>transform←type) then @<Put the current transform into |cur←exp|@>; @<If the current transform is entirely known, stash it in global variables; otherwise |return|@>; exit:end; @ @<Glob...@>= @!txx,@!txy,@!tyx,@!tyy,@!tx,@!ty:scaled; {current transform coefficients} @ @<Put the current transform...@>= begin p:=stash←cur←exp; cur←exp:=id←transform; cur←type:=transform←type; q:=value(cur←exp); case c of @<For each of the eight cases, change the relevant fields of |cur←exp| and |goto done|; but do nothing if capsule |p| doesn't have the appropriate type@>@; end; {there are no other cases} disp←err(p,"Improper transformation argument"); @.Improper transformation argument@> help3("The expression shown above has the wrong type,")@/ ("so I can't transform anything using it.")@/ ("Proceed, and I'll omit the transformation."); put←get←error; done: recycle←value(p); free←node(p,value←node←size); end @ @<If the current transform is entirely known, ...@>= q:=value(cur←exp); r:=q+transform←node←size; repeat r:=r-2; if type(r)<>known then return; until r=q; txx:=value(xx←part←loc(q)); txy:=value(xy←part←loc(q)); tyx:=value(yx←part←loc(q)); tyy:=value(yy←part←loc(q)); tx:=value(x←part←loc(q)); ty:=value(y←part←loc(q)); flush←cur←exp(0) @ @<For each of the eight cases...@>= rotated←by:if type(p)=known then @<Install sines and cosines, then |goto done|@>; slanted←by:if type(p)>pair←type then begin install(xy←part←loc(q),p); goto done; end; scaled←by:if type(p)>pair←type then begin install(xx←part←loc(q),p); install(yy←part←loc(q),p); goto done; end; shifted←by:if type(p)=pair←type then begin r:=value(p); install(x←part←loc(q),x←part←loc(r)); install(y←part←loc(q),y←part←loc(r)); goto done; end; x←scaled:if type(p)>pair←type then begin install(xx←part←loc(q),p); goto done; end; y←scaled:if type(p)>pair←type then begin install(yy←part←loc(q),p); goto done; end; z←scaled:if type(p)=pair←type then @<Install a complex multiplier, then |goto done|@>; transformed←by:do←nothing; @ @<Install sines and cosines, then |goto done|@>= begin n←sin←cos((value(p) mod three←sixty←units)*16); value(xx←part←loc(q)):=round←fraction(n←cos); value(yx←part←loc(q)):=round←fraction(n←sin); value(xy←part←loc(q)):=-value(yx←part←loc(q)); value(yy←part←loc(q)):=value(xx←part←loc(q)); goto done; end @ @<Install a complex multiplier, then |goto done|@>= begin r:=value(p); install(xx←part←loc(q),x←part←loc(r)); install(yy←part←loc(q),x←part←loc(r)); install(yx←part←loc(q),y←part←loc(r)); if type(y←part←loc(r))=known then negate(value(y←part←loc(r))) else negate←dep←list(dep←list(y←part←loc(r))); install(xy←part←loc(q),y←part←loc(r)); goto done; end @ Procedure |set←up←known←trans| is like |set←up←trans|, but it insists that the transformation be entirely known. @<Declare binary action...@>= procedure set←up←known←trans(@!c:quarterword); begin set←up←trans(c); if cur←type<>known then begin exp←err("Transform components aren't all known"); @.Transform components...@> help3("I'm unable to apply a partially specified transformation")@/ ("except to a fully known pair or transform.")@/ ("Proceed, and I'll omit the transformation."); put←get←flush←error(0); txx:=unity; txy:=0; tyx:=0; tyy:=unity; tx:=0; ty:=0; end; end; @ Here's a procedure that applies the transform |txx..ty| to a pair of coordinates in locations |p| and~|q|. @<Declare binary action...@>= procedure trans(@!p,@!q:pointer); var @!v:scaled; {the new |x| value} begin v:=take←scaled(mem[p].sc,txx)+take←scaled(mem[q].sc,txy)+tx; mem[q].sc:=take←scaled(mem[p].sc,tyx)+take←scaled(mem[q].sc,tyy)+ty; mem[p].sc:=v; end; @ The simplest transformation procedure applies a transform to all coordinates of a path. The |null←pen| remains unchanged if it isn't being shifted. @<Declare binary action...@>= procedure path←trans(@!p:pointer;@!c:quarterword); label exit; var @!q:pointer; {list traversor} begin set←up←known←trans(c); unstash←cur←exp(p); if cur←type=pen←type then begin if max←offset(cur←exp)=0 then if tx=0 then if ty=0 then return; flush←cur←exp(make←path(cur←exp)); cur←type:=future←pen; end; q:=cur←exp; repeat if left←type(q)<>endpoint then trans(q+3,q+4); {that's |left←x| and |left←y|} trans(q+1,q+2); {that's |x←coord| and |y←coord|} if right←type(q)<>endpoint then trans(q+5,q+6); {that's |right←x| and |right←y|} q:=link(q); until q=cur←exp; exit:end; @ The next simplest transformation procedure applies to edges. It is simple primarily because \MF\ doesn't allow very general transformations to be made, and because the tricky subroutines for edge transformation have already been written. @<Declare binary action...@>= procedure edges←trans(@!p:pointer;@!c:quarterword); label exit; begin set←up←known←trans(c); unstash←cur←exp(p); cur←edges:=cur←exp; if empty←edges(cur←edges) then return; {the empty set is easy to transform} if txx=0 then if tyy=0 then if txy mod unity=0 then if tyx mod unity=0 then begin xy←swap←edges; txx:=txy; tyy:=tyx; txy:=0; tyx:=0; if empty←edges(cur←edges) then return; end; if txy=0 then if tyx=0 then if txy mod unity=0 then if tyy mod unity=0 then @<Scale the edges, shift them, and |return|@>; print←err("That transformation is too hard"); @.That transformation...@> help3("I can apply complicated transformations to paths,")@/ ("but I can only do integer operations on pictures.")@/ ("Proceed, and I'll omit the transformation."); put←get←error; exit:end; @ @<Scale the edges, shift them, and |return|@>= begin if (txx=0)or(tyy=0) then begin toss←edges(cur←edges); cur←exp:=get←node(edge←header←size); init←edges(cur←exp); end else begin if txx<0 then begin x←reflect←edges; txx:=-txx; end; if tyy<0 then begin y←reflect←edges; tyy:=-tyy; end; if txx<>unity then x←scale←edges(txx div unity); if tyy<>unity then y←scale←edges(tyy div unity); @<Shift the edges by |(tx,ty)|, rounded@>; end; return; end @ @<Shift the edges...@>= tx:=round←unscaled(tx); ty:=round←unscaled(ty); if (m←min(cur←edges)+tx<=0)or(m←max(cur←edges)+tx>=8192)or@| (n←min(cur←edges)+ty<=0)or(n←max(cur←edges)+ty>=8191)or@| (abs(tx)>=4096)or(abs(ty)>=4096) then begin print←err("Too far to shift"); @.Too far to shift@> help3("I can't shift the picture as requested---it would")@/ ("make some coordinates too large or too small.")@/ ("Proceed, and I'll omit the transformation."); put←get←error; end else begin if tx<>0 then begin if not valid←range(m←offset(cur←edges)-tx) then fix←offset; m←min(cur←edges):=m←min(cur←edges)+tx; m←max(cur←edges):=m←max(cur←edges)+tx; m←offset(cur←edges):=m←offset(cur←edges)-tx; last←window←time(cur←edges):=0; end; if ty<>0 then begin n←min(cur←edges):=n←min(cur←edges)+ty; n←max(cur←edges):=n←max(cur←edges)+ty; n←pos(cur←edges):=n←pos(cur←edges)+ty; last←window←time(cur←edges):=0; end; end @ The hard cases of transformation occur when big nodes are involved, and when some of their components are unknown. @<Declare binary action...@>= @t\4@>@<Declare subroutines needed by |big←trans|@>@; procedure big←trans(@!p:pointer;@!c:quarterword); label exit; var @!q,@!r,@!pp,@!qq:pointer; {list manipulation registers} @!s:small←number; {size of a big node} begin s:=big←node←size[type(p)]; q:=value(p); r:=q+s; repeat r:=r-2; if type(r)<>known then @<Transform an unknown big node and |return|@>; until r=q; @<Transform a known big node@>; exit:end; {node |p| will now be recycled by |do←binary|} @ @<Transform an unknown big node and |return|@>= begin set←up←known←trans(c); make←exp←copy(p); r:=value(cur←exp); if cur←type=transform←type then begin bilin1(yy←part←loc(r),tyy,xy←part←loc(q),tyx,0); bilin1(yx←part←loc(r),tyy,xx←part←loc(q),tyx,0); bilin1(xy←part←loc(r),txx,yy←part←loc(q),txy,0); bilin1(xx←part←loc(r),txx,yx←part←loc(q),txy,0); end; bilin1(y←part←loc(r),tyy,x←part←loc(q),tyx,ty); bilin1(x←part←loc(r),txx,y←part←loc(q),txy,tx); return; end @ Let |p| point to a two-word value field inside a big node of |cur←exp|, and let |q| point to a another value field. The |bilin1| procedure replaces |p| by $p\cdot t+q\cdot u+\delta$. @<Declare subroutines needed by |big←trans|@>= procedure bilin1(@!p:pointer;@!t:scaled;@!q:pointer;@!u,@!delta:scaled); var @!r:pointer; {list traverser} begin if t<>unity then dep←mult(p,t,true); if u<>0 then if type(q)=known then delta:=delta+take←scaled(value(q),u) else begin @<Ensure that |type(p)=proto←dependent|@>; dep←list(p):=p←plus←fq(dep←list(p),u,dep←list(q),proto←dependent,type(q)); end; if type(p)=known then value(p):=value(p)+delta else begin r:=dep←list(p); while info(r)<>null do r:=link(r); delta:=value(r)+delta; if r<>dep←list(p) then value(r):=delta else begin recycle←value(p); type(p):=known; value(p):=delta; end; end; if fix←needed then fix←dependencies; end; @ @<Ensure that |type(p)=proto←dependent|@>= if type(p)<>proto←dependent then begin if type(p)=known then new←dep(p,const←dependency(value(p))) else dep←list(p):=p←times←v(dep←list(p),unity,dependent,proto←dependent,true); type(p):=proto←dependent; end @ @<Transform a known big node@>= set←up←trans(c); if cur←type=known then @<Transform known by known@> else begin pp:=stash←cur←exp; qq:=value(pp); make←exp←copy(p); r:=value(cur←exp); if cur←type=transform←type then begin bilin2(yy←part←loc(r),yy←part←loc(qq), value(xy←part←loc(q)),yx←part←loc(qq),null); bilin2(yx←part←loc(r),yy←part←loc(qq), value(xx←part←loc(q)),yx←part←loc(qq),null); bilin2(xy←part←loc(r),xx←part←loc(qq), value(yy←part←loc(q)),xy←part←loc(qq),null); bilin2(xx←part←loc(r),xx←part←loc(qq), value(yx←part←loc(q)),xy←part←loc(qq),null); end; bilin2(y←part←loc(r),yy←part←loc(qq), value(x←part←loc(q)),yx←part←loc(qq),y←part←loc(qq)); bilin2(x←part←loc(r),xx←part←loc(qq), value(y←part←loc(q)),xy←part←loc(qq),x←part←loc(qq)); recycle←value(pp); free←node(pp,value←node←size); end; @ Let |p| be a |proto←dependent| value whose dependency list ends at |dep←final|. The following procedure adds |v| times another numeric quantity to~|p|. @<Declare subroutines needed by |big←trans|@>= procedure add←mult←dep(@!p:pointer;@!v:scaled;@!r:pointer); begin if type(r)=known then value(dep←final):=value(dep←final)+take←scaled(value(r),v) else begin dep←list(p):= p←plus←fq(dep←list(p),v,dep←list(r),proto←dependent,type(r)); if fix←needed then fix←dependencies; end; end; @ The |bilin2| procedure is something like |bilin1|, but with known and unknown quantities reversed. Parameter |p| points to a value field within the big node for |cur←exp|; and |type(p)=known|. Parameters |t| and~|u| point to value fields elsewhere; so does parameter~|q|, unless it is |null| (which stands for zero). Location~|p| will be replaced by $p\cdot t+v\cdot u+q$. @<Declare subroutines needed by |big←trans|@>= procedure bilin2(@!p,@!t:pointer;@!v:scaled;@!u,@!q:pointer); var @!vv:scaled; {temporary storage for |value(p)|} begin vv:=value(p); type(p):=proto←dependent; new←dep(p,const←dependency(0)); {this sets |dep←final|} if vv<>0 then add←mult←dep(p,vv,t); {|dep←final| doesn't change} if v<>0 then add←mult←dep(p,v,u); if q<>null then add←mult←dep(p,unity,q); if dep←list(p)=dep←final then begin vv:=value(dep←final); recycle←value(p); type(p):=known; value(p):=vv; end; end; @ @<Transform known by known@>= begin make←exp←copy(p); r:=value(cur←exp); if cur←type=transform←type then begin bilin3(yy←part←loc(r),tyy,value(xy←part←loc(q)),tyx,0); bilin3(yx←part←loc(r),tyy,value(xx←part←loc(q)),tyx,0); bilin3(xy←part←loc(r),txx,value(yy←part←loc(q)),txy,0); bilin3(xx←part←loc(r),txx,value(yx←part←loc(q)),txy,0); end; bilin3(y←part←loc(r),tyy,value(x←part←loc(q)),tyx,ty); bilin3(x←part←loc(r),txx,value(y←part←loc(q)),txy,tx); end @ Finally, in |bilin3| everything is |known|. @<Declare subroutines needed by |big←trans|@>= procedure bilin3(@!p:pointer;@!t,@!v,@!u,@!delta:scaled); begin if t<>unity then delta:=delta+take←scaled(value(p),t) else delta:=delta+value(p); if u<>0 then value(p):=delta+take←scaled(v,u) else value(p):=delta; end; @ @<Additional cases of binary operators@>= concatenate: if (cur←type=string←type)and(type(p)=string←type) then cat(p) else bad←binary(p,concatenate); substring←of: if nice←pair(p,type(p))and(cur←type=string←type) then chop←string(value(p)) else bad←binary(p,substring←of); subpath←of: begin if cur←type=pair←type then pair←to←path; if nice←pair(p,type(p))and(cur←type=path←type) then chop←path(value(p)) else bad←binary(p,subpath←of); end; @ @<Declare binary action...@>= procedure cat(@!p:pointer); var @!a,@!b:str←number; {the strings being concatenated} @!k:pool←pointer; {index into |str←pool|} begin a:=value(p); b:=cur←exp; str←room(length(a)+length(b)); for k:=str←start[a] to str←start[a+1]-1 do append←char(str←pool[k]); for k:=str←start[b] to str←start[b+1]-1 do append←char(str←pool[k]); cur←exp:=make←string; delete←str←ref(b); end; @ @<Declare binary action...@>= procedure chop←string(@!p:pointer); var @!a,@!b:integer; {start and stop points} @!l:integer; {length of the original string} @!k:integer; {runs from |a| to |b|} @!s:str←number; {the original string} @!reversed:boolean; {was |a>b|?} begin a:=round←unscaled(value(x←part←loc(p))); b:=round←unscaled(value(y←part←loc(p))); if a<=b then reversed:=false else begin reversed:=true; k:=a; a:=b; b:=k; end; s:=cur←exp; l:=length(s); if a<0 then begin a:=0; if b<0 then b:=0; end; if b>l then begin b:=l; if a>l then a:=l; end; str←room(b-a); if reversed then for k:=str←start[s]+b-1 downto str←start[s]+a do append←char(str←pool[k]) else for k:=str←start[s]+a to str←start[s]+b-1 do append←char(str←pool[k]); cur←exp:=make←string; delete←str←ref(s); end; @ @<Declare binary action...@>= procedure chop←path(@!p:pointer); var @!q:pointer; {a knot in the original path} @!pp,@!qq,@!rr,@!ss:pointer; {link variables for copies of path nodes} @!a,@!b,@!k,@!l:scaled; {indices for chopping} @!reversed:boolean; {was |a>b|?} begin l:=path←length; a:=value(x←part←loc(p)); b:=value(y←part←loc(p)); if a<=b then reversed:=false else begin reversed:=true; k:=a; a:=b; b:=k; end; @<Dispense with the cases |a<0| and/or |b>l|@>; q:=cur←exp; while a>=unity do begin q:=link(q); a:=a-unity; b:=b-unity; end; if b=a then @<Construct a path from |pp| to |qq| of length zero@> else @<Construct a path from |pp| to |qq| of length $\lceil b\rceil$@>; left←type(pp):=endpoint; right←type(qq):=endpoint; link(qq):=pp; toss←knot←list(cur←exp); if reversed then begin cur←exp:=link(htap←ypoc(pp)); toss←knot←list(pp); end else cur←exp:=pp; end; @ @<Dispense with the cases |a<0| and/or |b>l|@>= if a<0 then if left←type(cur←exp)=endpoint then begin a:=0; if b<0 then b:=0; end else repeat a:=a+l; b:=b+l; until a>=0; {a cycle always has length |l>0|} if b>l then if left←type(cur←exp)=endpoint then begin b:=l; if a>l then a:=l; end else while a>=l do begin a:=a-l; b:=b-l; end @ @<Construct a path from |pp| to |qq| of length $\lceil b\rceil$@>= begin pp:=copy←knot(q); qq:=pp; repeat q:=link(q); rr:=qq; qq:=copy←knot(q); link(rr):=qq; b:=b-unity; until b<=0; if a>0 then begin ss:=pp; pp:=link(pp); split←cubic(ss,a*@'10000,x←coord(pp),y←coord(pp)); pp:=link(ss); free←node(ss,knot←node←size); if rr=ss then begin b:=make←scaled(b,unity-a); rr:=pp; end; end; if b<0 then begin split←cubic(rr,(b+unity)*@'10000,x←coord(qq),y←coord(qq)); free←node(qq,knot←node←size); qq:=link(rr); end; end @ @<Construct a path from |pp| to |qq| of length zero@>= begin if a>0 then begin qq:=link(q); split←cubic(q,a*@'10000,x←coord(qq),y←coord(qq)); q:=link(q); end; pp:=copy←knot(q); qq:=pp; end @ The |pair←value| routine changes the current expression to a given ordered pair of values. @<Declare binary action...@>= procedure pair←value(@!x,@!y:scaled); var @!p:pointer; {a pair node} begin p:=get←node(value←node←size); flush←cur←exp(p); cur←type:=pair←type; type(p):=pair←type; name←type(p):=capsule; init←big←node(p); p:=value(p);@/ type(x←part←loc(p)):=known; value(x←part←loc(p)):=x;@/ type(y←part←loc(p)):=known; value(y←part←loc(p)):=y;@/ end; @ @<Additional cases of binary operators@>= point←of,precontrol←of,postcontrol←of: begin if cur←type=pair←type then pair←to←path; if (cur←type=path←type)and(type(p)=known) then find←point(value(p),c) else bad←binary(p,c); end; pen←offset←of: begin if cur←type=future←pen then materialize←pen; if (cur←type=pen←type)and nice←pair(p,type(p)) then set←up←offset(value(p)) else bad←binary(p,pen←offset←of); end; direction←time←of: begin if cur←type=pair←type then pair←to←path; if (cur←type=path←type)and nice←pair(p,type(p)) then set←up←direction←time(value(p)) else bad←binary(p,direction←time←of); end; @ @<Declare binary action...@>= procedure set←up←offset(@!p:pointer); begin find←offset(value(x←part←loc(p)),value(y←part←loc(p)),cur←exp); pair←value(cur←x,cur←y); end; @# procedure set←up←direction←time(@!p:pointer); begin flush←cur←exp(find←direction←time(value(x←part←loc(p)), value(y←part←loc(p)),cur←exp)); end; @ @<Declare binary action...@>= procedure find←point(@!v:scaled;@!c:quarterword); var @!p:pointer; {the path} @!n:scaled; {its length} @!vv:scaled; {initial value of |v|} @!q:pointer; {successor of |p|} begin vv:=v; p:=cur←exp;@/ if left←type(p)=endpoint then n:=-unity@+else n:=0; repeat p:=link(p); n:=n+unity; until p=cur←exp; if n=0 then v:=0 else if v<0 then if left←type(p)=endpoint then v:=0 else v:=n-1-((-v-1) mod n) else if v>n then if left←type(p)=endpoint then v:=n else v:=v mod n; p:=cur←exp; while v>=unity do begin p:=link(p); v:=v-unity; end; if v<>0 then @<Insert a fractional node by splitting the cubic@>; @<Set the current expression to the desired path coordinates@>; end; @ @<Insert a fractional node...@>= begin q:=link(p); split←cubic(p,v*@'10000,x←coord(q),y←coord(q)); p:=link(p); end @ @<Set the current expression to the desired path coordinates...@>= case c of point←of: pair←value(x←coord(p),y←coord(p)); precontrol←of: if left←type(p)=endpoint then pair←value(x←coord(p),y←coord(p)) else pair←value(left←x(p),left←y(p)); postcontrol←of: if right←type(p)=endpoint then pair←value(x←coord(p),y←coord(p)) else pair←value(right←x(p),right←y(p)); end {there are no other cases} @ @<Additional cases of bin...@>= intersect: begin if type(p)=pair←type then begin q:=stash←cur←exp; unstash←cur←exp(p); pair←to←path; p:=stash←cur←exp; unstash←cur←exp(q); end; if cur←type=pair←type then pair←to←path; if (cur←type=path←type)and(type(p)=path←type) then begin path←intersection(value(p),cur←exp); pair←value(cur←t,cur←tt); end else bad←binary(p,intersect); end; @* \[43] Statements and commands. The chief executive of \MF\ is the |do←statement| routine, which contains the master switch that causes all the various pieces of \MF\ to do their things, in the right order. In a sense, this is the grand climax of the program: It applies all of the tools that we have worked so hard to construct. In another sense, this is the messiest part of the program: It necessarily refers to other pieces of code all over the place, so that a person can't fully understand what is going on without paging back and forth to be reminded of conventions that are defined elsewhere. We are now at the hub of the web. The structure of |do←statement| itself is quite simple. The first token of the statement is fetched using |get←x←next|. If it can be the first token of an expression, we look for an equation, an assignment, or a title. Otherwise we use a \&{case} construction to branch at high speed to the appropriate routine for various and sundry other types of commands, each of which has an ``action procedure'' that does the necessary work. The program uses the fact that |min←primary←command=max←statement←command =type←name| to interpret a statement that starts with, e.g., `\&{string}', as a type declaration rather than a boolean expression. @p @t\4@>@<Declare generic font output procedures@>@; @t\4@>@<Declare action procedures for use by |do←statement|@>@; procedure do←statement; {governs \MF's activities} begin cur←type:=vacuous; get←x←next; if cur←cmd>max←primary←command then @<Worry about bad statement@> else if cur←cmd>max←statement←command then @<Do an equation, assignment, title, or `$\langle\,$expression$\,\rangle\,$\&{endgroup}'@> else @<Do a statement that doesn't begin with an expression@>; if cur←cmd<semicolon then @<Flush unparsable junk that was found after the statement@>; error←count:=0; end; @ The only command codes |>max←primary←command| that can be present at the beginning of a statement are |semicolon| and higher; these occur when the statement is null. @<Worry about bad statement@>= begin if cur←cmd<semicolon then begin print←err("A statement can't begin with `"); @.A statement can't begin with x@> print←cmd←mod(cur←cmd,cur←mod); print←char("'"); help5("I was looking for the beginning of a new statement.")@/ ("If you just proceed without changing anything, I'll ignore")@/ ("everything up to the next `;'. Please insert a semicolon")@/ ("now in front of anything that you don't want me to delete.")@/ ("(See Chapter 27 of The METAFONTbook for an example.)");@/ @:METAFONTbook}{\sl The {\logos METAFONT\/}book@> back←error; get←x←next; end; end @ The help message printed here says that everything is flushed up to a semicolon, but actually the commands |end←group| and |stop| will also terminate a statement. @<Flush unparsable junk that was found after the statement@>= begin print←err("Extra tokens will be flushed"); @.Extra tokens will be flushed@> help6("I've just read as much of that statement as I could fathom,")@/ ("so a semicolon should have been next. It's very puzzling...")@/ ("but I'll try to get myself back together, by ignoring")@/ ("everything up to the next `;'. Please insert a semicolon")@/ ("now in front of anything that you don't want me to delete.")@/ ("(See Chapter 27 of The METAFONTbook for an example.)");@/ @:METAFONTbook}{\sl The {\logos METAFONT\/}book@> back←error; scanner←status:=flushing; repeat get←next; @<Decrease the string reference count...@>; until end←of←statement; {|cur←cmd=semicolon|, |end←group|, or |stop|} scanner←status:=normal; end @ If |do←statement| ends with |cur←cmd=end←group|, we should have |cur←type=vacuous| unless the statement was simply an expression; in the latter case, |cur←type| and |cur←exp| should represent that expression. @<Do a statement that doesn't...@>= begin if internal[tracing←commands]>0 then show←cur←cmd←mod; case cur←cmd of type←name:do←type←declaration; macro←def:if cur←mod>var←def then make←op←def else if cur←mod>end←def then scan←def; @t\4@>@<Cases of |do←statement| that invoke particular commands@>@; end; {there are no other cases} cur←type:=vacuous; end @ The most important statements begin with expressions. @<Do an equation, assignment, title, or...@>= begin var←flag:=assignment; scan←expression; if cur←cmd<end←group then begin if cur←cmd=equals then do←equation else if cur←cmd=assignment then do←assignment else if cur←type=string←type then @<Do a title@> else if cur←type<>vacuous then begin exp←err("Isolated expression"); @.Isolated expression@> help3("I couldn't find an `=' or `:=' after the")@/ ("expression that is shown above this error message,")@/ ("so I guess I'll just ignore it and carry on."); put←get←error; end; flush←cur←exp(0); cur←type:=vacuous; end; end @ @<Do a title@>= begin if internal[tracing←titles]>0 then begin print←nl(""); slow←print(cur←exp); update←terminal; end; if internal[proofing]>0 then @<Send the current expression as a title to the output file@>; end @ Equations and assignments are performed by the pair of mutually recursive @↑recursion@> routines |do←equation| and |do←assignment|. These routines are called when |cur←cmd=equals| and |cur←cmd=assignment|, respectively; the left-hand side is in |cur←type| and |cur←exp|, while the right-hand side is yet to be scanned. After the routines are finished, |cur←type| and |cur←exp| will be equal to the right-hand side (which will normally be equal to the left-hand side). @<Declare action procedures for use by |do←statement|@>= @t\4@>@<Declare the procedure called |try←eq|@>@; @t\4@>@<Declare the procedure called |make←eq|@>@; procedure@?do←assignment; forward;@t\2@>@/ procedure do←equation; var @!lhs:pointer; {capsule for the left-hand side} @!p:pointer; {temporary register} begin lhs:=stash←cur←exp; get←x←next; var←flag:=assignment; scan←expression; if cur←cmd=equals then do←equation else if cur←cmd=assignment then do←assignment; if internal[tracing←commands]>two then @<Trace the current equation@>; if cur←type=unknown←path then if type(lhs)=pair←type then begin p:=stash←cur←exp; unstash←cur←exp(lhs); lhs:=p; end; {in this case |make←eq| will change the pair to a path} make←eq(lhs); {equate |lhs| to |(cur←type,cur←exp)|} end; @ And |do←assignment| is similar to |do←expression|: @<Declare action procedures for use by |do←statement|@>= procedure do←assignment; var @!lhs:pointer; {token list for the left-hand side} @!p:pointer; {where the left-hand value is stored} @!q:pointer; {temporary capsule for the right-hand value} begin if cur←type<>token←list then begin exp←err("Improper `:=' will be changed to `='"); @.Improper `:='@> help2("I didn't find a variable name at the left of the `:=',")@/ ("so I'm going to pretend that you said `=' instead.");@/ error; do←equation; end else begin lhs:=cur←exp; cur←type:=vacuous;@/ get←x←next; var←flag:=assignment; scan←expression; if cur←cmd=equals then do←equation else if cur←cmd=assignment then do←assignment; if internal[tracing←commands]>two then @<Trace the current assignment@>; if info(lhs)>hash←end then @<Assign the current expression to an internal variable@> else @<Assign the current expression to the variable |lhs|@>; flush←node←list(lhs); end; end; @ @<Trace the current equation@>= begin begin←diagnostic; print←nl("{("); print←exp(lhs,0); print(")=("); print←exp(null,0); print(")}"); end←diagnostic(false); end @ @<Trace the current assignment@>= begin begin←diagnostic; print←nl("{"); if info(lhs)>hash←end then print(int←name[info(lhs)-(hash←end)]) else show←token←list(lhs,null,1000,0); print(":="); print←exp(null,0); print←char("}"); end←diagnostic(false); end @ @<Assign the current expression to an internal variable@>= if cur←type=known then internal[info(lhs)-(hash←end)]:=cur←exp else begin exp←err("Internal quantity `"); @.Internal quantity...@> print(int←name[info(lhs)-(hash←end)]); print("' must receive a known value"); help2("I can't set an internal quantity to anything but a known")@/ ("numeric value, so I'll have to ignore this assignment."); put←get←error; end @ @<Assign the current expression to the variable |lhs|@>= begin p:=find←variable(lhs); if p<>null then begin q:=stash←cur←exp; cur←type:=und←type(p); recycle←value(p); type(p):=cur←type; value(p):=null; make←exp←copy(p); p:=stash←cur←exp; unstash←cur←exp(q); make←eq(p); end else begin obliterated(lhs); put←get←error; end; end @ And now we get to the nitty-gritty. The |make←eq| procedure is given a pointer to a capsule that is to be equated to the current expression. @<Declare the procedure called |make←eq|@>= procedure make←eq(@!lhs:pointer); label restart,done, not←found; var @!t:small←number; {type of the left-hand side} @!v:integer; {value of the left-hand side} @!p,@!q:pointer; {pointers inside of big nodes} begin restart: t:=type(lhs); if t<=pair←type then v:=value(lhs); case t of @t\4@>@<For each type |t|, make an equation and |goto done| unless |cur←type| is incompatible with |t|@>@; end; {all cases have been listed} @<Announce that the equation cannot be performed@>; done:check←arith; recycle←value(lhs); free←node(lhs,value←node←size); end; @ @<Announce that the equation cannot be performed@>= disp←err(lhs,""); exp←err("Equation cannot be performed ("); @.Equation cannot be performed@> if type(lhs)<=pair←type then print←type(type(lhs))@+else print("numeric"); print←char("="); if cur←type<=pair←type then print←type(cur←type)@+else print("numeric"); print←char(")");@/ help2("I'm sorry, but I don't know how to make such things equal.")@/ ("(See the two expressions just above the error message.)"); put←get←error @ @<For each type |t|, make an equation and |goto done| unless...@>= boolean←type,string←type,pen←type,path←type,picture←type: if cur←type=t+unknown←tag then begin nonlinear←eq(v,cur←exp,false); goto done; end else if cur←type=t then @<Report redundant or inconsistent equation and |goto done|@>; unknown←types:if cur←type=t-unknown←tag then begin nonlinear←eq(cur←exp,lhs,true); goto done; end else if cur←type=t then begin ring←merge(lhs,cur←exp); goto done; end else if cur←type=pair←type then if t=unknown←path then begin pair←to←path; goto restart; end; transform←type,pair←type:if cur←type=t then @<Do multiple equations and |goto done|@>; known,dependent,proto←dependent,independent:if cur←type>=known then begin try←eq(lhs,null); goto done; end; vacuous:do←nothing; @ @<Report redundant or inconsistent equation and |goto done|@>= begin if cur←type<=string←type then begin if cur←type=string←type then begin if str←vs←str(v,cur←exp)<>0 then goto not←found; end else if v<>cur←exp then goto not←found; @<Exclaim about a redundant equation@>; goto done; end; print←err("Redundant or inconsistent equation"); @.Redundant or inconsistent equation@> help2("An equation between already-known quantities can't help.")@/ ("But don't worry; continue and I'll just ignore it."); put←get←error; goto done; not←found: print←err("Inconsistent equation"); @.Inconsistent equation@> help2("The equation I just read contradicts what was said before.")@/ ("But don't worry; continue and I'll just ignore it."); put←get←error; goto done; end @ @<Do multiple equations and |goto done|@>= begin p:=v+big←node←size[t]; q:=value(cur←exp)+big←node←size[t]; repeat p:=p-2; q:=q-2; try←eq(p,q); until p=v; goto done; end @ The first argument to |try←eq| is the location of a value node in a capsule that will soon be recycled. The second argument is either a location within a pair or transform node pointed to by |cur←exp|, or it is |null| (which means that |cur←exp| itself serves as the second argument). The idea is to leave |cur←exp| unchanged, but to equate the two operands. @<Declare the procedure called |try←eq|@>= procedure try←eq(@!l,@!r:pointer); label done,done1; var @!p:pointer; {dependency list for right operand minus left operand} @!t:known..independent; {the type of list |p|} @!q:pointer; {the constant term of |p| is here} @!pp:pointer; {dependency list for right operand} @!tt:dependent..independent; {the type of list |pp|} @!copied:boolean; {have we copied a list that ought to be recycled?} begin @<Remove the left operand from its container, negate it, and put it into dependency list~|p| with constant term~|q|@>; @<Add the right operand to list |p|@>; if info(p)=null then @<Deal with redundant or inconsistent equation@> else begin linear←eq(p,t); if r=null then if cur←type<>known then if type(cur←exp)=known then begin pp:=cur←exp; cur←exp:=value(cur←exp); cur←type:=known; free←node(pp,value←node←size); end; end; end; @ @<Remove the left operand from its container, negate it, and...@>= t:=type(l); if t=known then begin t:=dependent; p:=const←dependency(-value(l)); q:=p; end else if t=independent then begin t:=dependent; p:=single←dependency(l); negate(value(p)); q:=dep←final; end else begin p:=dep←list(l); q:=p; loop@+ begin negate(value(q)); if info(q)=null then goto done; q:=link(q); end; done: link(prev←dep(l)):=link(q); prev←dep(link(q)):=prev←dep(l); type(l):=known; end @ @<Deal with redundant or inconsistent equation@>= begin if abs(value(p))>64 then {off by .001 or more} begin print←err("Inconsistent equation");@/ @.Inconsistent equation@> print(" (off by "); print←scaled(value(p)); print←char(")"); help2("The equation I just read contradicts what was said before.")@/ ("But don't worry; continue and I'll just ignore it."); put←get←error; end else if r=null then @<Exclaim about a redundant equation@>; free←node(p,dep←node←size); end @ @<Add the right operand to list |p|@>= if r=null then if cur←type=known then begin value(q):=value(q)+cur←exp; goto done1; end else begin tt:=cur←type; if tt=independent then pp:=single←dependency(cur←exp) else pp:=dep←list(cur←exp); end else if type(r)=known then begin value(q):=value(q)+value(r); goto done1; end else begin tt:=type(r); if tt=independent then pp:=single←dependency(r) else pp:=dep←list(r); end; if tt<>independent then copied:=false else begin copied:=true; tt:=dependent; end; @<Add dependency list |pp| of type |tt| to dependency list~|p| of type~|t|@>; if copied then flush←node←list(pp); done1: @ @<Add dependency list |pp| of type |tt| to dependency list~|p| of type~|t|@>= watch←coefs:=false; if t=tt then p:=p←plus←q(p,pp,t) else if t=proto←dependent then p:=p←plus←fq(p,unity,pp,proto←dependent,dependent) else begin q:=p; while info(q)<>null do begin value(q):=round←fraction(value(q)); q:=link(q); end; t:=proto←dependent; p:=p←plus←q(p,pp,t); end; watch←coefs:=true; @ Our next goal is to process type declarations. For this purpose it's convenient to have a procedure that scans a $\langle\,$declared variable$\,\rangle$ and returns the corresponding token list. After the following procedure has acted, the token after the declared variable will have been scanned, so it will appear in |cur←cmd|, |cur←mod|, and~|cur←sym|. @<Declare the function called |scan←declared←variable|@>= function scan←declared←variable:pointer; label done; var @!x:pointer; {hash address of the variable's root} @!h,@!t:pointer; {head and tail of the token list to be returned} @!l:pointer; {hash address of left bracket} begin get←symbol; x:=cur←sym; if cur←cmd<>tag←token then clear←symbol(x,false); if equiv(x)=null then new←root(x); h:=get←avail; info(h):=x; t:=h;@/ loop@+ begin get←x←next; if cur←sym=0 then goto done; if cur←cmd<>tag←token then if cur←cmd<>internal←quantity then if cur←cmd=left←bracket then @<Descend past a collective subscript@> else goto done; link(t):=get←avail; t:=link(t); info(t):=cur←sym; end; done:scan←declared←variable:=h; end; @ If the subscript isn't collective, we don't accept it as part of the declared variable. @<Descend past a collective subscript@>= begin l:=cur←sym; get←x←next; if cur←cmd<>right←bracket then begin back←input; cur←sym:=l; cur←cmd:=left←bracket; goto done; end else cur←sym:=collective←subscript; end @ Type declarations are introduced by the following primitive operations. @<Put each...@>= primitive("numeric",type←name,numeric←type);@/ @!@:numeric←}{\&{numeric} primitive@> primitive("string",type←name,string←type);@/ @!@:string←}{\&{string} primitive@> primitive("boolean",type←name,boolean←type);@/ @!@:boolean←}{\&{boolean} primitive@> primitive("path",type←name,path←type);@/ @!@:path←}{\&{path} primitive@> primitive("pen",type←name,pen←type);@/ @!@:pen←}{\&{pen} primitive@> primitive("picture",type←name,picture←type);@/ @!@:picture←}{\&{picture} primitive@> primitive("transform",type←name,transform←type);@/ @!@:transform←}{\&{transform} primitive@> primitive("pair",type←name,pair←type);@/ @!@:pair←}{\&{pair} primitive@> @ @<Cases of |print←cmd...@>= type←name: print←type(m); @ Now we are ready to handle type declarations, assuming that a |type←name| has just been scanned. @<Declare action procedures for use by |do←statement|@>= procedure do←type←declaration; var @!t:small←number; {the type being declared} @!p:pointer; {token list for a declared variable} @!q:pointer; {value node for the variable} begin if cur←mod>=transform←type then t:=cur←mod@+else t:=cur←mod+unknown←tag; repeat p:=scan←declared←variable; flush←variable(equiv(info(p)),link(p),false);@/ q:=find←variable(p); if q<>null then begin type(q):=t; value(q):=null; end else begin print←err("Declared variable conflicts with previous vardef"); @.Declared variable conflicts...@> help2("You can't use, e.g., `numeric foo[]' after `vardef foo'.")@/ ("Proceed, and I'll ignore the illegal redeclaration."); put←get←error; end; flush←list(p); if cur←cmd<comma then @<Flush spurious symbols after the declared variable@>; until end←of←statement; end; @ @<Flush spurious symbols after the declared variable@>= begin print←err("Illegal suffix of declared variable will be flushed"); @.Illegal suffix...flushed@> help5("Variables in declarations must consist entirely of")@/ ("names and collective subscripts, e.g., `x[]a'.")@/ ("Are you trying to use a reserved word in a variable name?")@/ ("I'm going to discard the junk I found here,")@/ ("up to the next comma or the end of the declaration."); if cur←cmd=numeric←token then help←line[2]:="Explicit subscripts like `x15a' aren't permitted."; put←get←error; scanner←status:=flushing; repeat get←next; @<Decrease the string reference count...@>; until cur←cmd>=comma; {either |end←of←statement| or |cur←cmd=comma|} scanner←status:=normal; end @ \MF's |main←control| procedure just calls |do←statement| repeatedly until coming to the end. Each execution of |do←statement| concludes with |cur←cmd=semicolon|, |end←group|, or |stop|. @p procedure main←control; begin repeat do←statement; if cur←cmd=end←group then begin print←err("Extra `endgroup'"); @.Extra `endgroup'@> help2("I'm not currently working on a `begingroup',")@/ ("so I had better not try to end anything."); flush←error(0); end; until cur←cmd=stop; end; @ @<Put each...@>= primitive("end",stop,0);@/ @!@:end←}{\&{end} primitive@> primitive("dump",stop,1);@/ @!@:dump←}{\&{dump} primitive@> @ @<Cases of |print←cmd...@>= stop:if m=0 then print("end")@+else print("dump"); @* \[44] Commands. Let's turn now to statements that are classified as ``commands'' because of their imperative nature. We'll begin with simple ones, so that it will be clear how to hook command processing into the |do←statement| routine; then we'll tackle the tougher commands. Here's one of the simplest: @<Cases of |do←statement|...@>= random←seed: do←random←seed; @ @<Declare action procedures for use by |do←statement|@>= procedure do←random←seed; begin get←x←next; if cur←cmd<>assignment then begin missing←err(":="); @.Missing `:='@> help1("Always say `randomseed:=<numeric expression>'."); back←error; end; get←x←next; scan←expression; if cur←type<>known then begin exp←err("Unknown value will be ignored"); @.Unknown value...ignored@> help2("Your expression was too random for me to handle,")@/ ("so I won't change the random seed just now.");@/ put←get←flush←error(0); end else @<Initialize the random seed to |cur←exp|@>; end; @ @<Initialize the random seed to |cur←exp|@>= begin init←randoms(cur←exp); if selector>=log←only then begin old←setting:=selector; selector:=log←only; print←nl("{randomseed:="); print←scaled(cur←exp); print←char("}"); print←nl(""); selector:=old←setting; end; end @ And here's another simple one (somewhat different in flavor): @<Cases of |do←statement|...@>= mode←command: begin print←ln; interaction:=cur←mod; @<Initialize the print |selector| based on |interaction|@>; if job←name<>0 then selector:=selector+2; get←x←next; end; @ @<Put each...@>= primitive("batchmode",mode←command,batch←mode); @!@:batch←mode←}{\&{batchmode} primitive@> primitive("nonstopmode",mode←command,nonstop←mode); @!@:nonstop←mode←}{\&{nonstopmode} primitive@> primitive("scrollmode",mode←command,scroll←mode); @!@:scroll←mode←}{\&{scrollmode} primitive@> primitive("errorstopmode",mode←command,error←stop←mode); @!@:error←stop←mode←}{\&{errorstopmode} primitive@> @ @<Cases of |print←cmd←mod|...@>= mode←command: case m of batch←mode: print("batchmode"); nonstop←mode: print("nonstopmode"); scroll←mode: print("scrollmode"); othercases print("errorstopmode") endcases; @ The `\&{inner}' and `\&{outer}' commands are only slightly harder. @<Cases of |do←statement|...@>= protection←command: do←protection; @ @<Put each...@>= primitive("inner",protection←command,0);@/ @!@:inner←}{\&{inner} primitive@> primitive("outer",protection←command,1);@/ @!@:outer←}{\&{outer} primitive@> @ @<Cases of |print←cmd...@>= protection←command: if m=0 then print("inner")@+else print("outer"); @ @<Declare action procedures for use by |do←statement|@>= procedure do←protection; var @!m:0..1; {0 to unprotect, 1 to protect} @!t:halfword; {the |eq←type| before we change it} begin m:=cur←mod; repeat get←symbol; t:=eq←type(cur←sym); if m=0 then begin if t>=outer←tag then eq←type(cur←sym):=t-outer←tag; end else if t<outer←tag then eq←type(cur←sym):=t+outer←tag; get←x←next; until cur←cmd<>comma; end; @ \MF\ never defines the tokens `\.(' and `\.)' to be primitives, but plain \MF\ begins with the declaration `\&{delimiters} \.{()}'. Such a declaration assigns the command code |left←delimiter| to `\.{(}' and |right←delimiter| to `\.{)}'; the |equiv| of each delimiter is the hash address of its mate. @<Cases of |do←statement|...@>= delimiters: def←delims; @ @<Declare action procedures for use by |do←statement|@>= procedure def←delims; var l←delim,r←delim:pointer; {the new delimiter pair} begin get←clear←symbol; l←delim:=cur←sym;@/ get←clear←symbol; r←delim:=cur←sym;@/ eq←type(l←delim):=left←delimiter; equiv(l←delim):=r←delim;@/ eq←type(r←delim):=right←delimiter; equiv(r←delim):=l←delim;@/ get←x←next; end; @ Here is a procedure that is called when \MF\ has reached a point where some right delimiter is mandatory. @<Declare the procedure called |check←delimiter|@>= procedure check←delimiter(@!l←delim,@!r←delim:pointer); label exit; begin if cur←cmd=right←delimiter then if cur←mod=l←delim then return; if cur←sym<>r←delim then begin missing←err(text(r←delim));@/ @.Missing `)'@> help2("I found no right delimiter to match a left one. So I've")@/ ("put one in, behind the scenes; this may fix the problem."); back←error; end else begin print←err("The token `"); print(text(r←delim)); @.The token...delimiter@> print("' is no longer a right delimiter"); help3("Strange: This token has lost its former meaning!")@/ ("I'll read it as a right delimiter this time;")@/ ("but watch out, I'll probably miss it later."); error; end; exit:end; @ The next four commands save or change the values associated with tokens. @<Cases of |do←statement|...@>= save←command: repeat get←symbol; save←variable(cur←sym); get←x←next; until cur←cmd<>comma; interim←command: do←interim; let←command: do←let; new←internal: do←new←internal; @ @<Declare action procedures for use by |do←statement|@>= procedure@?do←statement; forward;@t\2@>@/ procedure do←interim; begin get←x←next; if cur←cmd<>internal←quantity then begin print←err("The token `"); @.The token...quantity@> if cur←sym=0 then print("(%CAPSULE)") else print(text(cur←sym)); print("' isn't an internal quantity"); help1("Something like `tracingonline' should follow `interim'."); back←error; end else begin save←internal(cur←mod); back←input; end; do←statement; end; @ The following procedure is careful not to undefine the left-hand symbol too soon, lest commands like `{\tt let x=x}' have a surprising effect. @<Declare action procedures for use by |do←statement|@>= procedure do←let; var @!l:pointer; {hash location of the left-hand symbol} begin get←symbol; l:=cur←sym; get←x←next; if cur←cmd<>equals then if cur←cmd<>assignment then begin missing←err("="); @.Missing `='@> help3("You should have said `let symbol = something'.")@/ ("But don't worry; I'll pretend that an equals sign")@/ ("was present. The next token I read will be `something'."); back←error; end; get←symbol; case cur←cmd of defined←macro,secondary←primary←macro,tertiary←secondary←macro, expression←tertiary←macro: add←mac←ref(cur←mod); othercases do←nothing endcases;@/ clear←symbol(l,false); eq←type(l):=cur←cmd; if cur←cmd=tag←token then equiv(l):=null else equiv(l):=cur←mod; get←x←next; end; @ @<Declare action procedures for use by |do←statement|@>= procedure do←new←internal; begin repeat if int←ptr=max←internal then overflow("number of internals",max←internal); @:METAFONT capacity exceeded number of int}{\quad number of internals@> get←clear←symbol; incr(int←ptr); eq←type(cur←sym):=internal←quantity; equiv(cur←sym):=int←ptr; int←name[int←ptr]:=text(cur←sym); internal[int←ptr]:=0; get←x←next; until cur←cmd<>comma; end; @ The various `\&{show}' commands are distinguished by modifier fields in the usual way. @d show←token←code=0 {show the meaning of a single token} @d show←stats←code=1 {show current memory and string usage} @d show←code=2 {show a list of expressions} @d show←var←code=3 {show a variable and its descendents} @d show←dependencies←code=4 {show dependent variables in terms of independents} @<Put each...@>= primitive("showtoken",show←command,show←token←code);@/ @!@:show←token←}{\&{showtoken} primitive@> primitive("showstats",show←command,show←stats←code);@/ @!@:show←stats←}{\&{showstats} primitive@> primitive("show",show←command,show←code);@/ @!@:show←}{\&{show} primitive@> primitive("showvariable",show←command,show←var←code);@/ @!@:show←var←}{\&{showvariable} primitive@> primitive("showdependencies",show←command,show←dependencies←code);@/ @!@:show←dependencies←}{\&{showdependencies} primitive@> @ @<Cases of |print←cmd...@>= show←command: case m of show←token←code:print("showtoken"); show←stats←code:print("showstats"); show←code:print("show"); show←var←code:print("showvariable"); othercases print("showdependencies") endcases; @ @<Cases of |do←statement|...@>= show←command:do←show←whatever; @ The value of |cur←mod| controls the |verbosity| in the |print←exp| routine: if it's |show←code|, complicated structures are abbreviated, otherwise they aren't. @<Declare action procedures for use by |do←statement|@>= procedure do←show; begin repeat get←x←next; scan←expression; print←nl(">> "); @.>>@> print←exp(null,2); flush←cur←exp(0); until cur←cmd<>comma; end; @ @<Declare action procedures for use by |do←statement|@>= procedure disp←token; begin print←nl("> "); @.>@> if cur←sym=0 then @<Show a numeric or string or capsule token@> else begin print(text(cur←sym)); print←char("="); if eq←type(cur←sym)>=outer←tag then print("(outer) "); print←cmd←mod(cur←cmd,cur←mod); if cur←cmd=defined←macro then begin print←ln; show←macro(cur←mod,null,100000); end; {this avoids recursion between |show←macro| and |print←cmd←mod|} @↑recursion@> end; end; @ @<Show a numeric or string or capsule token@>= begin if cur←cmd=numeric←token then print←scaled(cur←mod) else if cur←cmd=capsule←token then begin g←pointer:=cur←mod; print←capsule; end else begin print←char(""""); print(cur←mod); print←char(""""); delete←str←ref(cur←mod); end; end @ The following cases of |print←cmd←mod| might arise in connection with |disp←token|, although they don't correspond to any primitive tokens. @<Cases of |print←cmd←...@>= left←delimiter,right←delimiter: begin if c=left←delimiter then print("lef") else print("righ"); print("t delimiter that matches "); print(text(m)); end; tag←token:if m=null then print("tag")@+else print("variable"); defined←macro: print("macro:"); secondary←primary←macro,tertiary←secondary←macro,expression←tertiary←macro: begin print←cmd←mod(macro←def,c); print("'d macro:"); print←ln; show←token←list(link(link(m)),null,1000,0); end; repeat←loop:print("[repeat the loop]"); internal←quantity:print(int←name[m]); @ @<Declare action procedures for use by |do←statement|@>= procedure do←show←token; begin repeat get←next; disp←token; get←x←next; until cur←cmd<>comma; end; @ @<Declare action procedures for use by |do←statement|@>= procedure do←show←stats; begin print←nl("Memory usage "); @.Memory usage...@> @!stat print←int(var←used); print←char("&"); print←int(dyn←used); if false then@+tats@t@>@;@/ print("unknown"); print(" ("); print←int(hi←mem←min-lo←mem←max-1); print(" still untouched)"); print←ln; print←nl("String usage "); print←int(str←ptr-init←str←ptr); print←char("&"); print←int(pool←ptr-init←pool←ptr); print(" ("); print←int(max←strings-max←str←ptr); print←char("&"); print←int(pool←size-max←pool←ptr); print(" still untouched)"); print←ln; get←x←next; end; @ Here's a recursive procedure that gives an abbreviated account of a variable, for use by |do←show←var|. @<Declare action procedures for use by |do←statement|@>= procedure disp←var(@!p:pointer); var @!q:pointer; {traverses attributes and subscripts} @!n:0..max←print←line; {amount of macro text to show} begin if type(p)=structured then @<Descend the structure@> else if type(p)>=unsuffixed←macro then @<Display a variable macro@> else if type(p)<>undefined then begin print←nl(""); print←variable←name(p); print←char("="); print←exp(p,0); end; end; @ @<Descend the structure@>= begin q:=attr←head(p); repeat disp←var(q); q:=link(q); until q=end←attr; q:=subscr←head(p); while name←type(q)=subscr do begin disp←var(q); q:=link(q); end; end @ @<Display a variable macro@>= begin print←nl(""); print←variable←name(p); if type(p)>unsuffixed←macro then print("@@#"); {|suffixed←macro|} print("=macro:"); if file←offset>=max←print←line-20 then n:=5 else n:=max←print←line-file←offset-15; show←macro(value(p),null,n); end @ @<Declare action procedures for use by |do←statement|@>= procedure do←show←var; label done; begin repeat get←next; if cur←sym>0 then if cur←sym<=hash←end then if cur←cmd=tag←token then if cur←mod<>null then begin disp←var(cur←mod); goto done; end; disp←token; done:get←x←next; until cur←cmd<>comma; end; @ @<Declare action procedures for use by |do←statement|@>= procedure do←show←dependencies; var @!p:pointer; {link that runs through all dependencies} begin p:=link(dep←head); while p<>dep←head do begin if interesting(p) then begin print←nl(""); print←variable←name(p); if type(p)=dependent then print←char("=") else print(" = "); {extra spaces imply proto-dependency} print←dependency(dep←list(p),type(p)); end; p:=dep←list(p); while info(p)<>null do p:=link(p); p:=link(p); end; get←x←next; end; @ Finally we are ready for the procedure that governs all of the show commands. @<Declare action procedures for use by |do←statement|@>= procedure do←show←whatever; begin if interaction=error←stop←mode then wake←up←terminal; case cur←mod of show←token←code:do←show←token; show←stats←code:do←show←stats; show←code:do←show; show←var←code:do←show←var; show←dependencies←code:do←show←dependencies; end; {there are no other cases} if internal[showstopping]>0 then begin print←err("OK"); @.OK@> if interaction<error←stop←mode then begin help0; decr(error←count); end else help1("This isn't an error message; I'm just showing something."); if cur←cmd=semicolon then error@+else put←get←error; end; end; @ The `\&{addto}' command needs the following additional primitives: @d drop←code=0 {command modifier for `\&{dropping}'} @d keep←code=1 {command modifier for `\&{keeping}'} @<Put each...@>= primitive("contour",thing←to←add,contour←code);@/ @!@:contour←}{\&{contour} primitive@> primitive("doublepath",thing←to←add,double←path←code);@/ @!@:double←path←}{\&{doublepath} primitive@> primitive("also",thing←to←add,also←code);@/ @!@:also←}{\&{also} primitive@> primitive("withpen",with←option,pen←type);@/ @!@:with←pen←}{\&{withpen} primitive@> primitive("withweight",with←option,known);@/ @!@:with←weight←}{\&{withweight} primitive@> primitive("dropping",cull←op,drop←code);@/ @!@:dropping←}{\&{dropping} primitive@> primitive("keeping",cull←op,keep←code);@/ @!@:keeping←}{\&{keeping} primitive@> @ @<Cases of |print←cmd...@>= thing←to←add:if m=contour←code then print("contour") else if m=double←path←code then print("doublepath") else print("also"); with←option:if m=pen←type then print("withpen") else print("withweight"); cull←op:if m=drop←code then print("dropping") else print("keeping"); @ @<Declare action procedures for use by |do←statement|@>= function scan←with:boolean; var @!t:small←number; {|known| or |pen←type|} @!result:boolean; {the value to return} begin t:=cur←mod; cur←type:=vacuous; get←x←next; scan←expression; result:=false; if cur←type<>t then @<Complain about improper type@> else if cur←type=pen←type then result:=true else @<Check the tentative weight@>; scan←with:=result; end; @ @<Complain about improper type@>= begin exp←err("Improper type"); @.Improper type@> help2("Next time say `withweight <known numeric expression>';")@/ ("I'll ignore the bad `with' clause and look for another."); if t=pen←type then help←line[1]:="Next time say `withpen <known pen expression>';"; put←get←flush←error(0); end @ @<Check the tentative weight@>= begin cur←exp:=round←unscaled(cur←exp); if (abs(cur←exp)<4)and(cur←exp<>0) then result:=true else begin print←err("Weight must be -3, -2, -1, +1, +2, or +3"); @.Weight must be...@> help1("I'll ignore the bad `with' clause and look for another."); put←get←flush←error(0); end; end @ One of the things we need to do when we've parsed an \&{addto} or similar command is set |cur←edges| to the header of a supposed \&{picture} variable, given a token list for that variable. @<Declare action procedures for use by |do←statement|@>= procedure find←edges←var(@!t:pointer); var @!p:pointer; begin p:=find←variable(t); cur←edges:=null; if p=null then begin obliterated(t); put←get←error; end else if type(p)<>picture←type then begin print←err("Variable "); show←token←list(t,null,1000,0); @.Variable x is the wrong type@> print(" is the wrong type ("); print←type(type(p)); print←char(")"); help2("I was looking for a ""known"" picture variable.")@/ ("So I'll not change anything just now."); put←get←error; end else cur←edges:=value(p); flush←node←list(t); end; @ @<Cases of |do←statement|...@>= add←to←command: do←add←to; @ @<Declare action procedures for use by |do←statement|@>= procedure do←add←to; label done, not←found; var @!lhs,@!rhs:pointer; {variable on left, path on right} @!t:small←number; {variant of |with←option|} @!w:integer; {tentative weight} @!p:pointer; {list manipulation register} @!q:pointer; {beginning of second half of doubled path} begin get←x←next; var←flag:=thing←to←add; scan←primary; if cur←type<>token←list then @<Abandon edges command because there's no variable@> else begin lhs:=cur←exp; cur←path←type:=cur←mod;@/ cur←type:=vacuous; get←x←next; scan←expression; if cur←path←type=also←code then @<Augment some edges by others@> else @<Get ready to fill a contour, and fill it@>; end; end; @ @<Abandon edges command because there's no variable@>= begin exp←err("Not a suitable variable"); @.Not a suitable variable@> help4("At this point I needed to see the name of a picture variable.")@/ ("(Or perhaps you have indeed presented me with one; I might")@/ ("have missed it, if it wasn't followed by the proper token.)")@/ ("So I'll not change anything just now."); put←get←flush←error(0); end @ @<Augment some edges by others@>= begin find←edges←var(lhs); if cur←edges=null then flush←cur←exp(0) else if cur←type<>picture←type then begin exp←err("Improper `addto'"); @.Improper `addto'@> help2("This expression should have specified a known picture.")@/ ("So I'll not change anything just now."); put←get←flush←error(0); end else begin merge←edges(cur←exp); flush←cur←exp(0); end; end @ @<Get ready to fill a contour...@>= begin if cur←type=pair←type then pair←to←path; if cur←type<>path←type then begin exp←err("Improper `addto'"); @.Improper `addto'@> help2("This expression should have been a known path.")@/ ("So I'll not change anything just now."); put←get←flush←error(0); flush←token←list(lhs); end else begin rhs:=cur←exp; w:=1; cur←pen:=null←pen; while cur←cmd=with←option do if scan←with then if cur←type=known then w:=cur←exp else @<Change the tentative pen@>; @<Complete the contour filling operation@>; delete←pen←ref(cur←pen); end; end @ We could say `|add←pen←ref(cur←pen)|; |flush←cur←exp(0)|' after changing |cur←pen| here. But that would have no effect, because the current expression will not be flushed. Thus we save a bit of code (at the risk of being too tricky). @<Change the tentative pen@>= begin delete←pen←ref(cur←pen); cur←pen:=cur←exp; end @ @<Complete the contour filling...@>= find←edges←var(lhs); if cur←edges=null then toss←knot←list(rhs) else begin lhs:=null; if left←type(rhs)=endpoint then if cur←path←type=double←path←code then @<Double the path@> else @<Complain about non-cycle and |goto not←found|@> else if cur←path←type=double←path←code then lhs:=htap←ypoc(rhs); cur←wt:=w; rhs:=make←spec(rhs,max←offset(cur←pen),internal[tracing←specs]); @<Check the turning number@>; if max←offset(cur←pen)=0 then fill←spec(rhs) else fill←envelope(rhs); if lhs<>null then begin rev←turns:=true; lhs:=make←spec(lhs,max←offset(cur←pen),internal[tracing←specs]); rev←turns:=false; if max←offset(cur←pen)=0 then fill←spec(lhs) else fill←envelope(lhs); end; not←found: end @ @<Double the path@>= if link(rhs)=rhs then @<Make a trivial one-point path cycle@> else begin p:=htap←ypoc(rhs); q:=link(p);@/ right←x(path←tail):=right←x(q); right←y(path←tail):=right←y(q); right←type(path←tail):=right←type(q); link(path←tail):=link(q); free←node(q,knot←node←size);@/ right←x(p):=right←x(rhs); right←y(p):=right←y(rhs); right←type(p):=right←type(rhs); link(p):=link(rhs); free←node(rhs,knot←node←size);@/ rhs:=p; end @ @<Make a trivial one-point path cycle@>= begin right←x(rhs):=x←coord(rhs); right←y(rhs):=y←coord(rhs); left←x(rhs):=x←coord(rhs); left←y(rhs):=y←coord(rhs); left←type(rhs):=explicit; right←type(rhs):=explicit; end @ @<Complain about non-cycle...@>= begin print←err("Not a cycle"); @.Not a cycle@> help2("That contour should have ended with `..cycle' or `&cycle'.")@/ ("So I'll not change anything just now."); put←get←error; toss←knot←list(rhs); goto not←found; end @ @<Check the turning number@>= if turning←number<=0 then if cur←path←type<>double←path←code then if internal[turning←check]>0 then if (turning←number<0)and(link(cur←pen)=null) then negate(cur←wt) else begin if turning←number=0 then if (internal[turning←check]<=unity)and(link(cur←pen)=null) then goto done else print←strange("Strange path (turning number is zero)") @.Strange path...@> else print←strange("Backwards path (turning number is negative)"); @.Backwards path...@> help3("The path doesn't have a counterclockwise orientation,")@/ ("so I'll probably have trouble drawing it.")@/ ("(See Chapter 27 of The METAFONTbook for more help.)"); @:METAFONTbook}{\sl The {\logos METAFONT\/}book@> put←get←error; end; done: @ @<Cases of |do←statement|...@>= ship←out←command: do←ship←out; display←command: do←display; open←window: do←open←window; cull←command: do←cull; @ @<Declare action procedures for use by |do←statement|@>= @t\4@>@<Declare the function called |tfm←check|@>@; procedure do←ship←out; label exit; var @!c:integer; {the character code} begin get←x←next; var←flag:=semicolon; scan←expression; if cur←type<>token←list then if cur←type=picture←type then cur←edges:=cur←exp else begin @<Abandon edges command because there's no variable@>; return; end else begin find←edges←var(cur←exp); cur←type:=vacuous; end; if cur←edges<>null then begin c:=round←unscaled(internal[char←code]) mod 256; if c<0 then c:=c+256; @<Store the width information for character code~|c|@>; if internal[proofing]>=0 then ship←out(c); end; flush←cur←exp(0); exit:end; @ @<Declare action procedures for use by |do←statement|@>= procedure do←display; label not←found,common←ending,exit; var @!e:pointer; {token list for a picture variable} begin get←x←next; var←flag:=in←window; scan←primary; if cur←type<>token←list then @<Abandon edges command because there's no variable@> else begin e:=cur←exp; cur←type:=vacuous; get←x←next; scan←expression; if cur←type<>known then goto common←ending; cur←exp:=round←unscaled(cur←exp); if cur←exp<0 then goto not←found; if cur←exp>15 then goto not←found; if not window←open[cur←exp] then goto not←found; find←edges←var(e); if cur←edges<>null then disp←edges(cur←exp); return; not←found: cur←exp:=cur←exp*unity; common←ending: exp←err("Bad window number"); @.Bad window number@> help1("It should be the number of an open window."); put←get←flush←error(0); flush←token←list(e); end; exit:end; @ The only thing difficult about `\&{openwindow}' is that the syntax allows the user to go astray in many ways. The following subroutine helps keep the necessary program reasonably short. @<Declare action procedures for use by |do←statement|@>= function get←pair(@!c:command←code):boolean; var @!p:pointer; {a pair of values that are known (we hope)} @!b:boolean; {did we find such a pair?} begin if cur←cmd<>c then get←pair:=false else begin get←x←next; scan←expression; if nice←pair(cur←exp,cur←type) then begin p:=value(cur←exp); cur←x:=value(x←part←loc(p)); cur←y:=value(y←part←loc(p)); b:=true; end else b:=false; flush←cur←exp(0); get←pair:=b; end; end; @ @<Declare action procedures for use by |do←statement|@>= procedure do←open←window; label not←found,exit; var @!k:integer; {the window number in question} @!r0,@!c0,@!r1,@!c1:scaled; {window coordinates} begin get←x←next; scan←expression; if cur←type<>known then goto not←found; k:=round←unscaled(cur←exp); if k<0 then goto not←found; if k>15 then goto not←found; if not get←pair(from←token) then goto not←found; r0:=cur←x; c0:=cur←y; if not get←pair(to←token) then goto not←found; r1:=cur←x; c1:=cur←y; if not get←pair(at←token) then goto not←found; open←a←window(k,r0,c0,r1,c1,cur←x,cur←y); return; not←found:print←err("Improper `openwindow'"); @.Improper `openwindow'@> help2("Say `openwindow k from (r0,c0) to (r1,c1) at (x,y)',")@/ ("where all quantities are known and k is between 0 and 15."); put←get←error; exit:end; @ @<Declare action procedures for use by |do←statement|@>= procedure do←cull; label not←found,exit; var @!e:pointer; {token list for a picture variable} @!keeping:drop←code..keep←code; {modifier of |cull←op|} @!w,@!w←in,@!w←out:integer; {culling weights} begin w:=1; get←x←next; var←flag:=cull←op; scan←primary; if cur←type<>token←list then @<Abandon edges command because there's no variable@> else begin e:=cur←exp; cur←type:=vacuous; keeping:=cur←mod; if not get←pair(cull←op) then goto not←found; while (cur←cmd=with←option)and(cur←mod=known) do if scan←with then w:=cur←exp; @<Set up the culling weights, or |goto not←found| if the thresholds are bad@>; find←edges←var(e); if cur←edges<>null then cull←edges(floor←unscaled(cur←x+unity-1),floor←unscaled(cur←y),w←out,w←in); return; not←found: print←err("Bad culling amounts"); @.Bad culling amounts@> help1("Always cull by known amounts that exclude 0."); put←get←error; flush←token←list(e); end; exit:end; @ @<Set up the culling weights, or |goto not←found| if the thresholds are bad@>= if cur←x>cur←y then goto not←found; if keeping=drop←code then begin if (cur←x>0)or(cur←y<0) then goto not←found; w←out:=w; w←in:=0; end else begin if (cur←x<=0)and(cur←y>=0) then goto not←found; w←out:=0; w←in:=w; end @ The \&{everyjob} command simply assigns a nonzero value to the global variable |start←sym|. @<Cases of |do←statement|...@>= every←job←command: begin get←symbol; start←sym:=cur←sym; get←x←next; end; @ @<Glob...@>= @!start←sym:halfword; {a symbolic token to insert at beginning of job} @ @<Set init...@>= start←sym:=0; @ Finally, we have only the ``message'' commands remaining. @d message←code=0 @d err←message←code=1 @d err←help←code=2 @<Put each...@>= primitive("message",message←command,message←code);@/ @!@:message←}{\&{message} primitive@> primitive("errmessage",message←command,err←message←code);@/ @!@:err←message←}{\&{errmessage} primitive@> primitive("errhelp",message←command,err←help←code);@/ @!@:err←help←}{\&{errhelp} primitive@> @ @<Cases of |print←cmd...@>= message←command: if m<err←message←code then print("message") else if m=err←message←code then print("errmessage") else print("errhelp"); @ @<Cases of |do←statement|...@>= message←command: do←message; @ @<Declare action procedures for use by |do←statement|@>= procedure do←message; var @!m:message←code..err←help←code; {the type of message} begin m:=cur←mod; get←x←next; scan←expression; if cur←type<>string←type then begin exp←err("Not a string"); @.Not a string@> help1("A message should be a known string expression."); put←get←error; end else case m of message←code:begin print←nl(""); slow←print(cur←exp); end; err←message←code:@<Print string |cur←exp| as an error message@>; err←help←code:@<Save string |cur←exp| as the |err←help|@>; end; {there are no other cases} flush←cur←exp(0); end; @ The global variable |err←help| is zero when the user has most recently given an empty help string, or if none has ever been given. @<Save string |cur←exp| as the |err←help|@>= begin if err←help<>0 then delete←str←ref(err←help); if length(cur←exp)=0 then err←help:=0 else begin err←help:=cur←exp; add←str←ref(err←help); end; end @ If \&{errmessage} occurs often in |scroll←mode|, without user-defined \&{errhelp}, we don't want to give a long help message each time. So we give a verbose explanation only once. @<Glob...@>= @!long←help←seen:boolean; {has the long \.{\\errmessage} help been used?} @ @<Set init...@>=long←help←seen:=false; @ @<Print string |cur←exp| as an error message@>= begin print←err(""); slow←print(cur←exp); if err←help<>0 then use←err←help:=true else if long←help←seen then help1("(That was another `errmessage'.)") else begin if interaction<error←stop←mode then long←help←seen:=true; help4("This error message was generated by an `errmessage'")@/ ("command, so I can't give any explicit help.")@/ ("Pretend that you're Miss Marple, examine all clues,")@/ @↑Marple, Jane@> ("and deduce the truth by inspired guesses."); end; put←get←error; use←err←help:=false; end @* \[45] Font metric data. \TeX\ gets its knowledge about fonts from font metric files, also called \.{TFM} files; the `\.T' in `\.{TFM}' stands for \TeX, but other programs know about them too. One of \MF's duties is to write \.{TFM} files so that the user's fonts can readily be applied to typesetting. @:TFM files}{\.{TFM} files@> @↑font metric files@> The information in a \.{TFM} file appears in a sequence of 8-bit bytes. Since the number of bytes is always a multiple of~4, we could also regard the file as a sequence of 32-bit words, but \MF\ uses the byte interpretation. The format of \.{TFM} files was designed by Lyle Ramshaw in 1980. The intent is to convey a lot of different kinds @↑Ramshaw, Lyle Harold@> of information in a compact but useful form. @<Glob...@>= @!tfm←file:byte←file; {the font metric output goes here} @!metric←file←name: str←number; {full name of the font metric file} @ The first 24 bytes (6 words) of a \.{TFM} file contain twelve 16-bit integers that give the lengths of the various subsequent portions of the file. These twelve integers are, in order: $$\vbox{\halign{\hfil#&$\null=\null$#\hfil\cr |lf|&length of the entire file, in words;\cr |lh|&length of the header data, in words;\cr |bc|&smallest character code in the font;\cr |ec|&largest character code in the font;\cr |nw|&number of words in the width table;\cr |nh|&number of words in the height table;\cr |nd|&number of words in the depth table;\cr |ni|&number of words in the italic correction table;\cr |nl|&number of words in the lig/kern table;\cr |nk|&number of words in the kern table;\cr |ne|&number of words in the extensible character table;\cr |np|&number of font parameter words.\cr}}$$ They are all nonnegative and less than $2↑{15}$. We must have |bc-1<=ec<=255|, |ne<=256|, and $$\hbox{|lf=6+lh+(ec-bc+1)+nw+nh+nd+ni+nl+nk+ne+np|.}$$ Note that a font may contain as many as 256 characters (if |bc=0| and |ec=255|), and as few as 0 characters (if |bc=ec+1|). Incidentally, when two or more 8-bit bytes are combined to form an integer of 16 or more bits, the most significant bytes appear first in the file. This is called BigEndian order. @!@↑BigEndian order@> @ The rest of the \.{TFM} file may be regarded as a sequence of ten data arrays having the informal specification $$\def\arr$[#1]#2${\&{array} $[#1]$ \&{of} #2} \vbox{\halign{\hfil\\{#}&$\,:\,$\arr#\hfil\cr header&|[0..lh-1]@t\\{stuff}@>|\cr char\←info&|[bc..ec]char←info←word|\cr width&|[0..nw-1]fix←word|\cr height&|[0..nh-1]fix←word|\cr depth&|[0..nd-1]fix←word|\cr italic&|[0..ni-1]fix←word|\cr lig\←kern&|[0..nl-1]lig←kern←command|\cr kern&|[0..nk-1]fix←word|\cr exten&|[0..ne-1]extensible←recipe|\cr param&|[1..np]fix←word|\cr}}$$ The most important data type used here is a |@!fix←word|, which is a 32-bit representation of a binary fraction. A |fix←word| is a signed quantity, with the two's complement of the entire word used to represent negation. Of the 32 bits in a |fix←word|, exactly 12 are to the left of the binary point; thus, the largest |fix←word| value is $2048-2↑{-20}$, and the smallest is $-2048$. We will see below, however, that all but two of the |fix←word| values must lie between $-16$ and $+16$. @ The first data array is a block of header information, which contains general facts about the font. The header must contain at least two words, |header[0]| and |header[1]|, whose meaning is explained below. Additional header information of use to other software routines might also be included, and \MF\ will generate it if the \.{headerbyte} command occurs. For example, 16 more words of header information are in use at the Xerox Palo Alto Research Center; the first ten specify the character coding scheme used (e.g., `\.{XEROX TEXT}' or `\.{TEX MATHSY}'), the next five give the font family name (e.g., `\.{HELVETICA}' or `\.{CMSY}'), and the last gives the ``face byte.'' Extensions of \TeX\ for oriental languages should be able to identify oriental fonts by means of this additional header information. @↑oriental characters@>@↑Chinese characters@>@↑Japanese characters@> \yskip\hang|header[0]| is a 32-bit check sum that \MF\ will copy into the \.{GF} output file. This helps ensure consistency between files, since \TeX\ records the check sums from the \.{TFM}'s it reads, and these should match the check sums on actual fonts that are used. The actual relation between this check sum and the rest of the \.{TFM} file is not important; the check sum is simply an identification number with the property that incompatible fonts almost always have distinct check sums. @↑check sum@> \yskip\hang|header[1]| is a |fix←word| containing the design size of the font, in units of \MF\ points. This number must be at least 1.0; it is fairly arbitrary, but usually the design size is 10.0 for a ``10 point'' font, i.e., a font that was designed to look best at a 10-point size, whatever that really means. When a \TeX\ user asks for a font `\.{at} $\delta$ \.{pt}', the effect is to override the design size and replace it by $\delta$, and to multiply the $x$ and~$y$ coordinates of the points in the font image by a factor of $\delta$ divided by the design size. {\sl All other dimensions in the\/ \.{TFM} file are |fix←word|\kern-1pt\ numbers in design-size units.} Thus, for example, the value of |param[6]|, which defines the \.{em} unit, is often the |fix←word| value $2↑{20}=1.0$, since many fonts have a design size equal to one em. The other dimensions must be less than 16 design-size units in absolute value; thus, |header[1]| and |param[1]| are the only |fix←word| entries in the whole \.{TFM} file whose first byte might be something besides 0 or 255. @ Next comes the |char←info| array, which contains one |@!char←info←word| per character. Each word in this part of the file contains six fields packed into four bytes as follows. \yskip\hang first byte: |@!width←index| (8 bits)\par \hang second byte: |@!height←index| (4 bits) times 16, plus |@!depth←index| (4~bits)\par \hang third byte: |@!italic←index| (6 bits) times 4, plus |@!tag| (2~bits)\par \hang fourth byte: |@!remainder| (8 bits)\par \yskip\noindent The actual width of a character is \\{width}|[width←index]|, in design-size units; this is a device for compressing information, since many characters have the same width. Since it is quite common for many characters to have the same height, depth, or italic correction, the \.{TFM} format imposes a limit of 16 different heights, 16 different depths, and 64 different italic corrections. @!@↑italic correction@> The italic correction of a character has two different uses. (a)~In ordinary text, the italic correction is added to the width only if the \TeX\ user specifies `\.{\\/}' after the character. (b)~In math formulas, the italic correction is always added to the width, unless the character has a subscript but no superscript. Incidentally, the relation $\\{width}[0]=\\{height}[0]=\\{depth}[0]= \\{italic}[0]=0$ should always hold, so that an index of zero implies a value of zero. The |width←index| should never be zero unless the character does not exist in the font, since a character is valid if and only if it lies between |bc| and |ec| and has a nonzero |width←index|. @ The |tag| field in a |char←info←word| has four values that explain how to interpret the |remainder| field. \yskip\hang|tag=0| (|no←tag|) means that |remainder| is unused.\par \hang|tag=1| (|lig←tag|) means that this character has a ligature/kerning program starting at |lig←kern[remainder]|.\par \hang|tag=2| (|list←tag|) means that this character is part of a chain of characters of ascending sizes, and not the largest in the chain. The |remainder| field gives the character code of the next larger character.\par \hang|tag=3| (|ext←tag|) means that this character code represents an extensible character, i.e., a character that is built up of smaller pieces so that it can be made arbitrarily large. The pieces are specified in |@!exten[remainder]|.\par \yskip\noindent Characters with |tag=2| and |tag=3| are treated as characters with |tag=0| unless they are used in special circumstances in math formulas. For example, \TeX's \.{\\sum} operation looks for a |list←tag|, and the \.{\\left} operation looks for both |list←tag| and |ext←tag|. @d no←tag=0 {vanilla character} @d lig←tag=1 {character has a ligature/kerning program} @d list←tag=2 {character has a successor in a charlist} @d ext←tag=3 {character is extensible} @ The |lig←kern| array contains instructions in a simple programming language that explains what to do for special letter pairs. Each word in this array is a |@!lig←kern←command| of four bytes. \yskip\hang first byte: |stop←bit|, indicates the final program step if the byte is 128 or more.\par \hang second byte: |next←char|, ``if |next←char| follows the current character, then perform the operation and stop, otherwise continue.''\par \hang third byte: |op←bit|, indicates a ligature step if less than~128, a kern step otherwise.\par \hang fourth byte: |remainder|.\par \yskip\noindent In a ligature step the current character and |next←char| are replaced by the single character whose code is |remainder|. In a kern step, an additional space equal to |@!kern[remainder]| is inserted between the current character and |next←char|. (The value of |kern[remainder]| is often negative, so that the characters are brought closer together by kerning; but it might be positive.) @d stop←flag=128+min←quarterword {value indicating `\.{STOP}' in a lig/kern program} @d kern←flag=128+min←quarterword {op code for a kern step} @d stop←bit(#)==lig←kern[#].b0 @d next←char(#)==lig←kern[#].b1 @d op←bit(#)==lig←kern[#].b2 @d rem←byte(#)==lig←kern[#].b3 @ Extensible characters are specified by an |@!extensible←recipe|, which consists of four bytes called |@!top|, |@!mid|, |@!bot|, and |@!rep| (in this order). These bytes are the character codes of individual pieces used to build up a large symbol. If |top|, |mid|, or |bot| are zero, they are not present in the built-up result. For example, an extensible vertical line is like an extensible bracket, except that the top and bottom pieces are missing. Let $T$, $M$, $B$, and $R$ denote the respective pieces, or an empty box if the piece isn't present. Then the extensible characters have the form $TR↑kMR↑kB$ from top to bottom, for some |k>=0|, unless $M$ is absent; in the latter case we can have $TR↑kB$ for both even and odd values of~|k|. The width of the extensible character is the width of $R$; and the height-plus-depth is the sum of the individual height-plus-depths of the components used, since the pieces are butted together in a vertical list. @d ext←top(#)==exten[#].b0 {|top| piece in a recipe} @d ext←mid(#)==exten[#].b1 {|mid| piece in a recipe} @d ext←bot(#)==exten[#].b2 {|bot| piece in a recipe} @d ext←rep(#)==exten[#].b3 {|rep| piece in a recipe} @ The final portion of a \.{TFM} file is the |param| array, which is another sequence of |fix←word| values. \yskip\hang|param[1]=slant| is the amount of italic slant, which is used to help position accents. For example, |slant=.25| means that when you go up one unit, you also go .25 units to the right. The |slant| is a pure number; it is the only |fix←word| other than the design size itself that is not scaled by the design size. \hang|param[2]=space| is the normal spacing between words in text. Note that character @'40 in the font need not have anything to do with blank spaces. \hang|param[3]=space←stretch| is the amount of glue stretching between words. \hang|param[4]=space←shrink| is the amount of glue shrinking between words. \hang|param[5]=x←height| is the size of one ex in the font; it is also the height of letters for which accents don't have to be raised or lowered. \hang|param[6]=quad| is the size of one em in the font. \hang|param[7]=extra←space| is the amount added to |param[2]| at the ends of sentences. \yskip\noindent If fewer than seven parameters are present, \TeX\ sets the missing parameters to zero. @d slant←code=1 @d space←code=2 @d space←stretch←code=3 @d space←shrink←code=4 @d x←height←code=5 @d quad←code=6 @d extra←space←code=7 @ So that is what \.{TFM} files hold. One of \MF's duties is to output such information, and it does this all at once at the end of a job. In order to prepare for such frenetic activity, it squirrels away the necessary facts in various arrays as they become available. Character dimensions (\&{charwd}, \&{charht}, \&{chardp}, and \&{charic}) are stored respectively in |tfm←width|, |tfm←height|, |tfm←depth|, and |tfm←ital←corr|. Other information about a character (e.g., about its ligatures or successors) is accessible via the |char←tag| and |char←remainder| arrays. Other information about the font as a whole is kept in additional arrays called |header←byte|, |lig←kern|, |kern|, |exten|, and |param|. @<Glob...@>= @!bc,@!ec:eight←bits; {smallest and largest character codes shipped out} @!tfm←width:array[eight←bits] of scaled; {\&{charwd} values} @!tfm←height:array[eight←bits] of scaled; {\&{charht} values} @!tfm←depth:array[eight←bits] of scaled; {\&{chardp} values} @!tfm←ital←corr:array[eight←bits] of scaled; {\&{charic} values} @!char←exists:array[eight←bits] of boolean; {has this code been shipped out?} @!char←tag:array[eight←bits] of no←tag..ext←tag; {|remainder| category} @!char←remainder:array[eight←bits] of eight←bits; {the |remainder| byte} @!header←byte:array[1..header←size] of -1..255; {bytes of the \.{TFM} header, or $-1$ if unset} @!lig←kern:array[0..lig←table←size] of four←quarters; {the ligature/kern table} @!nl:0..lig←table←size; {the number of ligature/kern steps so far} @!kern:array[eight←bits] of scaled; {distinct kerning amounts} @!nk:0..256; {the number of distinct kerns so far} @!exten:array[eight←bits] of four←quarters; {extensible character recipes} @!ne:0..256; {the number of extensible characters so far} @!param:array[1..max←font←dimen] of scaled; {\&{fontinfo} parameters} @!np:0..max←font←dimen; {the largest \&{fontinfo} parameter specified so far} @!nw,@!nh,@!nd,@!ni:0..256; {sizes of \.{TFM} subtables} @ @<Set init...@>= for k:=0 to 255 do begin tfm←width[k]:=0; tfm←height[k]:=0; tfm←depth[k]:=0; tfm←ital←corr[k]:=0; char←exists[k]:=false; char←tag[k]:=no←tag; char←remainder[k]:=0; end; for k:=1 to header←size do header←byte[k]:=-1; bc:=255; ec:=0; nl:=0; nk:=0; ne:=0; np:=0; @ @<Declare the function called |tfm←check|@>= function tfm←check(@!m:small←number):scaled; begin if abs(internal[m])>=fraction←half then begin print←err("Enormous "); print(int←name[m]); @.Enormous charwd...@> @.Enormous chardp...@> @.Enormous charht...@> @.Enormous charic...@> @.Enormous designsize...@> print(" has been reduced"); help1("Font metric dimensions must be less than 2048pt."); put←get←error; if internal[m]>0 then tfm←check:=fraction←half-1 else tfm←check:=1-fraction←half; end else tfm←check:=internal[m]; end; @ @<Store the width information for character code~|c|@>= if c<bc then bc:=c; if c>ec then ec:=c; char←exists[c]:=true; gf←dx[c]:=internal[char←dx]; gf←dy[c]:=internal[char←dy]; tfm←width[c]:=tfm←check(char←wd); tfm←height[c]:=tfm←check(char←ht); tfm←depth[c]:=tfm←check(char←dp); tfm←ital←corr[c]:=tfm←check(char←ic) @ Now let's consider \MF's special \.{TFM}-oriented commands. @<Cases of |do←statement|...@>= tfm←command: do←tfm←command; @ @d char←list←code=0 @d lig←table←code=1 @d extensible←code=2 @d header←byte←code=3 @d font←dimen←code=4 @<Put each...@>= primitive("charlist",tfm←command,char←list←code);@/ @!@:char←list←}{\&{charlist} primitive@> primitive("ligtable",tfm←command,lig←table←code);@/ @!@:lig←table←}{\&{ligtable} primitive@> primitive("extensible",tfm←command,extensible←code);@/ @!@:extensible←}{\&{extensible} primitive@> primitive("headerbyte",tfm←command,header←byte←code);@/ @!@:header←byte←}{\&{headerbyte} primitive@> primitive("fontdimen",tfm←command,font←dimen←code);@/ @!@:font←dimen←}{\&{fontdimen} primitive@> @ @<Cases of |print←cmd...@>= tfm←command: case m of char←list←code:print("charlist"); lig←table←code:print("ligtable"); extensible←code:print("extensible"); header←byte←code:print("headerbyte"); othercases print("fontdimen") endcases; @ @<Declare action procedures for use by |do←statement|@>= function get←code:eight←bits; {scans a character code value} label found; var @!c:integer; {the code value found} begin get←x←next; scan←expression; if cur←type=known then begin c:=round←unscaled(cur←exp); if c>=0 then if c<256 then goto found; end else if cur←type=string←type then if length(cur←exp)=1 then begin c:=str←pool[str←start[cur←exp]]; goto found; end; exp←err("Invalid code has been replaced by 0"); @.Invalid code...@> help2("I was looking for a number between 0 and 255, or for a")@/ ("string of length 1. Didn't find it; will use 0 instead."); put←get←flush←error(0); c:=0; found: get←code:=c; end; @ @<Declare action procedures for use by |do←statement|@>= procedure set←tag(@!c:eight←bits;@!t:small←number;@!r:eight←bits); begin if char←tag[c]=no←tag then begin char←tag[c]:=t; char←remainder[c]:=r; end else @<Complain about a character tag conflict@>; end; @ @<Complain about a character tag conflict@>= begin print←err("Character "); if (c>" ")and(c<128) then print(c) else begin print("code "); print←int(c); end; print(" is already "); @.Character c is already...@> case char←tag[c] of lig←tag: print("in a ligtable"); list←tag: print("in a charlist"); ext←tag: print("extensible"); end; {there are no other cases} help2("It's not legal to label a character more than once.")@/ ("So I'll not change anything just now."); put←get←error; end @ @<Declare action procedures for use by |do←statement|@>= procedure do←tfm←command; label continue; var @!c,@!cc:eight←bits; {character codes} @!k:0..256; {index into the |kern| array} @!j:integer; {index into |header←byte| or |param|} begin case cur←mod of char←list←code: @<Store a list of character successors@>; lig←table←code: @<Store a list of ligature/kern steps@>; extensible←code: @<Define an extensible recipe@>; header←byte←code, font←dimen←code: begin c:=cur←mod; get←x←next; scan←expression; if (cur←type<>known)or(cur←exp<half←unit) then begin exp←err("Improper location"); @.Improper location@> help2("I was looking for a known, positive number.")@/ ("For safety's sake I'll ignore the present command."); put←get←error; end else begin j:=round←unscaled(cur←exp); if cur←cmd<>colon then begin missing←err(":"); @.Missing `:'@> help1("A colon should follow a headerbyte or fontinfo location."); back←error; end; if c=header←byte←code then @<Store a list of header bytes@> else @<Store a list of font dimensions@>; end; end; end; {there are no other cases} end; @ @<Store a list of character successors@>= begin c:=get←code; while cur←cmd=colon do begin cc:=get←code; set←tag(c,list←tag,cc); c:=cc; end; end @ @<Store a list of ligature/kern steps@>= begin continue: c:=get←code; if cur←cmd=colon then @<Record the beginning of a lig/kern subprogram and |goto continue|@>; if cur←cmd=lig←kern←token then begin @<Compile a ligature/kern command@>; if nl=lig←table←size then overflow("ligtable size",lig←table←size); @:METAFONT capacity exceeded ligtable size}{\quad ligtable size@> incr(nl); if cur←cmd=comma then goto continue; end else begin print←err("Illegal ligtable step"); @.Illegal ligtable step@> help1("I was looking for `=:' or `kern' here."); back←error; end; if nl>0 then stop←bit(nl-1):=stop←flag; end @ @<Put each...@>= primitive("=:",lig←kern←token,0); @!@:=:←}{\.{=:} primitive@> primitive("kern",lig←kern←token,kern←flag-min←quarterword); @!@:kern←}{\&{kern} primitive@> @ @<Cases of |print←cmd...@>= lig←kern←token: if m=0 then print("=:")@+else print("kern"); @ @<Record the beginning...@>= begin if nl>255 then begin print←err("Ligature program too long"); @.Ligature program too long@> help1("After 256 ligtable steps, no more colons are allowed."); error; end else set←tag(c,lig←tag,nl); goto continue; end @ @<Compile a ligature/kern...@>= next←char(nl):=qi(c); op←bit(nl):=qi(cur←mod); stop←bit(nl):=qi(0); if cur←mod=0 then rem←byte(nl):=qi(get←code) else begin get←x←next; scan←expression; if cur←type<>known then begin exp←err("Improper kern"); @.Improper kern@> help2("The amount of kern should be a known numeric value.")@/ ("I'm zeroing this one. Proceed, with fingers crossed."); put←get←flush←error(0); end; kern[nk]:=cur←exp; k:=0;@+while kern[k]<>cur←exp do incr(k); if k=nk then begin if nk=256 then overflow("kern",256); @:METAFONT capacity exceeded kern}{\quad kern@> incr(nk); end; rem←byte(nl):=qi(k); end @ @d missing←extensible←punctuation(#)== begin missing←err(#); @.Missing `\char`\#'@> help1("I'm processing `extensible c: t,m,b,r'."); back←error; end @<Define an extensible recipe@>= begin if ne=256 then overflow("extensible",256); @:METAFONT capacity exceeded extensible}{\quad extensible@> c:=get←code; set←tag(c,ext←tag,ne); if cur←cmd<>colon then missing←extensible←punctuation(":"); ext←top(ne):=qi(get←code); if cur←cmd<>comma then missing←extensible←punctuation(","); ext←mid(ne):=qi(get←code); if cur←cmd<>comma then missing←extensible←punctuation(","); ext←bot(ne):=qi(get←code); if cur←cmd<>comma then missing←extensible←punctuation(","); ext←rep(ne):=qi(get←code); incr(ne); end @ @<Store a list of header bytes@>= repeat if j>header←size then overflow("headerbyte",header←size); @:METAFONT capacity exceeded headerbyte}{\quad headerbyte@> header←byte[j]:=get←code; incr(j); until cur←cmd<>comma @ @<Store a list of font dimensions@>= repeat if j>max←font←dimen then overflow("fontdimen",max←font←dimen); @:METAFONT capacity exceeded fontdimen}{\quad fontdimen@> while j>np do begin incr(np); param[np]:=0; end; get←x←next; scan←expression; if cur←type<>known then begin exp←err("Improper font parameter"); @.Improper font parameter@> help1("I'm zeroing this one. Proceed, with fingers crossed."); put←get←flush←error(0); end; param[j]:=cur←exp; incr(j); until cur←cmd<>comma @ OK: We've stored all the data that is needed for the \.{TFM} file. All that remains is to output it in the correct format. An interesting problem needs to be solved in this connection, because the \.{TFM} format allows at most 256~widths, 16~heights, 16~depths, and 64~italic corrections. If the data has more distinct values than this, we want to meet the necessary restrictions by perturbing the given values as little as possible. \MF\ solves this problem in two steps. First the values of a given kind (widths, heights, depths, or italic corrections) are sorted; then the list of sorted valuess is perturbed, if necessary. The sorting operation is facilitated by having a special node of essentially infinite |value| at the end of the current list. @<Initialize table entries...@>= value(inf←val):=fraction←four; @ Straight linear insertion is good enough for sorting, since the lists are usually not terribly long. As we work on the data, the current list will start at |link(temp←head)| and end at |inf←val|; the nodes in this list will be in increasing order of their |value| fields. Given such a list, the |sort←in| function takes a value and returns a pointer to where that value can be found in the list. The value is inserted in the proper place, if necessary. At the time we need to do these operations, most of \MF's work has been completed, so we will have plenty of memory to play with. The value nodes that are allocated for sorting will never be returned to free storage. @d clear←the←list==link(temp←head):=inf←val @p function sort←in(@!v:scaled):pointer; label found; var @!p,@!q,@!r:pointer; {list manipulation registers} begin p:=temp←head; loop@+ begin q:=link(p); if v<=value(q) then goto found; p:=q; end; found: if v<value(q) then begin r:=get←node(value←node←size); value(r):=v; link(r):=q; link(p):=r; end; sort←in:=link(p); end; @ Now we come to the interesting part, where we reduce the list if necessary until it has the required size. The |min←cover| routine is basic to this process; it computes the minimum number~|m| such that the values of the current sorted list can be covered by |m|~intervals of width~|d|. It also sets the global value |perturbation| to the smallest value $d'>d$ such that the covering found by this algorithm would be different. In particular, |min←cover(0)| returns the number of distinct values in the current list and sets |perturbation| to the minimum distance between adjacent values. @p function min←cover(@!d:scaled):integer; var @!p:pointer; {runs through the current list} @!l:scaled; {the least element covered by the current interval} @!m:integer; {lower bound on the size of the minimum cover} begin m:=0; p:=link(temp←head); perturbation:=el←gordo; while p<>inf←val do begin incr(m); l:=value(p); repeat p:=link(p); until value(p)>l+d; if value(p)-l<perturbation then perturbation:=value(p)-l; end; min←cover:=m; end; @ @<Glob...@>= @!perturbation:scaled; {quantity related to \.{TFM} rounding} @ The smallest |d| such that a given list can be covered with |m| intervals is determined by the |threshold| routine, which is sort of an inverse to |min←cover|. The idea is to increase the interval size rapidly until finding the range, then to go sequentially until the exact borderline has been discovered. @p function threshold(@!m:integer):scaled; var @!d:scaled; {lower bound on the smallest interval size} begin if min←cover(0)<=m then threshold:=0 else begin repeat d:=perturbation; until min←cover(d+d)<=m; while min←cover(d)>m do d:=perturbation; threshold:=d; end; end; @ The |skimp| procedure reduces the current list to at most |m| entries, by changing values if necessary. It also sets |info(p):=k| if |value(p)| is the |k|th distinct value on the resulting list, and it sets |perturbation| to the maximum amount by which a |value| field has been changed. The size of the resulting list is returned as the value of |skimp|. @p function skimp(@!m:integer):integer; var @!d:scaled; {the size of intervals being coalesced} @!p,@!q,@!r:pointer; {list manipulation registers} @!l:scaled; {the least value in the current interval} @!v:scaled; {a compromise value} begin d:=threshold(m); perturbation:=0; q:=temp←head; m:=0; p:=link(temp←head); while p<>inf←val do begin incr(m); l:=value(p); info(p):=m; if value(link(p))<=l+d then @<Replace an interval of values by its midpoint@>; q:=p; p:=link(p); end; skimp:=m; end; @ @<Replace an interval...@>= begin repeat p:=link(p); info(p):=m; until value(link(p))>l+d; v:=half(l+value(p)); if value(p)-v>perturbation then perturbation:=value(p)-v; r:=q; repeat r:=link(r); value(r):=v; until r=p; link(q):=p; {remove duplicate values from the current list} end @ A warning message is issued whenever something is perturbed by more than 1/16\thinspace pt. @p procedure tfm←warning(@!m:small←number); begin print←nl("(some "); print(int←name[m]); @.some charwds...@> @.some chardps...@> @.some charhts...@> @.some charics...@> print(" values had to be adjusted by as much as "); print←scaled(perturbation); print("pt)"); end; @ Here's an example of how we use these routines. The width data needs to be perturbed only if there are 256 distinct widths, but \MF\ must check for this case even though it is highly unusual. An integer variable |k| will be defined when we use this code. The |dimen←head| array will contain pointers to the sorted lists of dimensions. @<Massage the \.{TFM} widths@>= clear←the←list; for k:=bc to ec do if char←exists[k] then tfm←width[k]:=sort←in(tfm←width[k]); nw:=skimp(255)+1; dimen←head[1]:=link(temp←head); if perturbation>=@'10000 then tfm←warning(char←wd) @ @<Glob...@>= @!dimen←head:array[1..4] of pointer; {lists of \.{TFM} dimensions} @ Heights, depths, and italic corrections are different from widths not only because their list length is more severely restricted, but also because zero values do not need to be put into the lists. @<Massage the \.{TFM} heights, depths, and italic corrections@>= clear←the←list; for k:=bc to ec do if char←exists[k] then if tfm←height[k]=0 then tfm←height[k]:=zero←val else tfm←height[k]:=sort←in(tfm←height[k]); nh:=skimp(15)+1; dimen←head[2]:=link(temp←head); if perturbation>=@'10000 then tfm←warning(char←ht); clear←the←list; for k:=bc to ec do if char←exists[k] then if tfm←depth[k]=0 then tfm←depth[k]:=zero←val else tfm←depth[k]:=sort←in(tfm←depth[k]); nd:=skimp(15)+1; dimen←head[3]:=link(temp←head); if perturbation>=@'10000 then tfm←warning(char←dp); clear←the←list; for k:=bc to ec do if char←exists[k] then if tfm←ital←corr[k]=0 then tfm←ital←corr[k]:=zero←val else tfm←ital←corr[k]:=sort←in(tfm←ital←corr[k]); ni:=skimp(63)+1; dimen←head[4]:=link(temp←head); if perturbation>=@'10000 then tfm←warning(char←ic) @ @<Initialize table entries...@>= value(zero←val):=0; info(zero←val):=0; @ Bytes 5--8 of the header are set to the design size, unless the user has some crazy reason for specifying them differently. Error messages are not allowed at the time this procedure is called, so a warning is printed instead. The value of |max←tfm←dimen| is calculated so that $$\hbox{|make←scaled(16*max←tfm←dimen,internal[design←size])|} < \\{three\←bytes}.$$ @d three←bytes==@'100000000 {$2↑{24}$} @p procedure fix←design←size; var @!d:scaled; {the design size} begin d:=internal[design←size]; if (d<unity)or(d>=fraction←half) then begin if d<>0 then print←nl("(illegal design size has been changed to 128pt)"); @.illegal design size...@> d:=@'40000000; internal[design←size]:=d; end; if header←byte[5]<0 then if header←byte[6]<0 then if header←byte[7]<0 then if header←byte[8]<0 then begin header←byte[5]:=d div @'4000000; header←byte[6]:=(d div 4096) mod 256; header←byte[7]:=(d div 16) mod 256; header←byte[8]:=(d mod 16)*16; end; max←tfm←dimen:=16*internal[design←size]-internal[design←size] div @'10000000; if max←tfm←dimen>=fraction←half then max←tfm←dimen:=fraction←half-1; end; @ The |dimen←out| procedure computes a |fix←word| relative to the design size. If the data was out of range, it is corrected and the global variable |tfm←changed| is increased by~one. @p function dimen←out(@!x:scaled):integer; begin if abs(x)>max←tfm←dimen then begin incr(tfm←changed); if x>0 then x:=three←bytes-1@+else x:=1-three←bytes; end else x:=make←scaled(x*16,internal[design←size]); dimen←out:=x; end; @ @<Glob...@>= @!max←tfm←dimen:scaled; {bound on widths, heights, kerns, etc.} @!tfm←changed:integer; {the number of data entries that were out of bounds} @ If the user has not specified any of the first four header bytes, the |fix←check←sum| procedure replaces them by a ``check sum'' computed from the |tfm←width| data relative to the design size. @↑check sum@> @p procedure fix←check←sum; label exit; var @!k:eight←bits; {runs through character codes} @!b1,@!b2,@!b3,@!b4:eight←bits; {bytes of the check sum} @!x:integer; {hash value used in check sum computation} begin if header←byte[1]<0 then if header←byte[2]<0 then if header←byte[3]<0 then if header←byte[4]<0 then begin @<Compute a check sum in |(b1,b2,b3,b4)|@>; header←byte[1]:=b1; header←byte[2]:=b2; header←byte[3]:=b3; header←byte[4]:=b4; return; end; for k:=1 to 4 do if header←byte[k]<0 then header←byte[k]:=0; exit:end; @ @<Compute a check sum in |(b1,b2,b3,b4)|@>= b1:=bc; b2:=ec; b3:=bc; b4:=ec; tfm←changed:=0; for k:=bc to ec do if char←exists[k] then begin x:=dimen←out(value(tfm←width[k]))+(k+4)*@'20000000; {this is positive} b1:=(b1+b1+x) mod 255; b2:=(b2+b2+x) mod 253; b3:=(b3+b3+x) mod 251; b4:=(b4+b4+x) mod 247; end @ Finally we're ready to actually write the \.{TFM} information. Here are some utility routines for this purpose. @d tfm←out(#)==write(tfm←file,#) {output one byte to |tfm←file|} @p procedure tfm←two(@!x:integer); {output two bytes to |tfm←file|} begin tfm←out(x div 256); tfm←out(x mod 256); end; @# procedure tfm←four(@!x:integer); {output four bytes to |tfm←file|} begin if x>=0 then tfm←out(x div three←bytes) else begin x:=x+@'10000000000; {use two's complement for negative values} x:=x+@'10000000000; tfm←out((x div three←bytes) + 128); end; x:=x mod three←bytes; tfm←out(x div unity); x:=x mod unity; tfm←out(x div @'400); tfm←out(x mod @'400); end; @# procedure tfm←qqqq(@!x:four←quarters); {output four quarterwords to |tfm←file|} begin tfm←out(qo(x.b0)); tfm←out(qo(x.b1)); tfm←out(qo(x.b2)); tfm←out(qo(x.b3)); end; @ @<Finish the \.{TFM} file@>= if job←name=0 then open←log←file; pack←job←name(".tfm"); while not b←open←out(tfm←file) do prompt←file←name("file name for font metrics",".tfm"); metric←file←name:=b←make←name←string(tfm←file); @<Output the subfile sizes and header bytes@>; @<Output the character information bytes@>; @<Output the dimensions themselves@>; @<Output the ligature/kern program@>; @<Output the extensible character recipes@>; @<Output the font metric parameters@>; @!stat if internal[tracing←stats]>0 then @<Log the subfile sizes of the \.{TFM} file@>;@;@+tats@/ print←nl("Font metrics written on "); print(metric←file←name); @.Font metrics written...@> b←close(tfm←file) @ Integer variables |lh| and |k| will be defined when we use this code. @<Output the subfile sizes and header bytes@>= k:=header←size; while header←byte[k]<0 do decr(k); lh:=(k+3) div 4; {this is the number of header words} if bc>ec then bc:=1; {if there are no characters, |ec=0| and |bc=1|} tfm←two(6+lh+(ec-bc+1)+nw+nh+nd+ni+nl+nk+ne+np); {this is the total number of file words that will be output} tfm←two(lh); tfm←two(bc); tfm←two(ec); tfm←two(nw); tfm←two(nh); tfm←two(nd); tfm←two(ni); tfm←two(nl); tfm←two(nk); tfm←two(ne); tfm←two(np); for k:=1 to 4*lh do begin if header←byte[k]<0 then header←byte[k]:=0; tfm←out(header←byte[k]); end @ @<Log the subfile sizes of the \.{TFM} file@>= begin wlog←ln(' '); wlog←ln('(You used ',nw:1,'w,',@| nh:1,'h,',@| nd:1,'d,',@| ni:1,'i,',@| nl:1,'l,',@| nk:1,'k,',@| ne:1,'e,',@| np:1,'p metric file positions'); wlog←ln(' out of ',@| '256w,16h,16d,64i,',@| lig←table←size:1,'l,256k,256e,',@| max←font←dimen:1,'p)'); end @ @<Output the character information bytes@>= for k:=bc to ec do if not char←exists[k] then tfm←four(0) else begin tfm←out(info(tfm←width[k])); {the width index} tfm←out((info(tfm←height[k]))*16+info(tfm←depth[k])); tfm←out((info(tfm←ital←corr[k]))*4+char←tag[k]); tfm←out(char←remainder[k]); end @ @<Output the dimensions themselves@>= tfm←changed:=0; for k:=1 to 4 do begin tfm←four(0); p:=dimen←head[k]; while p<>inf←val do begin tfm←four(dimen←out(value(p))); p:=link(p); end; end @ @<Output the ligature/kern program@>= for k:=0 to nl-1 do tfm←qqqq(lig←kern[k]); for k:=0 to nk-1 do tfm←four(dimen←out(kern[k])) @ @<Output the extensible character recipes@>= for k:=0 to ne-1 do tfm←qqqq(exten[k]) @ @<Output the font metric parameters@>= for k:=1 to np do if k=1 then if abs(param[1])<fraction←half then tfm←four(param[1]*16) else begin incr(tfm←changed); if param[1]>0 then tfm←four(el←gordo) else tfm←four(-el←gordo); end else tfm←four(dimen←out(param[k])); if tfm←changed>0 then begin if tfm←changed=1 then print←nl("(a font metric dimension") @.a font metric dimension...@> else begin print←nl("("); print←int(tfm←changed); @.font metric dimensions...@> print(" font metric dimensions"); end; print(" had to be decreased)"); end @* \[46] Generic font file format. The most important output produced by a typical run of \MF\ is the ``generic font'' (\.{GF}) file that specifies the bit patterns of the characters that have been drawn. The term {\sl generic\/} indicates that this file format doesn't match the conventions of any name-brand manufacturer; but it is easy to convert \.{GF} files to the special format required by almost all digital phototypesetting equipment. There's a strong analogy between the \.{DVI} files written by \TeX\ and the \.{GF} files written by \MF; and, in fact, the file formats have a lot in common. A \.{GF} file is a stream of 8-bit bytes that may be regarded as a series of commands in a machine-like language. The first byte of each command is the operation code, and this code is followed by zero or more bytes that provide parameters to the command. The parameters themselves may consist of several consecutive bytes; for example, the `|boc|' (beginning of character) command has six parameters, each of which is four bytes long. Parameters are usually regarded as nonnegative integers; but four-byte-long parameters can be either positive or negative, hence they range in value from $-2↑{31}$ to $2↑{31}-1$. As in \.{TFM} files, numbers that occupy more than one byte position appear in BigEndian order, and negative numbers appear in two's complement notation. A \.{GF} file consists of a ``preamble,'' followed by a sequence of one or more ``characters,'' followed by a ``postamble.'' The preamble is simply a |pre| command, with its parameters that introduce the file; this must come first. Each ``character'' consists of a |boc| command, followed by any number of other commands that specify ``black'' pixels, followed by an |eoc| command. The characters appear in the order that \MF\ generated them. If we ignore no-op commands (which are allowed between any two commands in the file), each |eoc| command is immediately followed by a |boc| command, or by a |post| command; in the latter case, there are no more characters in the file, and the remaining bytes form the postamble. Further details about the postamble will be explained later. Some parameters in \.{GF} commands are ``pointers.'' These are four-byte quantities that give the location number of some other byte in the file; the first file byte is number~0, then comes number~1, and so on. @ The \.{GF} format is intended to be both compact and easily interpreted by a machine. Compactness is achieved by making most of the information relative instead of absolute. When a \.{GF}-reading program reads the commands for a character, it keeps track of two quantities: (a)~the current column number,~|m|; and (b)~the current row number,~|n|. These are 32-bit signed integers, although most actual font formats produced from \.{GF} files will need to curtail this vast range because of practical limitations. (\MF\ output will never allow $\vert m\vert$ or $\vert n\vert$ to get extremely large, but the \.{GF} format tries to be more general.) How do \.{GF}'s row and column numbers correspond to the conventions of \TeX\ and \MF? Well, the ``reference point'' of a character, in \TeX's view, is considered to be at the lower left corner of the pixel in row~0 and column~0. This point is the intersection of the baseline with the left edge of the type; it corresponds to location $(0,0)$ in \MF\ programs. Thus the pixel in \.{GF} row~0 and column~0 is \MF's unit square, comprising the region of the plane whose coordinates both lie between 0 and~1. The pixel in \.{GF} row~|n| and column~|m| consists of the points whose \MF\ coordinates |(x,y)| satisfy |m<=x<=m+1| and |n<=y<=n+1|. Negative values of |m| and~|x| correspond to columns of pixels {\sl left\/} of the reference point; negative values of |n| and~|y| correspond to rows of pixels {\sl below\/} the baseline. Besides |m| and |n|, there's also a third aspect of the current state, namely the @!|paint←switch|, which is always either \\{black} or \\{white}. Each \\{paint} command advances |m| by a specified amount~|d|, and blackens the intervening pixels if |paint←switch=black|; then the |paint←switch| changes to the opposite state. \.{GF}'s commands are designed so that |m| will never decrease within a row, and |n| will never increase within a character; hence there is no way to whiten a pixel that has been blackened. @ Here is a list of all the commands that may appear in a \.{GF} file. Each command is specified by its symbolic name (e.g., |boc|), its opcode byte (e.g., 67), and its parameters (if any). The parameters are followed by a bracketed number telling how many bytes they occupy; for example, `|d[2]|' means that parameter |d| is two bytes long. \yskip\hang|paint←0| 0. This is a \\{paint} command with |d=0|; it does nothing but change the |paint←switch| from \\{black} to \\{white} or vice~versa. \yskip\hang\\{paint\←1} through \\{paint\←63} (opcodes 1 to 63). These are \\{paint} commands with |d=1| to~63, defined as follows: If |paint←switch=black|, blacken |d|~pixels of the current row~|n|, in columns |m| through |m+d-1| inclusive. Then, in any case, complement the |paint←switch| and advance |m| by~|d|. \yskip\hang|paint1| 64 |d[1]|. This is a \\{paint} command with a specified value of~|d|; \MF\ uses it to paint when |64<=d<256|. \yskip\hang|@!paint2| 65 |d[2]|. Same as |paint1|, but |d|~can be as high as~65535. \yskip\hang|@!paint3| 66 |d[3]|. Same as |paint1|, but |d|~can be as high as $2↑{24}-1$. \MF\ never needs this command, and it is hard to imagine anybody making practical use of it; surely a more compact encoding will be desirable when characters can be this large. But the command is there, anyway, just in case. \yskip\hang|boc| 67 |c[4]| |p[4]| |min←m[4]| |max←m[4]| |min←n[4]| |max←n[4]|. Beginning of a character: Here |c| is the character code, and |p| points to the previous character beginning (if any) for characters having this code number modulo 256. (The pointer |p| is |-1| if there was no prior character with an equivalent code.) The values of registers |m| and |n| defined by the instructions that follow for this character must satisfy |min←m<=m<=max←m| and |min←n<=n<=max←n|. (The values of |max←m| and |min←n| need not be the tightest bounds possible.) When a \.{GF}-reading program sees a |boc|, it can use |min←m|, |max←m|, |min←n|, and |max←n| to initialize the bounds of an array. Then it sets |m:=min←m|, |n:=max←n|, and |paint←switch:=white|. \yskip\hang|boc1| 68 |c[1]| |@!del←m[1]| |max←m[1]| |@!del←n[1]| |max←n[1]|. Same as |boc|, but |p| is assumed to be~$-1$; also |del←m=max←m-min←m| and |del←n=max←n-min←n| are given instead of |min←m| and |min←n|. The one-byte parameters must be between 0 and 255, inclusive. \ (This abbreviated |boc| saves 19~bytes per character, in common cases.) \yskip\hang|eoc| 69. End of character: All pixels blackened so far constitute the pattern for this character. In particular, a completely blank character might have |eoc| immediately following |boc|. \yskip\hang|skip0| 70. Decrease |n| by 1 and set |m:=min←m|, |paint←switch:=white|. \ (This finishes one row and begins another, ready to whiten the leftmost pixel in the new row.) \yskip\hang|skip1| 71 |d[1]|. Decrease |n| by |d+1|, set |m:=min←m|, and set |paint←switch:=white|. This is a way to produce |d| all-white rows. \yskip\hang|@!skip2| 72 |d[2]|. Same as |skip1|, but |d| can be as large as 65535. \yskip\hang|@!skip3| 73 |d[3]|. Same as |skip1|, but |d| can be as large as $2↑{24}-1$. \MF\ obviously never needs this command. \yskip\hang|new←row←0| 74. Decrease |n| by 1 and set |m:=min←m|, |paint←switch:=black|. \ (This finishes one row and begins another, ready to {\sl blacken\/} the leftmost pixel in the new row.) \yskip\hang|@!new←row←1| through |@!new←row←164| (opcodes 75 to 238). Same as |new←row←0|, but with |m:=min←m+1| through |min←m+164|, respectively. \yskip\hang|xxx1| 239 |k[1]| |x[k]|. This command is undefined in general; it functions as a $(k+2)$-byte |no←op| unless special \.{GF}-reading programs are being used. \MF\ generates \\{xxx} commands when encountering a \&{special} string; this occurs in the \.{GF} file only between characters, after the preamble, and before the postamble. However, \\{xxx} commands might appear anywhere in \.{GF} files generated by other processors. It is recommended that |x| be a string having the form of a keyword followed by possible parameters relevant to that keyword. \yskip\hang|@!xxx2| 240 |k[2]| |x[k]|. Like |xxx1|, but |0<=k<65536|. \yskip\hang|xxx3| 241 |k[3]| |x[k]|. Like |xxx1|, but |0<=k<@t$2↑{24}$@>|. \MF\ uses this when sending a \&{special} string whose length exceeds~255. \yskip\hang|@!xxx4| 242 |k[4]| |x[k]|. Like |xxx1|, but |k| can be ridiculously large; |k| mustn't be negative. \yskip\hang|yyy| 243 |y[4]|. This command is undefined in general; it functions as a 5-byte |no←op| unless special \.{GF}-reading programs are being used. \MF\ puts |scaled| numbers into |yyy|'s, as a result of \&{numspecial} commands; the intent is to provide numeric parameters to \\{xxx} commands that immediately precede. \yskip\hang|@!no←op| 244. No operation, do nothing. Any number of |no←op|'s may occur between \.{GF} commands, but a |no←op| cannot be inserted between a command and its parameters or between two parameters. \yskip\hang|char←loc| 245 |c[1]| |dx[4]| |dy[4]| |w[4]| |p[4]|. This command will appear only in the postamble, which will be explained shortly. \yskip\hang|@!char←loc0| 246 |c[1]| |@!dm[1]| |w[4]| |p[4]|. Same as |char←loc|, except that |dy| is assumed to be zero, and the value of~|dx| is taken to be |65536*dm|, where |0<=dm<256|. \yskip\hang|pre| 247 |i[1]| |k[1]| |x[k]|. Beginning of the preamble; this must come at the very beginning of the file. Parameter |i| is an identifying number for \.{GF} format, currently 131. The other information is merely commentary; it is not given special interpretation like \\{xxx} commands are. (Note that \\{xxx} commands may immediately follow the preamble, before the first |boc|.) \yskip\hang|post| 248. Beginning of the postamble, see below. \yskip\hang|post←post| 249. Ending of the postamble, see below. \yskip\noindent Commands 250--255 are undefined at the present time. @d gf←id←byte=131 {identifies the kind of \.{GF} files described here} @ Here are the opcodes that \MF\ actually refers to. @d paint←0=0 {beginning of the \\{paint} commands} @d paint1=64 {move right a given number of columns, then black${}\swap{}$white} @d boc=67 {beginning of a character} @d boc1=68 {short form of |boc|} @d eoc=69 {end of a character} @d skip0=70 {skip no blank rows} @d skip1=71 {skip over blank rows} @d new←row←0=74 {move down one row and then right} @d max←new←row=164 {the largest \\{new\←row} command is |new←row←164|} @d xxx1=239 {for \&{special} strings} @d xxx3=241 {for long \&{special} strings} @d yyy=243 {for \&{numspecial} numbers} @d char←loc=245 {character locators in the postamble} @d pre=247 {preamble} @d post=248 {postamble beginning} @d post←post=249 {postamble ending} @ The last character in a \.{GF} file is followed by `|post|'; this command introduces the postamble, which summarizes important facts that \MF\ has accumulated. The postamble has the form $$\vbox{\halign{\hbox{#\hfil}\cr |post| |p[4]| |@!ds[4]| |@!cs[4]| |@!hppp[4]| |@!vppp[4]| |@!min←m[4]| |@!max←m[4]| |@!min←n[4]| |@!max←n[4]|\cr $\langle\,$character locators$\,\rangle$\cr |post←post| |q[4]| |i[1]| 223's$[{\G}4]$\cr}}$$ Here |p| is a pointer to the byte following the final |eoc| in the file (or to the byte following the preamble, if there are no characters); it can be used to locate the beginning of \\{xxx} commands that might have preceded the postamble. The |ds| and |cs| parameters @↑design size@> @↑check sum@> give the design size and check sum, respectively, which are exactly the values put into the header of the \.{TFM} file that \MF\ produces (or would produce) on this run. Parameters |hppp| and |vppp| are the ratios of pixels per point, horizontally and vertically, expressed as |scaled| integers (i.e., multiplied by $2↑{16}$); they can be used to correlate the font with specific device resolutions, magnifications, and ``at sizes.'' Then come |min←m|, |max←m|, |min←n|, and |max←n|, which bound the values that registers |m| and~|n| assume in all characters in this \.{GF} file. (These bounds need not be the best possible; |max←m| and |min←n| may, on the other hand, be tighter than the similar bounds in |boc| commands. For example, some character may have |min←n=-100| in its |boc|, but it might turn out that |n| never gets lower than |-50| in any character; then |min←n| can have any value |<=-50|. If there are no characters in the file, it's possible to have |min←m>max←m| and/or |min←n>max←n|.) @ Character locators are introduced by |char←loc| commands, which specify a character residue~|c|, character escapements (|dx,dy|), a character width~|w|, and a pointer~|p| to the beginning of that character. (If two or more characters have the same code~|c| modulo 256, only the last will be indicated; the others can be located by following backpointers. Characters whose codes differ by a multiple of 256 are assumed to share the same font metric information, hence the \.{TFM} file contains only residues of character codes modulo~256. This convention is intended for oriental languages, when there are many character shapes but few distinct widths.) @↑oriental characters@>@↑Chinese characters@>@↑Japanese characters@> The character escapements (|dx,dy|) are the values of \MF's \&{chardx} and \&{chardy} parameters; they are in units of |scaled| pixels; i.e., |dx| is in horizontal pixel units times $2↑{16}$, and |dy| is in vertical pixel units times $2↑{16}$. This is the intended amount of displacement after typesetting the character; for \.{DVI} files, |dy| should be zero, but other document file formats allow nonzero vertical escapement. The character width~|w| duplicates the information in the \.{TFM} file; it is a |fix←word| value relative to the design size, and it should be independent of magnification. The backpointer |p| points to the character's |boc|, or to the first of a sequence of consecutive \\{xxx} or |yyy| or |no←op| commands that immediately precede the |boc|, if such commands exist; such ``special'' commands essentially belong to the characters, while the special commands after the final character belong to the postamble (i.e., to the font as a whole). This convention about |p| applies also to the backpointers in |boc| commands, even though it wasn't explained in the description of~|boc|. @↑backpointers@> Pointer |p| might be |-1| if the character exists in the \.{TFM} file but not in the \.{GF} file. This unusual situation can arise in \MF\ output if the user had |proofing<0| when the character was being shipped out, but then made |proofing>=0| in order to get a \.{GF} file. @ The last part of the postamble, following the |post←post| byte that signifies the end of the character locators, contains |q|, a pointer to the |post| command that started the postamble. An identification byte, |i|, comes next; this currently equals~131, as in the preamble. The |i| byte is followed by four or more bytes that are all equal to the decimal number 223 (i.e., @'337 in octal). \MF\ puts out four to seven of these trailing bytes, until the total length of the file is a multiple of four bytes, since this works out best on machines that pack four bytes per word; but any number of 223's is allowed, as long as there are at least four of them. In effect, 223 is a sort of signature that is added at the very end. @↑Fuchs, David Raymond@> This curious way to finish off a \.{GF} file makes it feasible for \.{GF}-reading programs to find the postamble first, on most computers, even though \MF\ wants to write the postamble last. Most operating systems permit random access to individual words or bytes of a file, so the \.{GF} reader can start at the end and skip backwards over the 223's until finding the identification byte. Then it can back up four bytes, read |q|, and move to byte |q| of the file. This byte should, of course, contain the value 248 (|post|); now the postamble can be read, so the \.{GF} reader can discover all the information needed for individual characters. Unfortunately, however, standard \PASCAL\ does not include the ability to @↑system dependencies@> access a random position in a file, or even to determine the length of a file. Almost all systems nowadays provide the necessary capabilities, so \.{GF} format has been designed to work most efficiently with modern operating systems. But if \.{GF} files have to be processed under the restrictions of standard \PASCAL, one can simply read them from front to back. This will be adequate for most applications. However, the postamble-first approach would facilitate a program that merges two \.{GF} files, replacing data from one that is overridden by corresponding data in the other. @* \[47] Shipping characters out. The |ship←out| procedure, to be described below, is given a pointer to an edge structure. Its mission is to describe the the positive pixels in \.{GF} form, outputting a ``character'' to |gf←file|. Several global variables maintain information about the font file as a whole:\ |gf←min←m|, |gf←max←m|, |gf←min←n|, and |gf←max←n| are the minimum and maximum \.{GF} coordinates output so far; |gf←prev←ptr| is the byte number following the preamble or the last |eoc| command in the output; |total←chars| is the total number of characters (i.e., |boc..eoc| segments) shipped out. There's also an array, |char←ptr|, containing the starting positions of each character in the file, as required for the postamble. If character code~|c| has not yet been output, |char←ptr[c]=-1|. @<Glob...@>= @!gf←min←m,@!gf←max←m,@!gf←min←n,@!gf←max←n:integer; {bounding rectangle} @!gf←prev←ptr:integer; {where the present/next character started/starts} @!total←chars:integer; {the number of characters output so far} @!char←ptr:array[eight←bits] of integer; {where individual characters started} @!gf←dx,@!gf←dy:array[eight←bits] of integer; {device escapements} @ @<Set init...@>= gf←prev←ptr:=0; total←chars:=0; @ The \.{GF} bytes are output to a buffer instead of being sent byte-by-byte to |gf←file|, because this tends to save a lot of subroutine-call overhead. \MF\ uses the same conventions for |gf←file| as \TeX\ uses for its \\{dvi\←file}; hence if system-dependent changes are needed, they should probably be the same for both programs. The output buffer is divided into two parts of equal size; the bytes found in |gf←buf[0..half←buf-1]| constitute the first half, and those in |gf←buf[half←buf..gf←buf←size-1]| constitute the second. The global variable |gf←ptr| points to the position that will receive the next output byte. When |gf←ptr| reaches |gf←limit|, which is always equal to one of the two values |half←buf| or |gf←buf←size|, the half buffer that is about to be invaded next is sent to the output and |gf←limit| is changed to its other value. Thus, there is always at least a half buffer's worth of information present, except at the very beginning of the job. Bytes of the \.{GF} file are numbered sequentially starting with 0; the next byte to be generated will be number |gf←offset+gf←ptr|. @<Types...@>= @!gf←index=0..gf←buf←size; {an index into the output buffer} @ Some systems may find it more efficient to make |gf←buf| a |packed| array, since output of four bytes at once may be facilitated. @↑system dependencies@> @<Glob...@>= @!gf←buf:array[gf←index] of eight←bits; {buffer for \.{GF} output} @!half←buf:gf←index; {half of |gf←buf←size|} @!gf←limit:gf←index; {end of the current half buffer} @!gf←ptr:gf←index; {the next available buffer address} @!gf←offset:integer; {|gf←buf←size| times the number of times the output buffer has been fully emptied} @ Initially the buffer is all in one piece; we will output half of it only after it first fills up. @<Set init...@>= half←buf:=gf←buf←size div 2; gf←limit:=gf←buf←size; gf←ptr:=0; gf←offset:=0; @ The actual output of |gf←buf[a..b]| to |gf←file| is performed by calling |write←gf(a,b)|. It is safe to assume that |a| and |b+1| will both be multiples of 4 when |write←gf(a,b)| is called; therefore it is possible on many machines to use efficient methods to pack four bytes per word and to output an array of words with one system call. @↑system dependencies@> @<Declare generic font output procedures@>= procedure write←gf(@!a,@!b:gf←index); var k:gf←index; begin for k:=a to b do write(gf←file,gf←buf[k]); end; @ To put a byte in the buffer without paying the cost of invoking a procedure each time, we use the macro |gf←out|. @d gf←out(#)==@+begin gf←buf[gf←ptr]:=#; incr(gf←ptr); if gf←ptr=gf←limit then gf←swap; end @<Declare generic font output procedures@>= procedure gf←swap; {outputs half of the buffer} begin if gf←limit=gf←buf←size then begin write←gf(0,half←buf-1); gf←limit:=half←buf; gf←offset:=gf←offset+gf←buf←size; gf←ptr:=0; end else begin write←gf(half←buf,gf←buf←size-1); gf←limit:=gf←buf←size; end; end; @ Here is how we clean out the buffer when \MF\ is all through; |gf←ptr| will be a multiple of~4. @<Empty the last bytes out of |gf←buf|@>= if gf←limit=half←buf then write←gf(half←buf,gf←buf←size-1); if gf←ptr>0 then write←gf(0,gf←ptr-1) @ The |gf←four| procedure outputs four bytes in two's complement notation, without risking arithmetic overflow. @<Declare generic font output procedures@>= procedure gf←four(@!x:integer); begin if x>=0 then gf←out(x div three←bytes) else begin x:=x+@'10000000000; x:=x+@'10000000000; gf←out((x div three←bytes) + 128); end; x:=x mod three←bytes; gf←out(x div unity); x:=x mod unity; gf←out(x div @'400); gf←out(x mod @'400); end; @ Of course, it's even easier to output just two or three bytes. @<Declare generic font output procedures@>= procedure gf←two(@!x:integer); begin gf←out(x div @'400); gf←out(x mod @'400); end; @# procedure gf←three(@!x:integer); begin gf←out(x div unity); gf←out((x mod unity) div @'400); gf←out(x mod @'400); end; @ We need a simple routine to generate a \\{paint} command of the appropriate type. @<Declare generic font output procedures@>= procedure gf←paint(@!d:integer); {here |0<=d<65536|} begin if d<64 then gf←out(paint←0+d) else if d<256 then begin gf←out(paint1); gf←out(d); end else begin gf←out(paint1+1); gf←two(d); end; end; @ And |gf←string| outputs one or two strings. If the first string number is nonzero, an \\{xxx} command is generated. @<Declare generic font output procedures@>= procedure gf←string(@!s,@!t:str←number); var @!k:pool←pointer; @!l:integer; {length of the strings to output} begin if s<>0 then begin l:=length(s); if t<>0 then l:=l+length(t); if l<=255 then begin gf←out(xxx1); gf←out(l); end else begin gf←out(xxx3); gf←three(l); end; for k:=str←start[s] to str←start[s+1]-1 do gf←out(str←pool[k]); end; if t<>0 then for k:=str←start[t] to str←start[t+1]-1 do gf←out(str←pool[k]); end; @ The choice between |boc| commands is handled by |gf←boc|. @d one←byte(#)== #>=0 then if #<256 @<Declare generic font output procedures@>= procedure gf←boc(@!min←m,@!max←m,@!min←n,@!max←n:integer); label exit; begin if min←m<gf←min←m then gf←min←m:=min←m; if max←n>gf←max←n then gf←max←n:=max←n; if boc←p=-1 then if one←byte(boc←c) then if one←byte(max←m-min←m) then if one←byte(max←m) then if one←byte(max←n-min←n) then if one←byte(max←n) then begin gf←out(boc1); gf←out(boc←c);@/ gf←out(max←m-min←m); gf←out(max←m); gf←out(max←n-min←n); gf←out(max←n); return; end; gf←out(boc); gf←four(boc←c); gf←four(boc←p);@/ gf←four(min←m); gf←four(max←m); gf←four(min←n); gf←four(max←n); exit: end; @ Two of the parameters to |gf←boc| are global. @<Glob...@>= @!boc←c,@!boc←p:integer; {parameters of the next |boc| command} @ Here is a routine that gets a \.{GF} file off to a good start. @d check←gf==@t@>@+if output←file←name=0 then init←gf @<Declare generic font output procedures@>= procedure init←gf; var @!k:eight←bits; {runs through all possible character codes} @!t:integer; {the time of this run} begin gf←min←m:=4096; gf←max←m:=-4096; gf←min←n:=4096; gf←max←n:=-4096; for k:=0 to 255 do char←ptr[k]:=-1; @<Determine the file extension, |gf←ext|@>; set←output←file←name; gf←out(pre); gf←out(gf←id←byte); {begin to output the preamble} old←setting:=selector; selector:=new←string; print(" METAFONT output "); print←int(round←unscaled(internal[year])); print←char("."); print←dd(round←unscaled(internal[month])); print←char("."); print←dd(round←unscaled(internal[day])); print←char(":");@/ t:=round←unscaled(internal[time]); print←dd(t div 60); print←dd(t mod 60);@/ selector:=old←setting; gf←out(cur←length); str←start[str←ptr+1]:=pool←ptr; gf←string(0,str←ptr); pool←ptr:=str←start[str←ptr]; {flush that string from memory} gf←prev←ptr:=gf←offset+gf←ptr; end; @ @<Determine the file extension...@>= if internal[hppp]<=0 then gf←ext:=".gf" else begin old←setting:=selector; selector:=new←string; print←char("."); print←int(make←scaled(internal[hppp],59429463)); {$2↑{32}/72.27\approx59429463.07$} print("gf"); gf←ext:=make←string; selector:=old←setting; end @ With those preliminaries out of the way, |ship←out| is not especially difficult. @<Declare generic font output procedures@>= procedure ship←out(@!c:eight←bits); label done; var @!f:integer; {current character extension} @!prev←m,@!m,@!mm:integer; {previous and current pixel column numbers} @!prev←n,@!n:integer; {previous and current pixel row numbers} @!p,@!q:pointer; {for list traversal} @!prev←w,@!w,@!ww:integer; {old and new weights} @!d:integer; {data from edge-weight node} @!delta:integer; {number of rows to skip} @!cur←min←m:integer; {starting column, relative to the current offset} @!x←off,@!y←off:integer; {offsets, rounded to integers} begin check←gf; f:=round←unscaled(internal[char←ext]);@/ x←off:=round←unscaled(internal[x←offset]); y←off:=round←unscaled(internal[y←offset]); if term←offset>max←print←line-9 then print←ln else if (term←offset>0)or(file←offset>0) then print←char(" "); print←char("["); print←int(c); if f<>0 then begin print←char("."); print←int(f); end; update←terminal; boc←c:=256*f+c; boc←p:=char←ptr[c]; char←ptr[c]:=gf←prev←ptr;@/ if internal[proofing]>0 then @<Send nonzero offsets to the output file@>; @<Output the character represented in |cur←edges|@>; gf←out(eoc); gf←prev←ptr:=gf←offset+gf←ptr; incr(total←chars); print←char("]"); update←terminal; {progress report} if internal[tracing←output]>0 then print←edges(" (just shipped out)",true,x←off,y←off); end; @ @<Send nonzero offsets to the output file@>= begin if x←off<>0 then begin gf←string("xoffset",0); gf←out(yyy); gf←four(x←off*unity); end; if y←off<>0 then begin gf←string("yoffset",0); gf←out(yyy); gf←four(y←off*unity); end; end @ @<Output the character represented in |cur←edges|@>= prev←n:=4096; p:=knil(cur←edges); n:=n←max(cur←edges)-zero←field; while p<>cur←edges do begin @<Output the pixels of edge row |p| to font row |n|@>; p:=knil(p); decr(n); end; if prev←n=4096 then @<Finish off an entirely blank character@> else if prev←n+y←off<gf←min←n then gf←min←n:=prev←n+y←off @ @<Finish off an entirely blank...@>= begin gf←boc(0,0,0,0); if gf←max←m<0 then gf←max←m:=0; if gf←min←n>0 then gf←min←n:=0; end @ In this loop, |prev←w| represents the weight at column |prev←m|, which is the most recent column reflected in the output so far; |w| represents the weight at column~|m|, which is the most recent column in the edge data. Several edges might cancel at the same column position, so we need to look ahead to column~|mm| before actually outputting anything. @<Output the pixels of edge row |p| to font row |n|@>= if unsorted(p)>void then sort←edges(p); q:=sorted(p); w:=0; prev←m:=-fraction←one; {$|fraction←one|\approx\infty$} ww:=0; prev←w:=0; m:=prev←m; repeat if q=sentinel then mm:=fraction←one else begin d:=ho(info(q)); mm:=d div 8; ww:=ww+(d mod 8)-zero←w; end; if mm<>m then begin if prev←w<=0 then begin if w>0 then @<Start black at $(m,n)$@>; end else if w<=0 then @<Stop black at $(m,n)$@>; m:=mm; end; w:=ww; q:=link(q); until mm=fraction←one; if w<>0 then {this should be impossible} print←nl("(There's unbounded black in character shipped out!)"); @.There's unbounded black...@> if prev←m-m←offset(cur←edges)+x←off>gf←max←m then gf←max←m:=prev←m-m←offset(cur←edges)+x←off @ @<Start black at $(m,n)$@>= begin if prev←m=-fraction←one then @<Start a new row at $(m,n)$@> else gf←paint(m-prev←m); prev←m:=m; prev←w:=w; end @ @<Stop black at $(m,n)$@>= begin gf←paint(m-prev←m); prev←m:=m; prev←w:=w; end @ @<Start a new row at $(m,n)$@>= begin if prev←n=4096 then begin gf←boc(m←min(cur←edges)+x←off-zero←field, m←max(cur←edges)+x←off-zero←field,@| n←min(cur←edges)+y←off-zero←field,n+y←off); cur←min←m:=m←min(cur←edges)-zero←field+m←offset(cur←edges); end else if prev←n>n+1 then @<Skip down |prev←n-n| rows@> else @<Skip to column $m$ in the next row and |goto done|, or skip zero rows@>; gf←paint(m-cur←min←m); {skip to column $m$, painting white} done:prev←n:=n; end @ @<Skip to column $m$ in the next row...@>= begin delta:=m-cur←min←m; if delta>max←new←row then gf←out(skip0) else begin gf←out(new←row←0+delta); goto done; end; end @ @<Skip down...@>= begin delta:=prev←n-n-1; if delta<@'400 then if delta=0 then gf←out(skip0) else begin gf←out(skip1); gf←out(delta); end else begin gf←out(skip1+1); gf←two(delta); end; end @ Now that we've finished |ship←out|, let's look at the other commands by which a user can send things to the \.{GF} file. @<Cases of |do←statement|...@>= special←command: do←special; @ @<Put each...@>= primitive("special",special←command,string←type);@/ @!@:special←}{\&{special} primitive@> primitive("numspecial",special←command,known);@/ @!@:num←special←}{\&{numspecial} primitive@> @ @<Declare action procedures for use by |do←statement|@>= procedure do←special; var @!m:small←number; {either |string←type| or |known|} begin m:=cur←mod; get←x←next; scan←expression; if internal[proofing]>=0 then if cur←type<>m then @<Complain about improper special operation@> else begin check←gf; if m=string←type then gf←string(cur←exp,0) else begin gf←out(yyy); gf←four(cur←exp); end; end; flush←cur←exp(0); end; @ @<Complain about improper special operation@>= begin exp←err("Unsuitable expression"); @.Unsuitable expression@> help1("The expression shown above has the wrong type to be output."); put←get←error; end @ @<Send the current expression as a title to the output file@>= begin check←gf; gf←string("title ",cur←exp); end @ @<Cases of |print←cmd...@>= special←command:if m=known then print("numspecial") else print("special"); @ @<Determine if a character has been shipped out@>= begin cur←exp:=round←unscaled(cur←exp) mod 256; if cur←exp<0 then cur←exp:=cur←exp+256; boolean←reset(char←exists[cur←exp]); cur←type:=boolean←type; end @ At the end of the program we must finish things off by writing the postamble. The \.{TFM} information should have been computed first. An integer variable |k| and a |scaled| variable |x| will be declared for use by this routine. @<Finish the \.{GF} file@>= begin gf←out(post); {beginning of the postamble} gf←four(gf←prev←ptr); gf←prev←ptr:=gf←offset+gf←ptr-5; {|post| location} gf←four(internal[design←size]*16); for k:=1 to 4 do gf←out(header←byte[k]); {the check sum} gf←four(internal[hppp]); gf←four(internal[vppp]);@/ gf←four(gf←min←m); gf←four(gf←max←m); gf←four(gf←min←n); gf←four(gf←max←n); for k:=0 to 255 do if char←exists[k] then begin x:=gf←dx[k] div unity; if (gf←dy[k]=0)and(x>=0)and(x<256)and(gf←dx[k]=x*unity) then begin gf←out(char←loc+1); gf←out(k); gf←out(x); end else begin gf←out(char←loc); gf←out(k); gf←four(gf←dx[k]); gf←four(gf←dy[k]); end; x:=value(tfm←width[k]); if abs(x)>max←tfm←dimen then if x>0 then x:=three←bytes-1@+else x:=1-three←bytes else x:=make←scaled(x*16,internal[design←size]); gf←four(x); gf←four(char←ptr[k]); end; gf←out(post←post); gf←four(gf←prev←ptr); gf←out(gf←id←byte);@/ k:=4+((gf←buf←size-gf←ptr) mod 4); {the number of 223's} while k>0 do begin gf←out(223); decr(k); end; @<Empty the last bytes out of |gf←buf|@>; print←nl("Output written on "); print(output←file←name); @.Output written...@> print(" ("); print←int(total←chars); print(" character"); if total←chars<>1 then print←char("s"); print(", "); print←int(gf←offset+gf←ptr); print(" bytes)."); b←close(gf←file); end @* \[48] Dumping and undumping the tables. After \.{INIMF} has seen a collection of macros, it can write all the necessary information on an auxiliary file so that production versions of \MF\ are able to initialize their memory at high speed. The present section of the program takes care of such output and input. We shall consider simultaneously the processes of storing and restoring, so that the inverse relation between them is clear. @.INIMF@> The global variable |base←ident| is a string that is printed right after the |banner| line when \MF\ is ready to start. For \.{INIMF} this string says simply `\.{(INIMF)}'; for other versions of \MF\ it says, for example, `\.{(preloaded base=plain 84.2.29)}', showing the year, month, and day that the base file was created. We have |base←ident=0| before \MF's tables are loaded. @<Glob...@>= @!base←ident:str←number; @ @<Set init...@>= base←ident:=0; @ @<Initialize table entries...@>= base←ident:=" (INIMF)"; @ @<Declare act...@>= @!init procedure store←base←file; var @!k:integer; {all-purpose index} @!p,@!q: pointer; {all-purpose pointers} @!x: integer; {something to dump} @!w: four←quarters; {four ASCII codes} begin @<Create the |base←ident|, open the base file, and inform the user that dumping has begun@>; @<Dump constants for consistency check@>; @<Dump the string pool@>; @<Dump the dynamic memory@>; @<Dump the table of equivalents and the hash table@>; @<Dump a few more things and the closing check word@>; @<Close the base file@>; end; tini @ Corresponding to the procedure that dumps a base file, we have a function that reads one in. The function returns |false| if the dumped base is incompatible with the present \MF\ table sizes, etc. @d off←base=6666 {go here if the base file is unacceptable} @d too←small(#)==begin wake←up←terminal; wterm←ln('---! Must increase the ',#); @.Must increase the x@> goto off←base; end @p @t\4@>@<Declare the function called |open←base←file|@>@; function load←base←file:boolean; label off←base,exit; var @!k:integer; {all-purpose index} @!p,@!q: pointer; {all-purpose pointers} @!x: integer; {something undumped} @!w: four←quarters; {four ASCII codes} begin @<Undump constants for consistency check@>; @<Undump the string pool@>; @<Undump the dynamic memory@>; @<Undump the table of equivalents and the hash table@>; @<Undump a few more things and the closing check word@>; load←base←file:=true; return; {it worked!} off←base: wake←up←terminal; wterm←ln('(Fatal base file error; I''m stymied)'); @.Fatal base file error@> load←base←file:=false; exit:end; @ Base files consist of |memory←word| items, and we use the following macros to dump words of different types: @d dump←wd(#)==begin base←file↑:=#; put(base←file);@+end @d dump←int(#)==begin base←file↑.int:=#; put(base←file);@+end @d dump←hh(#)==begin base←file↑.hh:=#; put(base←file);@+end @d dump←qqqq(#)==begin base←file↑.qqqq:=#; put(base←file);@+end @<Glob...@>= @!base←file:word←file; {for input or output of base information} @ The inverse macros are slightly more complicated, since we need to check the range of the values we are reading in. We say `|undump(a)(b)(x)|' to read an integer value |x| that is supposed to be in the range |a<=x<=b|. @d undump←wd(#)==begin get(base←file); #:=base←file↑;@+end @d undump←int(#)==begin get(base←file); #:=base←file↑.int;@+end @d undump←hh(#)==begin get(base←file); #:=base←file↑.hh;@+end @d undump←qqqq(#)==begin get(base←file); #:=base←file↑.qqqq;@+end @d undump←end←end(#)==#:=x;@+end @d undump←end(#)==(x>#) then goto off←base@+else undump←end←end @d undump(#)==begin undump←int(x); if (x<#) or undump←end @d undump←size←end←end(#)==too←small(#)@+else undump←end←end @d undump←size←end(#)==if x># then undump←size←end←end @d undump←size(#)==begin undump←int(x); if x<# then goto off←base; undump←size←end @ The next few sections of the program should make it clear how we use the dump/undump macros. @<Dump constants for consistency check@>= dump←int(@$);@/ dump←int(mem←min);@/ dump←int(mem←top);@/ dump←int(hash←size);@/ dump←int(hash←prime);@/ dump←int(max←in←open) @ Sections of a \.{WEB} program that are ``commented out'' still contribute strings to the string pool; therefore \.{INIMF} and \MF\ will have the same strings. (And it is, of course, a good thing that they do.) @.WEB@> @↑string pool@> @<Undump constants for consistency check@>= x:=base←file↑.int; if x<>@$ then goto off←base; {check that strings are the same} undump←int(x); if x<>mem←min then goto off←base; undump←int(x); if x<>mem←top then goto off←base; undump←int(x); if x<>hash←size then goto off←base; undump←int(x); if x<>hash←prime then goto off←base; undump←int(x); if x<>max←in←open then goto off←base @ @d dump←four←ASCII== w.b0:=str←pool[k]; w.b1:=str←pool[k+1]; w.b2:=str←pool[k+2]; w.b3:=str←pool[k+3]; dump←qqqq(w) @<Dump the string pool@>= dump←int(pool←ptr); dump←int(str←ptr); for k:=0 to str←ptr do dump←int(str←start[k]); k:=0; while k+4<pool←ptr do begin dump←four←ASCII; k:=k+4; end; k:=pool←ptr-4; dump←four←ASCII; print←ln; print←int(str←ptr); print(" strings of total length "); print←int(pool←ptr) @ @d undump←four←ASCII== undump←qqqq(w); str←pool[k]:=w.b0; str←pool[k+1]:=w.b1; str←pool[k+2]:=w.b2; str←pool[k+3]:=w.b3 @<Undump the string pool@>= undump←size(0)(pool←size)('string pool size')(pool←ptr); undump←size(0)(max←strings)('max strings')(str←ptr); for k:=0 to str←ptr do begin undump(0)(pool←ptr)(str←start[k]); str←ref[k]:=max←str←ref; end; k:=0; while k+4<pool←ptr do begin undump←four←ASCII; k:=k+4; end; k:=pool←ptr-4; undump←four←ASCII @ By sorting the list of available spaces in the variable-size portion of |mem|, we are usually able to get by without having to dump very much of the dynamic memory. We recompute |var←used| and |dyn←used|, so that \.{INIMF} dumps valid information even when it has not been gathering statistics. @<Dump the dynamic memory@>= sort←avail; var←used:=0; dump←int(lo←mem←max); dump←int(rover); p:=mem←min; q:=rover; x:=0; repeat for k:=p to q+1 do dump←wd(mem[k]); x:=x+q+2-p; var←used:=var←used+q-p; p:=q+node←size(q); q:=rlink(q); until q=rover; var←used:=var←used+lo←mem←max-p; dyn←used:=mem←end+1-hi←mem←min;@/ for k:=p to lo←mem←max do dump←wd(mem[k]); x:=x+lo←mem←max+1-p; dump←int(hi←mem←min); dump←int(avail); for k:=hi←mem←min to mem←end do dump←wd(mem[k]); x:=x+mem←end+1-hi←mem←min; p:=avail; while p<>null do begin decr(dyn←used); p:=link(p); end; dump←int(var←used); dump←int(dyn←used); print←ln; print←int(x); print(" memory locations dumped; current usage is "); print←int(var←used); print←char("&"); print←int(dyn←used) @ @<Undump the dynamic memory@>= undump(lo←mem←stat←max+1000)(hi←mem←stat←min-1)(lo←mem←max); undump(lo←mem←stat←max+1)(lo←mem←max)(rover); p:=mem←min; q:=rover; x:=0; repeat for k:=p to q+1 do undump←wd(mem[k]); p:=q+node←size(q); if (p>lo←mem←max)or((q>=rlink(q))and(rlink(q)<>rover)) then goto off←base; q:=rlink(q); until q=rover; for k:=p to lo←mem←max do undump←wd(mem[k]); undump(lo←mem←max+1)(hi←mem←stat←min)(hi←mem←min); undump(null)(mem←top)(avail); mem←end:=mem←top; for k:=hi←mem←min to mem←end do undump←wd(mem[k]); undump←int(var←used); undump←int(dyn←used) @ A different scheme is used to compress the hash table, since its lower region is usually sparse. When |text(p)<>0| for |p<=hash←used|, we output three words: |p|, |hash[p]|, and |eqtb[p]|. The hash table is, of course, densely packed for |p>=hash←used|, so the remaining entries are output in a block. @<Dump the table of equivalents and the hash table@>= dump←int(hash←used); st←count:=frozen←inaccessible-1-hash←used; for p:=1 to hash←used do if text(p)<>0 then begin dump←int(p); dump←hh(hash[p]); dump←hh(eqtb[p]); incr(st←count); end; for p:=hash←used+1 to hash←end do begin dump←hh(hash[p]); dump←hh(eqtb[p]); end; dump←int(st←count);@/ print←ln; print←int(st←count); print(" symbolic tokens") @ @<Undump the table of equivalents and the hash table@>= undump(1)(frozen←inaccessible)(hash←used); p:=0; repeat undump(p+1)(hash←used)(p); undump←hh(hash[p]); undump←hh(eqtb[p]); until p=hash←used; for p:=hash←used+1 to hash←end do begin undump←hh(hash[p]); undump←hh(eqtb[p]); end; undump←int(st←count) @ We have already printed a lot of statistics, so we set |tracing←stats:=0| to prevent them appearing again. @<Dump a few more things and the closing check word@>= dump←int(int←ptr); for k:=1 to int←ptr do begin dump←int(internal[k]); dump←int(int←name[k]); end; dump←int(start←sym); dump←int(interaction); dump←int(base←ident); dump←int(bg←loc); dump←int(eg←loc); dump←int(69069); internal[tracing←stats]:=0 @ @<Undump a few more things and the closing check word@>= undump(max←given←internal)(max←internal)(int←ptr); for k:=1 to int←ptr do begin undump←int(internal[k]); undump(0)(str←ptr)(int←name[k]); end; undump(0)(frozen←inaccessible)(start←sym); undump(batch←mode)(error←stop←mode)(interaction); undump(0)(str←ptr)(base←ident); undump(1)(hash←end)(bg←loc); undump(1)(hash←end)(eg←loc); undump←int(x); if (x<>69069)or eof(base←file) then goto off←base @ @<Create the |base←ident|...@>= selector:=new←string; print(" (preloaded base="); print(job←name); print←char(" "); print←int(round←unscaled(internal[year]) mod 100); print←char("."); print←int(round←unscaled(internal[month])); print←char("."); print←int(round←unscaled(internal[day])); print←char(")"); if interaction=batch←mode then selector:=log←only else selector:=term←and←log; str←room(1); base←ident:=make←string; str←ref[base←ident]:=max←str←ref;@/ pack←job←name(".base"); while not w←open←out(base←file) do prompt←file←name("base file name",".base"); print←nl("Beginning to dump on file "); @.Beginning to dump...@> print(w←make←name←string(base←file)); flush←string(str←ptr-1); print←nl(base←ident) @ @<Close the base file@>= w←close(base←file) @* \[49] The main program. This is it: the part of \MF\ that executes all those procedures we have written. Well---almost. We haven't put the parsing subroutines into the program yet; and we'd better leave space for a few more routines that may have been forgotten. @p @<Declare the basic parsing subroutines@>@; @<Declare miscellaneous procedures that were declared |forward|@>@; @<Last-minute procedures@> @ We've noted that there are two versions of \MF84. One, called \.{INIMF}, @.INIMF@> has to be run first; it initializes everything from scratch, without reading a base file, and it has the capability of dumping a base file. The other one is called `\.{VIRMF}'; it is a ``virgin'' program that needs @.VIRMF@> to input a base file in order to get started. \.{VIRMF} typically has a bit more memory capacity than \.{INIMF}, because it does not need the space consumed by the dumping/undumping routines and the numerous calls on |primitive|, etc. The \.{VIRMF} program cannot read a base file instantaneously, of course; the best implementations therefore allow for production versions of \MF\ that not only avoid the loading routine for \PASCAL\ object code, they also have a base file pre-loaded. This is impossible to do if we stick to standard \PASCAL, but there is a simple way to fool many systems into avoiding the initialization, as follows.\quad(1)~We declare a global integer variable called |ready←already|. The probability is negligible that this variable holds any particular value like 314159 when \.{VIRMF} is first loaded.\quad(2)~After we have read in a base file and initialized everything, we set |ready←already:=314159|.\quad(3)~Soon \.{VIRMF} will print `\.*', waiting for more input; and at this point we interrupt the program and save its core image in some form that the operating system can reload speedily.\quad(4)~When that core image is activated, the program starts again at the beginning; but now |ready←already=314159| and all the other global variables have their initial values too. The former chastity has vanished! In other words, if we allow ourselves to test the condition |ready←already=314159|, before |ready←already| has been assigned a value, we can avoid the lengthy initialization. Dirty tricks rarely pay off so handsomely. @↑dirty \PASCAL@> @↑system dependencies@> On systems that allow such preloading, the standard program called \.{MF} should be the one that has \.{plain} base preloaded, since that agrees with {\sl The {\logos METAFONT\/}book}. Other versions, e.g., \.{cmbase}, should also be provided for commonly used bases. @:METAFONTbook}{\sl The {\logos METAFONT\/}book@> @.cmbase@> @.plain@> @<Glob...@>= @!ready←already:integer; {a sacrifice of purity for economy} @ Now this is really it: \MF\ starts and ends here. The initial test involving |ready←already| should be deleted if the \PASCAL\ runtime system is smart enough to detect such a ``mistake.'' @↑system dependencies@> @p begin @!{|start←here|} history:=fatal←error←stop; {in case we quit during initialization} t←open←out; {open the terminal for output} if ready←already=314159 then goto start←of←MF; @<Check the ``constant'' values...@>@; if bad>0 then begin wterm←ln('Ouch---my internal constants have been clobbered!', '---case ',bad:1); @.Ouch...clobbered@> goto final←end; end; initialize; {set global variables to their starting values} @!init if not get←strings←started then goto final←end; init←tab; {initialize the tables} init←prim; {call |primitive| for each primitive} tini@/ ready←already:=314159; start←of←MF: @<Initialize the output routines@>; @<Get the first line of input and prepare to start@>; init←str←ptr:=str←ptr; init←pool←ptr:=pool←ptr;@/ max←str←ptr:=str←ptr; max←pool←ptr:=pool←ptr;@/ history:=spotless; {ready to go!} if start←sym>0 then {insert the `\&{everyjob}' symbol} begin cur←sym:=start←sym; back←input; end; main←control; {come to life} final←cleanup; {prepare for death} end←of←MF: close←files←and←terminate; final←end: ready←already:=0; end. @ Here we do whatever is needed to complete \MF's job gracefully on the local operating system. The code here might come into play after a fatal error; it must therefore consist entirely of ``safe'' operations that cannot produce error messages. For example, it would be a mistake to call |str←room| or |make←string| at this time, because a call on |overflow| might lead to an infinite loop. @↑system dependencies@> @<Last-minute...@>= procedure close←files←and←terminate; var @!k:integer; {all-purpose index} @!lh:integer; {the length of the \.{TFM} header, in words} @!p:pointer; {runs through a list of \.{TFM} dimensions} @!x:scaled; {a |tfm←width| value being output to the \.{GF} file} begin @!stat if internal[tracing←stats]>0 then @<Output statistics about this job@>;@;@+tats@/ wake←up←terminal; @<Finish the \.{TFM} and \.{GF} files@>; if job←name>0 then begin wlog←cr; a←close(log←file); selector:=selector-2; if selector=term←only then begin print←nl("Transcript written on "); @.Transcript written...@> print(log←name); print←char("."); end; end; end; @ We want to finish the \.{GF} file if and only if it has already been started; this will be true if and only if |gf←prev←ptr| is positive. We want to produce a \.{TFM} file if and only if |fontmaking| is positive. The \.{TFM} widths must be computed if there's a \.{GF} file, even if there's going to be no \.{TFM}. We reclaim all of the variable-size memory at this point, so that there is no chance of another memory overflow after the memory capacity has already been exceeded. @<Finish the \.{TFM} and \.{GF} files@>= if (gf←prev←ptr>0)or(internal[fontmaking]>0) then begin @<Make the dynamic memory into one big available node@>; @<Massage the \.{TFM} widths@>; fix←design←size; fix←check←sum; if internal[fontmaking]>0 then begin @<Massage the \.{TFM} heights, depths, and italic corrections@>; @<Finish the \.{TFM} file@>; end; if gf←prev←ptr>0 then @<Finish the \.{GF} file@>; end @ @<Make the dynamic memory into one big available node@>= rover:=lo←mem←stat←max+1; link(rover):=empty←flag; lo←mem←max:=hi←mem←min-1; if lo←mem←max-rover>max←halfword then lo←mem←max:=max←halfword+rover; node←size(rover):=lo←mem←max-rover; llink(rover):=rover; rlink(rover):=rover; link(lo←mem←max):=null; info(lo←mem←max):=null @ The present section goes directly to the log file instead of using |print| commands, because there's no need for these strings to take up |str←pool| memory when a non-{\bf stat} version of \MF\ is being used. @<Output statistics...@>= if job←name>0 then {the log file is open} begin wlog←ln(' '); wlog←ln('Here is how much of METAFONT''s memory',' you used:'); @.Here is how much...@> wlog(' ',max←str←ptr-init←str←ptr:1,' string'); if max←str←ptr<>init←str←ptr+1 then wlog('s'); wlog←ln(' out of ', max←strings-init←str←ptr:1);@/ wlog←ln(' ',max←pool←ptr-init←pool←ptr:1,' string characters out of ', pool←size-init←pool←ptr:1);@/ wlog←ln(' ',lo←mem←max-mem←min+mem←end-hi←mem←min+2:1,@| ' words of memory out of ',mem←end+1-mem←min:1);@/ wlog←ln(' ',st←count:1,' symbolic tokens out of ', hash←size:1);@/ wlog←ln(' ',max←in←stack:1,'i,',@| int←ptr:1,'n,',@| max←rounding←ptr:1,'r,',@| max←param←stack:1,'p,',@| max←buf←stack+1:1,'b stack positions out of ',@| stack←size:1,'i,', max←internal:1,'n,', max←wiggle:1,'r,', param←size:1,'p,', buf←size:1,'b'); end @ We get to the |final←cleanup| routine when \&{end} or \&{dump} has been scanned. @<Last-minute...@>= procedure final←cleanup; label exit; var c:small←number; {0 for \&{end}, 1 for \&{dump}} begin c:=cur←mod; if job←name=0 then open←log←file; while cond←ptr<>null do begin print←nl("(end occurred when "); @.end occurred...@> print←cmd←mod(fi←or←else,cur←if); {"if" or "elseif" or "else"} if if←line<>0 then begin print(" on line "); print←int(if←line); end; print(" was incomplete)"); if←line:=if←line←field(cond←ptr); cur←if:=name←type(cond←ptr); cond←ptr:=link(cond←ptr); end; if history<>spotless then if ((history=warning←issued)or(interaction<error←stop←mode)) then if selector=term←and←log then begin selector:=term←only; print←nl("(see the transcript file for additional information)"); @.see the transcript file...@> selector:=term←and←log; end; if c=1 then begin @!init store←base←file; return;@+tini@/ print←nl("(dump is performed only by INIMF)"); return; @.dump...only by INIMF@> end; exit:end; @ @<Last-minute...@>= @!init procedure init←prim; {initialize all the primitives} begin @<Put each...@>; end; @# procedure init←tab; {initialize other tables} var @!k:integer; {all-purpose index} begin @<Initialize table entries (done by \.{INIMF} only)@>@; end; tini @ When we begin the following code, \MF's tables may still contain garbage; the strings might not even be present. Thus we must proceed cautiously to get bootstrapped in. But when we finish this part of the program, \MF\ is ready to call on the |main←control| routine to do its work. @<Get the first line...@>= begin @<Initialize the input routines@>; if (base←ident=0)or(buffer[loc]="&") then begin if base←ident<>0 then initialize; {erase preloaded base} if not open←base←file then goto final←end; if not load←base←file then begin w←close(base←file); goto final←end; end; w←close(base←file); while (loc<limit)and(buffer[loc]=" ") do incr(loc); end; buffer[limit]:="%";@/ fix←date←and←time; init←randoms((internal[time] div unity)+internal[day]);@/ @<Initialize the print |selector|...@>; if loc<limit then if buffer[loc]<>"\" then start←input; {\&{input} assumed} end @* \[50] Debugging. Once \MF\ is working, it should be possible to diagnose most errors with the \.{show} commands and other diagnostic features. But for the initial stages of debugging, and for the revelation of really deep mysteries, you can compile \MF\ with a few more aids including the \PASCAL\ runtime checks and its debugger. An additional routine called |debug←help| will also come into play when you type `\.D' after an error message; |debug←help| also occurs just before a fatal error causes \MF\ to succumb. @↑debugging@> @↑system dependencies@> The interface to |debug←help| is primitive, but it is good enough when used with a \PASCAL\ debugger that allows you to set breakpoints and to read variables and change their values. After getting the prompt `\.{debug \#}', you type either a negative number (this exits |debug←help|), or zero (this goes to a location where you can set a breakpoint, thereby entering into dialog with the \PASCAL\ debugger), or a positive number |m| followed by an argument |n|. The meaning of |m| and |n| will be clear from the program below. (If |m=13|, there is an additional argument, |l|.) @.debug \#@> @d breakpoint=888 {place where a breakpoint is desirable} @<Last-minute...@>= @!debug procedure debug←help; {routine to display various things} label breakpoint,exit; var @!k,@!l,@!m,@!n:integer; begin loop begin wake←up←terminal; print←nl("debug # (-1 to exit):"); update←terminal; @.debug \#@> read(term←in,m); if m<0 then return else if m=0 then begin goto breakpoint;@\ {go to every label at least once} breakpoint: m:=0; @{'BREAKPOINT'@}@\ end else begin read(term←in,n); case m of @t\4@>@<Numbered cases for |debug←help|@>@; othercases print("?") endcases; end; end; exit:end; gubed @ @<Numbered cases...@>= 1: print←word(mem[n]); {display |mem[n]| in all forms} 2: print←int(info(n)); 3: print←int(link(n)); 4: begin print←int(eq←type(n)); print←char(":"); print←int(equiv(n)); end; 5: print←variable←name(n); 6: print←int(internal[n]); 7: do←show←dependencies; 9: show←token←list(n,null,100000,0); 10: print(n); 11: check←mem(n>0); {check wellformedness; print new busy locations if |n>0|} 12: search←mem(n); {look for pointers to |n|} 13: begin read(term←in,l); print←cmd←mod(n,l); end; 14: for k:=0 to n do print(buffer[k]); 15: panicking:=not panicking; @* \[51] System-dependent changes. This section should be replaced, if necessary, by changes to the program that are necessary to make \MF\ work at a particular installation. It is usually best to design your change file so that all changes to previous sections preserve the section numbering; then everybody's version will be consistent with the published program. More extensive changes, which introduce new sections, can be inserted here; then only the index itself will get a new section number. @↑system dependencies@> @* \[52] Index. Here is where you can find all uses of each identifier in the program, with underlined entries pointing to where the identifier was defined. If the identifier is only one letter long, however, you get to see only the underlined entries. All references are to section numbers instead of page numbers. This index also lists error messages and other aspects of the program that you might want to look up some day. For example, the entry for ``system dependencies'' lists all sections that should receive special attention from people who are installing \MF\ in a new operating environment. A list of various things that can't happen appears under ``this can't happen''. Approximately 25 sections are listed under ``inner loop''; these account for more than 60\% of \MF's running time, exclusive of input and output.