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"
|
error "unknown assign expression"
|
||||||
end
|
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)
|
local function sym(chars)
|
||||||
return Space * chars
|
return Space * chars
|
||||||
end
|
end
|
||||||
@ -389,7 +398,7 @@ local build_grammar = wrap_env(function()
|
|||||||
SwitchCase = key"when" * Exp * key"then"^-1 * Body / mark"case",
|
SwitchCase = key"when" * Exp * key"then"^-1 * Body / mark"case",
|
||||||
SwitchElse = key"else" * Body / mark"else",
|
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"elseif" * Exp * key"then"^-1 * Body / mark"elseif")^0 *
|
||||||
((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"if",
|
((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"if",
|
||||||
|
|
||||||
|
@ -519,9 +519,27 @@ Statement = Transformer({
|
|||||||
end
|
end
|
||||||
return construct_comprehension(action(exp), clauses)
|
return construct_comprehension(action(exp), clauses)
|
||||||
end,
|
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
|
if ret then
|
||||||
smart_node(node)
|
|
||||||
node['then'] = apply_to_last(node['then'], ret)
|
node['then'] = apply_to_last(node['then'], ret)
|
||||||
for i = 4, #node do
|
for i = 4, #node do
|
||||||
local case = node[i]
|
local case = node[i]
|
||||||
|
@ -226,16 +226,31 @@ Statement = Transformer {
|
|||||||
action = action or (exp) -> {exp}
|
action = action or (exp) -> {exp}
|
||||||
construct_comprehension action(exp), clauses
|
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) =>
|
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
|
if ret
|
||||||
smart_node node
|
|
||||||
-- mutate all the bodies
|
-- mutate all the bodies
|
||||||
node['then'] = apply_to_last node['then'], ret
|
node['then'] = apply_to_last node['then'], ret
|
||||||
for i = 4, #node
|
for i = 4, #node
|
||||||
case = node[i]
|
case = node[i]
|
||||||
body_idx = #node[i]
|
body_idx = #node[i]
|
||||||
case[body_idx] = apply_to_last case[body_idx], ret
|
case[body_idx] = apply_to_last case[body_idx], ret
|
||||||
|
|
||||||
node
|
node
|
||||||
|
|
||||||
with: (node, ret) =>
|
with: (node, ret) =>
|
||||||
|
@ -50,4 +50,19 @@ if lets go
|
|||||||
elseif "just us"
|
elseif "just us"
|
||||||
print "will smith" else show 5555555
|
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")
|
print("will smith")
|
||||||
else
|
else
|
||||||
show(5555555)
|
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)()
|
5
todo
5
todo
@ -50,3 +50,8 @@ not working right:
|
|||||||
|
|
||||||
* let array items in table be defined without {} when indented
|
* let array items in table be defined without {} when indented
|
||||||
|
|
||||||
|
|
||||||
|
* allow dot access with names that are keywords: hello.then
|
||||||
|
|
||||||
|
* convert tree transformer to be depth first instead of breadth first (lazy)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user