diff --git a/moonscript/transform.lua b/moonscript/transform.lua index dd0351d..caa773c 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -456,78 +456,43 @@ Statement = Transformer({ end, import = function(self, node) local _, names, source = unpack(node) - local stubs = (function() + local table_values = (function() local _accum_0 = { } local _len_0 = 1 for _index_0 = 1, #names do local name = names[_index_0] - if type(name) == "table" then - _accum_0[_len_0] = name + local dest_val + if ntype(name) == "colon_stub" then + dest_val = name[2] else - _accum_0[_len_0] = { - "dot", - name - } + dest_val = name end + local _value_0 = { + { + "key_literal", + name + }, + dest_val + } + _accum_0[_len_0] = _value_0 _len_0 = _len_0 + 1 end return _accum_0 end)() - local real_names = (function() - local _accum_0 = { } - local _len_0 = 1 - for _index_0 = 1, #names do - local name = names[_index_0] - _accum_0[_len_0] = type(name) == "table" and name[2] or name - _len_0 = _len_0 + 1 - end - return _accum_0 - end)() - if type(source) == "string" then - return build.assign({ - names = real_names, - values = (function() - local _accum_0 = { } - local _len_0 = 1 - for _index_0 = 1, #stubs do - local stub = stubs[_index_0] - _accum_0[_len_0] = build.chain({ - base = source, - stub - }) - _len_0 = _len_0 + 1 - end - return _accum_0 - end)() - }) - else - local source_name = NameProxy("table") - return build.group({ - { - "declare", - real_names - }, - build["do"]({ - build.assign_one(source_name, source), - build.assign({ - names = real_names, - values = (function() - local _accum_0 = { } - local _len_0 = 1 - for _index_0 = 1, #stubs do - local stub = stubs[_index_0] - _accum_0[_len_0] = build.chain({ - base = source_name, - stub - }) - _len_0 = _len_0 + 1 - end - return _accum_0 - end)() - }) - }) - }) - end + local dest = { + "table", + table_values + } + return { + "assign", + { + dest + }, + { + source + }, + [-1] = node[-1] + } end, comprehension = function(self, node, action) local _, exp, clauses = unpack(node) diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 92bc441..c6501b0 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -251,33 +251,16 @@ Statement = Transformer { import: (node) => _, names, source = unpack node - - stubs = for name in *names - if type(name) == "table" - name + table_values = for name in *names + dest_val = if ntype(name) == "colon_stub" + name[2] else - {"dot", name} + name - real_names = for name in *names - type(name) == "table" and name[2] or name + {{"key_literal", name}, dest_val} - if type(source) == "string" - build.assign { - names: real_names - values: [build.chain { base: source, stub} for stub in *stubs] - } - else - source_name = NameProxy "table" - build.group { - {"declare", real_names} - build["do"] { - build.assign_one source_name, source - build.assign { - names: real_names - values: [build.chain { base: source_name, stub} for stub in *stubs] - } - } - } + dest = { "table", table_values } + { "assign", {dest}, {source}, [-1]: node[-1] } comprehension: (node, action) => _, exp, clauses = unpack node diff --git a/moonscript/transform/destructure.lua b/moonscript/transform/destructure.lua index 7ad4b4e..2cb8897 100644 --- a/moonscript/transform/destructure.lua +++ b/moonscript/transform/destructure.lua @@ -19,6 +19,7 @@ do local _table_0 = require("moonscript.errors") user_error = _table_0.user_error end +local util = require("moonscript.util") local join join = function(...) do @@ -75,10 +76,15 @@ extract_assign_names = function(name, accum, prefix) local key = tuple[1] local s if ntype(key) == "key_literal" then - s = { - "dot", - key[2] - } + local key_name = key[2] + if ntype(key_name) == "colon_stub" then + s = key_name + else + s = { + "dot", + key_name + } + end else s = { "index", diff --git a/moonscript/transform/destructure.moon b/moonscript/transform/destructure.moon index df20bbb..7175854 100644 --- a/moonscript/transform/destructure.moon +++ b/moonscript/transform/destructure.moon @@ -6,6 +6,8 @@ import unpack from require "moonscript.util" import user_error from require "moonscript.errors" +util = require "moonscript.util" + join = (...) -> with out = {} i = 1 @@ -29,7 +31,11 @@ extract_assign_names = (name, accum={}, prefix={}) -> else key = tuple[1] s = if ntype(key) == "key_literal" - {"dot", key[2]} + key_name = key[2] + if ntype(key_name) == "colon_stub" + key_name + else + {"dot", key_name} else {"index", key} @@ -67,7 +73,6 @@ build_assign = (scope, destruct_literal, receiver) -> for tuple in *extracted_names insert names, tuple[1] insert values, NameProxy.chain obj, unpack tuple[2] - -- insert values, obj\chain unpack tuple[2] build.group { {"declare", names} diff --git a/spec/outputs/import.lua b/spec/outputs/import.lua index 1bd218e..f7467f4 100644 --- a/spec/outputs/import.lua +++ b/spec/outputs/import.lua @@ -1,21 +1,29 @@ -local hello = yeah.hello +local hello +do + local _obj_0 = yeah + hello = _obj_0.hello +end local world do - local _table_0 = table["cool"] - hello, world = _table_0.hello, _table_0.world + local _obj_0 = table["cool"] + hello, world = _obj_0.hello, _obj_0.world +end +local a, b, c +do + local _obj_0 = items + a, b, c = _obj_0.a, (function() + local _base_0 = _obj_0 + local _fn_0 = _base_0.b + return function(...) + return _fn_0(_base_0, ...) + end + end)(), _obj_0.c end -local a, b, c = items.a, (function() - local _base_0 = items - local _fn_0 = _base_0.b - return function(...) - return _fn_0(_base_0, ...) - end -end)(), items.c local master, ghost do - local _table_0 = find("mytable") - master, ghost = _table_0.master, (function() - local _base_0 = _table_0 + local _obj_0 = find("mytable") + master, ghost = _obj_0.master, (function() + local _base_0 = _obj_0 local _fn_0 = _base_0.ghost return function(...) return _fn_0(_base_0, ...) @@ -27,15 +35,15 @@ a, yumm = 3434, "hello" local _table_0 = 232 local something do - local _table_1 = a(table) - something = _table_1.something + local _obj_0 = a(table) + something = _obj_0.something end if indent then local okay, well do - local _table_1 = tables[100] - okay, well = _table_1.okay, (function() - local _base_0 = _table_1 + local _obj_0 = tables[100] + okay, well = _obj_0.okay, (function() + local _base_0 = _obj_0 local _fn_0 = _base_0.well return function(...) return _fn_0(_base_0, ...) @@ -44,17 +52,32 @@ if indent then end end do - a, b, c = z.a, z.b, z.c + do + local _obj_0 = z + a, b, c = _obj_0.a, _obj_0.b, _obj_0.c + end end do - a, b, c = z.a, z.b, z.c + do + local _obj_0 = z + a, b, c = _obj_0.a, _obj_0.b, _obj_0.c + end end do - a, b, c = z.a, z.b, z.c + do + local _obj_0 = z + a, b, c = _obj_0.a, _obj_0.b, _obj_0.c + end end do - a, b, c = z.a, z.b, z.c + do + local _obj_0 = z + a, b, c = _obj_0.a, _obj_0.b, _obj_0.c + end end do - a, b, c = z.a, z.b, z.c + do + local _obj_0 = z + a, b, c = _obj_0.a, _obj_0.b, _obj_0.c + end end \ No newline at end of file