Skip to content

Commit de3372d

Browse files
committed
import done through transform, binding import also works now
1 parent 4dd7e7d commit de3372d

File tree

6 files changed

+139
-106
lines changed

6 files changed

+139
-106
lines changed

moonscript/compile/line.lua

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -92,71 +92,6 @@ line_compile = {
9292
["break"] = function(self, node)
9393
return "break"
9494
end,
95-
import = function(self, node)
96-
local _, names, source = unpack(node)
97-
local final_names, to_bind = { }, { }
98-
local _list_0 = names
99-
for _index_0 = 1, #_list_0 do
100-
local name = _list_0[_index_0]
101-
local final
102-
if ntype(name) == ":" then
103-
local tmp = self:name(name[2])
104-
to_bind[tmp] = true
105-
final = tmp
106-
else
107-
final = self:name(name)
108-
end
109-
self:put_name(final)
110-
insert(final_names, final)
111-
end
112-
local get_value
113-
get_value = function(name)
114-
if to_bind[name] then
115-
return moonlib.bind(source, name)
116-
else
117-
return source .. "." .. name
118-
end
119-
end
120-
if type(source) == "string" then
121-
local values = (function()
122-
local _accum_0 = { }
123-
local _len_0 = 0
124-
local _list_1 = final_names
125-
for _index_0 = 1, #_list_1 do
126-
local name = _list_1[_index_0]
127-
_len_0 = _len_0 + 1
128-
_accum_0[_len_0] = get_value(name)
129-
end
130-
return _accum_0
131-
end)()
132-
local line
133-
do
134-
local _with_0 = self:line("local ", concat(final_names, ", "), " = ")
135-
_with_0:append_list(values, ", ")
136-
line = _with_0
137-
end
138-
return line
139-
end
140-
self:add(self:line("local ", concat(final_names, ", ")))
141-
do
142-
local _with_0 = self:block("do")
143-
source = _with_0:init_free_var("table", source)
144-
local _list_1 = final_names
145-
for _index_0 = 1, #_list_1 do
146-
local name = _list_1[_index_0]
147-
_with_0:stm({
148-
"assign",
149-
{
150-
name
151-
},
152-
{
153-
get_value(name)
154-
}
155-
})
156-
end
157-
return _with_0
158-
end
159-
end,
16095
["if"] = function(self, node)
16196
local cond, block = node[2], node[3]
16297
local root

moonscript/compile/line.moon

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -52,39 +52,6 @@ line_compile =
5252
break: (node) =>
5353
"break"
5454

55-
import: (node) =>
56-
_, names, source = unpack node
57-
58-
final_names, to_bind = {}, {}
59-
for name in *names
60-
final = if ntype(name) == ":"
61-
tmp = @name name[2]
62-
to_bind[tmp] = true
63-
tmp
64-
else
65-
@name name
66-
67-
@put_name final
68-
insert final_names, final
69-
70-
get_value = (name) ->
71-
if to_bind[name]
72-
moonlib.bind source, name
73-
else
74-
source.."."..name
75-
76-
-- from constant expression, put it on one line
77-
if type(source) == "string"
78-
values = [get_value name for name in *final_names]
79-
line = with @line "local ", concat(final_names, ", "), " = "
80-
\append_list values, ", "
81-
return line
82-
83-
@add @line "local ", concat(final_names, ", ")
84-
with @block "do"
85-
source = \init_free_var "table", source
86-
\stm {"assign", {name}, {get_value name}} for name in *final_names
87-
8855
if: (node) =>
8956
cond, block = node[2], node[3]
9057
root = with @block @line "if ", @value(cond), " then"

moonscript/parse.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ local build_grammar = wrap(function()
284284
InBlock = Advance * Block * PopIndent,
285285

286286
Import = key"import" * Ct(ImportNameList) * key"from" * Exp / mark"import",
287-
ImportName = (sym"\\" * Ct(Cc":" * Name) + Name),
287+
ImportName = (sym"\\" * Ct(Cc"colon_stub" * Name) + Name),
288288
ImportNameList = ImportName * (sym"," * ImportName)^0,
289289

290290
NameList = Name * (sym"," * Name)^0,

moonscript/transform.lua

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,92 @@ Statement = Transformer({
228228
exp
229229
})
230230
end,
231+
import = function(node)
232+
local _, names, source = unpack(node)
233+
local stubs = (function()
234+
local _accum_0 = { }
235+
local _len_0 = 0
236+
local _list_0 = names
237+
for _index_0 = 1, #_list_0 do
238+
local name = _list_0[_index_0]
239+
local _value_0
240+
if type(name) == "table" then
241+
_value_0 = name
242+
else
243+
_value_0 = {
244+
"dot",
245+
name
246+
}
247+
end
248+
if _value_0 ~= nil then
249+
_len_0 = _len_0 + 1
250+
_accum_0[_len_0] = _value_0
251+
end
252+
end
253+
return _accum_0
254+
end)()
255+
local real_names = (function()
256+
local _accum_0 = { }
257+
local _len_0 = 0
258+
local _list_0 = names
259+
for _index_0 = 1, #_list_0 do
260+
local name = _list_0[_index_0]
261+
local _value_0 = type(name) == "table" and name[2] or name
262+
if _value_0 ~= nil then
263+
_len_0 = _len_0 + 1
264+
_accum_0[_len_0] = _value_0
265+
end
266+
end
267+
return _accum_0
268+
end)()
269+
if type(source) == "string" then
270+
return build.assign({
271+
names = real_names,
272+
values = (function()
273+
local _accum_0 = { }
274+
local _len_0 = 0
275+
local _list_0 = stubs
276+
for _index_0 = 1, #_list_0 do
277+
local stub = _list_0[_index_0]
278+
_len_0 = _len_0 + 1
279+
_accum_0[_len_0] = build.chain({
280+
base = source,
281+
stub
282+
})
283+
end
284+
return _accum_0
285+
end)()
286+
})
287+
else
288+
local source_name = NameProxy("table")
289+
return build.group({
290+
{
291+
"declare",
292+
real_names
293+
},
294+
build["do"]({
295+
build.assign_one(source_name, source),
296+
build.assign({
297+
names = real_names,
298+
values = (function()
299+
local _accum_0 = { }
300+
local _len_0 = 0
301+
local _list_0 = stubs
302+
for _index_0 = 1, #_list_0 do
303+
local stub = _list_0[_index_0]
304+
_len_0 = _len_0 + 1
305+
_accum_0[_len_0] = build.chain({
306+
base = source_name,
307+
stub
308+
})
309+
end
310+
return _accum_0
311+
end)()
312+
})
313+
})
314+
})
315+
end
316+
end,
231317
comprehension = function(node, action)
232318
local _, exp, clauses = unpack(node)
233319
action = action or function(exp)

moonscript/transform.moon

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,36 @@ Statement = Transformer {
130130
error "Unknown op: "..op if not op_final
131131
build.assign_one name, {"exp", name, op_final, exp}
132132

133+
import: (node) ->
134+
_, names, source = unpack node
135+
136+
stubs = for name in *names
137+
if type(name) == "table"
138+
name
139+
else
140+
{"dot", name}
141+
142+
real_names = for name in *names
143+
type(name) == "table" and name[2] or name
144+
145+
if type(source) == "string"
146+
build.assign {
147+
names: real_names
148+
values: [build.chain { base: source, stub} for stub in *stubs]
149+
}
150+
else
151+
source_name = NameProxy "table"
152+
build.group {
153+
{"declare", real_names}
154+
build["do"] {
155+
build.assign_one source_name, source
156+
build.assign {
157+
names: real_names
158+
values: [build.chain { base: source_name, stub} for stub in *stubs]
159+
}
160+
}
161+
}
162+
133163
comprehension: (node, action) ->
134164
_, exp, clauses = unpack node
135165

tests/outputs/import.lua

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@ local hello = yeah.hello
22
local hello, world
33
do
44
local _table_0 = table["cool"]
5-
hello = _table_0.hello
6-
world = _table_0.world
5+
hello, world = _table_0.hello, _table_0.world
76
end
8-
local a, b, c = items.a, moon.bind(items.b, items), items.c
7+
local a, b, c = items.a, (function()
8+
local _base_0 = items
9+
local _fn_0 = _base_0.b
10+
return function(...)
11+
return _fn_0(_base_0, ...)
12+
end
13+
end)(), items.c
914
local master, ghost
1015
do
1116
local _table_0 = find("mytable")
12-
master = _table_0.master
13-
ghost = moon.bind(_table_0.ghost, _table_0)
17+
master, ghost = _table_0.master, (function()
18+
local _base_0 = _table_0
19+
local _fn_0 = _base_0.ghost
20+
return function(...)
21+
return _fn_0(_base_0, ...)
22+
end
23+
end)()
1424
end
1525
local yumm
1626
a, yumm = 3434, "hello"
@@ -24,7 +34,12 @@ if indent then
2434
local okay, well
2535
do
2636
local _table_1 = tables[100]
27-
okay = _table_1.okay
28-
well = moon.bind(_table_1.well, _table_1)
37+
okay, well = _table_1.okay, (function()
38+
local _base_0 = _table_1
39+
local _fn_0 = _base_0.well
40+
return function(...)
41+
return _fn_0(_base_0, ...)
42+
end
43+
end)()
2944
end
3045
end

0 commit comments

Comments
 (0)