summaryrefslogtreecommitdiff
path: root/tex/context/base/mkxl/mlib-scn.lmt
blob: 33aa74ad2e123ee5e1fdc146a427caf9193efed4 (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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
if not modules then modules = { } end modules ['mlib-scn'] = {
    version   = 1.001,
    comment   = "companion to mlib-ctx.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files",
}

-- Very experimental, for Alan and me.

-- for i = 1 upto 32000 : % 0.062
--     ts := 5mm / 20;
-- endfor ;
--
-- for i = 1 upto 32000 : % 0.219
--     ts := (getparameter "axis" "sy") / 20;
-- endfor ;
--
-- for i = 1 upto 32000 : % 0.266
--     ts := (getparameterx "axis" "sy") / 20;
-- endfor ;
--
-- pushparameters "axis";
-- for i = 1 upto 32000 : % 0.250
--     ts := (getparameterx "sy") / 20;
-- endfor ;
-- popparameters;

local type, next, rawget, getmetatable, tonumber = type, next, rawget, getmetatable, tonumber
local byte, gmatch = string.byte, string.gmatch
local insert, remove = table.insert, table.remove

local mplib    = mplib
local metapost = metapost

local codes = metapost.codes
local types = metapost.types

local setmetatableindex   = table.setmetatableindex

local scanners            = mp.scan
local injectors           = mp.inject

local scannext            = scanners.next
local scanexpression      = scanners.expression
local scantoken           = scanners.token
local scansymbol          = scanners.symbol
local scanproperty        = scanners.property
local scannumeric         = scanners.numeric
local scannumber          = scanners.number
local scaninteger         = scanners.integer
local scanboolean         = scanners.boolean
local scanstring          = scanners.string
local scanpair            = scanners.pair
local scancolor           = scanners.color
local scancmykcolor       = scanners.cmykcolor
local scantransform       = scanners.transform
local scanpath            = scanners.path
local scanpen             = scanners.pen

local mpprint             = mp.print
local injectnumeric       = injectors.numeric
local injectstring        = injectors.string
local injectboolean       = injectors.boolean
local injectpair          = injectors.pair
local injecttriplet       = injectors.color
local injectquadruple     = injectors.cmykcolor
local injecttransform     = injectors.transform
local injectpath          = injectors.path

local report              = logs.reporter("metapost")

local semicolon_code      = codes.semicolon
local equals_code         = codes.equals
local comma_code          = codes.comma
local colon_code          = codes.colon
local leftbrace_code      = codes.leftbrace
local rightbrace_code     = codes.rightbrace
local leftbracket_code    = codes.leftbracket
local rightbracket_code   = codes.rightbracket
local leftdelimiter_code  = codes.leftdelimiter
local rightdelimiter_code = codes.rightdelimiter
local numeric_code        = codes.numeric
local string_code         = codes.string
local capsule_code        = codes.capsule
local nullary_code        = codes.nullary
local tag_code            = codes.tag
local definedmacro_code   = codes.definedmacro

local typescanners   = nil
local tokenscanners  = nil
local scanset        = nil
local scanparameters = nil

scanset = function() -- can be optimized, we now read twice
    scantoken()
    if scantoken(true) == rightbrace_code then
        scantoken()
        return { }
    else
        local l = { }
        local i = 0
        while true do
            i = i + 1
            local s = scansymbol(true)
            if s == "{" then
                l[i] = scanset()
            elseif s == "[" then
                local d = { }
                scansymbol()
                while true do
                    local s = scansymbol()
                    if s == "]" then
                        break;
                    elseif s == "," then
                        -- continue
                    else
                        local t = scantoken(true)
                        if t == equals_code or t == colon_code then
                            scantoken()
                        end
                        d[s] = tokenscanners[scantoken(true)]()
                    end
                end
                l[i] = d
            else
                local e = scanexpression(true)
                l[i] = (typescanners[e] or scanexpression)()
            end
            if scantoken() == rightbrace_code then
                break
            else
             -- whatever
            end
        end
        return l
    end
end

tokenscanners = {
    [leftbrace_code] = scanset,
    [numeric_code]   = scannumeric,
    [string_code]    = scanstring,
    [nullary_code]   = scanboolean,   -- todo
}

typescanners = {
    [types.known]     = scannumeric,
    [types.numeric]   = scannumeric,
    [types.string]    = scanstring,
    [types.boolean]   = scanboolean,
    [types.pair]      = function() return scanpair     (true) end,
    [types.color]     = function() return scancolor    (true) end,
    [types.cmykcolor] = function() return scancmykcolor(true) end,
    [types.transform] = function() return scantransform(true) end,
    [types.path]      = scanpath,
    [types.pen]       = scanpen,
}

table.setmetatableindex(tokenscanners,function()
    local e = scanexpression(true)
    return typescanners[e] or scanexpression
end)

scanners.typescanners  = typescanners
scanners.tokenscanners = tokenscanners

scanners.whatever = function()
    local kind = scantoken(true)
    if kind == leftdelimiter_code or kind == tag_code or kind == capsule_code then
        return (typescanners[scanexpression(true)] or scanexpression)()
    else
        return tokenscanners[kind]()
    end
end

-- a key like 'color' has code 'declare'

local function scanparameters(fenced)
    local data  = { }
    local close = "]"
    if not fenced then
        close = ";"
    elseif scansymbol(true) == "[" then
        scansymbol()
    else
        return data
    end
    while true do
     -- local s = scansymbol()
        local s = scansymbol(false,false) -- keep expand
        if s == close then
            break;
        elseif s == "," then
            -- continue
        else
--             local t = scantoken(true)
--             if t == equals_code or t == colon_code then
--                 -- optional equal or :
--                 scantoken()
--             else
--             end
--             local kind = scantoken(true)

-- test:
--
            local kind = scantoken(true)
            if kind == equals_code or kind == colon_code then
                -- optional equal or :
                scantoken()
                local kind = scantoken(true)
            end

            if kind == leftdelimiter_code or kind == tag_code or kind == capsule_code then
                kind = scanexpression(true)
                data[s] = (typescanners[kind] or scanexpression)()
            elseif kind == leftbracket_code then
                data[s] = get_parameters(true)
            else
                data[s] = tokenscanners[kind]()
            end
        end
    end
    return data
end

local namespaces = { }
local presets    = { }
local passed     = { }

local function get_parameters(nested)
    local data = { }
    if nested or scansymbol(true) == "[" then
        scansymbol()
    else
        return data
    end
    while true do
        local s = scansymbol(false,false)
        if s == "]" then
            break;
        elseif s == "," then
            -- continue
        else
            local t = scantoken(true)
            if t == equals_code or t == colon_code then
                -- optional equal or :
                scantoken()
            end
            local kind = scantoken(true)
            if kind == leftdelimiter_code or kind == tag_code or kind == capsule_code then
                kind = scanexpression(true)
                data[s] = (typescanners[kind] or scanexpression)()
            elseif kind == leftbracket_code then
                data[s] = get_parameters(true)
            elseif kind == comma_code then
                goto again
            else
                data[s] = tokenscanners[kind]()
            end
        end
      ::again::
    end
    return data
end

local function getparameters()
    local namespace  = scanstring()
    -- same as below
    local parameters = get_parameters()
    local presets    = presets[namespace]
    local passed     = passed[namespace]
    if passed then
        if presets then
            setmetatableindex(passed,presets)
        end
        setmetatableindex(parameters,passed)
    elseif presets then
        setmetatableindex(parameters,presets)
    end
    namespaces[namespace] = parameters
end

local function applyparameters()
    local saved      = namespaces
    local namespace  = scanstring()
    local action     = scanstring() -- before we scan the parameters
    -- same as above
    local parameters = get_parameters()
    local presets    = presets[namespace]
    local passed     = passed[namespace]
    if passed then
        if presets then
            setmetatableindex(passed,presets)
        end
        setmetatableindex(parameters,passed)
    elseif presets then
        setmetatableindex(parameters,presets)
    end
    namespaces[namespace] = parameters
    -- till here
 -- mpprint(action)
    namespaces = saved
    return action
end

local knownparameters    = { }
metapost.knownparameters = knownparameters

local function presetparameters()
    local namespace = scanstring()
    local parent = nil
    local t = scantoken(true)
    if t == string_code then
        parent = presets[scanstring()]
    end
    local p = get_parameters()
    for k in next, p do
        knownparameters[k] = true
    end
    if parent then
        setmetatableindex(p,parent)
    end
    presets[namespace] = p
end

local function collectnames()
    local l = { } -- can be reused but then we can't nest
    local n = 0
    while true do
        local t = scantoken(true)
        -- (1) not really needed
        if t == numeric_code then
            n = n + 1 l[n] = scannumeric(1)
        elseif t == string_code then
            n = n + 1 l[n] = scanstring(1)
        elseif t == nullary_code then
            n = n + 1 l[n] = scanboolean(1)
        elseif t == leftbracket_code then
            scantoken() -- leftbacket
            n = n + 1 l[n] = scaninteger(1)
            scantoken() -- rightbacket
        elseif t == leftdelimiter_code or t == tag_code or t == capsule_code then
            t = scanexpression(true)
            n = n + 1 l[n] = (typescanners[t] or scanexpression)()
        else
            break
        end
    end
    return l, n
end

local function get(v)
    local t = type(v)
    if t == "number" then
        return injectnumeric(v)
    elseif t == "boolean" then
        return injectboolean(v)
    elseif t == "string" then
        return injectstring(v)
    elseif t == "table" then
        local n = #v
        if type(v[1]) == "table" then
            return injectpath(v)
        elseif n == 2 then
            return injectpair(v)
        elseif n == 3 then
            return injecttriplet(v)
        elseif n == 4 then
            return injectquadruple(v)
        elseif n == 6 then
            return injecttransform(v)
        end
    end
    return injectnumeric(0)
end

local stack = { }

local function pushparameters()
    local l, n = collectnames()
    insert(stack,namespaces)
    for i=1,n do
        local n = namespaces[l[i]]
        if type(n) == "table" then
            namespaces = n
        else
            break
        end
    end
end

local function popparameters()
    local n = remove(stack)
    if n then
        namespaces = n
    else
        report("stack error")
    end
end

-- todo:

local function getparameter(v)
    local list, n = collectnames()
    if not v then
        v = namespaces
    end
    for i=1,n do
        local l = list[i]
        local vl = v[l]
        if vl == nil then
            if type(l) == "number" then
                vl = v[1]
                if vl == nil then
                    return injectnumeric(0)
                end
            else
                return injectnumeric(0)
            end
        end
        v = vl
    end
    if v == nil then
        return 0
    else
        return v
    end
end

local function hasparameter()
    local list, n = collectnames()
    local v = namespaces
    for i=1,n do
        local l = list[i]
        local vl = rawget(v,l)
        if vl == nil then
            if type(l) == "number" then
                vl = rawget(v,1)
                if vl == nil then
                    return injectboolean(false)
                end
            else
                return injectboolean(false)
            end
        end
        v = vl
    end
 -- if v == nil then
 --     return injectboolean(false)
 -- else
 --     return injectboolean(true)
 -- end
    return v ~= nil
end

local function hasoption()
    local list, n = collectnames()
    if n > 1 then
        local v = namespaces
        if n > 2 then
            for i=1,n-1 do
                local l = list[i]
                local vl = v[l]
                if vl == nil then
                 -- return injectboolean(false)
                    return false
                end
                v = vl
            end
        else
            v = v[list[1]]
        end
        if type(v) == "string" then
            -- no caching .. slow anyway
            local o = list[n]
            if v == o then
             -- return injectboolean(true)
                return true
            end
            for vv in gmatch(v,"[^%s,]+") do
                for oo in gmatch(o,"[^%s,]+") do
                    if vv == oo then
                     -- return injectboolean(true)
                        return true
                    end
                end
            end
        end
    end
 -- return injectboolean(false)
    return false
end

local function getparameterdefault()
    local list, n = collectnames()
    local v = namespaces
    if n == 1 then
        local l = list[1]
        local vl = v[l]
        if vl == nil then
            -- maybe backtrack
            local top = stack[#stack]
            if top then
                vl = top[l]
            end
        end
        if vl == nil then
         -- return injectnumeric(0)
            return 0
        else
            if type(vl) == "string" then
                local td = type(list[n])
                if td == "number" then
                    vl = tonumber(vl)
                elseif td == "boolean" then
                    vl = vl == "true"
                end
            end
         -- return get(vl)
            return vl
        end
    else
        for i=1,n-1 do
            local l = list[i]
            local vl = v[l]
            if vl == nil then
                if type(l) == "number" then
                    vl = v[1]
                    if vl == nil then
                     -- return get(list[n])
                        return list[n]
                    end
                else
                    local last = list[n]
                    if last == "*" then
                        -- so, only when not pushed
                        local m = getmetatable(namespaces[list[1]])
                        if n then
                            m = m.__index -- can also be a _m_
                        end
                        if m then
                            local v = m
                            for i=2,n-1 do
                                local l = list[i]
                                local vl = v[l]
                                if vl == nil then
                                 -- return injectnumeric(0)
                                    return 0
                                end
                                v = vl
                            end
                            if v == nil then
                                return 0
                            else
                                return v
                            end
                        end
                     -- return injectnumeric(0)
                        return 0
                    else
                     -- return get(last)
                        return last
                    end
                end
            end
            v = vl
        end
        if v == nil then
         -- return get(list[n])
            return list[n]
        else
            if type(v) == "string" then
                local td = type(list[n])
                if td == "number" then
                    v = tonumber(v)
                elseif td == "boolean" then
                    v = v == "true"
                end
            end
         -- return get(v)
            return v
        end
    end
end

local function getparametercount()
    local list, n = collectnames()
    local v = namespaces
    for i=1,n do
        v = v[list[i]]
        if not v then
            break
        end
    end
 -- return injectnumeric(type(v) == "table" and #v or 0)
    return type(v) == "table" and #v or 0
end

local function getmaxparametercount()
    local list, n = collectnames()
    local v = namespaces
    for i=1,n do
        v = v[list[i]]
        if not v then
            break
        end
    end
    local n = 0
    if type(v) == "table" then
        local v1 = v[1]
        if type(v1) == "table" then
            n = #v1
            for i=2,#v do
                local vi = v[i]
                if type(vi) == "table" then
                    local vn = #vi
                    if vn > n then
                        n = vn
                    end
                else
                    break;
                end
            end
        end

    end
 -- return injectnumeric(n)
    return n
end

local validconnectors = {
    [".."]  = true,
    ["..."] = true,
    ["--"]  = true,
}

local function getparameterpath()
    local list, n = collectnames()
    local close = list[n]
    if type(close) == "boolean" then
        n = n - 1
    else
        close = false
    end
    local connector = list[n]
    if type(connector) == "string" and validconnectors[connector] then
        n = n - 1
    else
        connector = "--"
    end
    local v = namespaces
    for i=1,n do
        v = v[list[i]]
        if not v then
            break
        end
    end
    if type(v) == "table" then
        return injectpath(v,connector,close)
    elseif type(v) == "string" then
        local code = load("return " .. v)
        if code then
            return code()
        end
    else
        return injectpair(0,0)
    end
end

local function getparameterpen()
    local list, n = collectnames()
    local v = namespaces
    for i=1,n do
        v = v[list[i]]
        if not v then
            break
        end
    end
    if type(v) == "table" then
        return injectpath(v,"..",true)
    else
        return injectpair(0,0)
    end
end

local function getparametertext()
    local list, n = collectnames()
    local strut = list[n]
    if type(strut) == "boolean" then
        n = n - 1
    else
        strut = false
    end
    local v = namespaces
    for i=1,n do
        v = v[list[i]]
        if not v then
            break
        end
    end
    if type(v) == "string" then
        return injectstring("\\strut " .. v)
    else
        return injectstring("")
    end
end

-- local function getparameteroption()
--     local list, n = collectnames()
--     local last = list[n]
--     if type(last) == "string" then
--         n = n - 1
--     else
--         return false
--     end
--     local v = namespaces
--     for i=1,n do
--         v = v[list[i]]
--         if not v then
--             break
--         end
--     end
--     if type(v) == "string" and v ~= "" then
--         for s in gmatch(v,"[^ ,]+") do
--             if s == last then
--                 return true
--             end
--         end
--     end
--     return false
-- end

function metapost.scanparameters()
--     scantoken() -- we scan the semicolon
    return get_parameters()
end

local registerscript = metapost.registerscript
local registerdirect = metapost.registerdirect
local registertokens = metapost.registertokens

registerdirect("getparameters",       getparameters)        -- nothing
registertokens("applyparameters",     applyparameters)      -- action    : todo "token"
registerdirect("presetparameters",    presetparameters)     -- nothing
registerdirect("hasparameter",        hasparameter)         -- boolean
registerdirect("hasoption",           hasoption)            -- boolean
registerdirect("getparameter",        getparameter)         -- whatever
registerdirect("getparameterdefault", getparameterdefault)  -- whatever
registerdirect("getparametercount",   getparametercount)    -- numeric
registerdirect("getmaxparametercount",getmaxparametercount) -- numeric
registerscript("getparameterpath",    getparameterpath)     -- tricky
registerscript("getparameterpen",     getparameterpen)      -- tricky
registerscript("getparametertext",    getparametertext)     -- tricky
--------direct("getparameteroption",  getparameteroption)   -- boolean
registerdirect("pushparameters",      pushparameters)       -- nothing
registerdirect("popparameters",       popparameters)        -- nothing

function metapost.getparameter(list)
    local n = #list
    local v = namespaces
    for i=1,n do
        local l = list[i]
        local vl = v[l]
        if vl == nil then
            return
        end
        v = vl
    end
    return v
end

function metapost.getparameterset(namespace)
    return namespace and namespaces[namespace] or namespaces
end

function metapost.setparameterset(namespace,t)
    namespaces[namespace] = t
end

function metapost.getparameterpreset(namespace,t)
    return namespace and presets[namespace] or presets
end

local function setluaparameter()
    local namespace = scanstring()
    local name      = scanstring()
    local value     = scanstring()
    local code      = load("return " .. value)
    if type(code) == "function" then
        local result = code()
        if result then
            local data = namespace and namespaces[namespace] or namespaces
            data[name] = result
        else
            report("no result from lua code: %s",value)
        end
    else
        report("invalid lua code: %s",value)
    end
end

registerdirect("setluaparameter", setluaparameter)

-- This is an experiment for Alan and me.

do

    local records    = { }
    local stack      = setmetatableindex("table")
    local nofrecords = 0
    local interim    = 0
    local names      = { }
 -- local types      = { }

    registerdirect("newrecord", function()
        scantoken() -- semicolon
        local p = get_parameters()
        local n = 0
        if interim > 0 then
            records[interim] = p
            local top = stack[interim]
            if top then
                top = stack[interim][#top]
                if top then
                    setmetatableindex(p,top)
                end
            end
            n = interim
            interim = 0
        else
            nofrecords = nofrecords + 1
            records[nofrecords] = p
            n = nofrecords
        end
        return n
    end)

    local function merge(old,new)
        for knew, vnew in next, new do
            local vold = old[knew]
            if vold then
                if type(vnew) == "table" then
                    if type(vold) == "table" then
                        merge(vold,vnew)
                    else
                        old[knew] = vnew
                    end
                else
                    old[knew] = vnew
                end
            else
                old[knew] = vnew
            end
        end
    end

    registerdirect("setrecord", function()
        scantoken() -- semicolon
        local p = get_parameters()
        local n = 0
        if interim > 0 then
            local r = records[interim]
            if r then
                merge(r,p)
            else
                records[interim] = p
            end
            local top = stack[interim]
            if top then
                top = stack[interim][#top]
                if top then
                    setmetatableindex(p,top)
                end
            end
            n = interim
            interim = 0
        else
            nofrecords = nofrecords + 1
            records[nofrecords] = p
            n = nofrecords
        end
        return n
    end)


    registerdirect("getrecord", function()
        local n = scaninteger()
        local v = records[n]
        while true do
            local t = scansymbol(true)
            if t == ";" or t == ")" or t == ":" then
                return v
            elseif t == "." then
                scansymbol()
            elseif t == "#" or t == "##" then -- from tex's we get a double
                scansymbol()
                t = scansymbol()
                v = v[t]
                return type(v) == "table" and #v or 0
            elseif t == "[" then
                scansymbol()
                t = scansymbol(true)
                if t == "]" then
                    scansymbol()
                    return #v
                else
                    t = scaninteger()
                    v = v[t]
                    if scansymbol(true) == "]" then
                        scansymbol()
                    else
                        report("] expected")
                    end
                end
            else
                t = scansymbol()
                v = v[t]
            end
        end
    end)

    function metapost.getrecord(name)
        local index = names[name]
        if index then
            return records[index]
        end
    end

    function metapost.setrecord(name,data)
        if type(data) == "table" then
            local index = names[name]
            if index then
                records[index] = data
            end
        end
    end

    function metapost.runinternal(action,index,kind,name)
        if action == 0 then
            -- allocate
            names[name] = index
         -- types[index] = kind
        elseif action == 1 then
            -- save
            insert(stack[index],records[index])
            interim = index
        elseif action == 2 then
            -- restore
            records[index] = remove(stack[index]) or records[index]
        elseif action == 3 then
            metapost.checktracingonline(kind)
        end
    end

end

-- goodies

registerdirect("definecolor", function()
    scantoken() -- we scan the semicolon
    local s = get_parameters()
    attributes.colors.defineprocesscolordirect(s)
end)

-- tex scanners

local scanners      = tokens.scanners
local scanhash      = scanners.hash
local scanstring    = scanners.string
local scanvalue     = scanners.value
local scaninteger   = scanners.integer
local scanboolean   = scanners.boolean
local scanfloat     = scanners.float
local scandimension = scanners.dimension

local definitions   = { }

local bpfactor      = number.dimenfactors.bp
local comma         = byte(",")
local close         = byte("]")

local scanrest      = function() return scanvalue(comma,close) or "" end
local scandimension = function() return scandimension() * bpfactor end

local scanners = {
    ["integer"]   = scaninteger,
    ["number"]    = scanfloat,
    ["numeric"]   = scanfloat,
    ["boolean"]   = scanboolean,
    ["string"]    = scanrest,
    ["dimension"] = scandimension,
}

interfaces.implement {
    name      = "lmt_parameters_define",
    arguments = "string",
    actions   = function(namespace)
        local d = scanhash()
        for k, v in next, d do
            d[k] = scanners[v] or scanrest
        end
        definitions[namespace] = d
    end,
}

interfaces.implement {
    name      = "lmt_parameters_preset",
    arguments = "string",
    actions   = function(namespace)
        passed[namespace] = scanhash(definitions[namespace])
    end,
}

interfaces.implement {
    name      = "lmt_parameters_reset",
    arguments = "string",
    actions   = function(namespace)
        passed[namespace] = nil
    end,
}