diff --git a/moonscript/transform/statement.lua b/moonscript/transform/statement.lua index 7303666..edeab69 100644 --- a/moonscript/transform/statement.lua +++ b/moonscript/transform/statement.lua @@ -353,18 +353,85 @@ return Transformer({ if not op_final then error("Unknown op: " .. op) end + local lifted + if ntype(name) == "chain" then + lifted = { } + local new_chain + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 3, #name do + local part = name[_index_0] + if ntype(part) == "index" then + local proxy = NameProxy("update") + table.insert(lifted, { + proxy, + part[2] + }) + _accum_0[_len_0] = { + "index", + proxy + } + else + _accum_0[_len_0] = part + end + _len_0 = _len_0 + 1 + end + new_chain = _accum_0 + end + if next(lifted) then + name = { + name[1], + name[2], + unpack(new_chain) + } + end + end if not (value_is_singular(exp)) then exp = { "parens", exp } end - return build.assign_one(name, { + local out = build.assign_one(name, { "exp", name, op_final, exp }) + if lifted and next(lifted) then + local names + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #lifted do + local l = lifted[_index_0] + _accum_0[_len_0] = l[1] + _len_0 = _len_0 + 1 + end + names = _accum_0 + end + local values + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #lifted do + local l = lifted[_index_0] + _accum_0[_len_0] = l[2] + _len_0 = _len_0 + 1 + end + values = _accum_0 + end + out = build.group({ + { + "assign", + names, + values + }, + out + }) + end + return out end, import = function(self, node) local names, source = unpack(node, 2) diff --git a/moonscript/transform/statement.moon b/moonscript/transform/statement.moon index f5cf3a5..2e4b8e8 100644 --- a/moonscript/transform/statement.moon +++ b/moonscript/transform/statement.moon @@ -229,9 +229,37 @@ Transformer { update: (node) => name, op, exp = unpack node, 2 op_final = op\match "^(.+)=$" + error "Unknown op: "..op if not op_final + + local lifted + + if ntype(name) == "chain" + lifted = {} + new_chain = for part in *name[3,] + if ntype(part) == "index" + proxy = NameProxy "update" + table.insert lifted, { proxy, part[2] } + { "index", proxy } + else + part + + if next lifted + name = {name[1], name[2], unpack new_chain} + exp = {"parens", exp} unless value_is_singular exp - build.assign_one name, {"exp", name, op_final, exp} + out = build.assign_one name, {"exp", name, op_final, exp} + + if lifted and next lifted + names = [l[1] for l in *lifted] + values = [l[2] for l in *lifted] + + out = build.group { + {"assign", names, values} + out + } + + out import: (node) => names, source = unpack node, 2 diff --git a/spec/inputs/syntax.moon b/spec/inputs/syntax.moon index 7b49d04..854f629 100644 --- a/spec/inputs/syntax.moon +++ b/spec/inputs/syntax.moon @@ -158,6 +158,10 @@ hello ..= "world" @@something += 10 @something += 10 +a["hello"] += 10 +a["hello#{tostring ff}"] += 10 +a[four].x += 10 + x = 0 (if ntype(v) == "fndef" then x += 1) for v in *values diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index bf32319..fef1a73 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua @@ -162,6 +162,12 @@ local m = m % 2 local hello = hello .. "world" self.__class.something = self.__class.something + 10 self.something = self.something + 10 +local _update_0 = "hello" +a[_update_0] = a[_update_0] + 10 +local _update_1 = "hello" .. tostring(tostring(ff)) +a[_update_1] = a[_update_1] + 10 +local _update_2 = four +a[_update_2].x = a[_update_2].x + 10 x = 0 local _list_0 = values for _index_0 = 1, #_list_0 do