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
...
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
...
265
266
267
268
269
270
271
272
273
274
275
276
277
278
...
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
...
384
385
386
387
388
389
390
|
end
else
new[k] = v
end
end
return new
end
function ss.delegate(tbl,tpl) -- returns a table that looks up keys it lacks from
-- tbl (lightweight alternative to shallow copies)
tpl = tpl or {}
return setmetatable({}, {__index=tbl})
end
ss.str = {}
function ss.str.begins(str, pfx)
return string.sub(str, 1, #pfx) == pfx
end
ss.str.enc = {
utf8 = {
char = utf8.char;
codepoint = utf8.codepoint;
};
c6b = {};
ascii = {};
}
function ss.str.enc.utf8.each(str, ascode)
local pos = {
code = 1;
byte = 1;
}
return function()
if pos.byte > #str then return nil end
local thischar = utf8.codepoint(str, pos.byte)
local lastpos = {
code = pos.code;
byte = pos.byte;
next = pos;
}
if not ascode then
thischar = utf8.char(thischar)
pos.byte = pos.byte + #thischar
else
pos.byte = pos.byte + #utf8.char(thischar)
end
pos.code = pos.code + 1
return thischar, lastpos
end
end
ss.math = {}
function ss.math.lerp(t, a, b)
return (1-t)*a + (t*b)
end
................................................................................
elseif to == 'int' then return math.floor(tonumber(self))
elseif c.cast and c.cast[to] then
return c.cast[to](self, ...)
elseif type(to) == 'table' and getmetatable(to) and getmetatable(to).cvt and getmetatable(to).cvt[cls] then
else error((c.ident or 'class') .. ' is not convertible to ' .. (type(to) == 'string' and to or tostring(to))) end
end
end
if c.fns then return c.fns[k] end
end
if c.cast then
if c.cast.string then
cls.__tostring = c.cast.string
end
if c.cast.number then
................................................................................
if c.construct then
c.construct(val, ...)
end
return val
end
getmetatable(cls).__call = function(_, ...) return cls.mk(...) end
cls.is = function(o) return getmetatable(o) == cls end
return cls
end
-- tidy exceptions
ss.exn = ss.declare {
ident = 'exn';
................................................................................
}
end;
call = function(me, ...)
return ss.exn(me, ...)
end;
}
ss.str.exn = ss.exnkind 'failure while string munging'
function ss.str.delimit(encoding, start, stop, s)
local depth = 0
encoding = encoding or ss.str.enc.utf8
if not ss.str.begins(s, start) then return nil end
for c,p in encoding.each(s) do
if c == (encoding.escape or '\\') then
p.next.byte = p.next.byte + #encoding.char(encoding.codepoint(s, p.next.byte))
p.next.code = p.next.code + 1
elseif c == start then
depth = depth + 1
elseif c == stop then
depth = depth - 1
................................................................................
return x
elseif select('#', ...) == 0 then
return nil
else
return ss.coalesce(...)
end
end
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
<
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
...
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
...
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
...
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
...
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
|
end
else
new[k] = v
end
end
return new
end
function ss.push(tbl, ...)
local idx = #tbl + 1
local function rec(v, ...)
tbl[idx] = v
idx = idx + 1
if ss.tuple.any(...) then rec(...) end
end
rec(...)
return tbl
end
function ss.delegate(tbl,tpl) -- returns a table that looks up keys it lacks from
-- tbl (lightweight alternative to shallow copies)
tpl = tpl or {}
return setmetatable({}, {__index=tbl})
end
ss.str = {}
function ss.str.begins(str, pfx)
-- appallingly, this is actually ~2/5ths faster than either
-- of the below. i hate scripting languages so much
return string.find(str, pfx, 1, true) == 1
-- to my shock, disgust, and horror, even writing my own
-- string scanning library for lua IN C only sped this up by
-- a tiny fraction. i am just speechless.
-- return string.sub(str, 1, #pfx) == pfx
-- local pl = string.len(pfx)
-- local sl = string.len(str)
-- if sl < pl then return false end
-- for i=1,pl do
-- if string.byte(str,i) ~= string.byte(pfx,i) then
-- return false
-- end
-- end
-- return true
end
function ss.enum(syms)
local e = {}
for i,v in pairs(syms) do
e[v] = i
e[i] = v
end
return e
end
function ss.bitmask_bytes(n,ofs)
ofs = ofs or 0
local function rec(i)
if i > n then return end
return 1<<(i+ofs), rec(i+1)
end
return 1<<ofs, rec(1)
end
function ss.bitmask(tbl,ofs)
local codes = {ss.bitmask_bytes(#tbl,ofs)}
local m = {}
local maxbit
for i, s in ipairs(tbl) do
m[s] = codes[i]
m[codes[i]] = s
maxbit = i
end
m[true] = {ofs or 0,maxbit}
return m
end
ss.str.charclass = ss.enum {
'numeral'; 'letter'; 'symbol'; 'punct';
'space'; 'ctl'; 'glyph'; -- hanji
}
ss.str.charprop = ss.bitmask({
'hexnumeral', -- character that can be used to write hexadecimal notation
'upper', 'lower';
'diac'; -- diacritic/modifier letter
'wordbreak'; -- char causes following characters to be treated as a separate word (e.g. punctuation)
'wordsep'; -- char causes previous and following characters to be treated as separate words; char constitutes a word of its own in between (e.g. interpunct)
'breakokay'; -- is it okay to break words at this character? (eg hyphen)
'mathop'; -- char is a mathematical operator
'disallow', -- char is not allowed in narrative text
'brack', 'right', 'left', -- brackets
'noprint', -- character deposits no ink
'superimpose' -- character is superimposed over previous
}, 3)
ss.str.enc_generics = {
pfxescape = function(ch, enc, chain)
local bytes = #ch
local codes = enc.len(ch)
return function(s)
if s == ch then
return 0, 0, ch
elseif ss.str.begins(s, ch) then
local nc = enc.char(enc.codepoint(s, bytes + 1))
return bytes, codes, nc
elseif chain then
return chain(s)
end
end
end;
};
local cc,cp = ss.str.charclass, ss.str.charprop
ss.str.enc = {
utf8 = {
char = utf8.char;
codepoint = utf8.codepoint;
len = utf8.len;
encodeUCS = function(str) return str end;
iswhitespace = function(c)
return (c == ' ') or (c == '\t') or (c == '\n')
or (c == '\u{3000}')
or (c == '\u{200B}')
end;
};
ascii = {
len = string.len; char = string.char; codepoint = string.byte;
iswhitespace = function(c)
return (c == ' ') or (c == '\t') or (c == '\n')
end;
ranges = {
{0x00,0x1a, cc.ctl};
{0x1b,0x1b, cc.ctl, cp.disallow};
{0x1c,0x1f, cc.ctl};
{0x20,0x20, cc.space};
{0x21,0x22, cc.punct};
{0x23,0x26, cc.symbol};
{0x27,0x29, cc.punct};
{0x2a,0x2b, cc.symbol};
{0x2c,0x2f, cc.punct};
{0x30,0x39, cc.numeral, cp.hexnumeral};
{0x3a,0x3b, cc.punct};
{0x3c,0x3e, cc.symbol, cp.mathop};
{0x3f,0x3f, cc.punct};
{0x40,0x40, cc.symbol};
{0x41,0x46, cc.letter, cp.ucase, cp.hexnumeral};
{0x47,0x5a, cc.letter, cp.ucase};
{0x5b,0x5d, cc.symbol, cp.mathop};
{0x5e,0x5e, cc.symbol, mathop};
{0x5f,0x60, cc.symbol};
{0x61,0x66, cc.letter, cp.lcase, cp.hexnumeral};
{0x67,0x7a, cc.letter, cp.lcase};
{0x7b,0x7e, cc.symbol};
{0x7f,0x7f, cc.ctl, cp.disallow};
}
};
raw = {len = string.len; char = string.char; codepoint = string.byte;
encodeUCS = function(str) return str end;
iswhitespace = function(c)
return (c == ' ') or (c == '\t') or (c == '\n')
end;
};
}
-- unicode ranges are optionally generated from consortium data
-- files and injected through a generated source file. if this
-- part of the build process is disabled (e.g. due to lack of
-- internet access, or to keep the size of the executable as
-- small as possible), we still at least can make the ascii
-- ranges available to UTF8 (UTF8 being a superset of ascii)
ss.str.enc.utf8.ranges = ss.delegate(ss.str.enc.ascii.ranges)
function ss.str.enc.ascii.encodeUCS(str)
local newstr = ''
for c,p in ss.str.each(ss.str.enc.utf8, str, true) do
if c > 0x7F then
newstr = newstr .. '?'
else
newstr = newstr .. string.char(c)
end
end
end
for _, v in pairs{'utf8','ascii','raw'} do
ss.str.enc[v].parse_escape = ss.str.enc_generics.pfxescape('\\',ss.str.enc[v])
end
function ss.str.classify(enc, ch)
if not enc.ranges then return {} end
if type(ch)=='string' then ch = enc.codepoint(ch) end
-- TODO
end
function ss.str.each(enc, str, ascode)
if enc.each then return enc.each(enc,str,ascode) end
local pm = {
__index = {
esc = function(self)
local ba, bc, nc = enc.parse_escape(str:sub(self.byte))
if ba then
self.next.byte = self.next.byte + ba - 1
self.next.code = self.next.code + bc - 1
return nc
end
end;
};
}
local pos = {
code = 1;
byte = 1;
}
return function()
if pos.byte > #str then return nil end
local thischar = enc.codepoint(str, pos.byte)
local lastpos = setmetatable({
code = pos.code;
byte = pos.byte;
next = pos;
},pm)
if not ascode then
thischar = enc.char(thischar)
pos.byte = pos.byte + #thischar
else
pos.byte = pos.byte + #enc.char(thischar)
end
pos.code = pos.code + 1
return thischar, lastpos
end
end
function ss.str.breakwords(enc, str, max, opts)
if enc.breakwords then return enc.breakwords(str) end
local words = {}
opts = opts or {}
local buf = ''
local flush = function()
if buf ~= '' then table.insert(words,buf) buf = '' end
end
for c, p in ss.str.each(enc,str) do
local nc
if opts.escape then
nc = p:esc()
end
if nc then
buf = buf + nc
elseif enc.iswhitespace(c) then
flush()
if max and #words == max then
local rs = str:sub(p.next.byte)
if rs ~= '' then
table.insert(words, rs)
end
break
end
else
buf = buf .. c
end
end
flush()
return words
end
function ss.str.mergewords(enc, lst)
if enc.mergewords then return enc.mergewords(lst) end
return table.concat(lst, enc.wordsep or ' ')
end
function ss.str.breaklines(enc, str, opts)
if enc.breaklines then return enc.breaklines(lst,opts) end
return ss.str.split(enc, str, enc.encodeUCS'\n', opts)
end
function ss.str.split(enc, str, delim, opts)
if enc.split then return enc.split(str,delim,opts) end
opts = opts or {}
local elts = {}
local buf = ''
local flush = function()
if buf ~= '' or opts.keep_empties then
table.insert(elts,buf)
buf = ''
end
end
local esc = enc.parse_escape
local tryesc if opts.escape then
tryesc = function(str, p)
local ba, ca, escd = enc.parse_escape(str:sub(p.byte))
if ba then
p.next.byte = p.next.byte + ba
p.next.code = p.next.code + ca
buf = buf .. escd
return true
end
end
else
tryesc = function(...) end
end
if type(delim) == 'function' then
for c, p in ss.str.each(enc,str) do
if not tryesc(str,p) then
local skip = delim(str:sub(p.byte))
if skip then
flush()
p.next.byte = p.next.byte + skip - 1
else
buf = buf .. c
end
end
end
elseif enc.len(delim) == 1 then
for c, p in ss.str.each(enc,str) do
if not tryesc(str,p) then
if c == delim then
flush()
else
buf = buf .. c
end
end
end
else
local dlcode = enc.len(delim)
for c, p in ss.str.each(enc,str) do
if not tryesc(str,p) then
if str:sub(p.byte, p.byte+#delim-1) == delim then
flush()
p.next.byte = p.next.byte + #delim - 1
p.next.code = p.next.code + dlcode
else
buf = buf .. c
end
end
end
end
flush()
return elts
end
function ss.str.langmatch(tbl, lang, enc)
-- this performs primitive language matching. NOTE: THIS IS NOT
-- STANDARDS COMPLIANT. it's "good enough" for now, but in the
-- long term it needs to be rewritten to actually understand the
-- format, primarily so that e.g. 'en-US-Latn' and 'en-Latn-US'
-- match -- currently order is significant. it shouldn't be
-- ref: IETF BCP 47 (RFC 5646) https://www.ietf.org/rfc/bcp/bcp47.html
local dash = enc.encodeUCS'-'
local tags = ss.str.split(enc, lang, dash, {escape=true})
local bestlen = 0
local bestmatch
for k,v in pairs(tbl) do
if k ~= true then
local kt = ss.str.split(enc, k, dash, {escape=true})
for i=1,math.min(#kt,#tags) do
if kt[i] ~= tags[i] then goto skip end
end
if #kt > bestlen then
-- match the most specific matching tag
bestmatch = k
bestlen = #kt
end
end
::skip::end
return tbl[bestmatch] or tbl[true], bestmatch
end
ss.math = {}
function ss.math.lerp(t, a, b)
return (1-t)*a + (t*b)
end
................................................................................
elseif to == 'int' then return math.floor(tonumber(self))
elseif c.cast and c.cast[to] then
return c.cast[to](self, ...)
elseif type(to) == 'table' and getmetatable(to) and getmetatable(to).cvt and getmetatable(to).cvt[cls] then
else error((c.ident or 'class') .. ' is not convertible to ' .. (type(to) == 'string' and to or tostring(to))) end
end
end
if c.fns and c.fns[k] then return c.fns[k] end
if c.index then return c.index(self,k) end
end
if c.cast then
if c.cast.string then
cls.__tostring = c.cast.string
end
if c.cast.number then
................................................................................
if c.construct then
c.construct(val, ...)
end
return val
end
getmetatable(cls).__call = function(_, ...) return cls.mk(...) end
cls.is = function(o) return getmetatable(o) == cls end
cls.__metatable = cls -- lock metatable
return cls
end
-- tidy exceptions
ss.exn = ss.declare {
ident = 'exn';
................................................................................
}
end;
call = function(me, ...)
return ss.exn(me, ...)
end;
}
ss.str.exn = ss.exnkind 'failure while string munging'
ss.bug = ss.exnkind 'tripped over bug'
function ss.str.delimit(encoding, start, stop, s)
local depth = 0
encoding = encoding or ss.str.enc.utf8
if not ss.str.begins(s, start) then return nil end
for c,p in ss.str.each(encoding,s) do
if c == (encoding.escape or '\\') then
p.next.byte = p.next.byte + #encoding.char(encoding.codepoint(s, p.next.byte))
p.next.code = p.next.code + 1
elseif c == start then
depth = depth + 1
elseif c == stop then
depth = depth - 1
................................................................................
return x
elseif select('#', ...) == 0 then
return nil
else
return ss.coalesce(...)
end
end
ss.tuple = {}
function ss.tuple.any(...)
return select('#',...) > 0
end
function ss.tuple.cat(...)
local a = {...}
return function(...)
ss.push(a, ...)
return table.unpack(a)
end
end
function ss.tuple.suffix(sfx,n,...)
if n ~= nil then
return n, ss.tuple.suffix(...)
else
return sfx
end
end
function ss.tuple.cdr(x, ...) return ... end
ss.stack = ss.declare {
ident = 'stack';
mk = function() return {
top = 0;
store = {};
} end;
index = function(me, i)
if i <= 0 then
return me.store[me.top + i]
else
return me.store[i]
end
end;
fns = {
push = function(me, val, ...)
if val~=nil then
me.top = me.top + 1
me.store[me.top] = val
me:push(...)
end
return val, ...
end;
pop = function(me,n) n = n or 1
local r = {}
if n < me.top then
for i = 0,n-1 do
r[i+1] = me.store[me.top - i]
me.store[me.top - i] = nil
end
me.top = me.top - n
else
r = me.store
me.store = {}
end
return table.unpack(r)
end;
set = function(me,val)
if me.top == 0 then
me.top = me.top + 1 --autopush
end
me.store[me.top] = val
end;
all = function(me) return table.unpack(me.store) end;
each = function(forward)
if forward then
local idx = 0
return function()
idx = idx + 1
if idx > top
then return nil
else return me.store[idx], idx
end
end
else
local idx = top + 1
return function()
idx = idx - 1
if idx == 0
then return nil
else return me.store[idx], idx
end
end
end
end;
};
}
ss.automat = ss.declare {
ident = 'automat';
mk = function() return {
state = ss.stack();
states = {};
ttns = {};
mem = {};
match = function(sym, ttn, mach)
if ttn.pred and ttn:pred(mach, sym)~=true then
return false
end
if ttn.on then
return sym == ttn.on
end
return false
end;
} end;
construct = function(me, def)
for k,v in pairs{'states','ttns','mem','syms'} do
if def[k] then me[k] = v end
end
end;
fns = {
react = function(me,sym)
local s = me.states[me.state.id]
if s and s.input then
s:react(me, sym)
end
end;
drop = function(me,n)
for i = 0, math.min(n-1,me.state.top-1) do
local s = me.states[me.state[-i].id]
if s.exit then s:exit(s.mem, me) end
end
if n < me.state.top then
local newtop = me.states[me.state[-n].id]
if newtop.activate then newtop:activate(me.state[-n].mem, me, n) end
end
return me.state:pop(n)
end;
clear = function(me) return me:drop(me.state.top) end;
transition = function(me,ttn,oldstates)
local s = me.state:push {id = ttn.to, mem = {}}
local to = me.states[ttn.to]
if to.enter then
to:enter(s.mem, me)
end
end;
input = function(me,sym)
local ttns = me.ttns[me.state.id]
local _, ttn = ss.find(ttns, function(ttn)
return me.match(sym, ttn, me)
end)
if ttn then
if ttn.pop then
local oldstates = {me.state:drop(ttn.pop)}
me:transition(ttn, sym, oldstates)
else
me:transition(ttn, sym)
end
else
me:react(sym)
end
end;
};
}
|