diff --git a/moonscript/compile/line.lua b/moonscript/compile/line.lua index f6f2152..d73be1a 100644 --- a/moonscript/compile/line.lua +++ b/moonscript/compile/line.lua @@ -86,7 +86,7 @@ line_compile = { end, ["return"] = function(self, node) return self:add_line("return", self:value(node[2])) end, ["break"] = function(self, node) return self:add_line("break") end, - ["import"] = function(self, node) + import = function(self, node) local _, names, source = unpack(node) local to_bind = { } local get_name @@ -197,7 +197,7 @@ line_compile = { self:add_line(inner:render()) return self:add_line("end") end, - ["export"] = function(self, node) + export = function(self, node) local _, names = unpack(node) local _item_0 = names for _index_0=1,#_item_0 do @@ -208,7 +208,7 @@ line_compile = { end return nil end, - ["class"] = function(self, node) + class = function(self, node) local _, name, parent_val, tbl = unpack(node) local constructor = nil local final_properties = { } diff --git a/moonscript/compile/line.moon b/moonscript/compile/line.moon index caba9e8..17da8e3 100644 --- a/moonscript/compile/line.moon +++ b/moonscript/compile/line.moon @@ -58,13 +58,13 @@ line_compile = error"unknown op: "..op if not op_final @stm {"assign", {name}, {{"exp", name, op_final, exp}}} - ["return"]: (node) => + return: (node) => @add_line "return", @value node[2] - ["break"]: (node) => + break: (node) => @add_line "break" - ["import"]: (node) => + import: (node) => _, names, source = unpack node to_bind = {} @@ -105,7 +105,7 @@ line_compile = @add_line "end" - ["if"]: (node, ret) => + if: (node, ret) => cond, block = node[2], node[3] add_clause = (clause) -> @@ -131,7 +131,7 @@ line_compile = @add_line "end" - ["while"]: (node) => + while: (node) => _, cond, block = unpack node inner = @block() @@ -146,7 +146,7 @@ line_compile = @add_line inner:render() @add_line "end" - ["for"]: (node) => + for: (node) => _, name, bounds, block = unpack node bounds = @value {"explist", unpack bounds} @add_line "for", @name(name), "=", bounds, "do" @@ -155,12 +155,12 @@ line_compile = @add_line inner:render() @add_line "end" - ["export"]: (node) => + export: (node) => _, names = unpack node @put_name name for name in *names when type(name) == "string" nil - ["class"]: (node) => + class: (node) => _, name, parent_val, tbl = unpack node constructor = nil diff --git a/moonscript/compile2.lua b/moonscript/compile2.lua index 37c31be..3e7d9de 100644 --- a/moonscript/compile2.lua +++ b/moonscript/compile2.lua @@ -129,6 +129,9 @@ local value_compile = { local out if #tuple == 2 then local key, value = unpack(tuple) + if type(key) == "string" and data.lua_keywords[key] then + key = { "string", '"', key } + end local key_val = self:value(key) if type(key) ~= "string" then key = ("[%s]"):format(key_val) diff --git a/moonscript/compile2.moon b/moonscript/compile2.moon index be9c1dc..b4666ac 100644 --- a/moonscript/compile2.moon +++ b/moonscript/compile2.moon @@ -38,7 +38,7 @@ value_compile = _, delim, inner, delim_end = unpack node delim..inner..(delim_end or delim) - ["if"]: (node) => + if: (node) => func = @block() func:stm node, returner @format "(function()", func:render(), "end)()" @@ -113,8 +113,11 @@ value_compile = _comp = (i, tuple) -> out = if #tuple == 2 key, value = unpack tuple - key_val = @value key + if type(key) == "string" and data.lua_keywords[key] + key = {"string", '"', key} + + key_val = @value key key = if type(key) != "string" ("[%s]"):format key_val else @@ -143,7 +146,7 @@ value_compile = length: (node) => "#"..@value node[2] - ["not"]: (node) => + not: (node) => "not "..@value node[2] self: (node) => diff --git a/moonscript/data.lua b/moonscript/data.lua index 49dcde9..1eb670f 100644 --- a/moonscript/data.lua +++ b/moonscript/data.lua @@ -43,3 +43,10 @@ function ntype(node) return node[1] end +lua_keywords = Set{ + 'and', 'break', 'do', 'else', 'elseif', + 'end', 'false', 'for', 'function', 'if', + 'in', 'local', 'nil', 'not', 'or', + 'repeat', 'return', 'then', 'true', + 'until', 'while' +} diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 9c29642..1a515ac 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -177,6 +177,8 @@ local build_grammar = wrap(function() return false end + local SimpleName = Name -- for table key + -- make sure name is not a keyword local Name = Cmt(Name, function(str, pos, name) if keywords[name] then return false end @@ -342,7 +344,7 @@ local build_grammar = wrap(function() ClassDecl = key"class" * Name * (key"extends" * Exp + C"")^-1 * TableBlock / mark"class", Export = key"export" * Ct(NameList) / mark"export", - KeyValue = Ct((Name + sym"[" * Exp * sym"]") * symx":" * (Exp + TableBlock)), + KeyValue = Ct((SimpleName + sym"[" * Exp * sym"]") * symx":" * (Exp + TableBlock)), KeyValueList = KeyValue * (sym"," * KeyValue)^0, KeyValueLine = Cmt(Indent, check_indent) * KeyValueList * sym","^-1, diff --git a/tests/inputs/syntax.moon b/tests/inputs/syntax.moon index e92c4bc..90f9e34 100644 --- a/tests/inputs/syntax.moon +++ b/tests/inputs/syntax.moon @@ -150,3 +150,12 @@ m %= 2 x = 0 (if ntype(v) == "fndef" then x += 1) for v in *values + +hello = + something: world + if: "hello" + else: 3434 + function: "okay" + good: 230203 + + diff --git a/tests/outputs/syntax.lua b/tests/outputs/syntax.lua index 8b2c970..a92df46 100644 --- a/tests/outputs/syntax.lua +++ b/tests/outputs/syntax.lua @@ -111,4 +111,11 @@ for _index_0=1,#_item_0 do return x end end)()) -end \ No newline at end of file +end +local hello = { + something = world, + ["if"] = "hello", + ["else"] = 3434, + ["function"] = "okay", + good = 230203 +} \ No newline at end of file