summaryrefslogtreecommitdiff
path: root/doc/context/sources/general/manuals/lowlevel/lowlevel-scope.tex
blob: cfd9f96117f3d7d0187baca11d800d50a18b94e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
% language=us runpath=texruns:manuals/lowlevel

% \hfil \hss
% spread

\environment lowlevel-style

\startdocument
  [title=scope,
   color=middleblue]

\startsectionlevel[title=Introduction]

When I visited the file where register allocations are implemented I wondered to
what extend it made sense to limit allocation to global instances only. This
chapter deals with this phenomena.

\stopsectionlevel

\startsectionlevel[title=Registers]

In \TEX\ definitions can be local or global. Most assignments are local within a
group. Registers and definitions can be assigned global by using the \type
{\global} prefix. There are also some properties that are global by design, like
\type {\prevdepth}. A mixed breed are boxes. When you tweak its dimensions you
actually tweak the current box, which can be an outer level. Compare:

\starttyping[option=TEX]
\scratchcounter = 1
here the counter has value 1
\begingroup
    \scratchcounter = 2
    here the counter has value 2
\endgroup
here the counter has value 1
\stoptyping

with:

\starttyping[option=TEX]
\setbox\scratchbox=\hbox{}
here the box has zero width
\begingroup
    \wd\scratchbox=10pt
    here the box is 10pt wide
\endgroup
here the box is 10pt wide
\stoptyping

It all makes sense so a remark like \quotation {Assignments to box dimensions are
always global} are sort of confusing. Just look at this:

\startbuffer
\setbox\scratchbox=\hbox to 20pt{}
here the box is \the\wd\scratchbox\ wide\par
\begingroup
    \setbox\scratchbox=\hbox{}
    here the box is \the\wd\scratchbox\ wide\par
    \begingroup
        \wd\scratchbox=15pt
        here the box is \the\wd\scratchbox\ wide\par
    \endgroup
    here the box is \the\wd\scratchbox\ wide\par
\endgroup
here the box is \the\wd\scratchbox\ wide\par
\stopbuffer

\typebuffer[option=TEX] \startlines \getbuffer \stoplines

If you don't think about it, what happens is what you expect. Now watch the next
variant:

\startbuffer
\setbox\scratchbox=\hbox to 20pt{}
here the box is \the\wd\scratchbox\ wide\par
\begingroup
    \setbox\scratchbox=\hbox{}
    here the box is \the\wd\scratchbox\ wide\par
    \begingroup
        \global\wd\scratchbox=15pt
        here the box is \the\wd\scratchbox\ wide\par
    \endgroup
    here the box is \the\wd\scratchbox\ wide\par
\endgroup
here the box is \the\wd\scratchbox\ wide\par
\stopbuffer

The \type {\global} is only effective for the current box. It is good to realize
that when we talk registers, the box register behaves just like any other
register but the manipulations happen to the current one.

\typebuffer \startlines \getbuffer \stoplines

\startbuffer
\scratchdimen=20pt
here the dimension is \the\scratchdimen\par
\begingroup
    \scratchdimen=0pt
    here the dimension is \the\scratchdimen\par
    \begingroup
        \global\scratchdimen=15pt
        here the dimension is \the\scratchdimen\par
    \endgroup
    here the dimension is \the\scratchdimen\par
\endgroup
here the dimension is \the\scratchdimen\par
\stopbuffer

\typebuffer[option=TEX] \startlines \getbuffer \stoplines

\stopsectionlevel

\startsectionlevel[title=Allocation]

The plain \TEX\ format has set some standards and one of them is that registers
are allocated with \type {\new...} commands. So we can say:

\starttyping[option=TEX]
\newcount\mycounta
\newdimen\mydimena
\stoptyping

These commands take a register from the pool and relate the given name to that
entry. In \CONTEXT\ we have a bunch of predefined scratch registers for general
use, like:

\startbuffer
scratchcounter    : \meaningfull\scratchcounter
scratchcounterone : \meaningfull\scratchcounterone
scratchcountertwo : \meaningfull\scratchcountertwo
scratchdimen      : \meaningfull\scratchdimen
scratchdimenone   : \meaningfull\scratchdimenone
scratchdimentwo   : \meaningfull\scratchdimentwo
\stopbuffer

\typebuffer[option=TEX]

The meaning reveals what these are:

\startlines \tttf \getbuffer \stoplines

You can use the numbers directly but that is a bad idea because they can clash!
In the original \TEX\ engine there are only 256 registers and some are used by
the engine and the core of a macro package itself, so that leaves a little amount
for users. The \ETEX\ extension lifted that limitation and bumped to 32K and
\LUATEX\ upped that to 64K. One could go higher but what makes sense? These
registers are taking part of the fixed memory slots because that makes nested
(grouped) usage efficient and access fast. The number you see above is deduced
from the so called command code (here indicated by \type {\count}) and an index
encoded in the same token. So, \type {\scratchcounter} takes a single token
contrary to the verbose \type {\count257} that takes four tokens where the number
gets parsed every time it is needed. But those are details that a user can
forget.

As mentioned, commands like \type {\newcount \foo} create a global control
sequence \type {\foo} referencing a counter. You can locally redefine that
control sequence unless in \LUAMETATEX\ you have so called overload mode enabled.
You can do local or global assignments to these registers.

\starttyping[option=TEX]
\scratchcounter = 123
\begingroup
    \scratchcounter = 456
    \begingroup
        \global\scratchcounter = 789
    \endgroup
\endgroup
\stoptyping

And in both cases count register 257 is set. When an assignment is global,
all current values to that register get the same value. Normally this is all
quite transparent: you get what you ask for. However the drawback is that
as a user you cannot know what variables are already defined, which means
that this will fail (that is: it will issue a message):

\starttyping[option=TEX]
\newcount\scratchcounter
\stoptyping

as will the second line in:

\starttyping[option=TEX]
\newcount\myscratchcounter
\newcount\myscratchcounter
\stoptyping

In \CONTEXT\ the scratch registers are visible but there are lots of internally
used ones are protected from the user by more obscure names. So what if you want
to use your own register names without \CONTEXT\ barking to you about not being
able to define it. This is why in \LMTX\ (and maybe some day in \MKIV) we now
have local definitions:

\startbuffer
\begingroup
  \newlocaldimen\mydimena     \mydimena1\onepoint
  \newlocaldimen\mydimenb     \mydimenb2\onepoint
  (\the\mydimena,\the\mydimenb)
  \begingroup
    \newlocaldimen\mydimena   \mydimena3\onepoint
    \newlocaldimen\mydimenb   \mydimenb4\onepoint
    \newlocaldimen\mydimenc   \mydimenc5\onepoint
    (\the\mydimena,\the\mydimenb,\the\mydimenc)
    \begingroup
      \newlocaldimen\mydimena \mydimena6\onepoint
      \newlocaldimen\mydimenb \mydimenb7\onepoint
      (\the\mydimena,\the\mydimenb)
    \endgroup
    \newlocaldimen\mydimend   \mydimend8\onepoint
    (\the\mydimena,\the\mydimenb,\the\mydimenc,\the\mydimend)
  \endgroup
  (\the\mydimena,\the\mydimenb)
\endgroup
\stopbuffer

\typebuffer[option=TEX]

The allocated registers get zero values but you can of course set them to any
value that fits their nature:

\startlines \getbuffer \stoplines

\startbuffer
\begingroup
  \setnewlocaldimen\mydimena     1\onepoint
  \setnewlocaldimen\mydimenb     2\onepoint
  (\the\mydimena,\the\mydimenb)
  \begingroup
    \setnewlocaldimen\mydimena   3\onepoint
    \setnewlocaldimen\mydimenb   4\onepoint
    \setnewlocaldimen\mydimenc   5\onepoint
    (\the\mydimena,\the\mydimenb,\the\mydimenc)
    \begingroup
      \setnewlocaldimen\mydimena 6\onepoint
      \setnewlocaldimen\mydimenb 7\onepoint
      (\the\mydimena,\the\mydimenb)
    \endgroup
    \setnewlocaldimen\mydimend   8\onepoint
    (\the\mydimena,\the\mydimenb,\the\mydimenc,\the\mydimend)
  \endgroup
  (\the\mydimena,\the\mydimenb)
\endgroup
\stopbuffer

You can also use the next variant where you also pass the initial value:

\typebuffer[option=TEX]

So, again we get:

\startlines \getbuffer \stoplines

When used in the body of the macro there is of course a little overhead
involved in the repetitive allocation but normally that can be neglected.

\stopsectionlevel

\startsectionlevel[title=Files]

When adding these new allocators I also wondered about the read and write
allocators. We don't use them in \CONTEXT\ but maybe users like them, so let's
give an example and see what more demands they have:

\starttyping[option=TEX]
\integerdef\StartHere\numexpr\inputlineno+2\relax
\starthiding
SOME LINE 1
SOME LINE 2
SOME LINE 3
SOME LINE 4
\stophiding
\integerdef\StopHere\numexpr\inputlineno-2\relax
\stoptyping

% We need to be in the file!

\integerdef\StartHere\numexpr\inputlineno+2\relax
\starthiding
SOME LINE 1
SOME LINE 2
SOME LINE 3
SOME LINE 4
\stophiding
\integerdef\StopHere\numexpr\inputlineno-2\relax

\startbuffer
\begingroup
  \newlocalread\myreada
  \immediate\openin\myreada {lowlevel-scope.tex}
  \dostepwiserecurse{\StopHere}{\StartHere}{-1}{
    \readline\myreada line #1 to \scratchstring #1 : \scratchstring \par
  }
  \blank
  \dostepwiserecurse{\StartHere}{\StopHere}{1}{
    \read    \myreada line #1 to \scratchstring #1 : \scratchstring \par
  }
  \immediate\closein\myreada
\endgroup
\stopbuffer

\typebuffer[option=TEX]

Here, instead of hard coded line numbers we used the stored values. The
optional \type {line} keyword is a \LMTX\ speciality.

\startlines \getbuffer \stoplines

Actually an application can be found in a small (demonstration) module:

\startbuffer
\usemodule[system-readers]
\stopbuffer

\typebuffer[option=TEX] \getbuffer

This provides the code for doing this:

\starttyping[option=TEX]
\startmarkedlines[test]
SOME LINE 1
SOME LINE 2
SOME LINE 3
\stopmarkedlines

\begingroup
  \newlocalread\myreada
  \immediate\openin\myreada {\markedfilename{test}}
  \dostepwiserecurse{\lastmarkedline{test}}{\firstmarkedline{test}}{-1}{
    \readline\myreada line #1 to \scratchstring #1 : \scratchstring \par
  }
  \immediate\closein\myreada
\endgroup
\stoptyping

As you see in these examples, we an locally define a read channel without
getting a message about it already being defined.

\stopsectionlevel

\stopdocument