mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
allow assignment in if condition
This commit is contained in:
parent
7f3f17a8e4
commit
1d6bf564af
@ -191,6 +191,15 @@ local function format_assign(lhs_exps, assign)
|
||||
error "unknown assign expression"
|
||||
end
|
||||
|
||||
-- the if statement only takes a single lhs, so we wrap in table to git to
|
||||
-- "assign" tuple format
|
||||
local function format_assign_for_if(lhs, assign)
|
||||
if assign then
|
||||
return format_assign({lhs}, assign)
|
||||
end
|
||||
return lhs
|
||||
end
|
||||
|
||||
local function sym(chars)
|
||||
return Space * chars
|
||||
end
|
||||
@ -389,7 +398,7 @@ local build_grammar = wrap_env(function()
|
||||
SwitchCase = key"when" * Exp * key"then"^-1 * Body / mark"case",
|
||||
SwitchElse = key"else" * Body / mark"else",
|
||||
|
||||
If = key"if" * Exp * key"then"^-1 * Body *
|
||||
If = key"if" * (Exp * Assign^-1 / format_assign_for_if) * key"then"^-1 * Body *
|
||||
((Break * CheckIndent)^-1 * EmptyLine^0 * key"elseif" * Exp * key"then"^-1 * Body / mark"elseif")^0 *
|
||||
((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"if",
|
||||
|
||||
|
@ -519,9 +519,27 @@ Statement = Transformer({
|
||||
end
|
||||
return construct_comprehension(action(exp), clauses)
|
||||
end,
|
||||
["if"] = function(self, node, ret)
|
||||
["do"] = function(self, node, ret)
|
||||
if ret then
|
||||
node[2] = apply_to_last(node[2], ret)
|
||||
end
|
||||
return node
|
||||
end,
|
||||
["if"] = function(self, node, ret)
|
||||
smart_node(node)
|
||||
if ntype(node.cond) == "assign" then
|
||||
local _, assign, body = unpack(node)
|
||||
local name = assign[2][1]
|
||||
return build["do"]({
|
||||
assign,
|
||||
{
|
||||
"if",
|
||||
name,
|
||||
unpack(node, 3)
|
||||
}
|
||||
})
|
||||
end
|
||||
if ret then
|
||||
smart_node(node)
|
||||
node['then'] = apply_to_last(node['then'], ret)
|
||||
for i = 4, #node do
|
||||
local case = node[i]
|
||||
|
@ -226,16 +226,31 @@ Statement = Transformer {
|
||||
action = action or (exp) -> {exp}
|
||||
construct_comprehension action(exp), clauses
|
||||
|
||||
-- handle cascading return decorator
|
||||
do: (node, ret) =>
|
||||
node[2] = apply_to_last node[2], ret if ret
|
||||
node
|
||||
|
||||
if: (node, ret) =>
|
||||
smart_node node
|
||||
|
||||
-- extract assigns in cond
|
||||
if ntype(node.cond) == "assign"
|
||||
_, assign, body = unpack node
|
||||
name = assign[2][1]
|
||||
return build["do"] {
|
||||
assign
|
||||
{"if", name, unpack node, 3}
|
||||
}
|
||||
|
||||
-- handle cascading return decorator
|
||||
if ret
|
||||
smart_node node
|
||||
-- mutate all the bodies
|
||||
node['then'] = apply_to_last node['then'], ret
|
||||
for i = 4, #node
|
||||
case = node[i]
|
||||
body_idx = #node[i]
|
||||
case[body_idx] = apply_to_last case[body_idx], ret
|
||||
|
||||
node
|
||||
|
||||
with: (node, ret) =>
|
||||
|
@ -50,4 +50,19 @@ if lets go
|
||||
elseif "just us"
|
||||
print "will smith" else show 5555555
|
||||
|
||||
--
|
||||
|
||||
if something = 10
|
||||
print something
|
||||
else
|
||||
print "else"
|
||||
|
||||
hello = if something = 10
|
||||
print something
|
||||
else
|
||||
print "else"
|
||||
|
||||
|
||||
hello = 5 + if something = 10
|
||||
print something
|
||||
|
||||
|
@ -84,4 +84,29 @@ elseif "just us" then
|
||||
print("will smith")
|
||||
else
|
||||
show(5555555)
|
||||
end
|
||||
end
|
||||
do
|
||||
local something = 10
|
||||
if something then
|
||||
print(something)
|
||||
else
|
||||
print("else")
|
||||
end
|
||||
end
|
||||
local hello
|
||||
do
|
||||
local something = 10
|
||||
if something then
|
||||
hello = print(something)
|
||||
else
|
||||
hello = print("else")
|
||||
end
|
||||
end
|
||||
hello = 5 + (function()
|
||||
do
|
||||
local something = 10
|
||||
if something then
|
||||
return print(something)
|
||||
end
|
||||
end
|
||||
end)()
|
Loading…
Reference in New Issue
Block a user