4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
...
600
601
602
603
604
605
606
607
608
609
610
611
612
613
...
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
...
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
....
1107
1108
1109
1110
1111
1112
1113
|
-- ? utility library with functionality common to
-- cortav.lua and its extensions
-- from Ranuir "software utility"
-- > local ss = require 'sirsem.lua'
local ss
do -- pull ourselves up by our own bootstraps
local package = _G.package -- prevent namespace from being broken by env shenanigans
local function namespace(name, tbl)
local pkg = tbl or {}
if package then
package.loaded[name] = pkg
end
return pkg
end
................................................................................
if c.op then
cls.__add = c.op.sum
cls.__sub = c.op.sub
cls.__div = c.op.div
cls.__mul = c.op.mul
cls.__concat = c.op.cat
end
cls.mk = function(...)
local val = setmetatable(c.mk and c.mk(...) or {}, cls)
if c.init then
for k,v in pairs(c.init) do
val[k] = v
................................................................................
ss.version = ss.declare {
name = 'version';
mk = function(tbl) return tbl end;
fns = {
pre = function(self,other) end;
post = function(self,other) end;
string = function(self) return tostring(self) end;
};
cast = {
string = function(vers)
if not(next(vers)) then return '0.0' end
local str = ''
for _,v in pairs(vers) do
if type(v) == 'string' then
................................................................................
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();
................................................................................
-- versions at least can launch programs in a sane and secure
-- way.
else
return s
end
end, ...))
end
|
>
|
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
...
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
...
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
...
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
....
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
|
-- ? utility library with functionality common to
-- cortav.lua and its extensions
-- from Ranuir "software utility"
-- > local ss = require 'sirsem.lua'
local ss
do -- pull ourselves up by our own bootstraps
local package = _G.package
-- prevent namespace from being broken by env shenanigans
local function namespace(name, tbl)
local pkg = tbl or {}
if package then
package.loaded[name] = pkg
end
return pkg
end
................................................................................
if c.op then
cls.__add = c.op.sum
cls.__sub = c.op.sub
cls.__div = c.op.div
cls.__mul = c.op.mul
cls.__concat = c.op.cat
cls.__eq = c.op.eq
cls.__lt = c.op.lt
end
cls.mk = function(...)
local val = setmetatable(c.mk and c.mk(...) or {}, cls)
if c.init then
for k,v in pairs(c.init) do
val[k] = v
................................................................................
ss.version = ss.declare {
name = 'version';
mk = function(tbl) return tbl end;
fns = {
pre = function(self,other) end;
post = function(self,other) end;
string = function(self) return tostring(self) end;
};
cast = {
string = function(vers)
if not(next(vers)) then return '0.0' end
local str = ''
for _,v in pairs(vers) do
if type(v) == 'string' then
................................................................................
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(me,forward)
if forward then
local idx = 0
return function()
idx = idx + 1
if idx > me.top
then return nil
else return me.store[idx], idx
end
end
else
local idx = me.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();
................................................................................
-- versions at least can launch programs in a sane and secure
-- way.
else
return s
end
end, ...))
end
ss.mime = ss.declare {
ident = 'mime-type';
mk = function() return {
class = nil;
kind = nil;
opts = {};
} end;
construct = function(me,str)
if not str then return end
local p,o = str:match '^([^;]+);?%s*(.-)$'
if not p then ss.mime.exn('invalid type syntax %s',str):throw() end
local c,k = p:match '^([^/]+)/?(.-)$'
me.class = (c ~= '') and c or nil
me.kind = (k ~= '') and k or nil
if o and o ~= '' then
for key, e, val in o:gmatch '%s*([^=;]+)(=?)([^;]*)' do
if me.opts[key] then
ss.mime.exn('mime type cannot contain multiple %s options',key):throw()
elseif me.opts.hex and key == 'base64'
or me.opts.base64 and key == 'hex' then
ss.mime.exn('mime type cannot more than one of (base64, hex)',key):throw()
end
if e == '' then val = true end
me.opts[key] = val
end
end
end;
op = {
eq = function(self, other)
-- exact match operator
if not ss.mime.is(other) then return ss.mime.exn("tried to compare MIME type %s against %s (%s)", tostring(self), type(other), tostring(other)):throw() end
if (self.kind == other.kind or (self.kind == '*' or other.kind == '*')) and
(self.class == other.class or (self.class == '*' or other.class == '*')) and
(#self.opts ==#other.opts) then
for k,v in pairs(self.opts) do
if not(other.opts[k] == '*' or (v == '*' and other.opts[k])) then
if other.opts[k] ~= v then return false end
end
end
for k,v in pairs(other.opts) do
if not(self.opts[k] == '*' or (v == '*' and self.opts[k])) then
if self.opts[k] ~= v then return false end
end
end
return true
else
return false
end
end;
lt = function(self,other)
-- lt is the "subset?" operator -- it returns true if self
-- matches at least as many fields as other has. use this
-- when you have a base type and want to check whether
-- another type is compatible with that type. say all you
-- care about is whether a file is "text/plain", and it
-- can be encoded however as long as that much fits.
-- you would then ask ss.mime'text/plain' < file.mime
return other:superset_of(self)
end;
};
cast = {
string = function(me)
local r
if me.kind and me.class then
r = string.format('%s/%s',me.class,me.kind)
elseif me.class then
r = me.class
end
for k,v in pairs(me.opts) do
if v and v ~= true then
r = r .. string.format(';%s=%s',k,v)
elseif v == true then
r = r .. string.format(';%s',k)
end
end
return r
end;
};
fns = {
superset_of = function(self, other)
-- a mime type is greater than another if all the fields
-- other has have a matching field in self. think of this
-- as the "superset?" operator -- all fields and options
-- on other must either match self or be unset
if not ss.mime.is(other) then return ss.mime.exn("tried to compare MIME type %s against %s (%s)", tostring(self), type(other), tostring(other)):throw() end
if (other.class and self.class ~= other.class and other.class ~='*')
or (other.kind and self.kind ~= other.kind and other.kind ~= '*')
then return false end
for k,v in pairs(other.opts) do
if self.opts[k] and self.opts[k] ~= v and v ~='*' then
return false
end
end
return true
end;
is = function(me, pc)
local mimeclasses = {
['application/svg+xml'] = 'image';
['application/x-tar'] = 'archive';
}
local c = me.class
for k,v in pairs(mimeclasses) do
if me > ss.mime(k) then
c = v break
end
end
print(c)
return c == pc
end;
};
}
ss.mime.exn = ss.exnkind 'MIME error'
|