-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.lua
More file actions
433 lines (380 loc) · 12 KB
/
Copy pathtypes.lua
File metadata and controls
433 lines (380 loc) · 12 KB
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
local types = {}
local type_mt = {}
type_mt.__index = type_mt
function type_mt:to_str()
if self.str then
return self.str
elseif self.valstruct and self.valstruct.kind == 'tuple' then
local s = '('
for i, v in ipairs(self.valstruct.members) do
if i ~= 1 then s = s .. ',' end
s = s .. v:to_str()
end
return s..')'
elseif self.valstruct and self.valstruct.kind == 'struct' then
local s = 'struct('
for i, v in ipairs(self.valstruct.members) do
if i ~= 1 then s = s .. ',' end
s = s .. self.valstruct.member_names[i] .. ':' .. v:to_str()
end
return s..')'
elseif self.valstruct and self.valstruct.kind == 'variant' then
local s = 'variant('
for i, v in ipairs(self.valstruct.member_names) do
if i ~= 1 then s = s .. ',' end
s = s .. v
end
return s..')'
elseif self.cls then
local s = 'class('
for i, v in ipairs(self.cls.members) do
if i ~= 1 then s = s .. ',' end
s = s .. self.cls.member_names[i] .. ':' .. v:to_str()
end
return s..')'
elseif self.is_function then
local s = 'fn('
for i, v in ipairs(self.call.params) do
if i ~= 1 then s = s .. ',' end
s = s .. v.ty:to_str()
end
return s..'->'..self.call.ret:to_str()..')'
end
return '?'
end
function type_mt:resolve_named(s, visited)
if visited and visited[self] then
s.error('pass-by-value structures cannot be recursive', false, self.line, self.col)
end
if self.names_resolved then return self end
if self.named then
local t = self.scope:get_type_from_name(self.named)
if not t then
s.error(('no type named `%s` in current scope'):format(self.named), false, self.line, self.col)
end
return t:resolve_named(s)
elseif self.is_function == true then
for i, v in ipairs(self.call.params) do
v.ty = v.ty:resolve_named(s)
end
self.call.ret = self.call.ret:resolve_named(s)
elseif self.valstruct then
self.names_resolved = true
visited = visited or {}
visited[self] = true
for i, v in ipairs(self.valstruct.members) do
self.valstruct.members[i] = v:resolve_named(s, visited)
end
elseif self.cls then
self.names_resolved = true
if self.cls.base then
self.cls.base = self.cls.base:resolve_named(s)
if not self.cls.base.cls then
s.error(('classes cannot inherit non-class types'):format(), false, self.line, self.col)
end
for i = #self.cls.base.cls._members, 1 do
table.insert(self.cls._members, 1, self.cls.base.cls._members[i])
end
end
local members = self.cls._members
local member_types, member_names, name_to_index = {}, {}, {}
for i, v in ipairs(members) do
member_types[i] = members[i].ty
member_names[i] = members[i].name
name_to_index[members[i].name] = i
end
self.cls.packed = true
self.cls.members = member_types
self.cls.num_members = #members
self.cls.member_offsets = {}
self.cls.member_names = member_names
self.cls.name_to_index = name_to_index
local prev_offset = 0
for i, v in ipairs(member_types) do
self.cls.member_offsets[i] = prev_offset
prev_offset = prev_offset + self.cls.members[i]:num_vars()
end
self.cls.num_vars = prev_offset
for i, v in ipairs(self.cls.members) do
self.cls.members[i] = v:resolve_named(s)
end
end
return self
end
function type_mt:num_vars()
if self.valstruct then
return self.valstruct.num_vars
elseif self == types.builtin_void then
return 0
else
return 1
end
end
function type_mt:equal(other)
if self.valstruct and other.valstruct and self.valstruct.kind == 'tuple' and other.valstruct.kind == 'tuple' then
for i, v in ipairs(self.valstruct.members) do
if not v:equal(other.valstruct.members[i]) then return false end
end
return true
elseif self.is_function and other.is_function then
if #self.call.params ~= #other.call.params then return false end
if (not self.call.vararg) ~= (not other.call.vararg) then return false end
if not self.call.ret:equal(other.call.ret) then return false end
for i, v in ipairs(self.call.params) do
if not v.ty:equal(other.call.params[i].ty) then return false end
end
return true
else
return self == other
end
end
function type_mt:is_acceptable(other)
if self.is_acceptable_fn then
return self:is_acceptable_fn(other)
elseif self.cls then
if other.cls then
local c = other
repeat
if c == self then return true end
c = c.cls.base
until not c
end
else
return self:equal(other)
end
end
function types.new_named(name, scope)
return setmetatable({
named = name,
scope = scope,
}, type_mt)
end
function types.new_function(params, ret, vararg)
return setmetatable({
is_function = true,
call = {
params = params,
ret = ret,
vararg = vararg,
},
}, type_mt)
end
function types.new_tuple(members)
local t = setmetatable({
valstruct = {
kind = 'tuple',
members = members,
num_members = #members,
member_offsets = {},
},
}, type_mt)
local prev_offset = 0
for i, v in ipairs(members) do
t.valstruct.member_offsets[i] = prev_offset
prev_offset = prev_offset + t.valstruct.members[i]:num_vars()
end
t.valstruct.num_vars = prev_offset
return t
end
function types.new_struct(members)
local member_types, member_names, name_to_index = {}, {}, {}
for i, v in ipairs(members) do
member_types[i] = members[i].ty
member_names[i] = members[i].name
name_to_index[members[i].name] = i
end
local t = setmetatable({
valstruct = {
kind = 'struct',
members = member_types,
num_members = #members,
member_offsets = {},
member_names = member_names,
name_to_index = name_to_index,
},
}, type_mt)
local prev_offset = 0
for i, v in ipairs(member_types) do
t.valstruct.member_offsets[i] = prev_offset
prev_offset = prev_offset + t.valstruct.members[i]:num_vars()
end
t.valstruct.num_vars = prev_offset
return t
end
function types.new_variant(members)
local variant_names = {}
local variant_types = {}
local name_to_index = {}
local num_vars = 0
for i, v in ipairs(members) do
num_vars = math.max(num_vars, v.ty:num_vars())
variant_names[i] = v.name
variant_types[i] = v.ty
name_to_index[v.name] = i
end
local t = setmetatable({
valstruct = {
kind = 'variant',
members = variant_types,
num_variants = #members,
variant_names = variant_names,
name_to_index = name_to_index,
num_vars = num_vars + 1,
},
}, type_mt)
return t
end
function types.new_class(members, base)
--[[
local member_types, member_names, name_to_index = {}, {}, {}
for i, v in ipairs(members) do
member_types[i] = members[i].ty
member_names[i] = members[i].name
name_to_index[members[i].name] = i
end
--]]
local t = setmetatable({
cls = {
_members = members,
base = base,
--[[
packed = true,
members = member_types,
num_members = #members,
member_offsets = {},
member_names = member_names,
name_to_index = name_to_index,
base = base,
--]]
},
}, type_mt)
--[[
local prev_offset = 0
for i, v in ipairs(member_types) do
t.cls.member_offsets[i] = prev_offset
prev_offset = prev_offset + t.cls.members[i]:num_vars()
end
t.cls.num_vars = prev_offset
--]]
return t
end
types.builtin_bool = setmetatable({
str = 'bool',
}, type_mt)
types.builtin_int = setmetatable({
str = 'int',
}, type_mt)
types.builtin_float = setmetatable({
str = 'float',
}, type_mt)
types.builtin_string = setmetatable({
str = 'string',
}, type_mt)
types.builtin_void = setmetatable({
str = '()',
--valstruct = {kind = 'tuple', num_vars = 0},
noval = true,
}, type_mt)
types.builtin_noret = setmetatable({
str = '!',
noval = true,
}, type_mt)
types.builtin_vararg = setmetatable({
str = '...',
is_acceptable_fn = function (self, t)
return t ~= types.builtin_noret
end,
}, type_mt)
types.builtin_dynamic = setmetatable({
str = 'dyn',
is_acceptable_fn = function (self, t)
return not (t.valstruct or t.noval)
end,
}, type_mt)
function types.add_binop(op, lhs, rhs, res)
lhs.binops_lhs = lhs.binops_lhs or {}
rhs.binops_rhs = rhs.binops_rhs or {}
lhs.binops_lhs[op] = lhs.binops_lhs[op] or {}
rhs.binops_rhs[op] = rhs.binops_rhs[op] or {}
lhs.binops_lhs[op][rhs] = res
rhs.binops_rhs[op][lhs] = res
end
function types.add_unop(op, val, res)
val.unops = val.unops or {}
val.unops[op] = res
end
local cmp_ops = {['<']=true, ['<=']=true, ['>']=true, ['>=']=true}
function types.binop_supported(op, lhs, rhs)
if op == '==' or op == '!=' then
if lhs:equal(rhs) and lhs:num_vars() == 1 then
return types.builtin_bool
else
return
end
end
if cmp_ops[op] then
op = '<'
end
local t = lhs.binops_lhs
if t then
local res = t[op] and t[op][rhs]
if res then return res end
end
local t = rhs.binops_rhs
if t then
local res = t[op] and t[op][lhs]
if res then return res end
end
if (lhs == types.builtin_dynamic and rhs:num_vars() == 1) or
(rhs == types.builtin_dynamic and lhs:num_vars() == 1) then
return types.builtin_dynamic
end
end
function types.unop_supported(op, ty)
local t = ty.unops
if t then
local res = t[op]
if res then return res end
end
end
types.add_binop('+', types.builtin_int, types.builtin_int, types.builtin_int )
types.add_binop('+', types.builtin_float, types.builtin_int, types.builtin_float)
types.add_binop('+', types.builtin_int, types.builtin_float, types.builtin_float)
types.add_binop('+', types.builtin_float, types.builtin_float, types.builtin_float)
types.add_binop('-', types.builtin_int, types.builtin_int, types.builtin_int )
types.add_binop('-', types.builtin_float, types.builtin_int, types.builtin_float)
types.add_binop('-', types.builtin_int, types.builtin_float, types.builtin_float)
types.add_binop('-', types.builtin_float, types.builtin_float, types.builtin_float)
types.add_binop('*', types.builtin_int, types.builtin_int, types.builtin_int )
types.add_binop('*', types.builtin_float, types.builtin_int, types.builtin_float)
types.add_binop('*', types.builtin_int, types.builtin_float, types.builtin_float)
types.add_binop('*', types.builtin_float, types.builtin_float, types.builtin_float)
types.add_binop('/', types.builtin_int, types.builtin_int, types.builtin_float)
types.add_binop('/', types.builtin_float, types.builtin_int, types.builtin_float)
types.add_binop('/', types.builtin_int, types.builtin_float, types.builtin_float)
types.add_binop('/', types.builtin_float, types.builtin_float, types.builtin_float)
types.add_binop('//', types.builtin_int, types.builtin_int, types.builtin_int )
types.add_binop('//', types.builtin_float, types.builtin_int, types.builtin_int )
types.add_binop('//', types.builtin_int, types.builtin_float, types.builtin_int )
types.add_binop('//', types.builtin_float, types.builtin_float, types.builtin_int )
types.add_binop('%', types.builtin_int, types.builtin_int, types.builtin_int )
types.add_binop('%', types.builtin_float, types.builtin_int, types.builtin_float)
types.add_binop('%', types.builtin_int, types.builtin_float, types.builtin_float)
types.add_binop('%', types.builtin_float, types.builtin_float, types.builtin_float)
types.add_binop('^', types.builtin_int, types.builtin_int, types.builtin_int )
types.add_binop('^', types.builtin_float, types.builtin_int, types.builtin_float)
types.add_binop('^', types.builtin_int, types.builtin_float, types.builtin_float)
types.add_binop('^', types.builtin_float, types.builtin_float, types.builtin_float)
types.add_binop('<', types.builtin_int, types.builtin_int, types.builtin_bool)
types.add_binop('<', types.builtin_float, types.builtin_int, types.builtin_bool)
types.add_binop('<', types.builtin_int, types.builtin_float, types.builtin_bool)
types.add_binop('<', types.builtin_float, types.builtin_float, types.builtin_bool)
types.add_binop('..', types.builtin_string, types.builtin_string, types.builtin_string)
types.add_binop('<', types.builtin_string, types.builtin_string, types.builtin_bool)
types.add_unop('-', types.builtin_int, types.builtin_int)
types.add_unop('-', types.builtin_float, types.builtin_float)
types.add_binop('and', types.builtin_bool, types.builtin_bool, types.builtin_bool)
types.add_binop('or', types.builtin_bool, types.builtin_bool, types.builtin_bool)
types.add_unop('not', types.builtin_bool, types.builtin_bool)
types.add_unop('not', types.builtin_dynamic, types.builtin_bool)
return types