From 063b50a38f6e2f67457953d1bb0efe970c11b2c1 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 25 Oct 2012 18:55:04 -0700 Subject: [PATCH] assignment declaration is pulled out of line decorators #44 --- moonscript/transform.lua | 30 ++++++++++++++++++++++++++---- moonscript/transform.moon | 10 +++++++++- tests/inputs/cond.moon | 12 +++++++++--- tests/outputs/cond.lua | 19 +++++++++++++------ 4 files changed, 57 insertions(+), 14 deletions(-) diff --git a/moonscript/transform.lua b/moonscript/transform.lua index de2eb46..a0a407c 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -551,6 +551,7 @@ Statement = Transformer({ end, decorated = function(self, node) local stm, dec = unpack(node, 2) + local wrapped local _exp_0 = dec[1] if "if" == _exp_0 then local cond, fail = unpack(dec, 2) @@ -562,7 +563,7 @@ Statement = Transformer({ } } end - return { + wrapped = { "if", cond, { @@ -571,7 +572,7 @@ Statement = Transformer({ fail } elseif "unless" == _exp_0 then - return { + wrapped = { "unless", dec[2], { @@ -579,14 +580,35 @@ Statement = Transformer({ } } elseif "comprehension" == _exp_0 then - return { + wrapped = { "comprehension", stm, dec[2] } else - return error("Unknown decorator " .. dec[1]) + wrapped = error("Unknown decorator " .. dec[1]) end + if ntype(stm) == "assign" then + wrapped = build.group({ + build.declare({ + names = (function() + local _accum_0 = { } + local _len_0 = 0 + local _list_0 = stm[2] + for _index_0 = 1, #_list_0 do + local name = _list_0[_index_0] + if type(name) == "string" then + _len_0 = _len_0 + 1 + _accum_0[_len_0] = name + end + end + return _accum_0 + end)() + }), + wrapped + }) + end + return wrapped end, unless = function(self, node) return { diff --git a/moonscript/transform.moon b/moonscript/transform.moon index d28b20e..129dadf 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -247,7 +247,7 @@ Statement = Transformer { decorated: (node) => stm, dec = unpack node, 2 - switch dec[1] + wrapped = switch dec[1] when "if" cond, fail = unpack dec, 2 fail = { "else", { fail } } if fail @@ -259,6 +259,14 @@ Statement = Transformer { else error "Unknown decorator " .. dec[1] + if ntype(stm) == "assign" + wrapped = build.group { + build.declare names: [name for name in *stm[2] when type(name) == "string"] + wrapped + } + + wrapped + unless: (node) => { "if", {"not", {"parens", node[2]}}, unpack node, 3 } diff --git a/tests/inputs/cond.moon b/tests/inputs/cond.moon index 8fe3d83..d4d2643 100644 --- a/tests/inputs/cond.moon +++ b/tests/inputs/cond.moon @@ -113,10 +113,10 @@ x = unless true x = unless true and false print "cool!" -x = unless false then print "cool!" -x = unless false then print "cool!" else print "no way!" +y = unless false then print "cool!" +y = unless false then print "cool!" else print "no way!" -x = unless nil +z = unless nil print "hello" else print "world" @@ -141,4 +141,10 @@ print "hello" unless value dddd = {1,2,3} unless value +---------------- + +a = 12 +a,c,b = "cool" if something + + diff --git a/tests/outputs/cond.lua b/tests/outputs/cond.lua index 380ef36..5da6d04 100644 --- a/tests/outputs/cond.lua +++ b/tests/outputs/cond.lua @@ -176,18 +176,19 @@ end if not (true and false) then x = print("cool!") end +local y if not (false) then - x = print("cool!") + y = print("cool!") end if not (false) then - x = print("cool!") + y = print("cool!") else - x = print("no way!") + y = print("no way!") end if not (nil) then - x = print("hello") + z = print("hello") else - x = print("world") + z = print("world") end print((function() if not (true) then @@ -221,10 +222,16 @@ end)()) if not (value) then print("hello") end +local dddd if not (value) then - local dddd = { + dddd = { 1, 2, 3 } +end +local a = 12 +local c, b +if something then + a, c, b = "cool" end \ No newline at end of file