conds and while

This commit is contained in:
leaf corcoran 2011-05-23 01:16:49 -07:00
parent 1cb205608a
commit c70b298b68
4 changed files with 199 additions and 4 deletions

View File

@ -91,9 +91,39 @@ local compiler_index = {
end,
["if"] = function(self, node)
local cond, block = node[2], node[3]
local ichr = self:ichar()
local out = {
("if %s then"):format(self:value(cond)),
self:block(block, 1)
}
for i = 4,#node do
local clause = node[i]
local block
if clause[1] == "else" then
table.insert(out, ichr.."else")
block = clause[2]
elseif clause[1] == "elseif" then
table.insert(out, ichr.."elseif "..self:value(clause[2]).." then")
block = clause[3]
else
error("Unknown if clause: "..clause[1])
end
table.insert(out, self:block(block, 1))
end
table.insert(out, ichr.."end")
return table.concat(out, "\n")
end,
['while'] = function(self, node)
local _, cond, block = unpack(node)
return ("if %s then\n%s\n%send"):format(
self:value(cond), self:block(block, 1), self:ichar())
local ichr = self:ichar()
return ("while %s do\n%s\n%send"):format(self:value(cond), self:block(block, 1), ichr)
end,
block = function(self, node, inc)

View File

@ -177,14 +177,18 @@ local build_grammar = wrap(function()
File = Block + Ct"",
Block = Ct(Line * (Break^1 * Line)^0),
Line = Cmt(Indent, check_indent) * Statement + _Space * Comment,
Statement = Ct(If) + Exp * Space,
Statement = If + While + Exp * Space,
Body = Break * InBlock + Ct(Statement),
InBlock = #Cmt(Indent, advance_indent) * Block * OutBlock,
OutBlock = Cmt("", pop_indent),
If = key"if" * Exp * Body / mark"if",
If = key"if" * Exp * key"then"^-1 * Body *
((Break * Cmt(Indent, check_indent))^-1 * key"elseif" * Exp * key"then"^-1 * Body / mark"elseif")^0 *
((Break * Cmt(Indent, check_indent))^-1 * key"else" * Body / mark"else")^-1 / mark"if",
While = key"while" * Exp * key"do"^-1 * Body / mark"while",
Assign = Ct(AssignableList) * sym"=" * Ct(TableBlock + ExpList) / mark"assign",

63
tests/inputs/cond.moon Normal file
View File

@ -0,0 +1,63 @@
you_cool = false
if cool
if you_cool
one
else if eatdic
yeah
else
two
three
else
no
if cool then no
if cool then no else yes
if cool then wow cool else
noso cool
if working
if cool then if cool then okay else what else nah
while true do print "name"
while 5 + 5
print "okay world"
working man
while also do
i work too
"okay"
if yeah then no day elseif cool me then okay ya else u way
if yeah then no dad else if cool you then okay bah else p way
if (->)() then what ever
if nil then flip me else
it be,rad
if things great then no way elseif okay sure
what here
if things then no chance
elseif okay
what now
if things
yes man
elseif okay person then hi there else hmm sure
if lets go
print "greetings"
elseif "just us"
print "will smith" else show 5555555

98
tests/outputs/cond.lua Normal file
View File

@ -0,0 +1,98 @@
local you_cool = false
if cool then
if you_cool then
one
else
if eatdic then
yeah
else
two
three
end
end
else
no
end
if cool then
no
end
if cool then
no
else
yes
end
if cool then
wow(cool)
else
noso(cool)
end
if working then
if cool then
if cool then
okay
else
what
end
else
nah
end
end
while true do
print("name")
end
while 5 + 5 do
print("okay world")
working(man)
end
while also do
i(work(too))
"okay"
end
if yeah then
no(day)
elseif cool(me) then
okay(ya)
else
u(way)
end
if yeah then
no(dad)
else
if cool(you) then
okay(bah)
else
p(way)
end
end
if (function() end)() then
what(ever)
end
if nil then
flip(me)
else
it(be, rad)
end
if things(great) then
no(way)
elseif okay(sure) then
what(here)
end
if things then
no(chance)
elseif okay then
what(now)
end
if things then
yes(man)
elseif okay(person) then
hi(there)
else
hmm(sure)
end
if lets(go) then
print("greetings")
elseif "just us" then
print("will smith")
else
show(5555555)
end