Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

allow assignment in if condition

  • Loading branch information...
commit 1d6bf564af34d0c03d7f9c444aa1a8bcb6e25030 1 parent 7f3f17a
@leafo authored
View
11 moonscript/parse.lua
@@ -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",
View
20 moonscript/transform.lua
@@ -519,9 +519,27 @@ Statement = Transformer({
end
return construct_comprehension(action(exp), clauses)
end,
+ ["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]
View
19 moonscript/transform.moon
@@ -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) =>
View
15 tests/inputs/cond.moon
@@ -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
View
27 tests/outputs/cond.lua
@@ -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)()
View
5 todo
@@ -50,3 +50,8 @@ not working right:
* 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)
+
Please sign in to comment.
Something went wrong with that request. Please try again.