From 99e1c9d38b82bb56f376ae5b93e291980f0fbb10 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 25 Oct 2012 17:57:21 -0700 Subject: [PATCH] move line decorator transformation out of the parser --- moonscript/parse.lua | 20 +------------------- moonscript/transform.lua | 39 +++++++++++++++++++++++++++++++++++++++ moonscript/transform.moon | 15 +++++++++++++++ 3 files changed, 55 insertions(+), 19 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index fe0f643..003877b 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -254,25 +254,7 @@ end -- transforms a statement that has a line decorator local function wrap_decorator(stm, dec) if not dec then return stm end - - local arg = {stm, dec} - - if dec[1] == "if" then - 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} - end - - return stm + return { "decorated", stm, dec } end -- wrap if statement if there is a conditional decorator diff --git a/moonscript/transform.lua b/moonscript/transform.lua index d47b7f3..de2eb46 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -549,6 +549,45 @@ Statement = Transformer({ end return node end, + decorated = function(self, node) + local stm, dec = unpack(node, 2) + local _exp_0 = dec[1] + if "if" == _exp_0 then + local cond, fail = unpack(dec, 2) + if fail then + fail = { + "else", + { + fail + } + } + end + return { + "if", + cond, + { + stm + }, + fail + } + elseif "unless" == _exp_0 then + return { + "unless", + dec[2], + { + stm + } + } + elseif "comprehension" == _exp_0 then + return { + "comprehension", + stm, + dec[2] + } + else + return error("Unknown decorator " .. dec[1]) + end + end, unless = function(self, node) return { "if", diff --git a/moonscript/transform.moon b/moonscript/transform.moon index ed37202..d28b20e 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -244,6 +244,21 @@ Statement = Transformer { node[2] = apply_to_last node[2], ret if ret node + decorated: (node) => + stm, dec = unpack node, 2 + + switch dec[1] + when "if" + cond, fail = unpack dec, 2 + fail = { "else", { fail } } if fail + { "if", cond, { stm }, fail } + when "unless" + { "unless", dec[2], { stm } } + when "comprehension" + { "comprehension", stm, dec[2] } + else + error "Unknown decorator " .. dec[1] + unless: (node) => { "if", {"not", {"parens", node[2]}}, unpack node, 3 }