diff options
author | Li Jin <dragon-fly@qq.com> | 2020-05-02 14:41:40 +0800 |
---|---|---|
committer | Li Jin <dragon-fly@qq.com> | 2020-05-02 14:41:40 +0800 |
commit | 6402a8896c78440aee03cc4b7bb315fc236e6ff8 (patch) | |
tree | a5c23153a2285ae4ae79d9b1ff5b64f39f7c379f /src/MoonP/moon_compiler.cpp | |
parent | 5eac89411f056e4e6ad56c9a41944a341780d68b (diff) | |
download | yuescript-6402a8896c78440aee03cc4b7bb315fc236e6ff8.tar.gz yuescript-6402a8896c78440aee03cc4b7bb315fc236e6ff8.tar.bz2 yuescript-6402a8896c78440aee03cc4b7bb315fc236e6ff8.zip |
fix Moonscript issue 416: ambiguous Lua output in some cases.
Diffstat (limited to 'src/MoonP/moon_compiler.cpp')
-rw-r--r-- | src/MoonP/moon_compiler.cpp | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index f79df07..6d07e9e 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp | |||
@@ -43,7 +43,7 @@ inline std::string s(std::string_view sv) { | |||
43 | } | 43 | } |
44 | 44 | ||
45 | const std::string_view version() { | 45 | const std::string_view version() { |
46 | return "0.3.11"sv; | 46 | return "0.3.12"sv; |
47 | } | 47 | } |
48 | 48 | ||
49 | // name of table stored in lua registry | 49 | // name of table stored in lua registry |
@@ -802,6 +802,25 @@ private: | |||
802 | } | 802 | } |
803 | default: assert(false); break; | 803 | default: assert(false); break; |
804 | } | 804 | } |
805 | if (statement->needSep && !out.back().empty()) { | ||
806 | auto index = std::string::npos; | ||
807 | if (_config.reserveLineNumber) { | ||
808 | index = out.back().rfind(" -- "sv); | ||
809 | } else { | ||
810 | index = out.back().find_last_not_of('\n'); | ||
811 | if (index != std::string::npos) index++; | ||
812 | } | ||
813 | if (index != std::string::npos) { | ||
814 | auto ending = out.back().substr(0, index); | ||
815 | auto ind = ending.find_last_of(" \t\n"sv); | ||
816 | if (ind != std::string::npos) { | ||
817 | ending = ending.substr(ind + 1); | ||
818 | } | ||
819 | if (Keywords.find(ending) == Keywords.end()) { | ||
820 | out.back().insert(index, ";"sv); | ||
821 | } | ||
822 | } | ||
823 | } | ||
805 | } | 824 | } |
806 | 825 | ||
807 | str_list getAssignVars(ExpListAssign_t* assignment) { | 826 | str_list getAssignVars(ExpListAssign_t* assignment) { |
@@ -2016,6 +2035,11 @@ private: | |||
2016 | returnNode->valueList.set(expListLow); | 2035 | returnNode->valueList.set(expListLow); |
2017 | returnNode->allowBlockMacroReturn = true; | 2036 | returnNode->allowBlockMacroReturn = true; |
2018 | last->content.set(returnNode); | 2037 | last->content.set(returnNode); |
2038 | last->needSep.set(nullptr); | ||
2039 | auto bLast = ++nodes.rbegin(); | ||
2040 | if (bLast != nodes.rend()) { | ||
2041 | static_cast<Statement_t*>(*bLast)->needSep.set(nullptr); | ||
2042 | } | ||
2019 | BLOCK_END | 2043 | BLOCK_END |
2020 | break; | 2044 | break; |
2021 | } | 2045 | } |
@@ -2041,6 +2065,11 @@ private: | |||
2041 | } | 2065 | } |
2042 | newAssignment->action.set(assign); | 2066 | newAssignment->action.set(assign); |
2043 | last->content.set(newAssignment); | 2067 | last->content.set(newAssignment); |
2068 | last->needSep.set(nullptr); | ||
2069 | auto bLast = ++nodes.rbegin(); | ||
2070 | if (bLast != nodes.rend()) { | ||
2071 | static_cast<Statement_t*>(*bLast)->needSep.set(nullptr); | ||
2072 | } | ||
2044 | } | 2073 | } |
2045 | break; | 2074 | break; |
2046 | } | 2075 | } |
@@ -4029,7 +4058,9 @@ private: | |||
4029 | _buf << indent(1) << "end"sv << nll(classDecl); | 4058 | _buf << indent(1) << "end"sv << nll(classDecl); |
4030 | _buf << indent() << "})"sv << nll(classDecl); | 4059 | _buf << indent() << "})"sv << nll(classDecl); |
4031 | _buf << indent() << baseVar << ".__class = "sv << classVar << nll(classDecl); | 4060 | _buf << indent() << baseVar << ".__class = "sv << classVar << nll(classDecl); |
4032 | if (!statements.empty()) _buf << indent() << "local self = "sv << classVar << nll(classDecl); | 4061 | if (!statements.empty()) { |
4062 | _buf << indent() << "local self = "sv << classVar << ';' << nll(classDecl); | ||
4063 | } | ||
4033 | _buf << join(statements); | 4064 | _buf << join(statements); |
4034 | if (extend) { | 4065 | if (extend) { |
4035 | _buf << indent() << "if "sv << parentVar << ".__inherited then"sv << nll(classDecl); | 4066 | _buf << indent() << "if "sv << parentVar << ".__inherited then"sv << nll(classDecl); |