forked from Fundament-Software/Alicorn0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend-builder.lua
43 lines (39 loc) · 1002 Bytes
/
backend-builder.lua
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
local backend_mt = {
__index = {
compile = function(self, code)
if self.cache[code] then
return self.cache[code]
end
local inputs = {}
for i, v in ipairs(code.inputs) do
inputs[i] = self:compile(v)
end
local selection = self.dispatch[code.operator]
local ok, res, docache = pcall(selection.handler, code.type_parameters, code.bound_data, inputs)
if not ok then
error("The compilation handler from " .. selection.origin.name .. " had an unhandled error " .. res)
end
if docache then
self.cache[code] = res
end
return res
end,
},
}
local function backend(components)
local dispatch = {}
for i, comp in ipairs(components) do
for token, handler in pairs(comp.handlers) do
if dispatch[token] then
error "duplicate definition of operator in backend"
else
dispatch[token] = { handler = handler, origin = comp }
end
end
end
local self = {
dispatch = dispatch,
}
return setmetatable(self, backend_mt)
end
return backend