From 5c2f5b0e0dc97adca2077cf797e9a866be17bc54 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 May 2011 12:19:13 -0700 Subject: [PATCH] amgibuity fix, parens, and some comment support --- moonscript/compile.lua | 22 +++++++++++++++------- moonscript/parse.lua | 11 ++++++----- test.lua | 14 +++++++++++--- tests/funcs.moon | 9 --------- tests/inputs/syntax.moon | 21 +++++++++++++++++++++ tests/outputs/syntax.lua | 19 ++++++++++++++++--- util.lua | 9 +++++++++ 7 files changed, 78 insertions(+), 27 deletions(-) delete mode 100644 tests/funcs.moon diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 30bde85..20bffe1 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -11,7 +11,7 @@ local data = require "moonscript.data" -- end, _G) -- }) -local map, bind = util.map, util.bind +local map, bind, itwos = util.map, util.bind, util.itwos local Stack = data.Stack local indent_char = " " @@ -102,6 +102,14 @@ local compiler_index = { end if inc then self._indent = self._indent - inc end self:pop() + + -- add semicolons where they might be needed + for i, left, k, right in itwos(lines) do + if left:sub(-1) == ")" and right:sub(1,1) == "(" then + lines[i] = lines[i]..";" + end + end + return table.concat(lines, "\n") end, @@ -152,6 +160,11 @@ local compiler_index = { return table.concat(values, " ") end, + parens = function(self, node) + local _, value = unpack(node) + return '('..self:value(value)..')' + end, + string = function(self, node) local _, delim, inner, delim_end = unpack(node) return delim..inner..(delim_end or delim) @@ -189,12 +202,7 @@ end function tree(tree) local compiler = build_compiler() - local buff = {} - for _, line in ipairs(tree) do - table.insert(buff, compiler:value(line)) - end - - return table.concat(buff, "\n") + return compiler:block(tree) end diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 620db57..8832f0c 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -26,12 +26,13 @@ local R, S, V, P = lpeg.R, lpeg.S, lpeg.V, lpeg.P local C, Ct, Cmt, Cg, Cb = lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cg, lpeg.Cb local White = S" \t\n"^0 -local Space = S" \t"^0 -local ASpace = S" \t"^1 +local _Space = S" \t"^0 local Break = S"\n" local Stop = Break + -1 local Indent = C(S"\t "^0) / count_indent +local Comment = P"--" * (1 - S"\n")^0 * #Stop +local Space = _Space * Comment^-1 local Name = Space * C(R("az", "AZ", "__") * R("az", "AZ", "09", "__")^0) local Num = Space * C(R("09")^1) / tonumber @@ -170,8 +171,8 @@ local build_grammar = wrap(function() File, File = Block + Ct"", Block = Ct(Line * (Break^1 * Line)^0), - Line = Cmt(Indent, check_indent) * Statement, - Statement = Ct(If) + Exp, + Line = Cmt(Indent, check_indent) * Statement + _Space * Comment, + Statement = Ct(If) + Exp * Space, Body = Break * InBlock + Ct(Statement), @@ -198,7 +199,7 @@ local build_grammar = wrap(function() LuaStringOpen = sym"[" * P"="^0 * "[" / trim, LuaStringClose = "]" * P"="^0 * "]", - Callable = Name + Parens, + Callable = Name + Parens / mark"parens", Parens = sym"(" * Exp * sym")", -- a list of funcalls and indexs on a callable diff --git a/test.lua b/test.lua index 1d2260f..9ce72c8 100755 --- a/test.lua +++ b/test.lua @@ -1,9 +1,14 @@ require "lfs" +require "alt_getopt" + +local opts, ind = alt_getopt.get_opts(arg, "d:", { }) + +local argv = {} +for i = ind, #arg do table.insert(argv, arg[i]) end -local argv = {...} local action = table.remove(argv, 1) or "run" -local diff_tool = "diff" +local diff_tool = opts.d or "diff" local opts = { in_dir = "tests/inputs", @@ -53,7 +58,10 @@ local actions = { for file in inputs(pattern) do local out_fname = output_name(file) print("Building: ", file, out_fname) - io.open(out_fname, "w"):write(run_file(file)) + local result = run_file(file) + if result then + io.open(out_fname, "w"):write() + end end end, run = function(pattern) diff --git a/tests/funcs.moon b/tests/funcs.moon deleted file mode 100644 index 34620f2..0000000 --- a/tests/funcs.moon +++ /dev/null @@ -1,9 +0,0 @@ - - -return 5 + () -> 4 + 2 - -return 5 + (() -> 4) + 2 - -print 5 + () -> - 34 - good nads diff --git a/tests/inputs/syntax.moon b/tests/inputs/syntax.moon index a7c75f5..a856398 100644 --- a/tests/inputs/syntax.moon +++ b/tests/inputs/syntax.moon @@ -48,3 +48,24 @@ hairy[hands][are](gross) okay okay[world] (get[something] + 5)[years] +i,x = 200, 300 + +yeah = (1 + 5) * 3 +yeah = ((1+5)*3)/2 +yeah = ((1+5)*3)/2 + i % 100 + +whoa = (1+2) * (3+4) * (4+5) + + +(->)() + + +return 5 + () -> 4 + 2 + +return 5 + (() -> 4) + 2 + +print 5 + () -> + 34 + good nads + + diff --git a/tests/outputs/syntax.lua b/tests/outputs/syntax.lua index 7cd93c8..8a289c6 100644 --- a/tests/outputs/syntax.lua +++ b/tests/outputs/syntax.lua @@ -18,12 +18,25 @@ local we = yeah local the, different = function() approach end, yeah dad() dad(lord) -hello(one, two)() +hello(one, two)(); (5 + 5)(world) fun(a)(b) fun(a)(b) fun(a)(b, bad(hello)) hello(world(what(are(you(doing(here)))))) what(the)[3243](world, yeck(heck)) -hairy[hands][are](gross)(okay(okay[world])) -(get[something] + 5)[years] \ No newline at end of file +hairy[hands][are](gross)(okay(okay[world])); +(get[something] + 5)[years] +local i = 200 +x = 300 +local yeah = (1 + 5) * 3 +yeah = ((1 + 5) * 3) / 2 +yeah = ((1 + 5) * 3) / 2 + i % 100 +local whoa = (1 + 2) * (3 + 4) * (4 + 5); +(function() end)() +return(5 + function() 4 + 2 end) +return(5 + (function() 4 end) + 2) +print(5 + function() + 34 + good(nads) +end) \ No newline at end of file diff --git a/util.lua b/util.lua index 43edef1..bc90b9e 100644 --- a/util.lua +++ b/util.lua @@ -24,6 +24,15 @@ function bind(obj, name) end end +function itwos(seq) + n = 2 + return coroutine.wrap(function() + for i = 1, #seq-n+1 do + coroutine.yield(i, seq[i], i+1, seq[i+1]) + end + end) +end + function dump(what) local seen = {} local function _dump(what, depth)