context-2020-datatypes.tex /size: 9846 b    last modification: 2021-10-28 13:50
1\usemodule[present-boring,abbreviations-logos]
2
3\startdocument
4  [title={DATATYPES},
5   banner={additional datatypes in lmtx},
6   location={context\enspace {\bf 2020}\enspace meeting}]
7
8\starttitle[title=Native \TEX\ datatypes: simple registers]
9
10\startbuffer
11integer: \count 123 = 456 \the\count123
12\stopbuffer
13
14\typebuffer {\getbuffer}
15
16\startbuffer
17dimension: \dimen123 = 456pt \the\dimen123
18\stopbuffer
19
20\typebuffer {\getbuffer}
21
22\startbuffer
23glue: \skip123 = 6pt plus 5pt minus 4pt\relax \the\skip123
24\stopbuffer
25
26\typebuffer {\getbuffer}
27
28\startbuffer
29muglue: \muskip123 = 6mu plus 5mu minus 4mu\relax \the\muskip123
30\stopbuffer
31
32\typebuffer {\getbuffer}
33
34\startbuffer
35attribute: \attribute123 = 456 \the\attribute123
36\stopbuffer
37
38\typebuffer {\getbuffer}
39
40\blank[2*line]
41
42\starttyping
43\global \the \countdef \dimendef \skipdef \muskipdef \attributedef
44\advance \multiply \divide \numexpr \dimexpr \glueexpr \muexpr
45\stoptyping
46
47\stoptitle
48
49\starttitle[title=Native \TEX\ datatypes: tokens]
50
51\startbuffer
52toks: \toks123 = {456} \the\toks123
53\stopbuffer
54
55\typebuffer {\getbuffer}
56
57\blank[2*line]
58
59\starttyping
60\global \the \toksdef
61\toksapp \etoksapp \xtoksapp \gtoksapp
62\tokspre \etokspre \xtokspre \gtokspre
63\stoptyping
64
65\blank[2*line]
66
67(in retrospect: eetex)
68
69\stoptitle
70
71\starttitle[title=Native \TEX\ datatypes: boxes]
72
73\startbuffer
74box: \box123 = \hbox {456} (\the\wd123,\the\ht123,\the\dp123) \box123
75\stopbuffer
76
77\typebuffer {\getbuffer}
78
79\blank[2*line]
80
81\starttyping
82\global \box \copy \unhbox \unvbox
83\hbox \vbox \vtop \hpack \vpack \tpack
84\wd \ht \dp \boxtotal
85\boxdirection \boxattr
86\boxorientation \boxxoffset \boxyoffset \boxxmove \boxymove
87\stoptyping
88
89\stoptitle
90
91\starttitle[title=Native \TEX\ datatypes: macros]
92
93\startbuffer
94\def\onetwothree{346} \onetwothree
95\stopbuffer
96
97\typebuffer {\getbuffer}
98
99\blank[2*line]
100
101\starttyping
102\global \protected \frozen
103\def \edef \edef \xdef
104\meaning
105\stoptyping
106
107\stoptitle
108
109\starttitle[title=Native \LUA\ datatypes: numbers]
110
111\startbuffer
112\ctxlua{local n = 123                  context(n)}\quad
113\ctxlua{local n = 123.456              context(n)}\quad
114\ctxlua{local n = 123.4E56             context(n)}\quad
115\ctxlua{local n = 0x123                context(n)}\quad
116\ctxlua{local n = 0x1.37fe4cd4b70b2p-1 context(n)}
117\stopbuffer
118
119\typebuffer {\getbuffer}
120
121\blank[2*line]
122
123\starttyping
124+ - * / // % ^ | ~ & << >> == ~= < > <= >= ( )
125\stoptyping
126
127\stoptitle
128
129\starttitle[title=Native \LUA\ datatypes: strings]
130
131\startbuffer
132\ctxlua{local s = "abc"       context(s)}\quad
133\ctxlua{local s = 'abc'       context(s)}\quad
134\ctxlua{local s = [[abc]]     context(s)}\quad
135\ctxlua{local s = [==[abc]==] context(s)}\quad
136\stopbuffer
137
138\typebuffer {\getbuffer}
139
140\blank[2*line]
141
142\starttyping
143.. # == ~= < > <= >=
144\stoptyping
145
146\stoptitle
147
148\starttitle[title=Native \LUA\ datatypes: booleans and nil]
149
150\startbuffer
151\ctxlua{local b = true  context(b)}\quad
152\ctxlua{local b = false context(b)}\quad
153\ctxlua{local n = nil   context(n)}\quad
154\stopbuffer
155
156\typebuffer {\getbuffer}
157
158\blank[2*line]
159
160\starttyping
161== ~= and or not
162\stoptyping
163
164\stoptitle
165
166\starttitle[title=Native \LUA\ datatypes: some more]
167
168\starttyping
169functions
170userdata (lpeg is userdata)
171coroutine
172\stoptyping
173
174\LUAMETATEX\ provides tokens and nodes as userdata and some libraries also
175use them (complex, decimal, pdf, etc).
176
177\stoptitle
178
179\starttitle[title=Both worlds combined]
180
181\startitemize[packed]
182\startitem There are only 64K registers (although we can extend that if needed). \stopitem
183\startitem Accessing registers at the \LUA\ end is not that efficient. \stopitem
184\startitem So we have now datatypes at the \LUA\ end with access at the \TEX\ end. \stopitem
185\startitem Their values can go beyond what \TEX\ registers provide. \stopitem
186\stopitemize
187
188\startbuffer
189\luacardinal bar  123
190\luainteger  bar -456
191\luafloat    bar  123.456E-3
192\stopbuffer
193
194\typebuffer \getbuffer
195
196\startbuffer
197\the\luacardinal bar \quad
198\the\luainteger  bar \quad
199\the\luafloat    bar
200\stopbuffer
201
202\typebuffer \getbuffer
203
204\page
205
206The usual \LUA\ semantics apply:
207
208\startbuffer
209\luacardinal bar  0x123
210\luainteger  bar -0x456
211\luafloat    bar  0x123.456p-3
212\stopbuffer
213
214\typebuffer \getbuffer
215
216So, now we get:
217
218\startbuffer
219\the\luacardinal bar \quad
220\the\luainteger  bar \quad
221\the\luafloat    bar
222\stopbuffer
223
224\getbuffer
225
226Equal signs are optional:
227
228\startbuffer
229\luainteger gnu=  123456   \luafloat gnu=  123.456e12
230\luainteger gnu = 123456   \luafloat gnu = 123.456e12
231\luainteger gnu  =123456   \luafloat gnu  =123.456e12
232\stopbuffer
233
234\typebuffer
235
236These commands can be uses for assignments as well as serialization. They use the
237\LUAMETATEX\ value function feature.
238
239\page
240
241Dimensions are serialized differently so that they can be used like this:
242
243\startbuffer
244\luadimen test 100pt \scratchdimen = .25 \luadimen test: \the\scratchdimen
245\stopbuffer
246
247\typebuffer
248
249\getbuffer
250
251\page
252
253Assume that we have this:
254
255\startbuffer
256\luacardinal x = -123   \luafloat x =  123.123
257\luacardinal y =  456   \luafloat y = -456.456
258\stopbuffer
259
260\typebuffer \getbuffer
261
262We can then use the macro \type {\luaexpression} that takes an optional keyword:
263
264\startbuffer
265- : \luaexpression          {n.x + 2*n.y}
266f : \luaexpression float    {n.x + 2*n.y}
267i : \luaexpression integer  {n.x + 2*n.y}
268c : \luaexpression cardinal {n.x + 2*n.y}
269b : \luaexpression boolean  {n.x + 2*n.y}
270l : \luaexpression lua      {n.x + 2*n.y}
271\stopbuffer
272
273\typebuffer
274
275The serialization can be different for these cases:
276
277\startlines
278\tt \getbuffer
279\stoplines
280
281Variables have their own namespace but get resolved across namespaces (f, i, c).
282
283\page
284
285Special tricks:
286
287\startbuffer
288\scratchdimen 123.456pt [\the\scratchdimen] [\the\nodimen\scratchdimen]
289\stopbuffer
290
291\typebuffer \getbuffer
292
293Does nothing, nor does:
294
295\startbuffer
296\nodimen\scratchdimen = 654.321pt
297\stopbuffer
298
299\typebuffer \getbuffer
300
301But:
302
303\starttabulate[|T|T|]
304\NC \type {\the\nodimen bp \scratchdimen} \NC \the\nodimen bp \scratchdimen \NC \NR
305\NC \type {\the\nodimen cc \scratchdimen} \NC \the\nodimen cc \scratchdimen \NC \NR
306\NC \type {\the\nodimen cm \scratchdimen} \NC \the\nodimen cm \scratchdimen \NC \NR
307\NC \type {\the\nodimen dd \scratchdimen} \NC \the\nodimen dd \scratchdimen \NC \NR
308\NC \type {\the\nodimen in \scratchdimen} \NC \the\nodimen in \scratchdimen \NC \NR
309\NC \type {\the\nodimen mm \scratchdimen} \NC \the\nodimen mm \scratchdimen \NC \NR
310\NC \type {\the\nodimen pt \scratchdimen} \NC \the\nodimen pt \scratchdimen \NC \NR
311\NC \type {\the\nodimen sp \scratchdimen} \NC \the\nodimen sp \scratchdimen \NC \NR
312\stoptabulate
313
314gives different units! In the coffee break it was decided to drop the \type {nc}
315and \type {nd} units in \LUAMETATEX\ when Arthur indicated that they never became
316a standard. Dropping the \type {true} variants also makes sense but we postponed
317dropping the \type {in} (inch).
318
319\stoptitle
320
321\starttitle[title=Arrays]
322
323Two dimensional arrays have names and a type:
324
325\startbuffer
326\newarray name integers   type integer   nx 2 ny 2
327\newarray name booleans   type boolean   nx 2 ny 2
328\newarray name floats     type float     nx 2 ny 2
329\newarray name dimensions type dimension nx 4
330\stopbuffer
331
332\typebuffer \getbuffer
333
334And a special accessor. Here we set values:
335
336\startbuffer
337\arrayvalue integers   1 2 4        \arrayvalue integers   2 1 8
338\arrayvalue booleans   1 2 true     \arrayvalue booleans   2 1 true
339\arrayvalue floats     1 2 12.34    \arrayvalue floats     2 1 34.12
340\arrayvalue dimensions 1   12.34pt  \arrayvalue dimensions 3   34.12pt
341\stopbuffer
342
343\typebuffer \getbuffer
344
345\page
346
347Here we get values:
348
349\startbuffer
350[\the\arrayvalue integers   1 2]
351[\the\arrayvalue booleans   1 2]
352[\the\arrayvalue floats     1 2]
353[\the\arrayvalue dimensions 1  ]\crlf
354[\the\arrayvalue integers   2 1]
355[\the\arrayvalue booleans   2 1]
356[\the\arrayvalue floats     2 1]
357[\the\arrayvalue dimensions   3]
358\stopbuffer
359
360\typebuffer
361
362\getbuffer
363
364When a value is expected the integer is serialized:
365
366\startbuffer
367\scratchcounter\arrayvalue integers 1 2\relax \the\scratchcounter
368\stopbuffer
369
370\typebuffer
371
372\getbuffer
373
374You can view an array on the console with:
375
376\starttyping
377\showarray integers
378\stoptyping
379
380\page
381
382Another expression example:
383
384\startbuffer
385\dostepwiserecurse {1} {4} {1} {
386    [\the\arrayvalue dimensions #1 :
387     \luaexpression dimen {math.sind(30) * a.dimensions[#1]}]
388}
389\stopbuffer
390
391\typebuffer
392
393\getbuffer
394
395\page
396
397We can combine it all with if tests:
398
399\startbuffer
400slot 1 is \ifboolean\arrayequals dimensions 1 0pt zero \else not zero \fi\quad
401slot 2 is \ifboolean\arrayequals dimensions 2 0pt zero \else not zero \fi
402\stopbuffer
403
404\typebuffer
405
406\getbuffer
407
408\startbuffer
409slot 1: \ifcase\arraycompare dimensions 1 3pt lt \or eq \else gt \fi zero\quad
410slot 2: \ifcase\arraycompare dimensions 2 3pt lt \or eq \else gt \fi zero\quad
411slot 3: \ifcase\arraycompare dimensions 3 3pt lt \or eq \else gt \fi zero\quad
412slot 4: \ifcase\arraycompare dimensions 4 3pt lt \or eq \else gt \fi zero
413
414slot 1: \ifcmpdim\arrayvalue dimensions 1 3pt lt \or eq \else gt \fi zero\quad
415slot 2: \ifcmpdim\arrayvalue dimensions 2 3pt lt \or eq \else gt \fi zero\quad
416slot 3: \ifcmpdim\arrayvalue dimensions 3 3pt lt \or eq \else gt \fi zero\quad
417slot 4: \ifcmpdim\arrayvalue dimensions 4 3pt lt \or eq \else gt \fi zero
418\stopbuffer
419
420\typebuffer
421
422\getbuffer
423
424\stoptitle
425
426\starttitle[title=Complex numbers]
427
428\startbuffer
429\startluacode
430local c1 = xcomplex.new(1,3)
431local c2 = xcomplex.new(2,4)
432context(c1) context.quad() context(c2) context.quad(c1 + c2)
433\stopluacode
434\stopbuffer
435
436\typebuffer \getbuffer
437
438\stoptitle
439
440\starttitle[title=Decimal numbers]
441
442\startbuffer
443\startluacode
444local c1 = xdecimal.new("123456789012345678901234567890")
445local c2 = xdecimal.new(1234567890)
446context(c1) context.crlf() context(c2) context.crlf(c1 * c2)
447\stopluacode
448\stopbuffer
449
450\typebuffer \getbuffer
451
452\stoptitle
453
454\stopdocument
455