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)
|
-- end, _G)
|
||||||
-- })
|
-- })
|
||||||
|
|
||||||
local map, bind = util.map, util.bind
|
local map, bind, itwos = util.map, util.bind, util.itwos
|
||||||
local Stack = data.Stack
|
local Stack = data.Stack
|
||||||
|
|
||||||
local indent_char = " "
|
local indent_char = " "
|
||||||
@ -102,6 +102,14 @@ local compiler_index = {
|
|||||||
end
|
end
|
||||||
if inc then self._indent = self._indent - inc end
|
if inc then self._indent = self._indent - inc end
|
||||||
self:pop()
|
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")
|
return table.concat(lines, "\n")
|
||||||
end,
|
end,
|
||||||
|
|
||||||
@ -152,6 +160,11 @@ local compiler_index = {
|
|||||||
return table.concat(values, " ")
|
return table.concat(values, " ")
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
parens = function(self, node)
|
||||||
|
local _, value = unpack(node)
|
||||||
|
return '('..self:value(value)..')'
|
||||||
|
end,
|
||||||
|
|
||||||
string = function(self, node)
|
string = function(self, node)
|
||||||
local _, delim, inner, delim_end = unpack(node)
|
local _, delim, inner, delim_end = unpack(node)
|
||||||
return delim..inner..(delim_end or delim)
|
return delim..inner..(delim_end or delim)
|
||||||
@ -189,12 +202,7 @@ end
|
|||||||
|
|
||||||
function tree(tree)
|
function tree(tree)
|
||||||
local compiler = build_compiler()
|
local compiler = build_compiler()
|
||||||
local buff = {}
|
return compiler:block(tree)
|
||||||
for _, line in ipairs(tree) do
|
|
||||||
table.insert(buff, compiler:value(line))
|
|
||||||
end
|
|
||||||
|
|
||||||
return table.concat(buff, "\n")
|
|
||||||
end
|
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 C, Ct, Cmt, Cg, Cb = lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cg, lpeg.Cb
|
||||||
|
|
||||||
local White = S" \t\n"^0
|
local White = S" \t\n"^0
|
||||||
local Space = S" \t"^0
|
local _Space = S" \t"^0
|
||||||
local ASpace = S" \t"^1
|
|
||||||
local Break = S"\n"
|
local Break = S"\n"
|
||||||
local Stop = Break + -1
|
local Stop = Break + -1
|
||||||
local Indent = C(S"\t "^0) / count_indent
|
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 Name = Space * C(R("az", "AZ", "__") * R("az", "AZ", "09", "__")^0)
|
||||||
local Num = Space * C(R("09")^1) / tonumber
|
local Num = Space * C(R("09")^1) / tonumber
|
||||||
@ -170,8 +171,8 @@ local build_grammar = wrap(function()
|
|||||||
File,
|
File,
|
||||||
File = Block + Ct"",
|
File = Block + Ct"",
|
||||||
Block = Ct(Line * (Break^1 * Line)^0),
|
Block = Ct(Line * (Break^1 * Line)^0),
|
||||||
Line = Cmt(Indent, check_indent) * Statement,
|
Line = Cmt(Indent, check_indent) * Statement + _Space * Comment,
|
||||||
Statement = Ct(If) + Exp,
|
Statement = Ct(If) + Exp * Space,
|
||||||
|
|
||||||
Body = Break * InBlock + Ct(Statement),
|
Body = Break * InBlock + Ct(Statement),
|
||||||
|
|
||||||
@ -198,7 +199,7 @@ local build_grammar = wrap(function()
|
|||||||
LuaStringOpen = sym"[" * P"="^0 * "[" / trim,
|
LuaStringOpen = sym"[" * P"="^0 * "[" / trim,
|
||||||
LuaStringClose = "]" * P"="^0 * "]",
|
LuaStringClose = "]" * P"="^0 * "]",
|
||||||
|
|
||||||
Callable = Name + Parens,
|
Callable = Name + Parens / mark"parens",
|
||||||
Parens = sym"(" * Exp * sym")",
|
Parens = sym"(" * Exp * sym")",
|
||||||
|
|
||||||
-- a list of funcalls and indexs on a callable
|
-- a list of funcalls and indexs on a callable
|
||||||
|
14
test.lua
14
test.lua
@ -1,9 +1,14 @@
|
|||||||
require "lfs"
|
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 action = table.remove(argv, 1) or "run"
|
||||||
|
|
||||||
local diff_tool = "diff"
|
local diff_tool = opts.d or "diff"
|
||||||
|
|
||||||
local opts = {
|
local opts = {
|
||||||
in_dir = "tests/inputs",
|
in_dir = "tests/inputs",
|
||||||
@ -53,7 +58,10 @@ local actions = {
|
|||||||
for file in inputs(pattern) do
|
for file in inputs(pattern) do
|
||||||
local out_fname = output_name(file)
|
local out_fname = output_name(file)
|
||||||
print("Building: ", file, out_fname)
|
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
|
||||||
end,
|
end,
|
||||||
run = function(pattern)
|
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]
|
(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
|
local the, different = function() approach end, yeah
|
||||||
dad()
|
dad()
|
||||||
dad(lord)
|
dad(lord)
|
||||||
hello(one, two)()
|
hello(one, two)();
|
||||||
(5 + 5)(world)
|
(5 + 5)(world)
|
||||||
fun(a)(b)
|
fun(a)(b)
|
||||||
fun(a)(b)
|
fun(a)(b)
|
||||||
fun(a)(b, bad(hello))
|
fun(a)(b, bad(hello))
|
||||||
hello(world(what(are(you(doing(here))))))
|
hello(world(what(are(you(doing(here))))))
|
||||||
what(the)[3243](world, yeck(heck))
|
what(the)[3243](world, yeck(heck))
|
||||||
hairy[hands][are](gross)(okay(okay[world]))
|
hairy[hands][are](gross)(okay(okay[world]));
|
||||||
(get[something] + 5)[years]
|
(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)
|
9
util.lua
9
util.lua
@ -24,6 +24,15 @@ function bind(obj, name)
|
|||||||
end
|
end
|
||||||
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)
|
function dump(what)
|
||||||
local seen = {}
|
local seen = {}
|
||||||
local function _dump(what, depth)
|
local function _dump(what, depth)
|
||||||
|
Loading…
Reference in New Issue
Block a user