mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
some table syntax, fixed tests
This commit is contained in:
parent
5c2f5b0e0d
commit
66b20ef1ce
@ -25,6 +25,10 @@ local compiler_index = {
|
||||
push = function(self) self._scope:push{} end,
|
||||
pop = function(self) self._scope:pop() end,
|
||||
|
||||
indent = function(self, amount)
|
||||
self._indent = self._indent + amount
|
||||
end,
|
||||
|
||||
has_name = function(self, name)
|
||||
for i = #self._scope,1,-1 do
|
||||
if self._scope[i][name] then return true end
|
||||
@ -94,13 +98,14 @@ local compiler_index = {
|
||||
|
||||
block = function(self, node, inc)
|
||||
self:push()
|
||||
if inc then self._indent = self._indent + inc end
|
||||
if inc then self:indent(inc) end
|
||||
|
||||
local lines = {}
|
||||
local i = self:ichar()
|
||||
for _, ln in ipairs(node) do
|
||||
table.insert(lines, i..self:value(ln))
|
||||
end
|
||||
if inc then self._indent = self._indent - inc end
|
||||
if inc then self:indent(-inc) end
|
||||
self:pop()
|
||||
|
||||
-- add semicolons where they might be needed
|
||||
@ -113,6 +118,29 @@ local compiler_index = {
|
||||
return table.concat(lines, "\n")
|
||||
end,
|
||||
|
||||
table = function(self, node)
|
||||
local _, items = unpack(node)
|
||||
|
||||
self:indent(1)
|
||||
local item_values = {}
|
||||
for _, item in ipairs(items) do
|
||||
local key = self:value(item[1])
|
||||
if type(item[1]) ~= "string" then
|
||||
key = ("[%s]"):format(key)
|
||||
end
|
||||
|
||||
table.insert(item_values, key.." = "..self:value(item[2]))
|
||||
end
|
||||
local i = self:ichar()
|
||||
self:indent(-1)
|
||||
|
||||
if #item_values > 3 then
|
||||
return ("{\n%s%s\n%s}"):format(i, table.concat(item_values, ",\n"..i), self:ichar())
|
||||
end
|
||||
|
||||
return "{ "..table.concat(item_values, ", ").." }"
|
||||
end,
|
||||
|
||||
assign = function(self, node)
|
||||
local _, names, values = unpack(node)
|
||||
local assigns, current = {}, nil
|
||||
@ -174,7 +202,7 @@ local compiler_index = {
|
||||
if type(node) == "table" then
|
||||
local fn = self[node[1]]
|
||||
if not fn then
|
||||
error("Unknown op: "..tostring(op))
|
||||
error("Unknown op: "..tostring(node[1]))
|
||||
end
|
||||
return fn(self, node)
|
||||
end
|
||||
|
@ -143,6 +143,11 @@ local build_grammar = wrap(function()
|
||||
return {"chain", callee, args}
|
||||
end
|
||||
|
||||
local function wrap_func_arg(value)
|
||||
return {"call", {value}}
|
||||
end
|
||||
|
||||
|
||||
-- makes sure the last item in a chain is an index
|
||||
local function check_assignable(str, pos, value)
|
||||
if ntype(value) == "chain" and ntype(value[#value]) == "index"
|
||||
@ -159,8 +164,8 @@ local build_grammar = wrap(function()
|
||||
return true, name
|
||||
end)
|
||||
|
||||
local function simple_string(delim)
|
||||
return sym(delim) / trim * C((P('\\'..delim) + (1 - S('\n'..delim)))^0) * sym(delim) / mark"string"
|
||||
local function simple_string(delim, x)
|
||||
return C(symx(delim)) * C((P('\\'..delim) + (1 - S('\n'..delim)))^0) * sym(delim) / mark"string"
|
||||
end
|
||||
|
||||
local function check_lua_string(str, pos, right, left)
|
||||
@ -181,7 +186,7 @@ local build_grammar = wrap(function()
|
||||
|
||||
If = key"if" * Exp * Body / mark"if",
|
||||
|
||||
Assign = Ct(AssignableList) * sym"=" * Ct(ExpList) / mark"assign",
|
||||
Assign = Ct(AssignableList) * sym"=" * Ct(TableBlock + ExpList) / mark"assign",
|
||||
|
||||
Assignable = Cmt(Chain, check_assignable) + Name,
|
||||
AssignableList = Assignable * (sym"," * Assignable)^0,
|
||||
@ -190,7 +195,9 @@ local build_grammar = wrap(function()
|
||||
|
||||
Value = Assign + FunLit + String + (Chain + Callable) * Ct(ExpList^0) / flatten_func + Num,
|
||||
|
||||
String = simple_string("'") + simple_string('"') + LuaString,
|
||||
String = Space * DoubleString + Space * SingleString + LuaString,
|
||||
SingleString = simple_string("'"),
|
||||
DoubleString = simple_string('"'),
|
||||
|
||||
LuaString = Cg(LuaStringOpen, "string_open") * Cb"string_open" * P"\n"^-1 *
|
||||
C((1 - Cmt(C(LuaStringClose) * Cb"string_open", check_lua_string))^0) *
|
||||
@ -203,10 +210,21 @@ local build_grammar = wrap(function()
|
||||
Parens = sym"(" * Exp * sym")",
|
||||
|
||||
-- a list of funcalls and indexs on a callable
|
||||
Chain = Callable * (symx"(" * Ct(ExpList^-1)/mark"call" * sym")" + symx"[" * Exp/mark"index" * sym"]")^1 / mark"chain",
|
||||
Chain = Callable * (symx"(" * Ct(ExpList^-1)/mark"call" * sym")" +
|
||||
SingleString / wrap_func_arg +
|
||||
DoubleString / wrap_func_arg +
|
||||
symx"[" * Exp/mark"index" * sym"]"
|
||||
)^1 / mark"chain",
|
||||
|
||||
TableLit = sym"{" * Ct(ExpList^-1) * sym"}" / mark"list",
|
||||
|
||||
TableBlockInner = Ct(KeyValueList * (Break^1 * KeyValueList)^0),
|
||||
|
||||
TableBlock = Break * #Cmt(Indent, advance_indent) * TableBlockInner * OutBlock / mark"table",
|
||||
|
||||
KeyValue = Ct((Name + sym"[" * Exp * sym"]") * symx":" * (Exp + TableBlock)),
|
||||
KeyValueList = Cmt(Indent, check_indent) * KeyValue * (sym"," * KeyValue)^0 * sym","^-1,
|
||||
|
||||
FunLit = (sym"(" * Ct(NameList^-1) * sym")" + Ct("")) * sym"->" * (Body + Ct"") / mark"fndef",
|
||||
|
||||
NameList = Name * (sym"," * Name)^0,
|
||||
|
2
test.lua
2
test.lua
@ -60,7 +60,7 @@ local actions = {
|
||||
print("Building: ", file, out_fname)
|
||||
local result = run_file(file)
|
||||
if result then
|
||||
io.open(out_fname, "w"):write()
|
||||
io.open(out_fname, "w"):write(result)
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
@ -69,3 +69,10 @@ print 5 + () ->
|
||||
good nads
|
||||
|
||||
|
||||
something 'else', "ya"
|
||||
|
||||
something'else'
|
||||
something"else"
|
||||
|
||||
here(we)"go"[12123]
|
||||
|
||||
|
31
tests/inputs/tables.moon
Normal file
31
tests/inputs/tables.moon
Normal file
@ -0,0 +1,31 @@
|
||||
|
||||
backpack =
|
||||
something:
|
||||
yeah: 200
|
||||
they: ->
|
||||
print "hello"
|
||||
yor_feet"small"
|
||||
pretty: hair
|
||||
gold: hmm
|
||||
yow: 1000
|
||||
|
||||
eat: goo
|
||||
yeah: dudd
|
||||
|
||||
|
||||
start =
|
||||
something: "cold"
|
||||
|
||||
bathe =
|
||||
on: "fire"
|
||||
|
||||
another =
|
||||
[4]: 232
|
||||
["good food"]: "is the best"
|
||||
|
||||
fwip =
|
||||
something: hello"what", number: 2323,
|
||||
what: yo "momma", "yeah",
|
||||
fruit: basket
|
||||
nuts: day
|
||||
|
@ -39,4 +39,8 @@ return(5 + (function() 4 end) + 2)
|
||||
print(5 + function()
|
||||
34
|
||||
good(nads)
|
||||
end)
|
||||
end)
|
||||
something('else', "ya")
|
||||
something('else')
|
||||
something("else")
|
||||
here(we)("go")[12123]
|
24
tests/outputs/tables.lua
Normal file
24
tests/outputs/tables.lua
Normal file
@ -0,0 +1,24 @@
|
||||
local backpack = {
|
||||
something = {
|
||||
yeah = 200,
|
||||
they = function()
|
||||
print("hello")
|
||||
yor_feet("small")
|
||||
end,
|
||||
pretty = hair,
|
||||
gold = hmm
|
||||
},
|
||||
yow = 1000,
|
||||
eat = goo,
|
||||
yeah = dudd
|
||||
}
|
||||
local start = { something = "cold" }
|
||||
local bathe = { on = "fire" }
|
||||
local another = { [4] = 232, ["good food"] = "is the best" }
|
||||
local fwip = {
|
||||
something = hello("what"),
|
||||
number = 2323,
|
||||
what = yo("momma", "yeah"),
|
||||
fruit = basket,
|
||||
nuts = day
|
||||
}
|
Loading…
Reference in New Issue
Block a user