mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
amgibuity fix, parens, and some comment support
This commit is contained in:
parent
9703146193
commit
5c2f5b0e0d
@ -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
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
14
test.lua
14
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)
|
||||
|
@ -1,9 +0,0 @@
|
||||
|
||||
|
||||
return 5 + () -> 4 + 2
|
||||
|
||||
return 5 + (() -> 4) + 2
|
||||
|
||||
print 5 + () ->
|
||||
34
|
||||
good nads
|
@ -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
|
||||
|
||||
|
||||
|
@ -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]
|
||||
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)
|
Loading…
Reference in New Issue
Block a user