From 1ba34563f99e3e8a9d6685a0c84c3970a4885ede Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 4 Nov 2022 13:07:16 -0700 Subject: [PATCH] allow lua keyword property access on self #410 --- moonscript/cmd/lint.lua | 3 ++- moonscript/compile/value.lua | 38 +++++++++++++++++++++++++++++++++-- moonscript/compile/value.moon | 14 +++++++++++-- spec/inputs/syntax.moon | 3 +++ spec/outputs/syntax.lua | 2 ++ 5 files changed, 55 insertions(+), 5 deletions(-) diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index 8f036ff..68053ca 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -76,7 +76,8 @@ do _continue_0 = true break end - names_by_position[pos] = names_by_position[pos] or { } + local _update_0 = pos + names_by_position[_update_0] = names_by_position[_update_0] or { } insert(names_by_position[pos], name) _continue_0 = true until true diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index ce20dad..913f965 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -296,10 +296,44 @@ return { return self:line("not ", self:value(node[2])) end, self = function(self, node) - return "self." .. self:name(node[2]) + if data.lua_keywords[node[2]] then + return self:value({ + "chain", + "self", + { + "index", + { + "string", + '"', + node[2] + } + } + }) + else + return "self." .. self:name(node[2]) + end end, self_class = function(self, node) - return "self.__class." .. self:name(node[2]) + if data.lua_keywords[node[2]] then + return self:value({ + "chain", + "self", + { + "dot", + "__class" + }, + { + "index", + { + "string", + '"', + node[2] + } + } + }) + else + return "self.__class." .. self:name(node[2]) + end end, self_colon = function(self, node) return "self:" .. self:name(node[2]) diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 7cfdda4..0c07898 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -183,10 +183,20 @@ string_chars = { @line "not ", @value node[2] self: (node) => - "self."..@name node[2] + if data.lua_keywords[node[2]] + @value {"chain", "self", {"index", { + "string", '"', node[2] + }}} + else + "self."..@name node[2] self_class: (node) => - "self.__class."..@name node[2] + if data.lua_keywords[node[2]] + @value {"chain", "self", {"dot", "__class"}, {"index", { + "string", '"', node[2] + }}} + else + "self.__class."..@name node[2] self_colon: (node) => "self:"..@name node[2] diff --git a/spec/inputs/syntax.moon b/spec/inputs/syntax.moon index 854f629..b5b92a8 100644 --- a/spec/inputs/syntax.moon +++ b/spec/inputs/syntax.moon @@ -158,6 +158,9 @@ hello ..= "world" @@something += 10 @something += 10 +@@then += 10 +@then += 10 + a["hello"] += 10 a["hello#{tostring ff}"] += 10 a[four].x += 10 diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index 52c5d88..8667f34 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua @@ -162,6 +162,8 @@ local m = m % 2 local hello = hello .. "world" self.__class.something = self.__class.something + 10 self.something = self.something + 10 +self.__class["then"] = self.__class["then"] + 10 +self["then"] = self["then"] + 10 local _update_0 = "hello" a[_update_0] = a[_update_0] + 10 local _update_1 = "hello" .. tostring(tostring(ff))