unless line decorator

This commit is contained in:
leaf corcoran 2012-01-13 21:41:38 -08:00
parent bf416dbb1c
commit 16f4c956bf
3 changed files with 16 additions and 1 deletions

View File

@ -179,7 +179,7 @@ local function flatten_func(callee, args)
return {"chain", callee, args}
end
-- wraps a statement that has a line decorator
-- transforms a statement that has a line decorator
local function wrap_decorator(stm, dec)
if not dec then return stm end
@ -189,6 +189,12 @@ local function wrap_decorator(stm, dec)
local _, cond, fail = unpack(dec)
if fail then fail = {"else", {fail}} end
stm = {"if", cond, {stm}, fail}
elseif dec[1] == "unless" then
stm = {
"if",
{"not", {"parens", dec[2]}},
{stm}
}
elseif dec[1] == "comprehension" then
local _, clauses = unpack(dec)
stm = {"comprehension", stm, clauses}
@ -288,6 +294,7 @@ local build_grammar = wrap_env(function()
) * Space * ((
-- statement decorators
key"if" * Exp * (key"else" * Exp)^-1 * Space / mark"if" +
key"unless" * Exp / mark"unless" +
CompInner / mark"comprehension"
) * Space)^-1 / wrap_decorator,

View File

@ -102,6 +102,8 @@ hi = -"herfef"
x = -[x for x in x]
print "hello" if cool
print "hello" unless cool
print "hello" unless 1212 and 3434 -- hello
print "nutjob"

View File

@ -101,6 +101,12 @@ end)()
if cool then
print("hello")
end
if not (cool) then
print("hello")
end
if not (1212 and 3434) then
print("hello")
end
print("nutjob")
if hello then
_ = 343