Skip to content

Commit 2134a33

Browse files
committed
add unless block
1 parent 14db695 commit 2134a33

File tree

7 files changed

+159
-3
lines changed

7 files changed

+159
-3
lines changed

moonscript/parse.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ local build_grammar = wrap_env(function()
404404
((Break * CheckIndent)^-1 * EmptyLine^0 * key"elseif" * IfCond * key"then"^-1 * Body / mark"elseif")^0 *
405405
((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"if",
406406

407+
Unless = key"unless" * IfCond * key"then"^-1 * Body *
408+
((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"unless",
409+
407410
While = key"while" * Exp * key"do"^-1 * Body / mark"while",
408411

409412
For = key"for" * (Name * sym"=" * Ct(Exp * sym"," * Exp * (sym"," * Exp)^-1)) *
@@ -435,7 +438,7 @@ local build_grammar = wrap_env(function()
435438
-- Term = Ct(Value * (TermOp * Value)^0) / flatten_or_mark"exp",
436439

437440
SimpleValue =
438-
If +
441+
If + Unless +
439442
Switch +
440443
With +
441444
ForEach + For + While +

moonscript/transform.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,19 @@ Statement = Transformer({
549549
end
550550
return node
551551
end,
552+
unless = function(self, node)
553+
return {
554+
"if",
555+
{
556+
"not",
557+
{
558+
"parens",
559+
node[2]
560+
}
561+
},
562+
unpack(node, 3)
563+
}
564+
end,
552565
["if"] = function(self, node, ret)
553566
if ntype(node[2]) == "assign" then
554567
local _, assign, body = unpack(node)
@@ -1284,6 +1297,11 @@ Value = Transformer({
12841297
node
12851298
})
12861299
end,
1300+
unless = function(self, node)
1301+
return build.block_exp({
1302+
node
1303+
})
1304+
end,
12871305
with = function(self, node)
12881306
return build.block_exp({
12891307
node

moonscript/transform.moon

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,10 @@ Statement = Transformer {
244244
node[2] = apply_to_last node[2], ret if ret
245245
node
246246

247-
if: (node, ret) =>
247+
unless: (node) =>
248+
{ "if", {"not", {"parens", node[2]}}, unpack node, 3 }
248249

250+
if: (node, ret) =>
249251
-- expand assign in cond
250252
if ntype(node[2]) == "assign"
251253
_, assign, body = unpack node
@@ -684,6 +686,7 @@ Value = Transformer {
684686
node
685687

686688
if: (node) => build.block_exp { node }
689+
unless: (node) =>build.block_exp { node }
687690
with: (node) => build.block_exp { node }
688691
switch: (node) =>
689692
build.block_exp { node }

moonscript/types.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ manual_return = data.Set({
1010
})
1111
cascading = data.Set({
1212
"if",
13+
"unless",
1314
"with",
1415
"switch"
1516
})

moonscript/types.moon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import insert from table
1212
manual_return = data.Set{"foreach", "for", "while", "return"}
1313

1414
-- assigns and returns are bubbled into their bodies
15-
cascading = data.Set{ "if", "with", "switch" }
15+
cascading = data.Set{ "if", "unless", "with", "switch" }
1616

1717
is_value = (stm) ->
1818
import compile, transform from moonscript

tests/inputs/cond.moon

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,56 @@ elseif z = true
8989
else
9090
four
9191

92+
---
93+
94+
unless true
95+
print "cool!"
96+
97+
unless true and false
98+
print "cool!"
99+
100+
unless false then print "cool!"
101+
unless false then print "cool!" else print "no way!"
102+
103+
unless nil
104+
print "hello"
105+
else
106+
print "world"
107+
108+
--
109+
110+
x = unless true
111+
print "cool!"
112+
113+
x = unless true and false
114+
print "cool!"
115+
116+
x = unless false then print "cool!"
117+
x = unless false then print "cool!" else print "no way!"
118+
119+
x = unless nil
120+
print "hello"
121+
else
122+
print "world"
123+
124+
print unless true
125+
print "cool!"
126+
127+
print unless true and false
128+
print "cool!"
129+
130+
print unless false then print "cool!"
131+
print unless false then print "cool!" else print "no way!"
132+
133+
print unless nil
134+
print "hello"
135+
else
136+
print "world"
137+
138+
--
139+
140+
print "hello" unless value
141+
142+
dddd = {1,2,3} unless value
143+
144+

tests/outputs/cond.lua

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,82 @@ else
149149
end
150150
end
151151
end
152+
end
153+
if not (true) then
154+
print("cool!")
155+
end
156+
if not (true and false) then
157+
print("cool!")
158+
end
159+
if not (false) then
160+
print("cool!")
161+
end
162+
if not (false) then
163+
print("cool!")
164+
else
165+
print("no way!")
166+
end
167+
if not (nil) then
168+
print("hello")
169+
else
170+
print("world")
171+
end
172+
local x
173+
if not (true) then
174+
x = print("cool!")
175+
end
176+
if not (true and false) then
177+
x = print("cool!")
178+
end
179+
if not (false) then
180+
x = print("cool!")
181+
end
182+
if not (false) then
183+
x = print("cool!")
184+
else
185+
x = print("no way!")
186+
end
187+
if not (nil) then
188+
x = print("hello")
189+
else
190+
x = print("world")
191+
end
192+
print((function()
193+
if not (true) then
194+
return print("cool!")
195+
end
196+
end)())
197+
print((function()
198+
if not (true and false) then
199+
return print("cool!")
200+
end
201+
end)())
202+
print((function()
203+
if not (false) then
204+
return print("cool!")
205+
end
206+
end)())
207+
print((function()
208+
if not (false) then
209+
return print("cool!")
210+
else
211+
return print("no way!")
212+
end
213+
end)())
214+
print((function()
215+
if not (nil) then
216+
return print("hello")
217+
else
218+
return print("world")
219+
end
220+
end)())
221+
if not (value) then
222+
print("hello")
223+
end
224+
if not (value) then
225+
local dddd = {
226+
1,
227+
2,
228+
3
229+
}
152230
end

0 commit comments

Comments
 (0)