summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/context/presentations/context/2020/context-2020-datatypes.tex
blob: 590cdbf93e8004215b1f172e11ebffce20669ba3 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
\usemodule[present-boring,abbreviations-logos]

\startdocument
  [title={DATATYPES},
   banner={additional datatypes in lmtx},
   location={context\enspace {\bf 2020}\enspace meeting}]

\starttitle[title=Native \TEX\ datatypes: simple registers]

\startbuffer
integer: \count 123 = 456 \the\count123
\stopbuffer

\typebuffer {\getbuffer}

\startbuffer
dimension: \dimen123 = 456pt \the\dimen123
\stopbuffer

\typebuffer {\getbuffer}

\startbuffer
glue: \skip123 = 6pt plus 5pt minus 4pt\relax \the\skip123
\stopbuffer

\typebuffer {\getbuffer}

\startbuffer
muglue: \muskip123 = 6mu plus 5mu minus 4mu\relax \the\muskip123
\stopbuffer

\typebuffer {\getbuffer}

\startbuffer
attribute: \attribute123 = 456 \the\attribute123
\stopbuffer

\typebuffer {\getbuffer}

\blank[2*line]

\starttyping
\global \the \countdef \dimendef \skipdef \muskipdef \attributedef
\advance \multiply \divide \numexpr \dimexpr \glueexpr \muexpr
\stoptyping

\stoptitle

\starttitle[title=Native \TEX\ datatypes: tokens]

\startbuffer
toks: \toks123 = {456} \the\toks123
\stopbuffer

\typebuffer {\getbuffer}

\blank[2*line]

\starttyping
\global \the \toksdef
\toksapp \etoksapp \xtoksapp \gtoksapp
\tokspre \etokspre \xtokspre \gtokspre
\stoptyping

\blank[2*line]

(in retrospect: eetex)

\stoptitle

\starttitle[title=Native \TEX\ datatypes: boxes]

\startbuffer
box: \box123 = \hbox {456} (\the\wd123,\the\ht123,\the\dp123) \box123
\stopbuffer

\typebuffer {\getbuffer}

\blank[2*line]

\starttyping
\global \box \copy \unhbox \unvbox
\hbox \vbox \vtop \hpack \vpack \tpack
\wd \ht \dp \boxtotal
\boxdirection \boxattr
\boxorientation \boxxoffset \boxyoffset \boxxmove \boxymove
\stoptyping

\stoptitle

\starttitle[title=Native \TEX\ datatypes: macros]

\startbuffer
\def\onetwothree{346} \onetwothree
\stopbuffer

\typebuffer {\getbuffer}

\blank[2*line]

\starttyping
\global \protected \frozen
\def \edef \edef \xdef
\meaning
\stoptyping

\stoptitle

\starttitle[title=Native \LUA\ datatypes: numbers]

\startbuffer
\ctxlua{local n = 123                  context(n)}\quad
\ctxlua{local n = 123.456              context(n)}\quad
\ctxlua{local n = 123.4E56             context(n)}\quad
\ctxlua{local n = 0x123                context(n)}\quad
\ctxlua{local n = 0x1.37fe4cd4b70b2p-1 context(n)}
\stopbuffer

\typebuffer {\getbuffer}

\blank[2*line]

\starttyping
+ - * / // % ^ | ~ & << >> == ~= < > <= >= ( )
\stoptyping

\stoptitle

\starttitle[title=Native \LUA\ datatypes: strings]

\startbuffer
\ctxlua{local s = "abc"       context(s)}\quad
\ctxlua{local s = 'abc'       context(s)}\quad
\ctxlua{local s = [[abc]]     context(s)}\quad
\ctxlua{local s = [==[abc]==] context(s)}\quad
\stopbuffer

\typebuffer {\getbuffer}

\blank[2*line]

\starttyping
.. # == ~= < > <= >=
\stoptyping

\stoptitle

\starttitle[title=Native \LUA\ datatypes: booleans and nil]

\startbuffer
\ctxlua{local b = true  context(b)}\quad
\ctxlua{local b = false context(b)}\quad
\ctxlua{local n = nil   context(n)}\quad
\stopbuffer

\typebuffer {\getbuffer}

\blank[2*line]

\starttyping
== ~= and or not
\stoptyping

\stoptitle

\starttitle[title=Native \LUA\ datatypes: some more]

\starttyping
functions
userdata (lpeg is userdata)
coroutine
\stoptyping

\LUAMETATEX\ provides tokens and nodes as userdata and some libraries also
use them (complex, decimal, pdf, etc).

\stoptitle

\starttitle[title=Both worlds combined]

\startitemize[packed]
\startitem There are only 64K registers (although we can extend that if needed). \stopitem
\startitem Accessing registers at the \LUA\ end is not that efficient. \stopitem
\startitem So we have now datatypes at the \LUA\ end with access at the \TEX\ end. \stopitem
\startitem Their values can go beyond what \TEX\ registers provide. \stopitem
\stopitemize

\startbuffer
\luacardinal bar  123
\luainteger  bar -456
\luafloat    bar  123.456E-3
\stopbuffer

\typebuffer \getbuffer

\startbuffer
\the\luacardinal bar \quad
\the\luainteger  bar \quad
\the\luafloat    bar
\stopbuffer

\typebuffer \getbuffer

\page

The usual \LUA\ semantics apply:

\startbuffer
\luacardinal bar  0x123
\luainteger  bar -0x456
\luafloat    bar  0x123.456p-3
\stopbuffer

\typebuffer \getbuffer

So, now we get:

\startbuffer
\the\luacardinal bar \quad
\the\luainteger  bar \quad
\the\luafloat    bar
\stopbuffer

\getbuffer

Equal signs are optional:

\startbuffer
\luainteger gnu=  123456   \luafloat gnu=  123.456e12
\luainteger gnu = 123456   \luafloat gnu = 123.456e12
\luainteger gnu  =123456   \luafloat gnu  =123.456e12
\stopbuffer

\typebuffer

These commands can be uses for assignments as well as serialization. They use the
\LUAMETATEX\ value function feature.

\page

Dimensions are serialized differently so that they can be used like this:

\startbuffer
\luadimen test 100pt \scratchdimen = .25 \luadimen test: \the\scratchdimen
\stopbuffer

\typebuffer

\getbuffer

\page

Assume that we have this:

\startbuffer
\luacardinal x = -123   \luafloat x =  123.123
\luacardinal y =  456   \luafloat y = -456.456
\stopbuffer

\typebuffer \getbuffer

We can then use the macro \type {\luaexpression} that takes an optional keyword:

\startbuffer
- : \luaexpression          {n.x + 2*n.y}
f : \luaexpression float    {n.x + 2*n.y}
i : \luaexpression integer  {n.x + 2*n.y}
c : \luaexpression cardinal {n.x + 2*n.y}
b : \luaexpression boolean  {n.x + 2*n.y}
l : \luaexpression lua      {n.x + 2*n.y}
\stopbuffer

\typebuffer

The serialization can be different for these cases:

\startlines
\tt \getbuffer
\stoplines

Variables have their own namespace but get resolved across namespaces (f, i, c).

\page

Special tricks:

\startbuffer
\scratchdimen 123.456pt [\the\scratchdimen] [\the\nodimen\scratchdimen]
\stopbuffer

\typebuffer \getbuffer

Does nothing, nor does:

\startbuffer
\nodimen\scratchdimen = 654.321pt
\stopbuffer

\typebuffer \getbuffer

But:

\starttabulate[|T|T|]
\NC \type {\the\nodimen bp \scratchdimen} \NC \the\nodimen bp \scratchdimen \NC \NR
\NC \type {\the\nodimen cc \scratchdimen} \NC \the\nodimen cc \scratchdimen \NC \NR
\NC \type {\the\nodimen cm \scratchdimen} \NC \the\nodimen cm \scratchdimen \NC \NR
\NC \type {\the\nodimen dd \scratchdimen} \NC \the\nodimen dd \scratchdimen \NC \NR
\NC \type {\the\nodimen in \scratchdimen} \NC \the\nodimen in \scratchdimen \NC \NR
\NC \type {\the\nodimen mm \scratchdimen} \NC \the\nodimen mm \scratchdimen \NC \NR
\NC \type {\the\nodimen pt \scratchdimen} \NC \the\nodimen pt \scratchdimen \NC \NR
\NC \type {\the\nodimen sp \scratchdimen} \NC \the\nodimen sp \scratchdimen \NC \NR
\stoptabulate

gives different units! In the coffee break it was decided to drop the \type {nc}
and \type {nd} units in \LUAMETATEX\ when Arthur indicated that they never became
a standard. Dropping the \type {true} variants also makes sense but we postponed
dropping the \type {in} (inch).

\stoptitle

\starttitle[title=Arrays]

Two dimensional arrays have names and a type:

\startbuffer
\newarray name integers   type integer   nx 2 ny 2
\newarray name booleans   type boolean   nx 2 ny 2
\newarray name floats     type float     nx 2 ny 2
\newarray name dimensions type dimension nx 4
\stopbuffer

\typebuffer \getbuffer

And a special accessor. Here we set values:

\startbuffer
\arrayvalue integers   1 2 4        \arrayvalue integers   2 1 8
\arrayvalue booleans   1 2 true     \arrayvalue booleans   2 1 true
\arrayvalue floats     1 2 12.34    \arrayvalue floats     2 1 34.12
\arrayvalue dimensions 1   12.34pt  \arrayvalue dimensions 3   34.12pt
\stopbuffer

\typebuffer \getbuffer

\page

Here we get values:

\startbuffer
[\the\arrayvalue integers   1 2]
[\the\arrayvalue booleans   1 2]
[\the\arrayvalue floats     1 2]
[\the\arrayvalue dimensions 1  ]\crlf
[\the\arrayvalue integers   2 1]
[\the\arrayvalue booleans   2 1]
[\the\arrayvalue floats     2 1]
[\the\arrayvalue dimensions   3]
\stopbuffer

\typebuffer

\getbuffer

When a value is expected the integer is serialized:

\startbuffer
\scratchcounter\arrayvalue integers 1 2\relax \the\scratchcounter
\stopbuffer

\typebuffer

\getbuffer

You can view an array on the console with:

\starttyping
\showarray integers
\stoptyping

\page

Another expression example:

\startbuffer
\dostepwiserecurse {1} {4} {1} {
    [\the\arrayvalue dimensions #1 :
     \luaexpression dimen {math.sind(30) * a.dimensions[#1]}]
}
\stopbuffer

\typebuffer

\getbuffer

\page

We can combine it all with if tests:

\startbuffer
slot 1 is \ifboolean\arrayequals dimensions 1 0pt zero \else not zero \fi\quad
slot 2 is \ifboolean\arrayequals dimensions 2 0pt zero \else not zero \fi
\stopbuffer

\typebuffer

\getbuffer

\startbuffer
slot 1: \ifcase\arraycompare dimensions 1 3pt lt \or eq \else gt \fi zero\quad
slot 2: \ifcase\arraycompare dimensions 2 3pt lt \or eq \else gt \fi zero\quad
slot 3: \ifcase\arraycompare dimensions 3 3pt lt \or eq \else gt \fi zero\quad
slot 4: \ifcase\arraycompare dimensions 4 3pt lt \or eq \else gt \fi zero

slot 1: \ifcmpdim\arrayvalue dimensions 1 3pt lt \or eq \else gt \fi zero\quad
slot 2: \ifcmpdim\arrayvalue dimensions 2 3pt lt \or eq \else gt \fi zero\quad
slot 3: \ifcmpdim\arrayvalue dimensions 3 3pt lt \or eq \else gt \fi zero\quad
slot 4: \ifcmpdim\arrayvalue dimensions 4 3pt lt \or eq \else gt \fi zero
\stopbuffer

\typebuffer

\getbuffer

\stoptitle

\starttitle[title=Complex numbers]

\startbuffer
\startluacode
local c1 = xcomplex.new(1,3)
local c2 = xcomplex.new(2,4)
context(c1) context.quad() context(c2) context.quad(c1 + c2)
\stopluacode
\stopbuffer

\typebuffer \getbuffer

\stoptitle

\starttitle[title=Decimal numbers]

\startbuffer
\startluacode
local c1 = xdecimal.new("123456789012345678901234567890")
local c2 = xdecimal.new(1234567890)
context(c1) context.crlf() context(c2) context.crlf(c1 * c2)
\stopluacode
\stopbuffer

\typebuffer \getbuffer

\stoptitle

\stopdocument