-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.lua
74 lines (63 loc) · 1.55 KB
/
runtime.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
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
--[[
Icht runtime
]]
local Object = require "luvit.core".Object
local function makeScope(parent)
return setmetatable({}, {__index=parent})
end
local Runtime = Object:extend()
function Runtime:initialize(parent)
self.parent = parent
self.blocks = parent and parent.blocks or {}
self.isExtending = false
end
function Runtime:run(info, cb)
local scope = makeScope(self)
scope._rootscope = scope
scope.source = info.source
scope._blocks = {}
info.run(scope)
cb()
end
function Runtime:_extends(parent)
assert(not runtime.isExtending, "Cannot extend multiple templates.")
self._rootscope.isExtending = parent
end
function Runtime:write(...)
if not self.isExtending then
for k, str in pairs({...}) do
p("OUT self:onOutput(", tostring(str))
end
end
end
function Runtime:_block(name, child)
if self.isExtending then
self._blocks[name] = function(runtime, ...) child(self, name, ...) end
elseif self._blocks[name] then
self._blocks[name](runtime, child)
else
child(self, name)
end
end
function Runtime:_for(v, child, doUnpack)
if type(v) == "table" then
for k, v in pairs(v) do
if doUnpack then
child(unpack(v))
else
child(v)
end
end
else
p(v)
error "Unkown varible to for."
end
end
function Runtime:_runParent()
if self.isExtending then
self:runParent(self.isExtending, childRuntime)
end
end
return {
Runtime = Runtime,
}