From c70b298b680b48da779d43b0c66c803e2326db99 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 23 May 2011 01:16:49 -0700 Subject: [PATCH] conds and while --- moonscript/compile.lua | 34 ++++++++++++++- moonscript/parse.lua | 8 +++- tests/inputs/cond.moon | 63 +++++++++++++++++++++++++++ tests/outputs/cond.lua | 98 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+), 4 deletions(-) create mode 100644 tests/inputs/cond.moon create mode 100644 tests/outputs/cond.lua diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 42ecdef..d36afdc 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -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) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index c5e36c7..7861aee 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -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", diff --git a/tests/inputs/cond.moon b/tests/inputs/cond.moon new file mode 100644 index 0000000..6689160 --- /dev/null +++ b/tests/inputs/cond.moon @@ -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 + + diff --git a/tests/outputs/cond.lua b/tests/outputs/cond.lua new file mode 100644 index 0000000..9230d15 --- /dev/null +++ b/tests/outputs/cond.lua @@ -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 \ No newline at end of file