Debian Codegraph @

texmf-dist/tex/generic/gates/gates.lua

raw symbols SCIP
1 -- This is the Gates package, Lua version.
2 -- Relevant information can be found in gates-doc.pdf
3 -- (or gates-doc.txt in a text editor).
4 --
5 -- Author: Paul Isambert.
6 -- E-mail: zappathustra AT free DOT fr
7 -- Comments and suggestions are welcome.
8 -- Date: May 2012.
9
10 if gates then
11 return
12 end
13
14 local _insert, _remove = table.insert, table.remove
15 local _gsub, _match, _format = string.gsub, string.match, string.format
16 local _write_nl, print_error
17 if texio then
18 _write_nl, print_error = texio.write_nl, tex.error
19 else
20 function _write_nl (s)
21 print("\n" .. s)
22 end
23 print_error = error
24 end
25 local unpack, maxn = table.unpack or unpack
26 if lua.version == "Lua 5.1" then
27 maxn = table.maxn
28 else
29 local pairs, type = pairs, type
30 function maxn (t)
31 local max = 0
32 for i in pairs(t) do
33 if type(i) == "number" then
34 max = i > max and i or max
35 end
36 end
37 return max
38 end
39 end
40 local function unpackargs (t)
41 return unpack(t, 1, maxn(t))
42 end
43
44 local function _copy (tb)
45 local t = {}
46 for a, b in pairs (tb) do
47 if type(b) == "table" then
48 b = _copy(b)
49 end
50 t[a] = b
51 end
52 return t
53 end
54
55 -- Errors.
56 local function _error (...)
57 print_error("Lua-Gates error: " .. _format(...))
58 end
59 local function _error_nogate (g)
60 _error("`%s' isn't a gate", g)
61 end
62 local function _error_nogatelist (g)
63 _error("`%s' isn't a gate list", g)
64 end
65 local function _error_nogatein (g, l)
66 _error("No gate `%s' in list `%s'", g, l)
67 end
68
69 local function _check_family(fam, name)
70 return _match(name, ":") and name or fam .. ":" .. name
71 end
72
73 -- Tracing and showing.
74 local function _write (...)
75 _write_nl(_format(...))
76 end
77 local _tab, _white = "", ""
78 local function _addtab ()
79 _tab = ". " .. _tab
80 _white = " " .. _white
81 end
82 local function _untab ()
83 _tab = _gsub(_tab, ". ", "", 1)
84 _white = _gsub(_white, " ", "", 1)
85 end
86 local _trace_values = {}
87 local function trace (fam, n)
88 _trace_values[fam] = n
89 end
90 local function _trace_value (name)
91 return _trace_values[_match(name, "(.-):") or name] or 0
92 end
93 local function _report (fam, s, ...)
94 if _trace_value(fam) > 0 then
95 if _white == "" then
96 _write(" ")
97 end
98 _write(_white .. s, ...)
99 end
100 end
101 local function _reporttab (fam, s, ...)
102 if _trace_value(fam) > 0 then
103 _write(_tab .. s, ...)
104 end
105 end
106
107 local _rawgate
108 local function _getinfo (g)
109 local s = g.status == 1 and "open"
110 or g.status == 2 and "ajar"
111 or g.status == 3 and "skip"
112 or g.status == 4 and "close"
113 local c = tostring(g.conditional)
114 local l = g.loop and tostring(g.loop)
115 local u = g.loopuntil and tostring(g.loopuntil)
116 local i = g.iterator and tostring(g.iterator)
117 local a
118 if type(g.autoreturn) == "function" then
119 a = " (autoreturn: " .. tostring (g.autoreturn) .. ")"
120 elseif g.autoreturn then
121 a = " (autoreturn)"
122 else
123 a = ""
124 end
125 return s, c, l, u, i, a
126 end
127
128 local shown
129 local function _show (gate, tab)
130 local g = _rawgate(gate.name)
131 local s, c, l, u, i, a = _getinfo(gate)
132 local info = _format(tab .. "%s (%s-gate) (status: %s) (conditional: %s)%s",
133 gate.name, g.list and "l" or "m", s, c, a)
134 if l then
135 info = _format(info .. " (loop: %s)", l)
136 end
137 if u then
138 info = _format(info .. " (loopuntil: %s)", u)
139 end
140 if i then
141 info = _format(info .. " (iterator: %s)", i)
142 end
143 _write(info)
144 if g.list then
145 if shown[g.name] then
146 texio.write(" [subgates already shown]")
147 else
148 shown[g.name] = true
149 for _, G in ipairs(g.list) do
150 _show(G, tab .. ". ")
151 end
152 end
153 end
154 end
155
156 local function show (fam, name)
157 shown = {}
158 name = _check_family(fam, name)
159 local g = _rawgate(name)
160 if g then
161 _show(g, "")
162 else
163 _error_nogate(name)
164 end
165 end
166
167 local _open = 1
168 local _ajar = 2
169 local _skip = 3
170 local _close = 4
171 local _statuses = {
172 open = _open,
173 ajar = _ajar,
174 skip = _skip,
175 close = _close
176 }
177
178 -- Some functions can take one argument or
179 -- multiple arguments in a table.
180 local function _totable (x)
181 return type(x) == "table" and x or {x}
182 end
183
184 local _default = function (...) return ... end
185
186 local execute = {[0] = {}}
187
188 function _rawgate (name) -- Localized above.
189 return execute[0][name]
190 end
191
192 local function _getmax (tb)
193 local max = 0
194 for n in pairs (tb) do
195 max = n > max and n or max
196 end
197 return max
198 end
199 local function _restoreargs (arg, orarg, autoreturn)
200 -- autoreturn may be a function
201 if autoreturn == true then
202 local argmax, orargmax = _getmax(arg), _getmax(orarg)
203 for i = argmax + 1, orargmax do
204 arg[i] = orarg[i]
205 end
206 end
207 return arg
208 end
209 local function _processfunction (fct, autoreturn, ...)
210 if type(autoreturn) == "function" then
211 return {autoreturn(...)}
212 elseif autoreturn then
213 local newarg = {fct(...)}
214 local argmax, newargmax = _getmax(arg), _getmax(newarg)
215 for i = newargmax + 1, argmax do
216 newarg[i] = arg[i]
217 end
218 return newarg
219 else
220 return {fct(...)}
221 end
222 end
223
224 local function _execute (f, sub, ...)
225 local name, list, fct, status, conditional, loop, loopuntil, iterator, autoreturn =
226 f.name, f.list, sub and execute[f.name] or f.fct, f.status, f.conditional, f.loop, f.loopuntil, f.iterator, f.autoreturn
227 local action, report, t
228 if sub then
229 action, report, t = "Calling", _reporttab, "subgate " .. name
230 else
231 action, report, t = "Executing", _report, (list and "l" or "m") .. "-gate " .. name
232 end
233 local s = ""
234 local orarg, arg = {...}, {...}
235 if status == _open or status == _ajar then
236 if status == _ajar then
237 s = " (ajar -> close)"
238 f.status = _close
239 end
240 if conditional(...) then
241 report(name, "%s %s%s", action, t, s)
242 if not sub and not list and _trace_value(name) > 1 then
243 for i=1, maxn(arg) do
244 _write_nl(_white .. "#" .. i .. "<-" .. tostring(arg[i]))
245 end
246 end
247 if loop then
248 while loop(unpackargs(arg)) do
249 arg = {fct(unpackargs(arg))}
250 orarg = _restoreargs(arg, orarg, autoreturn)
251 end
252 elseif loopuntil then
253 repeat
254 arg = {fct(unpackargs(arg))}
255 orarg = _restoreargs(arg, orarg, autoreturn)
256 until loopuntil(unpackargs(arg))
257 elseif iterator then
258 local iter, state, var = iterator(unpackargs(arg))
259 while iter do
260 local args = {iter(state, var)}
261 if args[1] ~= nil then
262 arg = {fct(unpackargs(args))}
263 orarg = _restoreargs(arg, orarg, autoreturn)
264 var = args[1]
265 else
266 break
267 end
268 end
269 else
270 arg = {fct(unpackargs(arg))}
271 orarg = _restoreargs(arg, orarg, autoreturn)
272 end
273 if type(autoreturn) == "function" then
274 arg = {autoreturn(...)}
275 end
276 else
277 report(name, "Ignoring %s%s (False conditional)", t, s)
278 end
279 else
280 if status == _skip then
281 s = " (skip -> open)"
282 f.status = _open
283 else
284 s = " (close)"
285 end
286 report(name, "Ignoring %s%s%s", t, s, conditional() and "" or " (False conditional)")
287 end
288 return unpackargs(arg)
289 end
290
291 execute.__index = function (_, name)
292 local f = _rawgate(name)
293 if f then
294 return function (...)
295 return _execute(f, nil, ...)
296 end
297 else
298 _error_nogate(name)
299 return _default
300 end
301 end
302
303 setmetatable(execute, execute)
304
305 local function _evertrue ()
306 return true
307 end
308
309 local function add (fam, tb, list, where)
310 list = _check_family(fam, list)
311 local gatelist = _rawgate(list)
312 if gatelist and gatelist.list then
313 tb = _totable(tb)
314 local position
315 if where then
316 where = _match(where, "^%s*(.-)%s*$"):gsub("%s+", " ")
317 if where == "first" or where == "before first" then
318 position = 1
319 elseif where == "after first" then
320 position = 2
321 elseif where == "last" or where == "after last" then
322 position = #gatelist.list + 1
323 elseif where == "before last" then
324 position = #gatelist.list
325 else
326 local prep, gate = _match(where, "^(.-)%s+(.-)$")
327 if prep and gate and (prep == "before" or prep == "after") then
328 gate = _check_family(fam, gate)
329 for n, g in ipairs(gatelist.list) do
330 if g.name == gate then
331 position = prep == "before" and n or n+1
332 break
333 end
334 end
335 if not position then
336 _error_nogatein(gate, list)
337 return
338 end
339 else
340 _error("Unknown position: `%s'", where)
341 return
342 end
343 end
344 else
345 position = #gatelist.list + 1
346 end
347 local _tb = {}
348 for _, x in ipairs(tb) do
349 _insert(_tb, 1, x)
350 end
351 tb = _tb
352 for _, name in ipairs(tb) do
353 name = _check_family(fam, name)
354 local macro = _rawgate(name)
355 if macro then
356 _insert(gatelist.list, position, { name = name, status = _open, conditional = _evertrue })
357 else
358 _error_nogate(name)
359 end
360 end
361 else
362 _error_nogatelist(list)
363 end
364 end
365
366 local function remove (fam, tb, list)
367 list = _check_family(fam, list)
368 local gatelist = _rawgate(list)
369 if gatelist and gatelist.list then
370 tb = _totable(tb)
371 for _, fct in ipairs(tb) do
372 fct = _check_family(fam, fct)
373 local n, found = 1
374 while n <= #gatelist.list do
375 if gatelist.list[n].name == fct then
376 found = true
377 _remove(gatelist.list, n)
378 else
379 n = n + 1
380 end
381 end
382 if not found then
383 _error_nogatein(fct, list)
384 end
385 end
386 else
387 _error_nogatelist(list)
388 end
389 end
390
391 local function subgates (fam, list, fct)
392 list = _check_family(fam, list)
393 local gatelist = _rawgate(list)
394 if gatelist and gatelist.list then
395 for _, g in ipairs (gatelist.list) do
396 fct(g.name)
397 end
398 else
399 _error_nogatelist(list)
400 end
401 end
402
403 local function _status (fam, fct, list, key, value)
404 if list then
405 list = _check_family(fam, list)
406 local gatelist = _rawgate(list)
407 if gatelist then
408 if type(fct) == "string" and (_match(fct, "^before ") or _match(fct, "^after ")) then
409 local prep, gate = _match(fct, "^(.-)%s+(.-)$")
410 if prep and gate and (prep == "before" or prep == "after") then
411 local list, position = gatelist.list
412 if gate == "first" then
413 position = 1
414 elseif gate == "last" then
415 position = #list
416 else
417 gate = _check_family(fam, gate)
418 for n, g in ipairs(list) do
419 if g.name == gate then
420 position = n
421 break
422 end
423 end
424 end
425 if position then
426 if prep == "before" then
427 for n, g in ipairs(list) do
428 if n == position then
429 break
430 else
431 g[key] = value
432 end
433 end
434 else
435 for n, g in ipairs(list) do
436 if n > position then
437 g[key] = value
438 end
439 end
440 end
441 else
442 _error_nogatein(gate, list)
443 end
444 else
445 _error("Unknown position `%s'", fct)
446 end
447 else
448 fct = _totable(fct)
449 for _, func in ipairs(fct) do
450 func = _check_family(fam, func)
451 local found
452 for n, tb in ipairs(gatelist.list) do
453 if tb.name == func then
454 found = true
455 gatelist.list[n][key] = value
456 end
457 end
458 if not found then
459 _error_nogatein(func, list)
460 end
461 end
462 end
463 else
464 _error_nogatelist(list)
465 end
466 else
467 fct = _totable(fct)
468 for _, func in ipairs(fct) do
469 func = _check_family(fam, func)
470 local macro = _rawgate(func)
471 if macro then
472 macro[key] = value
473 else
474 _error_nogate(func)
475 end
476 end
477 end
478 end
479
480 local function open (fam, fct, list)
481 _status(fam, fct, list, "status", _open)
482 end
483
484 local function ajar (fam, fct, list)
485 _status(fam, fct, list, "status", _ajar)
486 end
487
488 local function skip (fam, fct, list)
489 _status(fam, fct, list, "status", _skip)
490 end
491
492 local function close (fam, fct, list)
493 _status(fam, fct, list, "status", _close)
494 end
495
496 local function conditional (fam, fct, list, cond)
497 if type(list) == "string" then
498 _status(fam, fct, list, "conditional", cond)
499 else
500 _status(fam, fct, nil, "conditional", list)
501 end
502 end
503
504 local function loop (fam, fct, list, cond)
505 if type(list) == "string" then
506 _status(fam, fct, list, "loop", cond)
507 else
508 _status(fam, fct, nil, "loop", list)
509 end
510 end
511
512 local function loopuntil (fam, fct, list, cond)
513 if type(list) == "string" then
514 _status(fam, fct, list, "loopuntil", cond)
515 else
516 _status(fam, fct, nil, "loopuntil", list)
517 end
518 end
519
520 local function iterator (fam, fct, list, cond)
521 if type(list) == "string" then
522 _status(fam, fct, list, "iterator", cond)
523 else
524 _status(fam, fct, nil, "iterator", list)
525 end
526 end
527
528 local function autoreturn (fam, fct, list, cond)
529 if type(list) == "string" then
530 _status(fam, fct, list, "autoreturn", cond)
531 else
532 _status(fam, fct, nil, "autoreturn", list)
533 end
534 end
535
536
537 local function _store (tab)
538 local name = tab[1]
539 local fct = tab[2]
540 local tb = {
541 name = name,
542 status = _statuses[tab.status or "open"],
543 conditional = tab.conditional or _evertrue,
544 loop = tab.loop,
545 loopuntil = tab.loopuntil,
546 iterator = tab.iterator,
547 autoreturn = tab.autoreturn}
548 if not fct then
549 tb.list = {}
550 fct = function (...)
551 _addtab()
552 local arg = {...}
553 for n, func in ipairs(tb.list) do
554 arg = {_execute(func, true, unpackargs(arg))}
555 end
556 _untab()
557 return unpackargs(arg)
558 end
559 end
560 tb.fct = fct
561 execute[0][name] = tb
562 end
563
564 local function def (fam, tab)
565 tab[1] = _check_family(fam, tab[1])
566 _store(tab)
567 end
568
569 local function list (fam, tab)
570 tab[1] = _check_family(fam, tab[1])
571 local subgates = {}
572 for i = 2, #tab do
573 _insert(subgates, tab[i])
574 tab[i] = nil
575 end
576 _store(tab)
577 for _, g in ipairs(subgates) do
578 g = type(g) == "string" and {g} or g
579 g[1] = _check_family(fam, g[1])
580 if type(g[2]) == "function" then
581 _store({g[1], g[2]})
582 elseif type(g[2]) == "table" or type(g[2]) == "string" then
583 local a = {}
584 for i = 2, #g do
585 _insert(a, g[i])
586 end
587 list(fam, {g[1], unpack(a)})
588 elseif not (_rawgate(g[1])) then
589 _store({g[1]})
590 end
591 add(fam, g[1], tab[1])
592 local s = g.status == "open" and _open
593 or g.status == "ajar" and _ajar
594 or g.status == "skip" and _skip
595 or g.status == "close" and _close
596 or _open
597 _status(fam, g[1], tab[1], "status", s)
598 local c = type(g.conditional) == "function" and g.conditional or _evertrue
599 _status(fam, g[1], tab[1], "conditional", c)
600 local l = type(g.loop) == "function" and g.loop
601 _status(fam, g[1], tab[1], "loop", l)
602 local L = type(g.loopuntil) == "function" and g.loopuntil
603 _status(fam, g[1], tab[1], "loopuntil", L)
604 local I = type(g.iterator) == "function" and g.iterator
605 _status(fam, g[1], tab[1], "iterator", I)
606 _status(fam, g[1], tab[1], "autoreturn", g.autoreturn)
607 end
608 end
609
610 local function copy (fam, name1, name2)
611 name1, name2 = _check_family(fam, name1), _check_family(fam, name2)
612 local base = _rawgate(name2)
613 if base then
614 if base.list then
615 _store(name1)
616 _rawgate(name1).list = _copy(base.list)
617 else
618 _store(name1, base.fct)
619 end
620 _status(fam, name1, nil, "status", _open) -- Better than to copy the gate's status.
621 _status(fam, name1, nil, "conditional", base.conditional)
622 _status(fam, name1, nil, "loop", base.loop)
623 _status(fam, name1, nil, "loopuntil", base.loopuntil)
624 _status(fam, name1, nil, "iterator", base.iterator)
625 else
626 _error_nogate(name2)
627 end
628 end
629
630 local function Type (fam, name)
631 name = _check_family(fam, name)
632 local macro = _rawgate(name)
633 if macro then
634 return macro.list and 2 or 1
635 else
636 return 0
637 end
638 end
639
640 local function status (fam, gate, list)
641 gate = _check_family(fam, gate)
642 list = list and _check_family(fam, list)
643 local fct = _rawgate(gate)
644 list = list and _rawgate(list) and _rawgate(list).list
645 if fct then
646 if list then
647 for _, g in ipairs(list) do
648 if g.name == gate then
649 return g.status
650 end
651 end
652 else
653 return fct.status
654 end
655 else
656 return 0
657 end
658 end
659
660 local _mt = {
661 trace = trace,
662 show = show,
663 def = def,
664 list = list,
665 copy = copy,
666 add = add,
667 remove = remove,
668 open = open,
669 ajar = ajar,
670 skip = skip,
671 close = close,
672 type = Type,
673 status = status,
674 conditional = conditional,
675 loop = loop,
676 loopuntil = loopuntil,
677 iterator = iterator,
678 autoreturn = autoreturn,
679 subgates = subgates,
680 }
681
682 local function new (_, fam)
683 local t = {family = fam}
684 setmetatable(t, _mt)
685 return t
686 end
687
688 _mt.new = new
689
690 _mt.__index = function (tb, name)
691 if name == "execute" then
692 return setmetatable({},
693 {__call =
694 function (_, name, ...)
695 name = _check_family(tb.family, name)
696 return execute[name](...)
697 end,
698 __index = function (_, name)
699 name = _check_family(tb.family, name)
700 return execute[name]
701 end})
702 elseif _mt[name] then
703 return function (...) return _mt[name](tb.family, ...) end
704 else
705 name = _check_family(tb.family, name)
706 local macro = _rawgate(name)
707 if macro then
708 return execute[name]
709 -- The following was removed because a gate instance can contain other
710 -- things than gates; for instance "gates.foo = 5" is valid. But one might
711 -- want to know whether an entry exists or not (e.g. "if gates.foo then" or
712 -- "if type(gates.foo) ~= 'nil' then"), in which case the next three lines
713 -- would produce an error (and return a function).
714 -- else
715 -- _error("`%s' isn't an action or a gate", name)
716 -- return _default
717 end
718 end
719 end
720
721 _mt.__newindex = function (tb, key, value)
722 if _mt[key] then
723 _error("You can't assign to key `%s', it is an action", key)
724 else
725 if type(value) == "function" then
726 def(tb.family, {key, value})
727 else
728 local name = _check_family(tb.family, key)
729 if _rawgate(name) then
730 _error("You can't assign to key `%s', it is already a gate", key)
731 else
732 rawset(tb, key, value)
733 end
734 end
735 end
736 end
737
738 gates = new(nil, "gates")