some table syntax, fixed tests

This commit is contained in:
leaf corcoran 2011-05-22 19:28:25 -07:00
parent 5c2f5b0e0d
commit 66b20ef1ce
7 changed files with 122 additions and 10 deletions

View File

@ -25,6 +25,10 @@ local compiler_index = {
push = function(self) self._scope:push{} end, push = function(self) self._scope:push{} end,
pop = function(self) self._scope:pop() end, pop = function(self) self._scope:pop() end,
indent = function(self, amount)
self._indent = self._indent + amount
end,
has_name = function(self, name) has_name = function(self, name)
for i = #self._scope,1,-1 do for i = #self._scope,1,-1 do
if self._scope[i][name] then return true end if self._scope[i][name] then return true end
@ -94,13 +98,14 @@ local compiler_index = {
block = function(self, node, inc) block = function(self, node, inc)
self:push() self:push()
if inc then self._indent = self._indent + inc end if inc then self:indent(inc) end
local lines = {} local lines = {}
local i = self:ichar() local i = self:ichar()
for _, ln in ipairs(node) do for _, ln in ipairs(node) do
table.insert(lines, i..self:value(ln)) table.insert(lines, i..self:value(ln))
end end
if inc then self._indent = self._indent - inc end if inc then self:indent(-inc) end
self:pop() self:pop()
-- add semicolons where they might be needed -- add semicolons where they might be needed
@ -113,6 +118,29 @@ local compiler_index = {
return table.concat(lines, "\n") return table.concat(lines, "\n")
end, 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) assign = function(self, node)
local _, names, values = unpack(node) local _, names, values = unpack(node)
local assigns, current = {}, nil local assigns, current = {}, nil
@ -174,7 +202,7 @@ local compiler_index = {
if type(node) == "table" then if type(node) == "table" then
local fn = self[node[1]] local fn = self[node[1]]
if not fn then if not fn then
error("Unknown op: "..tostring(op)) error("Unknown op: "..tostring(node[1]))
end end
return fn(self, node) return fn(self, node)
end end

View File

@ -143,6 +143,11 @@ local build_grammar = wrap(function()
return {"chain", callee, args} return {"chain", callee, args}
end end
local function wrap_func_arg(value)
return {"call", {value}}
end
-- makes sure the last item in a chain is an index -- makes sure the last item in a chain is an index
local function check_assignable(str, pos, value) local function check_assignable(str, pos, value)
if ntype(value) == "chain" and ntype(value[#value]) == "index" if ntype(value) == "chain" and ntype(value[#value]) == "index"
@ -159,8 +164,8 @@ local build_grammar = wrap(function()
return true, name return true, name
end) end)
local function simple_string(delim) local function simple_string(delim, x)
return sym(delim) / trim * C((P('\\'..delim) + (1 - S('\n'..delim)))^0) * sym(delim) / mark"string" return C(symx(delim)) * C((P('\\'..delim) + (1 - S('\n'..delim)))^0) * sym(delim) / mark"string"
end end
local function check_lua_string(str, pos, right, left) 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", 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, Assignable = Cmt(Chain, check_assignable) + Name,
AssignableList = Assignable * (sym"," * Assignable)^0, 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, 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 * LuaString = Cg(LuaStringOpen, "string_open") * Cb"string_open" * P"\n"^-1 *
C((1 - Cmt(C(LuaStringClose) * Cb"string_open", check_lua_string))^0) * 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")", Parens = sym"(" * Exp * sym")",
-- a list of funcalls and indexs on a callable -- 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", 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", FunLit = (sym"(" * Ct(NameList^-1) * sym")" + Ct("")) * sym"->" * (Body + Ct"") / mark"fndef",
NameList = Name * (sym"," * Name)^0, NameList = Name * (sym"," * Name)^0,

View File

@ -60,7 +60,7 @@ local actions = {
print("Building: ", file, out_fname) print("Building: ", file, out_fname)
local result = run_file(file) local result = run_file(file)
if result then if result then
io.open(out_fname, "w"):write() io.open(out_fname, "w"):write(result)
end end
end end
end, end,

View File

@ -69,3 +69,10 @@ print 5 + () ->
good nads good nads
something 'else', "ya"
something'else'
something"else"
here(we)"go"[12123]

31
tests/inputs/tables.moon Normal file
View 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

View File

@ -40,3 +40,7 @@ print(5 + function()
34 34
good(nads) good(nads)
end) end)
something('else', "ya")
something('else')
something("else")
here(we)("go")[12123]

24
tests/outputs/tables.lua Normal file
View 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
}