move line decorator transformation out of the parser

This commit is contained in:
leaf corcoran 2012-10-25 17:57:21 -07:00
parent 2134a3320a
commit 99e1c9d38b
3 changed files with 55 additions and 19 deletions

View File

@ -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

View File

@ -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",

View File

@ -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 }