add unless block

This commit is contained in:
leaf corcoran 2012-10-25 15:23:24 -07:00
parent 14db695560
commit 2134a3320a
7 changed files with 159 additions and 3 deletions

View File

@ -404,6 +404,9 @@ local build_grammar = wrap_env(function()
((Break * CheckIndent)^-1 * EmptyLine^0 * key"elseif" * IfCond * key"then"^-1 * Body / mark"elseif")^0 *
((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"if",
Unless = key"unless" * IfCond * key"then"^-1 * Body *
((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"unless",
While = key"while" * Exp * key"do"^-1 * Body / mark"while",
For = key"for" * (Name * sym"=" * Ct(Exp * sym"," * Exp * (sym"," * Exp)^-1)) *
@ -435,7 +438,7 @@ local build_grammar = wrap_env(function()
-- Term = Ct(Value * (TermOp * Value)^0) / flatten_or_mark"exp",
SimpleValue =
If +
If + Unless +
Switch +
With +
ForEach + For + While +

View File

@ -549,6 +549,19 @@ Statement = Transformer({
end
return node
end,
unless = function(self, node)
return {
"if",
{
"not",
{
"parens",
node[2]
}
},
unpack(node, 3)
}
end,
["if"] = function(self, node, ret)
if ntype(node[2]) == "assign" then
local _, assign, body = unpack(node)
@ -1284,6 +1297,11 @@ Value = Transformer({
node
})
end,
unless = function(self, node)
return build.block_exp({
node
})
end,
with = function(self, node)
return build.block_exp({
node

View File

@ -244,8 +244,10 @@ Statement = Transformer {
node[2] = apply_to_last node[2], ret if ret
node
if: (node, ret) =>
unless: (node) =>
{ "if", {"not", {"parens", node[2]}}, unpack node, 3 }
if: (node, ret) =>
-- expand assign in cond
if ntype(node[2]) == "assign"
_, assign, body = unpack node
@ -684,6 +686,7 @@ Value = Transformer {
node
if: (node) => build.block_exp { node }
unless: (node) =>build.block_exp { node }
with: (node) => build.block_exp { node }
switch: (node) =>
build.block_exp { node }

View File

@ -10,6 +10,7 @@ manual_return = data.Set({
})
cascading = data.Set({
"if",
"unless",
"with",
"switch"
})

View File

@ -12,7 +12,7 @@ import insert from table
manual_return = data.Set{"foreach", "for", "while", "return"}
-- assigns and returns are bubbled into their bodies
cascading = data.Set{ "if", "with", "switch" }
cascading = data.Set{ "if", "unless", "with", "switch" }
is_value = (stm) ->
import compile, transform from moonscript

View File

@ -89,3 +89,56 @@ elseif z = true
else
four
---
unless true
print "cool!"
unless true and false
print "cool!"
unless false then print "cool!"
unless false then print "cool!" else print "no way!"
unless nil
print "hello"
else
print "world"
--
x = unless true
print "cool!"
x = unless true and false
print "cool!"
x = unless false then print "cool!"
x = unless false then print "cool!" else print "no way!"
x = unless nil
print "hello"
else
print "world"
print unless true
print "cool!"
print unless true and false
print "cool!"
print unless false then print "cool!"
print unless false then print "cool!" else print "no way!"
print unless nil
print "hello"
else
print "world"
--
print "hello" unless value
dddd = {1,2,3} unless value

View File

@ -149,4 +149,82 @@ else
end
end
end
end
if not (true) then
print("cool!")
end
if not (true and false) then
print("cool!")
end
if not (false) then
print("cool!")
end
if not (false) then
print("cool!")
else
print("no way!")
end
if not (nil) then
print("hello")
else
print("world")
end
local x
if not (true) then
x = print("cool!")
end
if not (true and false) then
x = print("cool!")
end
if not (false) then
x = print("cool!")
end
if not (false) then
x = print("cool!")
else
x = print("no way!")
end
if not (nil) then
x = print("hello")
else
x = print("world")
end
print((function()
if not (true) then
return print("cool!")
end
end)())
print((function()
if not (true and false) then
return print("cool!")
end
end)())
print((function()
if not (false) then
return print("cool!")
end
end)())
print((function()
if not (false) then
return print("cool!")
else
return print("no way!")
end
end)())
print((function()
if not (nil) then
return print("hello")
else
return print("world")
end
end)())
if not (value) then
print("hello")
end
if not (value) then
local dddd = {
1,
2,
3
}
end