From 16f4c956bf3730b5178ec345bdc30a2272eab147 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 13 Jan 2012 21:41:38 -0800 Subject: [PATCH] unless line decorator --- moonscript/parse.lua | 9 ++++++++- tests/inputs/syntax.moon | 2 ++ tests/outputs/syntax.lua | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index fb1a627..62534aa 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -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, diff --git a/tests/inputs/syntax.moon b/tests/inputs/syntax.moon index ea1e05e..334c914 100644 --- a/tests/inputs/syntax.moon +++ b/tests/inputs/syntax.moon @@ -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" diff --git a/tests/outputs/syntax.lua b/tests/outputs/syntax.lua index aa51a03..2998c7a 100644 --- a/tests/outputs/syntax.lua +++ b/tests/outputs/syntax.lua @@ -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