| 1 |
%% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \newdimen {Allocators for registers <2023-02-03>} % preloaded in format |
| 4 |
|
| 5 |
\_doc ----------------------------- |
| 6 |
The limits are set first. |
| 7 |
\_cod ----------------------------- |
| 8 |
|
| 9 |
\_chardef\_maicount = 65535 % Max Allocation Index for counts registers in LuaTeX |
| 10 |
\_let\_maidimen = \_maicount |
| 11 |
\_let\_maiskip = \_maicount |
| 12 |
\_let\_maimuskip = \_maicount |
| 13 |
\_let\_maibox = \_maicount |
| 14 |
\_let\_maitoks = \_maicount |
| 15 |
\_chardef\_mairead = 15 |
| 16 |
\_chardef\_maiwrite = 15 |
| 17 |
\_chardef\_maifam = 255 |
| 18 |
\_chardef\_mailanguage = 16380 % In fact 16383, but we reserve next numbers for dummy patterns |
| 19 |
|
| 20 |
\_doc ----------------------------- |
| 21 |
Each allocation macro needs its own counter. |
| 22 |
\_cod ----------------------------- |
| 23 |
|
| 24 |
\_countdef\_countalloc=10 \_countalloc=255 |
| 25 |
\_countdef\_dimenalloc=11 \_dimenalloc=255 |
| 26 |
\_countdef\_skipalloc=12 \_skipalloc=255 |
| 27 |
\_countdef\_muskipalloc=13 \_muskipalloc=255 |
| 28 |
\_countdef\_boxalloc=14 \_boxalloc=255 |
| 29 |
\_countdef\_toksalloc=15 \_toksalloc=255 |
| 30 |
\_countdef\_readalloc=16 \_readalloc=-1 |
| 31 |
\_countdef\_writealloc=17 \_writealloc=0 % should be -1 but there is bug in new luatex |
| 32 |
\_countdef\_famalloc=18 \_famalloc=42 % \newfam are 43, 44, 45, ... |
| 33 |
\_countdef\_languagealloc=19 \_languagealloc=0 |
| 34 |
|
| 35 |
\_doc ----------------------------- |
| 36 |
The common allocation macro |
| 37 |
\`\_allocator` `\<sequence> {<type>} \<primitive declarator>` |
| 38 |
is defined. This idea was used in classical plain \TeX/ by |
| 39 |
Donald Knuth too but the macro from plain \TeX/ seems to be |
| 40 |
more complicated:). |
| 41 |
\_cod ----------------------------- |
| 42 |
|
| 43 |
\_def\_allocator #1#2#3{% |
| 44 |
\_incr{\_cs{_#2alloc}}% |
| 45 |
\_ifnum\_cs{_#2alloc}>\_cs{_mai#2}% |
| 46 |
\_errmessage{No room for a new \_ea\_string\_csname #2\_endcsname}% |
| 47 |
\_else |
| 48 |
\_global#3#1=\_cs{_#2alloc}% |
| 49 |
\_wloga{\_string#1=\_ea\_string\_csname #2\_endcsname\_the\_cs{_#2alloc}}% |
| 50 |
\_fi |
| 51 |
} |
| 52 |
\_let\_wloga=\_wlog % you can suppress the logging by \_let\_wloga=\_ignoreit |
| 53 |
|
| 54 |
\_doc ----------------------------- |
| 55 |
The allocation macros |
| 56 |
\`\newcount`, \`\newdimen`, \`\newskip`, \`\newmuskip`, \`\newbox`, |
| 57 |
\`\newtoks`, \`\newread`, \`\newwrite`, \`\newfam`, and \`\newlanguage` |
| 58 |
are defined here. |
| 59 |
\_cod ----------------------------- |
| 60 |
|
| 61 |
\_def\_newcount #1{\_allocator #1{count}\_countdef} |
| 62 |
\_def\_newdimen #1{\_allocator #1{dimen}\_dimendef} |
| 63 |
\_def\_newskip #1{\_allocator #1{skip}\_skipdef} |
| 64 |
\_def\_newmuskip #1{\_allocator #1{muskip}\_muskipdef} |
| 65 |
\_def\_newbox #1{\_allocator #1{box}\_chardef} |
| 66 |
\_def\_newtoks #1{\_allocator #1{toks}\_toksdef} |
| 67 |
\_def\_newread #1{\_allocator #1{read}\_chardef} |
| 68 |
\_def\_newwrite #1{\_allocator #1{write}\_chardef} |
| 69 |
\_def\_newfam #1{\_allocator #1{fam}\_chardef} |
| 70 |
\_def\_newlanguage #1{\_allocator #1{language}\_chardef} |
| 71 |
|
| 72 |
\_public \newcount \newdimen \newskip \newmuskip \newbox \newtoks |
| 73 |
\newread \newwrite \newfam \newlanguage ; |
| 74 |
|
| 75 |
\_doc ----------------------------- |
| 76 |
The \`\newinsert` macro is defined differently than others. |
| 77 |
\_cod ----------------------------- |
| 78 |
|
| 79 |
\_newcount\_insertalloc \_insertalloc=255 |
| 80 |
\_chardef\_insertmin = 201 |
| 81 |
|
| 82 |
\_def\_newinsert #1{% |
| 83 |
\_decr\_insertalloc |
| 84 |
\_ifnum\_insertalloc <\_insertmin |
| 85 |
\_errmessage {No room for a new \_string\insert}% |
| 86 |
\_else |
| 87 |
\_global\_chardef#1=\_insertalloc |
| 88 |
\_wlog {\_string#1=\_string\_insert\_the\_insertalloc}% |
| 89 |
\_fi |
| 90 |
} |
| 91 |
\_public \newinsert ; |
| 92 |
|
| 93 |
\_doc ----------------------------- |
| 94 |
Other allocation macros \`\newmarks`. \`\newattribute` and \`\newcatcodetable` |
| 95 |
have their counter allocated by the `\newcount` macro. |
| 96 |
\`\_noattr` is constant `-"7FFFFFFF`, i.e. unused attribute |
| 97 |
\_cod ----------------------------- |
| 98 |
|
| 99 |
\_newcount \_marksalloc \_marksalloc=0 % start at 1, 0 is \mark |
| 100 |
\_chardef\_maimarks=\_maicount |
| 101 |
\_def\_newmarks #1{\_allocator #1{marks}\_chardef} |
| 102 |
|
| 103 |
\_newcount \_attributealloc \_attributealloc=0 |
| 104 |
\_chardef\_maiattribute=\_numexpr\_maicount -1\_relax |
| 105 |
\_attributedef\_noattr \_maicount |
| 106 |
\_def\_newattribute #1{\_allocator #1{attribute}\_attributedef} |
| 107 |
|
| 108 |
\_newcount \_catcodetablealloc \_catcodetablealloc=10 |
| 109 |
\_chardef\_maicatcodetable=32767 |
| 110 |
\_def\_newcatcodetable #1{\_allocator #1{catcodetable}\_chardef} |
| 111 |
|
| 112 |
\_public \newmarks \newattribute \newcatcodetable ; |
| 113 |
|
| 114 |
\_doc ----------------------------- |
| 115 |
We declare public and private versions of `\tmpnum` and `\tmpdim` |
| 116 |
registers separately. They are independent registers. |
| 117 |
\_cod ----------------------------- |
| 118 |
|
| 119 |
\_newcount \tmpnum \_newcount \_tmpnum |
| 120 |
\_newdimen \tmpdim \_newdimen \_tmpdim |
| 121 |
|
| 122 |
\_doc ----------------------------- |
| 123 |
A few registers: \`\maxdimen`, \`\hideskip` and |
| 124 |
\`\centering` are initialized like in plain\TeX/. We absolutely |
| 125 |
don't support the `@`category dance, so `\z@skip` |
| 126 |
`\z@`, `\p@` etc. are defined but not recommended. |
| 127 |
The \`\_zo`, \`\_zoskip` and \`\voidbox` (equivalents to |
| 128 |
`\z@`, `\z@skip` and `\voidb@x`) are preferred in \OpTeX. |
| 129 |
\_cod ----------------------------- |
| 130 |
|
| 131 |
\_newdimen\_maxdimen \_maxdimen=16383.99999pt % the largest legal <dimen> |
| 132 |
\_newskip\_hideskip \_hideskip=-1000pt plus 1fill % negative but can grow |
| 133 |
\_newskip\_centering \_centering=0pt plus 1000pt minus 1000pt |
| 134 |
\_newdimen\_zo \_zo=0pt |
| 135 |
\_newskip\_zoskip \_zoskip=0pt plus0pt minus0pt |
| 136 |
\_newbox\_voidbox % permanently void box register |
| 137 |
|
| 138 |
\_public \maxdimen \hideskip \centering \voidbox ; |
| 139 |
|
| 140 |
\_endcode %--------------------------------------------------- |
| 141 |
|
| 142 |
Like plain\TeX, the allocators `\newcount`, `\newwrite`, etc. are defined. |
| 143 |
The registers are allocated from 256 to the `\_mai<type>` which is 65535 in |
| 144 |
\LuaTeX/. |
| 145 |
|
| 146 |
Unlike in Plain\TeX/, the mentioned allocators are not `\outer`. |
| 147 |
|
| 148 |
User can use `\dimen0` to `\dimen200` and similarly for `\skip`, |
| 149 |
`\muskip`, `\box`, and `\toks` directly. User can use |
| 150 |
`\count20` to `\count200` directly too. This is the same |
| 151 |
philosophy as in old plain\TeX/, but the range of directly used registers |
| 152 |
is wider. |
| 153 |
|
| 154 |
Inserts are allocated from 254 to 201 using `\newinsert`. |
| 155 |
|
| 156 |
You can define your own allocation concept (for example for allocation of |
| 157 |
arrays) from the top of the registers array. The example shows a definition of |
| 158 |
the array-like declarator of counters. |
| 159 |
|
| 160 |
\nobreak |
| 161 |
\begtt |
| 162 |
\newcount \_maicount % redefine maximal allocation index as variable |
| 163 |
\_maicount = \maicount % first value is top of the array |
| 164 |
|
| 165 |
\def\newcountarray #1[#2]{% \newcountarray \foo[100] |
| 166 |
\global\advance\_maicount by -#2\relax |
| 167 |
\ifnum \_countalloc > \_maicount |
| 168 |
\errmessage{No room for a new array of \string\count}% |
| 169 |
\else |
| 170 |
\global\chardef#1=\_maicount |
| 171 |
\fi |
| 172 |
} |
| 173 |
\def\usecount #1[#2]{% \usecount \foo[2] |
| 174 |
\count\numexpr#1+#2\relax |
| 175 |
} |
| 176 |
\endtt |
| 177 |
|
| 178 |
\_endinput |
| 179 |
|
| 180 |
2023-02-03 \_wloga introduced |
| 181 |
2022-06-10 \_famalloc set to 42 (answer to the biggest fundamental question) |
| 182 |
2022-03-07 \_noattr allocated |
| 183 |
2022-02-19 \_newlanguage introduced |
| 184 |
2021-02-15 \_advance -> \_incr, \_decr |
| 185 |
2020-05-12 \newmath -> \newfam bug fixing |
| 186 |
2020-01-23 released |