Skip to content

Commit 3f6c1d5

Browse files
committed
switch tests, parse switch non-cascade expression
1 parent 7334678 commit 3f6c1d5

File tree

6 files changed

+99
-3
lines changed

6 files changed

+99
-3
lines changed

moonscript/parse.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ local build_grammar = wrap_env(function()
340340

341341
SimpleValue =
342342
If +
343+
Switch +
343344
With +
344345
ForEach + For + While +
345346
sym"-" * -SomeSpace * Exp / mark"minus" +

moonscript/transform.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,6 @@ Statement = Transformer({
494494
end,
495495
switch = function(self, node, ret)
496496
local _, exp, conds = unpack(node)
497-
print("compiling switch", ret)
498497
local exp_name = NameProxy("exp")
499498
local convert_cond
500499
convert_cond = function(cond)

moonscript/transform.moon

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,6 @@ Statement = Transformer {
269269

270270
switch: (node, ret) =>
271271
_, exp, conds = unpack node
272-
print "compiling switch", ret
273-
274272
exp_name = NameProxy "exp"
275273

276274
-- convert switch conds into if statment conds

tests/inputs/switch.moon

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
switch value
3+
when "cool"
4+
print "hello world"
5+
6+
7+
switch value
8+
when "cool"
9+
print "hello world"
10+
else
11+
print "okay rad"
12+
13+
14+
switch value
15+
when "cool"
16+
print "hello world"
17+
when "yeah"
18+
[[FFFF]] + [[MMMM]]
19+
when 2323 + 32434
20+
print "okay"
21+
else
22+
print "okay rad"
23+
24+
out = switch value
25+
when "cool" then print "hello world"
26+
else print "okay rad"
27+
28+
out = switch value
29+
when "cool" then xxxx
30+
when "umm" then 34340
31+
else error "this failed big time"
32+
33+
with something
34+
switch \value!
35+
when .okay
36+
"world"
37+
else
38+
"yesh"
39+
40+
fix this
41+
call_func switch something
42+
when 1 then "yes"
43+
else "no"
44+

tests/outputs/switch.lua

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
local _exp_0 = value
2+
if "cool" == _exp_0 then
3+
print("hello world")
4+
end
5+
local _exp_1 = value
6+
if "cool" == _exp_1 then
7+
print("hello world")
8+
else
9+
print("okay rad")
10+
end
11+
local _exp_2 = value
12+
if "cool" == _exp_2 then
13+
print("hello world")
14+
elseif "yeah" == _exp_2 then
15+
local _ = [[FFFF]] + [[MMMM]]
16+
elseif 2323 + 32434 == _exp_2 then
17+
print("okay")
18+
else
19+
print("okay rad")
20+
end
21+
local out
22+
local _exp_3 = value
23+
if "cool" == _exp_3 then
24+
out = print("hello world")
25+
else
26+
out = print("okay rad")
27+
end
28+
local _exp_4 = value
29+
if "cool" == _exp_4 then
30+
out = xxxx
31+
elseif "umm" == _exp_4 then
32+
out = 34340
33+
else
34+
out = error("this failed big time")
35+
end
36+
do
37+
local _with_0 = something
38+
local _exp_5 = _with_0:value()
39+
if _with_0.okay == _exp_5 then
40+
local _ = "world"
41+
else
42+
local _ = "yesh"
43+
end
44+
end
45+
fix(this)
46+
call_func((function()
47+
local _exp_5 = something
48+
if 1 == _exp_5 then
49+
return "yes"
50+
else
51+
return "no"
52+
end
53+
end)())

todo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- don't reuse _, put a local on it, so we don't keep around trash
44

5+
-- swithc X with Func
56

67
- or= and=
78

0 commit comments

Comments
 (0)