From ccb3381cdfc84a4dd37f5b7aea9ff21d6558c14b Mon Sep 17 00:00:00 2001 From: Li Jin Date: Mon, 13 Jan 2020 13:09:24 +0800 Subject: fix Moonscript issue 384. --- src/MoonP/moon_compiler.cpp | 53 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index f9a86a6..e015939 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp @@ -437,7 +437,7 @@ private: auto callable = ast_cast(chainValue->items.front()); BREAK_IF(!callable || !callable->item.is()); str_list tmp; - transformCallable(callable, tmp, false); + transformCallable(callable, tmp); return tmp.back(); BLOCK_END return Empty; @@ -1552,7 +1552,7 @@ private: } } - void transformCallable(Callable_t* callable, str_list& out, bool invoke) { + void transformCallable(Callable_t* callable, str_list& out, const ast_sel& invoke = {}) { auto item = callable->item.get(); switch (item->getId()) { case "Variable"_id: { @@ -1564,7 +1564,8 @@ private: } break; } - case "SelfName"_id: { transformSelfName(static_cast(item), out, invoke); + case "SelfName"_id: { + transformSelfName(static_cast(item), out, invoke); if (_config.lintGlobalVariable) { std::string self("self"sv); if (!isDefined(self)) { @@ -1938,12 +1939,26 @@ private: out.push_back(initCodes); } - void transformSelfName(SelfName_t* selfName, str_list& out, bool invoke) { + void transformSelfName(SelfName_t* selfName, str_list& out, const ast_sel& invoke = {}) { + auto x = selfName; auto name = selfName->name.get(); switch (name->getId()) { case "self_class_name"_id: { auto clsName = static_cast(name); - out.push_back(s("self.__class"sv) + s(invoke ? ":"sv : "."sv) + toString(clsName->name)); + auto nameStr = toString(clsName->name); + if (State::luaKeywords.find(nameStr) != State::luaKeywords.end()) { + out.push_back(s("self.__class[\""sv) + nameStr + s("\"]")); + if (invoke) { + if (auto invokePtr = invoke.as()) { + invokePtr->args.push_front(toAst("self.__class"sv, Exp, x)); + } else { + auto invokeArgsPtr = invoke.as(); + invokeArgsPtr->args.push_front(toAst("self.__class"sv, Exp, x)); + } + } + } else { + out.push_back(s("self.__class"sv) + s(invoke ? ":"sv : "."sv) + nameStr); + } break; } case "self_class"_id: @@ -1951,7 +1966,20 @@ private: break; case "self_name"_id: { auto sfName = static_cast(name); - out.push_back(s("self"sv) + s(invoke ? ":"sv : "."sv) + toString(sfName->name)); + auto nameStr = toString(sfName->name); + if (State::luaKeywords.find(nameStr) != State::luaKeywords.end()) { + out.push_back(s("self[\""sv) + nameStr + s("\"]")); + if (invoke) { + if (auto invokePtr = invoke.as()) { + invokePtr->args.push_front(toAst("self"sv, Exp, x)); + } else { + auto invokeArgsPtr = invoke.as(); + invokeArgsPtr->args.push_front(toAst("self"sv, Exp, x)); + } + } + } else { + out.push_back(s("self"sv) + s(invoke ? ":"sv : "."sv) + nameStr); + } break; } case "self"_id: @@ -2191,8 +2219,11 @@ private: case "Callable"_id: { auto next = it; ++next; auto followItem = next != chainList.end() ? *next : nullptr; - transformCallable(static_cast(item), temp, - followItem && ast_is(followItem)); + ast_sel invoke; + if (ast_is(followItem)) { + invoke.set(followItem); + } + transformCallable(static_cast(item), temp, invoke); break; } case "String"_id: @@ -2440,7 +2471,7 @@ private: endWithSlice = true; if (listVar.empty() && chainList.size() == 2 && ast_is(chainList.front())) { - transformCallable(static_cast(chainList.front()), temp, false); + transformCallable(static_cast(chainList.front()), temp); listVar = temp.back(); temp.pop_back(); } @@ -2796,7 +2827,7 @@ private: void transformKeyName(KeyName_t* keyName, str_list& out) { auto name = keyName->name.get(); switch (name->getId()) { - case "SelfName"_id: transformSelfName(static_cast(name), out, false); break; + case "SelfName"_id: transformSelfName(static_cast(name), out); break; case "Name"_id: out.push_back(toString(name)); break; default: break; } @@ -3223,7 +3254,7 @@ private: switch (item->getId()) { case "AssignableChain"_id: transformAssignableChain(static_cast(item), out); break; case "Variable"_id: transformVariable(static_cast(item), out); break; - case "SelfName"_id: transformSelfName(static_cast(item), out, false); break; + case "SelfName"_id: transformSelfName(static_cast(item), out); break; default: break; } } -- cgit v1.2.3-55-g6feb