allow assignment in unless block, fixes #251

This commit is contained in:
leaf corcoran 2016-09-25 14:07:35 -07:00
parent dfc4f7b016
commit c19a7fd2c3
4 changed files with 58 additions and 11 deletions

View File

@ -484,17 +484,35 @@ return Transformer({
return wrapped return wrapped
end, end,
unless = function(self, node) unless = function(self, node)
return { local clause = node[2]
"if", if ntype(clause) == "assign" then
{ if destructure.has_destructure(clause[2]) then
"not", error("destructure not allowed in unless assignment")
end
return build["do"]({
clause,
{ {
"parens", "if",
node[2] {
"not",
clause[2][1]
},
unpack(node, 3)
} }
}, })
unpack(node, 3) else
} return {
"if",
{
"not",
{
"parens",
clause
}
},
unpack(node, 3)
}
end
end, end,
["if"] = function(self, node, ret) ["if"] = function(self, node, ret)
if ntype(node[2]) == "assign" then if ntype(node[2]) == "assign" then

View File

@ -280,7 +280,19 @@ Transformer {
wrapped wrapped
unless: (node) => unless: (node) =>
{ "if", {"not", {"parens", node[2]}}, unpack node, 3 } clause = node[2]
if ntype(clause) == "assign"
if destructure.has_destructure clause[2]
error "destructure not allowed in unless assignment"
build.do {
clause
{ "if", {"not", clause[2][1]}, unpack node, 3 }
}
else
{ "if", {"not", {"parens", clause}}, unpack node, 3 }
if: (node, ret) => if: (node, ret) =>
-- expand assign in cond -- expand assign in cond
@ -300,7 +312,7 @@ Transformer {
} }
else else
name = assign[2][1] name = assign[2][1]
return build["do"] { return build.do {
assign assign
{"if", name, unpack node, 3} {"if", name, unpack node, 3}
} }

View File

@ -147,6 +147,14 @@ print "hello" unless value
dddd = {1,2,3} unless value dddd = {1,2,3} unless value
--
do
j = 100
unless j = hi!
error "not j!"
---------------- ----------------
a = 12 a = 12

View File

@ -246,6 +246,15 @@ if not (value) then
3 3
} }
end end
do
local j = 100
do
j = hi()
if not j then
error("not j!")
end
end
end
local a = 12 local a = 12
local c, b local c, b
if something then if something then