| 1 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 |
% |
| 3 |
% Module: ZZTeX Programming Constructs |
| 4 |
% |
| 5 |
% Synopsis: This module contains definitions for ZZTeX's programming |
| 6 |
% constructs. These constructs provide various extensions |
| 7 |
% to TeX's sometimes meager repertoire of programming |
| 8 |
% features. |
| 9 |
% |
| 10 |
% Author: Paul C. Anagnostopoulos |
| 11 |
% Created: 27 March 1989 |
| 12 |
% |
| 13 |
% Copyright 1989--2020 by Paul C. Anagnostopoulos |
| 14 |
% under The MIT License (opensource.org/licenses/MIT) |
| 15 |
% |
| 16 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 17 |
|
| 18 |
% Token Hackery |
| 19 |
% ----- ------- |
| 20 |
|
| 21 |
|
| 22 |
% We need a name for a space token. |
| 23 |
|
| 24 |
\def \\{\let \zspacetoken = } \\ % Now \zspacetoken is a space token. |
| 25 |
|
| 26 |
% This macro discards the next token. |
| 27 |
|
| 28 |
\def \discardtok #1{}% token |
| 29 |
|
| 30 |
% This macro removes the `pt' following a dimension. |
| 31 |
|
| 32 |
{\catcode `\p = \catother \catcode `\t = \catother |
| 33 |
|
| 34 |
\gdef \zremovedu #1pt{#1} |
| 35 |
|
| 36 |
} % \catcode |
| 37 |
|
| 38 |
% Using that macro, we can define a macro that "converts" a dimen register |
| 39 |
% into a factor. |
| 40 |
|
| 41 |
\def \thefactor #1{\expandafter\zremovedu \the#1} |
| 42 |
|
| 43 |
% This macro extracts the contents of a macro and returns it as plain text. |
| 44 |
% Usage: \expandafter\zdefof \meaning\macro\zmark |
| 45 |
|
| 46 |
\def \zdefof #1:->#2\zmark{#2} |
| 47 |
|
| 48 |
% Control Sequence Names |
| 49 |
% ------- -------- ----- |
| 50 |
|
| 51 |
|
| 52 |
\def \name #1{% {\tokens} |
| 53 |
\csname \expandafter\discardtok \string#1\endcsname} |
| 54 |
|
| 55 |
\def \withname #1#2{% {\command}{\tokens} |
| 56 |
\expandafter#1\csname \expandafter\discardtok \string#2\endcsname} |
| 57 |
|
| 58 |
% Macro Expansion |
| 59 |
% ----- --------- |
| 60 |
|
| 61 |
|
| 62 |
\def \expandaftertwice {\expandafter\expandafter\expandafter} |
| 63 |
\def \expandafterthrice {\expandafter\expandafter\expandafter\expandafter |
| 64 |
\expandafter\expandafter\expandafter} |
| 65 |
|
| 66 |
\def \zoneoftwo #1#2{#1}% {first}{second} |
| 67 |
\def \ztwooftwo #1#2{#2}% {first}{second} |
| 68 |
|
| 69 |
\def \zoneofthree #1#2#3{#1}% {first}{second}{third} |
| 70 |
\def \ztwoofthree #1#2#3{#2}% {first}{second}{third} |
| 71 |
\def \zthreeofthree #1#2#3{#3}% {first}{second}{third} |
| 72 |
|
| 73 |
\def \zoneoffour #1#2#3#4{#1}% {first}{second}{third}{fourth} |
| 74 |
\def \ztwooffour #1#2#3#4{#2}% {first}{second}{third}{fourth} |
| 75 |
\def \zthreeoffour #1#2#3#4{#3}% {first}{second}{third}{fourth} |
| 76 |
\def \zfouroffour #1#2#3#4{#4}% {first}{second}{third}{fourth} |
| 77 |
|
| 78 |
% Flags (Booleans) |
| 79 |
% ----- ---------- |
| 80 |
|
| 81 |
% The boolean literals \true and \false are appropriate for use with |
| 82 |
% the \if command, which tests the codes of the next two characters. |
| 83 |
|
| 84 |
\def \true {TT} |
| 85 |
\def \false {FL} |
| 86 |
|
| 87 |
\def \setflag #1=#2{\edef #1{#2}}% \flag = boolean |
| 88 |
|
| 89 |
% IF and Predicates |
| 90 |
% -- --- ---------- |
| 91 |
|
| 92 |
% A "predicate" is a macro that returns \true or \false as its value. |
| 93 |
% Such values are suitable for use with the \if conditional. For example: |
| 94 |
% |
| 95 |
% \if \oddp{\x} <then-clause> \else <else-clause> \fi |
| 96 |
|
| 97 |
% A predicate can be used with \setflag as follows: |
| 98 |
% |
| 99 |
% \setflag \flag = {<predicate>} |
| 100 |
|
| 101 |
% Here are the predicates for TeX's repertoire of conditional |
| 102 |
% commands. These might be more appropriately interspersed with |
| 103 |
% other definitions in this module, but what the heck. |
| 104 |
% Some additional "obvious" predicates are defined. |
| 105 |
|
| 106 |
|
| 107 |
\def \eqlp #1#2{\ifnum #1 = #2\true \else \false \fi} |
| 108 |
\def \neqlp #1#2{\ifnum #1 = #2\false \else \true \fi} |
| 109 |
\def \lssp #1#2{\ifnum #1 < #2\true \else \false \fi} |
| 110 |
\def \gtrp #1#2{\ifnum #1 > #2\true \else \false \fi} |
| 111 |
\def \zerop #1{\ifnum #1 = 0\true \else \false \fi} |
| 112 |
\def \onep #1{\ifnum #1 = 1\true \else \false \fi} |
| 113 |
\def \posp #1{\ifnum #1 > 0\true \else \false \fi} |
| 114 |
\def \negp #1{\ifnum #1 < 0\true \else \false \fi} |
| 115 |
\def \oddp #1{\ifodd #1\true \else \false \fi} |
| 116 |
\def \evenp #1{\ifodd #1\false \else \true \fi} |
| 117 |
\def \rangep #1#2#3{\if \orp{\lssp{#1}{#2}}{\gtrp{#1}{#3}}\false \else |
| 118 |
\true \fi} |
| 119 |
\def \tensp #1{\rangep{#1}{10}{19}} |
| 120 |
|
| 121 |
\def \dimeqlp #1#2{\ifdim #1 = #2\true \else \false \fi} |
| 122 |
\def \dimneqlp #1#2{\ifdim #1 = #2\false \else \true \fi} |
| 123 |
\def \dimlssp #1#2{\ifdim #1 < #2\true \else \false \fi} |
| 124 |
\def \dimgtrp #1#2{\ifdim #1 > #2\true \else \false \fi} |
| 125 |
\def \dimzerop #1{\ifdim #1 = 0pt\true \else \false \fi} |
| 126 |
\def \dimposp #1{\ifdim #1 > 0pt\true \else \false \fi} |
| 127 |
\def \dimnegp #1{\ifdim #1 < 0pt\true \else \false \fi} |
| 128 |
|
| 129 |
\def \vmodep {\ifvmode \true \else \false \fi} |
| 130 |
\def \hmodep {\ifhmode \true \else \false \fi} |
| 131 |
\def \mathmodep {\ifmmode \true \else \false \fi} |
| 132 |
\def \textmodep {\ifmmode \false \else \true \fi} |
| 133 |
\def \innermodep {\ifinner \true \else \false \fi} |
| 134 |
|
| 135 |
\long\def \codeeqlp #1#2{\if #1#2\true \else \false \fi} |
| 136 |
|
| 137 |
\long\def \cateqlp #1#2{\ifcat #1#2\true \else \false \fi} |
| 138 |
|
| 139 |
\long\def \tokeqlp #1#2{\ifx #1#2\true \else \false \fi} |
| 140 |
\long\def \xtokeqlp #1#2{\expandafter\ifx #1#2\true \else \false \fi} |
| 141 |
|
| 142 |
\long\def \definedp #1{% |
| 143 |
\expandafter\ifx \csname \expandafter\discardtok \string#1\endcsname |
| 144 |
\relax \false \else \true \fi} |
| 145 |
|
| 146 |
\long\def \undefinedp #1{% |
| 147 |
\expandafter\ifx \csname \expandafter\discardtok \string#1\endcsname |
| 148 |
\relax \true \else \false \fi} |
| 149 |
\def \zempty {} |
| 150 |
\def \emptydefp #1{\ifx #1\zempty \true \else \false \fi}% {\name} |
| 151 |
|
| 152 |
\let \emptylistp = \emptydefp |
| 153 |
|
| 154 |
\long\def \emptyargp #1{% {#n} |
| 155 |
\zempargp #1\zempargq\zmark} |
| 156 |
\long\def \zempargp #1#2\zmark{% |
| 157 |
\ifx #1\zempargq \true \else \false \fi} |
| 158 |
\def \zempargq {\zempargq} |
| 159 |
|
| 160 |
\def \emptytoksp #1{% {\tokenreg} |
| 161 |
\expandafter\zemptoksp \the#1\zmark} |
| 162 |
|
| 163 |
\long\def \zemptoksp #1\zmark{\emptyargp{#1}} |
| 164 |
|
| 165 |
\def \voidboxp #1{\ifvoid #1\true \else \false \fi} |
| 166 |
\def \hboxp #1{\ifhbox #1\true \else \false \fi} |
| 167 |
\def \vboxp #1{\ifvbox #1\true \else \false \fi} |
| 168 |
|
| 169 |
\def \eofp #1{\ifeof #1\true \else \false \fi} |
| 170 |
|
| 171 |
|
| 172 |
% Flags can also be used as predicates, as in: |
| 173 |
% |
| 174 |
% \if \flaga <then-clause> \else <else-clause> \fi |
| 175 |
|
| 176 |
|
| 177 |
% Now here we have predicates for the common logical operators. |
| 178 |
|
| 179 |
\def \notp #1{\if #1\false \else \true \fi} |
| 180 |
|
| 181 |
\def \andp #1#2{\if #1#2\else \false \fi}% {boolean1}{boolean2} |
| 182 |
|
| 183 |
\def \orp #1#2{\if #1\true \else #2\fi}% {boolean1}{boolean2} |
| 184 |
|
| 185 |
% Pseudo-Predicates |
| 186 |
% ----------------- |
| 187 |
|
| 188 |
|
| 189 |
\def \stringeql #1#2#3{% {\return-flag}{string1}{string2} |
| 190 |
\edef \zstra {#2}% |
| 191 |
\edef \zstrb {#3}% |
| 192 |
\edef #1{\ifx \zstra\zstrb \true \else \false \fi}} |
| 193 |
|
| 194 |
|
| 195 |
\def \gluevaries #1#2{% {\return-flag}{glue} |
| 196 |
\zskipa = #2\relax |
| 197 |
\tdimena = \zskipa |
| 198 |
\stringeql{#1}{\the\zskipa}{\the\tdimena}% |
| 199 |
\edef #1{\if #1\false \else \true \fi}} |
| 200 |
|
| 201 |
% Arithmetic |
| 202 |
% ---------- |
| 203 |
|
| 204 |
|
| 205 |
\def \negate #1{\multiply #1 by -1\relax}% {\register} |
| 206 |
|
| 207 |
\def \increment #1{\advance #1 by 1\relax}% {\count} |
| 208 |
|
| 209 |
\def \decrement #1{\advance #1 by -1\relax}% {\count} |
| 210 |
|
| 211 |
% Looping |
| 212 |
% ------- |
| 213 |
|
| 214 |
|
| 215 |
\def \loop #1\repeat{% body [actions] \if condition [actions] \repeat |
| 216 |
\def \zloopbody {#1}% |
| 217 |
\zloop} |
| 218 |
|
| 219 |
\def \zloop {% |
| 220 |
\zloopbody |
| 221 |
%\if condition |
| 222 |
\let \zloopnext = \zloop |
| 223 |
\else |
| 224 |
\let \zloopnext = \relax |
| 225 |
\fi |
| 226 |
\zloopnext} |
| 227 |
|
| 228 |
\let \repeat = \fi |