Skip to content

Commit 347b427

Browse files
committed
import is backed by destructure
1 parent 1fa83e1 commit 347b427

File tree

5 files changed

+97
-115
lines changed

5 files changed

+97
-115
lines changed

moonscript/transform.lua

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -456,78 +456,43 @@ Statement = Transformer({
456456
end,
457457
import = function(self, node)
458458
local _, names, source = unpack(node)
459-
local stubs = (function()
459+
local table_values = (function()
460460
local _accum_0 = { }
461461
local _len_0 = 1
462462
for _index_0 = 1, #names do
463463
local name = names[_index_0]
464-
if type(name) == "table" then
465-
_accum_0[_len_0] = name
464+
local dest_val
465+
if ntype(name) == "colon_stub" then
466+
dest_val = name[2]
466467
else
467-
_accum_0[_len_0] = {
468-
"dot",
469-
name
470-
}
468+
dest_val = name
471469
end
470+
local _value_0 = {
471+
{
472+
"key_literal",
473+
name
474+
},
475+
dest_val
476+
}
477+
_accum_0[_len_0] = _value_0
472478
_len_0 = _len_0 + 1
473479
end
474480
return _accum_0
475481
end)()
476-
local real_names = (function()
477-
local _accum_0 = { }
478-
local _len_0 = 1
479-
for _index_0 = 1, #names do
480-
local name = names[_index_0]
481-
_accum_0[_len_0] = type(name) == "table" and name[2] or name
482-
_len_0 = _len_0 + 1
483-
end
484-
return _accum_0
485-
end)()
486-
if type(source) == "string" then
487-
return build.assign({
488-
names = real_names,
489-
values = (function()
490-
local _accum_0 = { }
491-
local _len_0 = 1
492-
for _index_0 = 1, #stubs do
493-
local stub = stubs[_index_0]
494-
_accum_0[_len_0] = build.chain({
495-
base = source,
496-
stub
497-
})
498-
_len_0 = _len_0 + 1
499-
end
500-
return _accum_0
501-
end)()
502-
})
503-
else
504-
local source_name = NameProxy("table")
505-
return build.group({
506-
{
507-
"declare",
508-
real_names
509-
},
510-
build["do"]({
511-
build.assign_one(source_name, source),
512-
build.assign({
513-
names = real_names,
514-
values = (function()
515-
local _accum_0 = { }
516-
local _len_0 = 1
517-
for _index_0 = 1, #stubs do
518-
local stub = stubs[_index_0]
519-
_accum_0[_len_0] = build.chain({
520-
base = source_name,
521-
stub
522-
})
523-
_len_0 = _len_0 + 1
524-
end
525-
return _accum_0
526-
end)()
527-
})
528-
})
529-
})
530-
end
482+
local dest = {
483+
"table",
484+
table_values
485+
}
486+
return {
487+
"assign",
488+
{
489+
dest
490+
},
491+
{
492+
source
493+
},
494+
[-1] = node[-1]
495+
}
531496
end,
532497
comprehension = function(self, node, action)
533498
local _, exp, clauses = unpack(node)

moonscript/transform.moon

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -251,33 +251,16 @@ Statement = Transformer {
251251

252252
import: (node) =>
253253
_, names, source = unpack node
254-
255-
stubs = for name in *names
256-
if type(name) == "table"
257-
name
254+
table_values = for name in *names
255+
dest_val = if ntype(name) == "colon_stub"
256+
name[2]
258257
else
259-
{"dot", name}
258+
name
260259

261-
real_names = for name in *names
262-
type(name) == "table" and name[2] or name
260+
{{"key_literal", name}, dest_val}
263261

264-
if type(source) == "string"
265-
build.assign {
266-
names: real_names
267-
values: [build.chain { base: source, stub} for stub in *stubs]
268-
}
269-
else
270-
source_name = NameProxy "table"
271-
build.group {
272-
{"declare", real_names}
273-
build["do"] {
274-
build.assign_one source_name, source
275-
build.assign {
276-
names: real_names
277-
values: [build.chain { base: source_name, stub} for stub in *stubs]
278-
}
279-
}
280-
}
262+
dest = { "table", table_values }
263+
{ "assign", {dest}, {source}, [-1]: node[-1] }
281264

282265
comprehension: (node, action) =>
283266
_, exp, clauses = unpack node

moonscript/transform/destructure.lua

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ do
1919
local _table_0 = require("moonscript.errors")
2020
user_error = _table_0.user_error
2121
end
22+
local util = require("moonscript.util")
2223
local join
2324
join = function(...)
2425
do
@@ -75,10 +76,15 @@ extract_assign_names = function(name, accum, prefix)
7576
local key = tuple[1]
7677
local s
7778
if ntype(key) == "key_literal" then
78-
s = {
79-
"dot",
80-
key[2]
81-
}
79+
local key_name = key[2]
80+
if ntype(key_name) == "colon_stub" then
81+
s = key_name
82+
else
83+
s = {
84+
"dot",
85+
key_name
86+
}
87+
end
8288
else
8389
s = {
8490
"index",

moonscript/transform/destructure.moon

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import unpack from require "moonscript.util"
66

77
import user_error from require "moonscript.errors"
88

9+
util = require "moonscript.util"
10+
911
join = (...) ->
1012
with out = {}
1113
i = 1
@@ -29,7 +31,11 @@ extract_assign_names = (name, accum={}, prefix={}) ->
2931
else
3032
key = tuple[1]
3133
s = if ntype(key) == "key_literal"
32-
{"dot", key[2]}
34+
key_name = key[2]
35+
if ntype(key_name) == "colon_stub"
36+
key_name
37+
else
38+
{"dot", key_name}
3339
else
3440
{"index", key}
3541

@@ -67,7 +73,6 @@ build_assign = (scope, destruct_literal, receiver) ->
6773
for tuple in *extracted_names
6874
insert names, tuple[1]
6975
insert values, NameProxy.chain obj, unpack tuple[2]
70-
-- insert values, obj\chain unpack tuple[2]
7176

7277
build.group {
7378
{"declare", names}

spec/outputs/import.lua

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1-
local hello = yeah.hello
1+
local hello
2+
do
3+
local _obj_0 = yeah
4+
hello = _obj_0.hello
5+
end
26
local world
37
do
4-
local _table_0 = table["cool"]
5-
hello, world = _table_0.hello, _table_0.world
8+
local _obj_0 = table["cool"]
9+
hello, world = _obj_0.hello, _obj_0.world
10+
end
11+
local a, b, c
12+
do
13+
local _obj_0 = items
14+
a, b, c = _obj_0.a, (function()
15+
local _base_0 = _obj_0
16+
local _fn_0 = _base_0.b
17+
return function(...)
18+
return _fn_0(_base_0, ...)
19+
end
20+
end)(), _obj_0.c
621
end
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
1422
local master, ghost
1523
do
16-
local _table_0 = find("mytable")
17-
master, ghost = _table_0.master, (function()
18-
local _base_0 = _table_0
24+
local _obj_0 = find("mytable")
25+
master, ghost = _obj_0.master, (function()
26+
local _base_0 = _obj_0
1927
local _fn_0 = _base_0.ghost
2028
return function(...)
2129
return _fn_0(_base_0, ...)
@@ -27,15 +35,15 @@ a, yumm = 3434, "hello"
2735
local _table_0 = 232
2836
local something
2937
do
30-
local _table_1 = a(table)
31-
something = _table_1.something
38+
local _obj_0 = a(table)
39+
something = _obj_0.something
3240
end
3341
if indent then
3442
local okay, well
3543
do
36-
local _table_1 = tables[100]
37-
okay, well = _table_1.okay, (function()
38-
local _base_0 = _table_1
44+
local _obj_0 = tables[100]
45+
okay, well = _obj_0.okay, (function()
46+
local _base_0 = _obj_0
3947
local _fn_0 = _base_0.well
4048
return function(...)
4149
return _fn_0(_base_0, ...)
@@ -44,17 +52,32 @@ if indent then
4452
end
4553
end
4654
do
47-
a, b, c = z.a, z.b, z.c
55+
do
56+
local _obj_0 = z
57+
a, b, c = _obj_0.a, _obj_0.b, _obj_0.c
58+
end
4859
end
4960
do
50-
a, b, c = z.a, z.b, z.c
61+
do
62+
local _obj_0 = z
63+
a, b, c = _obj_0.a, _obj_0.b, _obj_0.c
64+
end
5165
end
5266
do
53-
a, b, c = z.a, z.b, z.c
67+
do
68+
local _obj_0 = z
69+
a, b, c = _obj_0.a, _obj_0.b, _obj_0.c
70+
end
5471
end
5572
do
56-
a, b, c = z.a, z.b, z.c
73+
do
74+
local _obj_0 = z
75+
a, b, c = _obj_0.a, _obj_0.b, _obj_0.c
76+
end
5777
end
5878
do
59-
a, b, c = z.a, z.b, z.c
79+
do
80+
local _obj_0 = z
81+
a, b, c = _obj_0.a, _obj_0.b, _obj_0.c
82+
end
6083
end

0 commit comments

Comments
 (0)