rufus-lua-moon 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +1 -0
- data/lib/rufus/lua/moon/version.rb +12 -0
- data/lib/rufus/lua/moon.rb +18 -0
- data/rufus-lua-moon.gemspec +25 -0
- data/vendor/lua/moon/all.moon +8 -0
- data/vendor/lua/moon/init.moon +136 -0
- data/vendor/lua/moonscript/compile/format.lua +55 -0
- data/vendor/lua/moonscript/compile/statement.lua +217 -0
- data/vendor/lua/moonscript/compile/value.lua +321 -0
- data/vendor/lua/moonscript/compile.lua +549 -0
- data/vendor/lua/moonscript/data.lua +87 -0
- data/vendor/lua/moonscript/dump.lua +42 -0
- data/vendor/lua/moonscript/errors.lua +65 -0
- data/vendor/lua/moonscript/init.lua +90 -0
- data/vendor/lua/moonscript/parse.lua +505 -0
- data/vendor/lua/moonscript/transform.lua +1174 -0
- data/vendor/lua/moonscript/types.lua +237 -0
- data/vendor/lua/moonscript/util.lua +101 -0
- data/vendor/lua/moonscript/version.lua +7 -0
- metadata +109 -0
@@ -0,0 +1,321 @@
|
|
1
|
+
module("moonscript.compile", package.seeall)
|
2
|
+
local util = require("moonscript.util")
|
3
|
+
local data = require("moonscript.data")
|
4
|
+
require("moonscript.compile.format")
|
5
|
+
local ntype
|
6
|
+
do
|
7
|
+
local _table_0 = require("moonscript.types")
|
8
|
+
ntype = _table_0.ntype
|
9
|
+
end
|
10
|
+
local concat, insert = table.concat, table.insert
|
11
|
+
local table_append
|
12
|
+
table_append = function(name, len, value)
|
13
|
+
return {
|
14
|
+
{
|
15
|
+
"update",
|
16
|
+
len,
|
17
|
+
"+=",
|
18
|
+
1
|
19
|
+
},
|
20
|
+
{
|
21
|
+
"assign",
|
22
|
+
{
|
23
|
+
{
|
24
|
+
"chain",
|
25
|
+
name,
|
26
|
+
{
|
27
|
+
"index",
|
28
|
+
len
|
29
|
+
}
|
30
|
+
}
|
31
|
+
},
|
32
|
+
{
|
33
|
+
value
|
34
|
+
}
|
35
|
+
}
|
36
|
+
}
|
37
|
+
end
|
38
|
+
value_compile = {
|
39
|
+
exp = function(self, node)
|
40
|
+
local _comp
|
41
|
+
_comp = function(i, value)
|
42
|
+
if i % 2 == 1 and value == "!=" then
|
43
|
+
value = "~="
|
44
|
+
end
|
45
|
+
return self:value(value)
|
46
|
+
end
|
47
|
+
do
|
48
|
+
local _with_0 = self:line()
|
49
|
+
_with_0:append_list((function()
|
50
|
+
local _accum_0 = { }
|
51
|
+
local _len_0 = 0
|
52
|
+
for i, v in ipairs(node) do
|
53
|
+
if i > 1 then
|
54
|
+
_len_0 = _len_0 + 1
|
55
|
+
_accum_0[_len_0] = _comp(i, v)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
return _accum_0
|
59
|
+
end)(), " ")
|
60
|
+
return _with_0
|
61
|
+
end
|
62
|
+
end,
|
63
|
+
update = function(self, node)
|
64
|
+
local _, name = unpack(node)
|
65
|
+
self:stm(node)
|
66
|
+
return self:name(name)
|
67
|
+
end,
|
68
|
+
explist = function(self, node)
|
69
|
+
do
|
70
|
+
local _with_0 = self:line()
|
71
|
+
_with_0:append_list((function()
|
72
|
+
local _accum_0 = { }
|
73
|
+
local _len_0 = 0
|
74
|
+
local _list_0 = node
|
75
|
+
for _index_0 = 2, #_list_0 do
|
76
|
+
local v = _list_0[_index_0]
|
77
|
+
_len_0 = _len_0 + 1
|
78
|
+
_accum_0[_len_0] = self:value(v)
|
79
|
+
end
|
80
|
+
return _accum_0
|
81
|
+
end)(), ", ")
|
82
|
+
return _with_0
|
83
|
+
end
|
84
|
+
end,
|
85
|
+
parens = function(self, node)
|
86
|
+
return self:line("(", self:value(node[2]), ")")
|
87
|
+
end,
|
88
|
+
string = function(self, node)
|
89
|
+
local _, delim, inner, delim_end = unpack(node)
|
90
|
+
return delim .. inner .. (delim_end or delim)
|
91
|
+
end,
|
92
|
+
chain = function(self, node)
|
93
|
+
local callee = node[2]
|
94
|
+
if callee == -1 then
|
95
|
+
callee = self:get("scope_var")
|
96
|
+
if not callee then
|
97
|
+
user_error("Short-dot syntax must be called within a with block")
|
98
|
+
end
|
99
|
+
end
|
100
|
+
local sup = self:get("super")
|
101
|
+
if callee == "super" and sup then
|
102
|
+
return self:value(sup(self, node))
|
103
|
+
end
|
104
|
+
local chain_item
|
105
|
+
chain_item = function(node)
|
106
|
+
local t, arg = unpack(node)
|
107
|
+
if t == "call" then
|
108
|
+
return "(", self:values(arg), ")"
|
109
|
+
elseif t == "index" then
|
110
|
+
return "[", self:value(arg), "]"
|
111
|
+
elseif t == "dot" then
|
112
|
+
return ".", tostring(arg)
|
113
|
+
elseif t == "colon" then
|
114
|
+
return ":", arg, chain_item(node[3])
|
115
|
+
elseif t == "colon_stub" then
|
116
|
+
return user_error("Uncalled colon stub")
|
117
|
+
else
|
118
|
+
return error("Unknown chain action: " .. t)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
if ntype(callee) == "self" and node[3] and ntype(node[3]) == "call" then
|
122
|
+
callee[1] = "self_colon"
|
123
|
+
end
|
124
|
+
local callee_value = self:value(callee)
|
125
|
+
if ntype(callee) == "exp" then
|
126
|
+
callee_value = self:line("(", callee_value, ")")
|
127
|
+
end
|
128
|
+
local actions
|
129
|
+
do
|
130
|
+
local _with_0 = self:line()
|
131
|
+
local _list_0 = node
|
132
|
+
for _index_0 = 3, #_list_0 do
|
133
|
+
local action = _list_0[_index_0]
|
134
|
+
_with_0:append(chain_item(action))
|
135
|
+
end
|
136
|
+
actions = _with_0
|
137
|
+
end
|
138
|
+
return self:line(callee_value, actions)
|
139
|
+
end,
|
140
|
+
fndef = function(self, node)
|
141
|
+
local _, args, whitelist, arrow, block = unpack(node)
|
142
|
+
local default_args = { }
|
143
|
+
local self_args = { }
|
144
|
+
local arg_names = (function()
|
145
|
+
local _accum_0 = { }
|
146
|
+
local _len_0 = 0
|
147
|
+
local _list_0 = args
|
148
|
+
for _index_0 = 1, #_list_0 do
|
149
|
+
local arg = _list_0[_index_0]
|
150
|
+
local name, default_value = unpack(arg)
|
151
|
+
if type(name) == "string" then
|
152
|
+
name = name
|
153
|
+
else
|
154
|
+
if name[1] == "self" then
|
155
|
+
insert(self_args, name)
|
156
|
+
end
|
157
|
+
name = name[2]
|
158
|
+
end
|
159
|
+
if default_value then
|
160
|
+
insert(default_args, arg)
|
161
|
+
end
|
162
|
+
local _value_0 = name
|
163
|
+
if _value_0 ~= nil then
|
164
|
+
_len_0 = _len_0 + 1
|
165
|
+
_accum_0[_len_0] = _value_0
|
166
|
+
end
|
167
|
+
end
|
168
|
+
return _accum_0
|
169
|
+
end)()
|
170
|
+
if arrow == "fat" then
|
171
|
+
insert(arg_names, 1, "self")
|
172
|
+
end
|
173
|
+
do
|
174
|
+
local _with_0 = self:block()
|
175
|
+
if #whitelist > 0 then
|
176
|
+
_with_0:whitelist_names(whitelist)
|
177
|
+
end
|
178
|
+
local _list_0 = arg_names
|
179
|
+
for _index_0 = 1, #_list_0 do
|
180
|
+
local name = _list_0[_index_0]
|
181
|
+
_with_0:put_name(name)
|
182
|
+
end
|
183
|
+
local _list_1 = default_args
|
184
|
+
for _index_0 = 1, #_list_1 do
|
185
|
+
local default = _list_1[_index_0]
|
186
|
+
local name, value = unpack(default)
|
187
|
+
if type(name) == "table" then
|
188
|
+
name = name[2]
|
189
|
+
end
|
190
|
+
_with_0:stm({
|
191
|
+
'if',
|
192
|
+
{
|
193
|
+
'exp',
|
194
|
+
name,
|
195
|
+
'==',
|
196
|
+
'nil'
|
197
|
+
},
|
198
|
+
{
|
199
|
+
{
|
200
|
+
'assign',
|
201
|
+
{
|
202
|
+
name
|
203
|
+
},
|
204
|
+
{
|
205
|
+
value
|
206
|
+
}
|
207
|
+
}
|
208
|
+
}
|
209
|
+
})
|
210
|
+
end
|
211
|
+
local self_arg_values = (function()
|
212
|
+
local _accum_0 = { }
|
213
|
+
local _len_0 = 0
|
214
|
+
local _list_2 = self_args
|
215
|
+
for _index_0 = 1, #_list_2 do
|
216
|
+
local arg = _list_2[_index_0]
|
217
|
+
_len_0 = _len_0 + 1
|
218
|
+
_accum_0[_len_0] = arg[2]
|
219
|
+
end
|
220
|
+
return _accum_0
|
221
|
+
end)()
|
222
|
+
if #self_args > 0 then
|
223
|
+
_with_0:stm({
|
224
|
+
"assign",
|
225
|
+
self_args,
|
226
|
+
self_arg_values
|
227
|
+
})
|
228
|
+
end
|
229
|
+
_with_0:stms(block)
|
230
|
+
if #args > #arg_names then
|
231
|
+
arg_names = (function()
|
232
|
+
local _accum_0 = { }
|
233
|
+
local _len_0 = 0
|
234
|
+
local _list_2 = args
|
235
|
+
for _index_0 = 1, #_list_2 do
|
236
|
+
local arg = _list_2[_index_0]
|
237
|
+
local _value_0 = arg[1]
|
238
|
+
if _value_0 ~= nil then
|
239
|
+
_len_0 = _len_0 + 1
|
240
|
+
_accum_0[_len_0] = _value_0
|
241
|
+
end
|
242
|
+
end
|
243
|
+
return _accum_0
|
244
|
+
end)()
|
245
|
+
end
|
246
|
+
_with_0.header = "function(" .. concat(arg_names, ", ") .. ")"
|
247
|
+
return _with_0
|
248
|
+
end
|
249
|
+
end,
|
250
|
+
table = function(self, node)
|
251
|
+
local _, items = unpack(node)
|
252
|
+
do
|
253
|
+
local _with_0 = self:block("{", "}")
|
254
|
+
_with_0.delim = ","
|
255
|
+
local format_line
|
256
|
+
format_line = function(tuple)
|
257
|
+
if #tuple == 2 then
|
258
|
+
local key, value = unpack(tuple)
|
259
|
+
if type(key) == "string" and data.lua_keywords[key] then
|
260
|
+
key = {
|
261
|
+
"string",
|
262
|
+
'"',
|
263
|
+
key
|
264
|
+
}
|
265
|
+
end
|
266
|
+
local assign
|
267
|
+
if type(key) ~= "string" then
|
268
|
+
assign = self:line("[", _with_0:value(key), "]")
|
269
|
+
else
|
270
|
+
assign = key
|
271
|
+
end
|
272
|
+
_with_0:set("current_block", key)
|
273
|
+
local out = self:line(assign, " = ", _with_0:value(value))
|
274
|
+
_with_0:set("current_block", nil)
|
275
|
+
return out
|
276
|
+
else
|
277
|
+
return self:line(_with_0:value(tuple[1]))
|
278
|
+
end
|
279
|
+
end
|
280
|
+
if items then
|
281
|
+
local _list_0 = items
|
282
|
+
for _index_0 = 1, #_list_0 do
|
283
|
+
local line = _list_0[_index_0]
|
284
|
+
_with_0:add(format_line(line))
|
285
|
+
end
|
286
|
+
end
|
287
|
+
return _with_0
|
288
|
+
end
|
289
|
+
end,
|
290
|
+
minus = function(self, node)
|
291
|
+
return self:line("-", self:value(node[2]))
|
292
|
+
end,
|
293
|
+
temp_name = function(self, node)
|
294
|
+
return node:get_name(self)
|
295
|
+
end,
|
296
|
+
number = function(self, node)
|
297
|
+
return node[2]
|
298
|
+
end,
|
299
|
+
length = function(self, node)
|
300
|
+
return self:line("#", self:value(node[2]))
|
301
|
+
end,
|
302
|
+
["not"] = function(self, node)
|
303
|
+
return self:line("not ", self:value(node[2]))
|
304
|
+
end,
|
305
|
+
self = function(self, node)
|
306
|
+
return "self." .. self:value(node[2])
|
307
|
+
end,
|
308
|
+
self_colon = function(self, node)
|
309
|
+
return "self:" .. self:value(node[2])
|
310
|
+
end,
|
311
|
+
raw_value = function(self, value)
|
312
|
+
local sup = self:get("super")
|
313
|
+
if value == "super" and sup then
|
314
|
+
return self:value(sup(self))
|
315
|
+
end
|
316
|
+
if value == "..." then
|
317
|
+
self.has_varargs = true
|
318
|
+
end
|
319
|
+
return tostring(value)
|
320
|
+
end
|
321
|
+
}
|
@@ -0,0 +1,549 @@
|
|
1
|
+
module("moonscript.compile", package.seeall)
|
2
|
+
local util = require("moonscript.util")
|
3
|
+
local dump = require("moonscript.dump")
|
4
|
+
require("moonscript.compile.format")
|
5
|
+
require("moonscript.compile.statement")
|
6
|
+
require("moonscript.compile.value")
|
7
|
+
local transform = require("moonscript.transform")
|
8
|
+
local NameProxy, LocalName = transform.NameProxy, transform.LocalName
|
9
|
+
local Set
|
10
|
+
do
|
11
|
+
local _table_0 = require("moonscript.data")
|
12
|
+
Set = _table_0.Set
|
13
|
+
end
|
14
|
+
local ntype
|
15
|
+
do
|
16
|
+
local _table_0 = require("moonscript.types")
|
17
|
+
ntype = _table_0.ntype
|
18
|
+
end
|
19
|
+
local concat, insert = table.concat, table.insert
|
20
|
+
local pos_to_line, get_closest_line, trim = util.pos_to_line, util.get_closest_line, util.trim
|
21
|
+
local Line
|
22
|
+
Line = (function()
|
23
|
+
local _parent_0 = nil
|
24
|
+
local _base_0 = {
|
25
|
+
_append_single = function(self, item)
|
26
|
+
if util.moon.type(item) == Line then
|
27
|
+
local _list_0 = item
|
28
|
+
for _index_0 = 1, #_list_0 do
|
29
|
+
value = _list_0[_index_0]
|
30
|
+
self:_append_single(value)
|
31
|
+
end
|
32
|
+
else
|
33
|
+
insert(self, item)
|
34
|
+
end
|
35
|
+
return nil
|
36
|
+
end,
|
37
|
+
append_list = function(self, items, delim)
|
38
|
+
for i = 1, #items do
|
39
|
+
self:_append_single(items[i])
|
40
|
+
if i < #items then
|
41
|
+
insert(self, delim)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end,
|
45
|
+
append = function(self, ...)
|
46
|
+
local _list_0 = {
|
47
|
+
...
|
48
|
+
}
|
49
|
+
for _index_0 = 1, #_list_0 do
|
50
|
+
local item = _list_0[_index_0]
|
51
|
+
self:_append_single(item)
|
52
|
+
end
|
53
|
+
return nil
|
54
|
+
end,
|
55
|
+
render = function(self)
|
56
|
+
local buff = { }
|
57
|
+
for i = 1, #self do
|
58
|
+
local c = self[i]
|
59
|
+
insert(buff, (function()
|
60
|
+
if util.moon.type(c) == Block then
|
61
|
+
c:bubble()
|
62
|
+
return c:render()
|
63
|
+
else
|
64
|
+
return c
|
65
|
+
end
|
66
|
+
end)())
|
67
|
+
end
|
68
|
+
return concat(buff)
|
69
|
+
end
|
70
|
+
}
|
71
|
+
_base_0.__index = _base_0
|
72
|
+
if _parent_0 then
|
73
|
+
setmetatable(_base_0, _parent_0.__base)
|
74
|
+
end
|
75
|
+
local _class_0 = setmetatable({
|
76
|
+
__init = function(self, ...)
|
77
|
+
if _parent_0 then
|
78
|
+
return _parent_0.__init(self, ...)
|
79
|
+
end
|
80
|
+
end,
|
81
|
+
__base = _base_0,
|
82
|
+
__name = "Line",
|
83
|
+
__parent = _parent_0
|
84
|
+
}, {
|
85
|
+
__index = function(cls, name)
|
86
|
+
local val = rawget(_base_0, name)
|
87
|
+
if val == nil and _parent_0 then
|
88
|
+
return _parent_0[name]
|
89
|
+
else
|
90
|
+
return val
|
91
|
+
end
|
92
|
+
end,
|
93
|
+
__call = function(cls, ...)
|
94
|
+
local _self_0 = setmetatable({}, _base_0)
|
95
|
+
cls.__init(_self_0, ...)
|
96
|
+
return _self_0
|
97
|
+
end
|
98
|
+
})
|
99
|
+
_base_0.__class = _class_0
|
100
|
+
return _class_0
|
101
|
+
end)()
|
102
|
+
Block = (function()
|
103
|
+
local _parent_0 = nil
|
104
|
+
local _base_0 = {
|
105
|
+
header = "do",
|
106
|
+
footer = "end",
|
107
|
+
export_all = false,
|
108
|
+
export_proper = false,
|
109
|
+
__tostring = function(self)
|
110
|
+
return "Block<> <- " .. tostring(self.parent)
|
111
|
+
end,
|
112
|
+
bubble = function(self, other)
|
113
|
+
if other == nil then
|
114
|
+
other = self.parent
|
115
|
+
end
|
116
|
+
local has_varargs = self.has_varargs and not self:has_name("...")
|
117
|
+
other.has_varargs = other.has_varargs or has_varargs
|
118
|
+
end,
|
119
|
+
line_table = function(self)
|
120
|
+
return self._posmap
|
121
|
+
end,
|
122
|
+
set = function(self, name, value)
|
123
|
+
self._state[name] = value
|
124
|
+
end,
|
125
|
+
get = function(self, name)
|
126
|
+
return self._state[name]
|
127
|
+
end,
|
128
|
+
declare = function(self, names)
|
129
|
+
local undeclared = (function()
|
130
|
+
local _accum_0 = { }
|
131
|
+
local _len_0 = 0
|
132
|
+
local _list_0 = names
|
133
|
+
for _index_0 = 1, #_list_0 do
|
134
|
+
local name = _list_0[_index_0]
|
135
|
+
local is_local = false
|
136
|
+
local real_name
|
137
|
+
local _exp_0 = util.moon.type(name)
|
138
|
+
if LocalName == _exp_0 then
|
139
|
+
is_local = true
|
140
|
+
real_name = name:get_name(self)
|
141
|
+
elseif NameProxy == _exp_0 then
|
142
|
+
real_name = name:get_name(self)
|
143
|
+
elseif "string" == _exp_0 then
|
144
|
+
real_name = name
|
145
|
+
end
|
146
|
+
local _value_0
|
147
|
+
if is_local or real_name and not self:has_name(real_name) then
|
148
|
+
_value_0 = real_name
|
149
|
+
end
|
150
|
+
if _value_0 ~= nil then
|
151
|
+
_len_0 = _len_0 + 1
|
152
|
+
_accum_0[_len_0] = _value_0
|
153
|
+
end
|
154
|
+
end
|
155
|
+
return _accum_0
|
156
|
+
end)()
|
157
|
+
local _list_0 = undeclared
|
158
|
+
for _index_0 = 1, #_list_0 do
|
159
|
+
local name = _list_0[_index_0]
|
160
|
+
self:put_name(name)
|
161
|
+
end
|
162
|
+
return undeclared
|
163
|
+
end,
|
164
|
+
whitelist_names = function(self, names)
|
165
|
+
self._name_whitelist = Set(names)
|
166
|
+
end,
|
167
|
+
put_name = function(self, name)
|
168
|
+
if util.moon.type(name) == NameProxy then
|
169
|
+
name = name:get_name(self)
|
170
|
+
end
|
171
|
+
self._names[name] = true
|
172
|
+
end,
|
173
|
+
has_name = function(self, name, skip_exports)
|
174
|
+
if not skip_exports then
|
175
|
+
if self.export_all then
|
176
|
+
return true
|
177
|
+
end
|
178
|
+
if self.export_proper and name:match("^[A-Z]") then
|
179
|
+
return true
|
180
|
+
end
|
181
|
+
end
|
182
|
+
local yes = self._names[name]
|
183
|
+
if yes == nil and self.parent then
|
184
|
+
if not self._name_whitelist or self._name_whitelist[name] then
|
185
|
+
return self.parent:has_name(name, true)
|
186
|
+
end
|
187
|
+
else
|
188
|
+
return yes
|
189
|
+
end
|
190
|
+
end,
|
191
|
+
free_name = function(self, prefix, dont_put)
|
192
|
+
prefix = prefix or "moon"
|
193
|
+
local searching = true
|
194
|
+
local name, i = nil, 0
|
195
|
+
while searching do
|
196
|
+
name = concat({
|
197
|
+
"",
|
198
|
+
prefix,
|
199
|
+
i
|
200
|
+
}, "_")
|
201
|
+
i = i + 1
|
202
|
+
searching = self:has_name(name, true)
|
203
|
+
end
|
204
|
+
if not dont_put then
|
205
|
+
self:put_name(name)
|
206
|
+
end
|
207
|
+
return name
|
208
|
+
end,
|
209
|
+
init_free_var = function(self, prefix, value)
|
210
|
+
local name = self:free_name(prefix, true)
|
211
|
+
self:stm({
|
212
|
+
"assign",
|
213
|
+
{
|
214
|
+
name
|
215
|
+
},
|
216
|
+
{
|
217
|
+
value
|
218
|
+
}
|
219
|
+
})
|
220
|
+
return name
|
221
|
+
end,
|
222
|
+
mark_pos = function(self, node)
|
223
|
+
if node[-1] then
|
224
|
+
self.last_pos = node[-1]
|
225
|
+
if not self._posmap[self.current_line] then
|
226
|
+
self._posmap[self.current_line] = self.last_pos
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end,
|
230
|
+
add_line_text = function(self, text)
|
231
|
+
return insert(self._lines, text)
|
232
|
+
end,
|
233
|
+
append_line_table = function(self, sub_table, offset)
|
234
|
+
offset = offset + self.current_line
|
235
|
+
for line, source in pairs(sub_table) do
|
236
|
+
local line = line + offset
|
237
|
+
if not self._posmap[line] then
|
238
|
+
self._posmap[line] = source
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end,
|
242
|
+
add_line_tables = function(self, line)
|
243
|
+
local _list_0 = line
|
244
|
+
for _index_0 = 1, #_list_0 do
|
245
|
+
local chunk = _list_0[_index_0]
|
246
|
+
if util.moon.type(chunk) == Block then
|
247
|
+
local current = chunk
|
248
|
+
while current do
|
249
|
+
if util.moon.type(current.header) == Line then
|
250
|
+
self:add_line_tables(current.header)
|
251
|
+
end
|
252
|
+
self:append_line_table(current:line_table(), 0)
|
253
|
+
self.current_line = self.current_line + current.current_line
|
254
|
+
current = current.next
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end,
|
259
|
+
add = function(self, line)
|
260
|
+
local t = util.moon.type(line)
|
261
|
+
if t == "string" then
|
262
|
+
self:add_line_text(line)
|
263
|
+
elseif t == Block then
|
264
|
+
self:add(self:line(line))
|
265
|
+
elseif t == Line then
|
266
|
+
self:add_line_tables(line)
|
267
|
+
self:add_line_text(line:render())
|
268
|
+
self.current_line = self.current_line + 1
|
269
|
+
else
|
270
|
+
error("Adding unknown item")
|
271
|
+
end
|
272
|
+
return nil
|
273
|
+
end,
|
274
|
+
_insert_breaks = function(self)
|
275
|
+
for i = 1, #self._lines - 1 do
|
276
|
+
local left, right = self._lines[i], self._lines[i + 1]
|
277
|
+
if left:sub(-1) == ")" and right:sub(1, 1) == "(" then
|
278
|
+
self._lines[i] = self._lines[i] .. ";"
|
279
|
+
end
|
280
|
+
end
|
281
|
+
end,
|
282
|
+
render = function(self)
|
283
|
+
local flatten
|
284
|
+
flatten = function(line)
|
285
|
+
if type(line) == "string" then
|
286
|
+
return line
|
287
|
+
else
|
288
|
+
return line:render()
|
289
|
+
end
|
290
|
+
end
|
291
|
+
local header = flatten(self.header)
|
292
|
+
if #self._lines == 0 then
|
293
|
+
local footer = flatten(self.footer)
|
294
|
+
return concat({
|
295
|
+
header,
|
296
|
+
footer
|
297
|
+
}, " ")
|
298
|
+
end
|
299
|
+
local indent = indent_char:rep(self.indent)
|
300
|
+
if not self.delim then
|
301
|
+
self:_insert_breaks()
|
302
|
+
end
|
303
|
+
local body = indent .. concat(self._lines, (self.delim or "") .. "\n" .. indent)
|
304
|
+
return concat({
|
305
|
+
header,
|
306
|
+
body,
|
307
|
+
indent_char:rep(self.indent - 1) .. (function()
|
308
|
+
if self.next then
|
309
|
+
return self.next:render()
|
310
|
+
else
|
311
|
+
return flatten(self.footer)
|
312
|
+
end
|
313
|
+
end)()
|
314
|
+
}, "\n")
|
315
|
+
end,
|
316
|
+
block = function(self, header, footer)
|
317
|
+
return Block(self, header, footer)
|
318
|
+
end,
|
319
|
+
line = function(self, ...)
|
320
|
+
do
|
321
|
+
local _with_0 = Line()
|
322
|
+
_with_0:append(...)
|
323
|
+
return _with_0
|
324
|
+
end
|
325
|
+
end,
|
326
|
+
is_stm = function(self, node)
|
327
|
+
return line_compile[ntype(node)] ~= nil
|
328
|
+
end,
|
329
|
+
is_value = function(self, node)
|
330
|
+
local t = ntype(node)
|
331
|
+
return value_compile[t] ~= nil or t == "value"
|
332
|
+
end,
|
333
|
+
name = function(self, node)
|
334
|
+
return self:value(node)
|
335
|
+
end,
|
336
|
+
value = function(self, node, ...)
|
337
|
+
node = self.root.transform.value(node)
|
338
|
+
local action
|
339
|
+
if type(node) ~= "table" then
|
340
|
+
action = "raw_value"
|
341
|
+
else
|
342
|
+
self:mark_pos(node)
|
343
|
+
action = node[1]
|
344
|
+
end
|
345
|
+
local fn = value_compile[action]
|
346
|
+
if not fn then
|
347
|
+
error("Failed to compile value: " .. dump.value(node))
|
348
|
+
end
|
349
|
+
return fn(self, node, ...)
|
350
|
+
end,
|
351
|
+
values = function(self, values, delim)
|
352
|
+
delim = delim or ', '
|
353
|
+
do
|
354
|
+
local _with_0 = Line()
|
355
|
+
_with_0:append_list((function()
|
356
|
+
local _accum_0 = { }
|
357
|
+
local _len_0 = 0
|
358
|
+
local _list_0 = values
|
359
|
+
for _index_0 = 1, #_list_0 do
|
360
|
+
local v = _list_0[_index_0]
|
361
|
+
_len_0 = _len_0 + 1
|
362
|
+
_accum_0[_len_0] = self:value(v)
|
363
|
+
end
|
364
|
+
return _accum_0
|
365
|
+
end)(), delim)
|
366
|
+
return _with_0
|
367
|
+
end
|
368
|
+
end,
|
369
|
+
stm = function(self, node, ...)
|
370
|
+
if not node then
|
371
|
+
return
|
372
|
+
end
|
373
|
+
node = self.root.transform.statement(node)
|
374
|
+
local fn = line_compile[ntype(node)]
|
375
|
+
if not fn then
|
376
|
+
if has_value(node) then
|
377
|
+
self:stm({
|
378
|
+
"assign",
|
379
|
+
{
|
380
|
+
"_"
|
381
|
+
},
|
382
|
+
{
|
383
|
+
node
|
384
|
+
}
|
385
|
+
})
|
386
|
+
else
|
387
|
+
self:add(self:value(node))
|
388
|
+
end
|
389
|
+
else
|
390
|
+
self:mark_pos(node)
|
391
|
+
local out = fn(self, node, ...)
|
392
|
+
if out then
|
393
|
+
self:add(out)
|
394
|
+
end
|
395
|
+
end
|
396
|
+
return nil
|
397
|
+
end,
|
398
|
+
stms = function(self, stms, ret)
|
399
|
+
if ret then
|
400
|
+
error("deprecated stms call, use transformer")
|
401
|
+
end
|
402
|
+
local _list_0 = stms
|
403
|
+
for _index_0 = 1, #_list_0 do
|
404
|
+
local stm = _list_0[_index_0]
|
405
|
+
self:stm(stm)
|
406
|
+
end
|
407
|
+
return nil
|
408
|
+
end
|
409
|
+
}
|
410
|
+
_base_0.__index = _base_0
|
411
|
+
if _parent_0 then
|
412
|
+
setmetatable(_base_0, _parent_0.__base)
|
413
|
+
end
|
414
|
+
local _class_0 = setmetatable({
|
415
|
+
__init = function(self, parent, header, footer)
|
416
|
+
self.parent, self.header, self.footer = parent, header, footer
|
417
|
+
self.current_line = 1
|
418
|
+
self._lines = { }
|
419
|
+
self._posmap = { }
|
420
|
+
self._names = { }
|
421
|
+
self._state = { }
|
422
|
+
if self.parent then
|
423
|
+
self.root = self.parent.root
|
424
|
+
self.indent = self.parent.indent + 1
|
425
|
+
return setmetatable(self._state, {
|
426
|
+
__index = self.parent._state
|
427
|
+
})
|
428
|
+
else
|
429
|
+
self.indent = 0
|
430
|
+
end
|
431
|
+
end,
|
432
|
+
__base = _base_0,
|
433
|
+
__name = "Block",
|
434
|
+
__parent = _parent_0
|
435
|
+
}, {
|
436
|
+
__index = function(cls, name)
|
437
|
+
local val = rawget(_base_0, name)
|
438
|
+
if val == nil and _parent_0 then
|
439
|
+
return _parent_0[name]
|
440
|
+
else
|
441
|
+
return val
|
442
|
+
end
|
443
|
+
end,
|
444
|
+
__call = function(cls, ...)
|
445
|
+
local _self_0 = setmetatable({}, _base_0)
|
446
|
+
cls.__init(_self_0, ...)
|
447
|
+
return _self_0
|
448
|
+
end
|
449
|
+
})
|
450
|
+
_base_0.__class = _class_0
|
451
|
+
return _class_0
|
452
|
+
end)()
|
453
|
+
local RootBlock
|
454
|
+
RootBlock = (function()
|
455
|
+
local _parent_0 = Block
|
456
|
+
local _base_0 = {
|
457
|
+
__tostring = function(self)
|
458
|
+
return "RootBlock<>"
|
459
|
+
end,
|
460
|
+
render = function(self)
|
461
|
+
self:_insert_breaks()
|
462
|
+
return concat(self._lines, "\n")
|
463
|
+
end
|
464
|
+
}
|
465
|
+
_base_0.__index = _base_0
|
466
|
+
if _parent_0 then
|
467
|
+
setmetatable(_base_0, _parent_0.__base)
|
468
|
+
end
|
469
|
+
local _class_0 = setmetatable({
|
470
|
+
__init = function(self, ...)
|
471
|
+
self.root = self
|
472
|
+
self.transform = {
|
473
|
+
value = transform.Value:instance(self),
|
474
|
+
statement = transform.Statement:instance(self)
|
475
|
+
}
|
476
|
+
return _parent_0.__init(self, ...)
|
477
|
+
end,
|
478
|
+
__base = _base_0,
|
479
|
+
__name = "RootBlock",
|
480
|
+
__parent = _parent_0
|
481
|
+
}, {
|
482
|
+
__index = function(cls, name)
|
483
|
+
local val = rawget(_base_0, name)
|
484
|
+
if val == nil and _parent_0 then
|
485
|
+
return _parent_0[name]
|
486
|
+
else
|
487
|
+
return val
|
488
|
+
end
|
489
|
+
end,
|
490
|
+
__call = function(cls, ...)
|
491
|
+
local _self_0 = setmetatable({}, _base_0)
|
492
|
+
cls.__init(_self_0, ...)
|
493
|
+
return _self_0
|
494
|
+
end
|
495
|
+
})
|
496
|
+
_base_0.__class = _class_0
|
497
|
+
return _class_0
|
498
|
+
end)()
|
499
|
+
format_error = function(msg, pos, file_str)
|
500
|
+
local line = pos_to_line(file_str, pos)
|
501
|
+
local line_str
|
502
|
+
line_str, line = get_closest_line(file_str, line)
|
503
|
+
line_str = line_str or ""
|
504
|
+
return concat({
|
505
|
+
"Compile error: " .. msg,
|
506
|
+
(" [%d] >> %s"):format(line, trim(line_str))
|
507
|
+
}, "\n")
|
508
|
+
end
|
509
|
+
value = function(value)
|
510
|
+
local out = nil
|
511
|
+
do
|
512
|
+
local _with_0 = RootBlock()
|
513
|
+
_with_0:add(_with_0:value(value))
|
514
|
+
out = _with_0:render()
|
515
|
+
end
|
516
|
+
return out
|
517
|
+
end
|
518
|
+
tree = function(tree)
|
519
|
+
local scope = RootBlock()
|
520
|
+
local runner = coroutine.create(function()
|
521
|
+
local _list_0 = tree
|
522
|
+
for _index_0 = 1, #_list_0 do
|
523
|
+
local line = _list_0[_index_0]
|
524
|
+
scope:stm(line)
|
525
|
+
end
|
526
|
+
return scope:render()
|
527
|
+
end)
|
528
|
+
local success, result = coroutine.resume(runner)
|
529
|
+
if not success then
|
530
|
+
local error_msg
|
531
|
+
if type(result) == "table" then
|
532
|
+
local error_type = result[1]
|
533
|
+
if error_type == "user-error" then
|
534
|
+
error_msg = result[2]
|
535
|
+
else
|
536
|
+
error_msg = error("Unknown error thrown", util.dump(error_msg))
|
537
|
+
end
|
538
|
+
else
|
539
|
+
error_msg = concat({
|
540
|
+
result,
|
541
|
+
debug.traceback(runner)
|
542
|
+
}, "\n")
|
543
|
+
end
|
544
|
+
return nil, error_msg, scope.last_pos
|
545
|
+
else
|
546
|
+
local tbl = scope:line_table()
|
547
|
+
return result, tbl
|
548
|
+
end
|
549
|
+
end
|