diff options
| author | Li Jin <dragon-fly@qq.com> | 2020-02-18 13:14:42 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2020-02-18 13:14:42 +0800 |
| commit | 0eaea3f979ef46fe195649ee18400f014957db26 (patch) | |
| tree | df63ea1ac9806bf0c501c96d28dd382f7139cceb | |
| parent | adfea5f0eee289fe729c2bcc68105417d77a8407 (diff) | |
| download | yuescript-0eaea3f979ef46fe195649ee18400f014957db26.tar.gz yuescript-0eaea3f979ef46fe195649ee18400f014957db26.tar.bz2 yuescript-0eaea3f979ef46fe195649ee18400f014957db26.zip | |
did some clean up.
| -rw-r--r-- | src/MoonP/ast.hpp | 2 | ||||
| -rw-r--r-- | src/MoonP/moon_ast.h | 6 | ||||
| -rw-r--r-- | src/MoonP/moon_compiler.cpp | 37 |
3 files changed, 22 insertions, 23 deletions
diff --git a/src/MoonP/ast.hpp b/src/MoonP/ast.hpp index d576395..104202d 100644 --- a/src/MoonP/ast.hpp +++ b/src/MoonP/ast.hpp | |||
| @@ -97,7 +97,7 @@ public: | |||
| 97 | virtual int getId() const = 0; | 97 | virtual int getId() const = 0; |
| 98 | 98 | ||
| 99 | template<class T> | 99 | template<class T> |
| 100 | inline ast_ptr<false, T> new_ptr() { | 100 | inline ast_ptr<false, T> new_ptr() const { |
| 101 | auto item = new T; | 101 | auto item = new T; |
| 102 | item->m_begin.m_line = m_begin.m_line; | 102 | item->m_begin.m_line = m_begin.m_line; |
| 103 | item->m_end.m_line = m_begin.m_line; | 103 | item->m_end.m_line = m_begin.m_line; |
diff --git a/src/MoonP/moon_ast.h b/src/MoonP/moon_ast.h index 26accbf..917429e 100644 --- a/src/MoonP/moon_ast.h +++ b/src/MoonP/moon_ast.h | |||
| @@ -13,18 +13,18 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI | |||
| 13 | namespace parserlib { | 13 | namespace parserlib { |
| 14 | 14 | ||
| 15 | #define AST_LEAF(type) \ | 15 | #define AST_LEAF(type) \ |
| 16 | COUNTER_INC;\ | 16 | COUNTER_INC; \ |
| 17 | class type##_t : public ast_node \ | 17 | class type##_t : public ast_node \ |
| 18 | { \ | 18 | { \ |
| 19 | public: \ | 19 | public: \ |
| 20 | virtual int getId() const override { return COUNTER_READ; } | 20 | virtual int getId() const override { return COUNTER_READ; } |
| 21 | 21 | ||
| 22 | #define AST_NODE(type) \ | 22 | #define AST_NODE(type) \ |
| 23 | COUNTER_INC;\ | 23 | COUNTER_INC; \ |
| 24 | class type##_t : public ast_container \ | 24 | class type##_t : public ast_container \ |
| 25 | { \ | 25 | { \ |
| 26 | public: \ | 26 | public: \ |
| 27 | virtual int getId() const override { return COUNTER_READ; } \ | 27 | virtual int getId() const override { return COUNTER_READ; } |
| 28 | 28 | ||
| 29 | #define AST_MEMBER(type, ...) \ | 29 | #define AST_MEMBER(type, ...) \ |
| 30 | type##_t() { \ | 30 | type##_t() { \ |
diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp index 23ce6ce..4596542 100644 --- a/src/MoonP/moon_compiler.cpp +++ b/src/MoonP/moon_compiler.cpp | |||
| @@ -488,7 +488,7 @@ private: | |||
| 488 | return true; | 488 | return true; |
| 489 | case id<SimpleValue_t>(): { | 489 | case id<SimpleValue_t>(): { |
| 490 | auto simpleValue = static_cast<SimpleValue_t*>(item); | 490 | auto simpleValue = static_cast<SimpleValue_t*>(item); |
| 491 | if (simpleValue->value. is<TableLit_t>()) { | 491 | if (simpleValue->value.is<TableLit_t>()) { |
| 492 | return true; | 492 | return true; |
| 493 | } | 493 | } |
| 494 | return false; | 494 | return false; |
| @@ -1020,7 +1020,7 @@ private: | |||
| 1020 | 1020 | ||
| 1021 | std::list<DestructItem> destructFromExp(ast_node* node) { | 1021 | std::list<DestructItem> destructFromExp(ast_node* node) { |
| 1022 | const node_container* tableItems = nullptr; | 1022 | const node_container* tableItems = nullptr; |
| 1023 | if (ast_cast<Exp_t>(node)) { | 1023 | if (ast_is<Exp_t>(node)) { |
| 1024 | auto item = singleValueFrom(node)->item.get(); | 1024 | auto item = singleValueFrom(node)->item.get(); |
| 1025 | if (!item) throw std::logic_error(_info.errorMessage("Invalid destructure value."sv, node)); | 1025 | if (!item) throw std::logic_error(_info.errorMessage("Invalid destructure value."sv, node)); |
| 1026 | auto tbA = item->getByPath<TableLit_t>(); | 1026 | auto tbA = item->getByPath<TableLit_t>(); |
| @@ -1044,7 +1044,7 @@ private: | |||
| 1044 | } | 1044 | } |
| 1045 | auto value = singleValueFrom(pair); | 1045 | auto value = singleValueFrom(pair); |
| 1046 | auto item = value->item.get(); | 1046 | auto item = value->item.get(); |
| 1047 | if (ast_cast<simple_table_t>(item) || | 1047 | if (ast_is<simple_table_t>(item) || |
| 1048 | item->getByPath<TableLit_t>()) { | 1048 | item->getByPath<TableLit_t>()) { |
| 1049 | auto subPairs = destructFromExp(pair); | 1049 | auto subPairs = destructFromExp(pair); |
| 1050 | for (auto& p : subPairs) { | 1050 | for (auto& p : subPairs) { |
| @@ -1088,7 +1088,7 @@ private: | |||
| 1088 | if (auto exp = np->value.as<Exp_t>()) { | 1088 | if (auto exp = np->value.as<Exp_t>()) { |
| 1089 | if (!isAssignable(exp)) throw std::logic_error(_info.errorMessage("Can't destructure value."sv, exp)); | 1089 | if (!isAssignable(exp)) throw std::logic_error(_info.errorMessage("Can't destructure value."sv, exp)); |
| 1090 | auto item = singleValueFrom(exp)->item.get(); | 1090 | auto item = singleValueFrom(exp)->item.get(); |
| 1091 | if (ast_cast<simple_table_t>(item) || | 1091 | if (ast_is<simple_table_t>(item) || |
| 1092 | item->getByPath<TableLit_t>()) { | 1092 | item->getByPath<TableLit_t>()) { |
| 1093 | auto subPairs = destructFromExp(exp); | 1093 | auto subPairs = destructFromExp(exp); |
| 1094 | auto name = _parser.toString(key); | 1094 | auto name = _parser.toString(key); |
| @@ -1129,7 +1129,7 @@ private: | |||
| 1129 | } | 1129 | } |
| 1130 | break; | 1130 | break; |
| 1131 | } | 1131 | } |
| 1132 | if (np->value.as<TableBlock_t>()) { | 1132 | if (np->value.is<TableBlock_t>()) { |
| 1133 | auto subPairs = destructFromExp(pair); | 1133 | auto subPairs = destructFromExp(pair); |
| 1134 | for (auto& p : subPairs) { | 1134 | for (auto& p : subPairs) { |
| 1135 | pairs.push_back({p.isVariable, p.name, | 1135 | pairs.push_back({p.isVariable, p.name, |
| @@ -1367,7 +1367,7 @@ private: | |||
| 1367 | auto var = singleVariableFrom(exp); | 1367 | auto var = singleVariableFrom(exp); |
| 1368 | if (var.empty()) { | 1368 | if (var.empty()) { |
| 1369 | storingValue = true; | 1369 | storingValue = true; |
| 1370 | auto desVar = getUnusedName("_des_"); | 1370 | auto desVar = getUnusedName("_des_"sv); |
| 1371 | if (assign->values.objects().size() == 1) { | 1371 | if (assign->values.objects().size() == 1) { |
| 1372 | auto var = singleVariableFrom(assign->values.objects().front()); | 1372 | auto var = singleVariableFrom(assign->values.objects().front()); |
| 1373 | if (!var.empty()) { | 1373 | if (!var.empty()) { |
| @@ -2016,7 +2016,6 @@ private: | |||
| 2016 | arg.name = "self.__class"sv; | 2016 | arg.name = "self.__class"sv; |
| 2017 | break; | 2017 | break; |
| 2018 | case id<self_name_t>(): { | 2018 | case id<self_name_t>(): { |
| 2019 | |||
| 2020 | auto sfName = static_cast<self_name_t*>(selfName->name.get()); | 2019 | auto sfName = static_cast<self_name_t*>(selfName->name.get()); |
| 2021 | arg.name = _parser.toString(sfName->name); | 2020 | arg.name = _parser.toString(sfName->name); |
| 2022 | arg.assignSelf = s("self."sv) + arg.name; | 2021 | arg.assignSelf = s("self."sv) + arg.name; |
| @@ -2083,7 +2082,7 @@ private: | |||
| 2083 | auto clsName = static_cast<self_class_name_t*>(name); | 2082 | auto clsName = static_cast<self_class_name_t*>(name); |
| 2084 | auto nameStr = _parser.toString(clsName->name); | 2083 | auto nameStr = _parser.toString(clsName->name); |
| 2085 | if (LuaKeywords.find(nameStr) != LuaKeywords.end()) { | 2084 | if (LuaKeywords.find(nameStr) != LuaKeywords.end()) { |
| 2086 | out.push_back(s("self.__class[\""sv) + nameStr + s("\"]")); | 2085 | out.push_back(s("self.__class[\""sv) + nameStr + s("\"]"sv)); |
| 2087 | if (invoke) { | 2086 | if (invoke) { |
| 2088 | if (auto invokePtr = invoke.as<Invoke_t>()) { | 2087 | if (auto invokePtr = invoke.as<Invoke_t>()) { |
| 2089 | invokePtr->args.push_front(toAst<Exp_t>("self.__class"sv, x)); | 2088 | invokePtr->args.push_front(toAst<Exp_t>("self.__class"sv, x)); |
| @@ -2104,7 +2103,7 @@ private: | |||
| 2104 | auto sfName = static_cast<self_class_name_t*>(name); | 2103 | auto sfName = static_cast<self_class_name_t*>(name); |
| 2105 | auto nameStr = _parser.toString(sfName->name); | 2104 | auto nameStr = _parser.toString(sfName->name); |
| 2106 | if (LuaKeywords.find(nameStr) != LuaKeywords.end()) { | 2105 | if (LuaKeywords.find(nameStr) != LuaKeywords.end()) { |
| 2107 | out.push_back(s("self[\""sv) + nameStr + s("\"]")); | 2106 | out.push_back(s("self[\""sv) + nameStr + s("\"]"sv)); |
| 2108 | if (invoke) { | 2107 | if (invoke) { |
| 2109 | if (auto invokePtr = invoke.as<Invoke_t>()) { | 2108 | if (auto invokePtr = invoke.as<Invoke_t>()) { |
| 2110 | invokePtr->args.push_front(toAst<Exp_t>("self"sv, x)); | 2109 | invokePtr->args.push_front(toAst<Exp_t>("self"sv, x)); |
| @@ -2763,7 +2762,7 @@ private: | |||
| 2763 | _buf << join(temp); | 2762 | _buf << join(temp); |
| 2764 | _buf << assignStr; | 2763 | _buf << assignStr; |
| 2765 | _buf << indent(int(temp.size())) << lenVar << " = "sv << lenVar << " + 1"sv << nll(comp); | 2764 | _buf << indent(int(temp.size())) << lenVar << " = "sv << lenVar << " + 1"sv << nll(comp); |
| 2766 | for (int ind = int(temp.size()) - 1; ind > -1 ; --ind) { | 2765 | for (int ind = int(temp.size()) - 1; ind > -1; --ind) { |
| 2767 | _buf << indent(ind) << "end"sv << nll(comp); | 2766 | _buf << indent(ind) << "end"sv << nll(comp); |
| 2768 | } | 2767 | } |
| 2769 | switch (usage) { | 2768 | switch (usage) { |
| @@ -2827,7 +2826,7 @@ private: | |||
| 2827 | case id<star_exp_t>(): { | 2826 | case id<star_exp_t>(): { |
| 2828 | auto star_exp = static_cast<star_exp_t*>(loopTarget); | 2827 | auto star_exp = static_cast<star_exp_t*>(loopTarget); |
| 2829 | auto listVar = singleVariableFrom(star_exp->value); | 2828 | auto listVar = singleVariableFrom(star_exp->value); |
| 2830 | auto indexVar = getUnusedName("_index_"); | 2829 | auto indexVar = getUnusedName("_index_"sv); |
| 2831 | varAfter.push_back(indexVar); | 2830 | varAfter.push_back(indexVar); |
| 2832 | auto value = singleValueFrom(star_exp->value); | 2831 | auto value = singleValueFrom(star_exp->value); |
| 2833 | if (!value) throw std::logic_error(_info.errorMessage("Invalid star syntax."sv, star_exp)); | 2832 | if (!value) throw std::logic_error(_info.errorMessage("Invalid star syntax."sv, star_exp)); |
| @@ -2869,14 +2868,14 @@ private: | |||
| 2869 | temp.pop_back(); | 2868 | temp.pop_back(); |
| 2870 | } | 2869 | } |
| 2871 | if (listVar.empty()) { | 2870 | if (listVar.empty()) { |
| 2872 | listVar = getUnusedName("_list_"); | 2871 | listVar = getUnusedName("_list_"sv); |
| 2873 | varBefore.push_back(listVar); | 2872 | varBefore.push_back(listVar); |
| 2874 | transformChainValue(chain, temp, ExpUsage::Closure); | 2873 | transformChainValue(chain, temp, ExpUsage::Closure); |
| 2875 | _buf << indent() << "local "sv << listVar << " = "sv << temp.back() << nll(nameList); | 2874 | _buf << indent() << "local "sv << listVar << " = "sv << temp.back() << nll(nameList); |
| 2876 | } | 2875 | } |
| 2877 | std::string maxVar; | 2876 | std::string maxVar; |
| 2878 | if (!stopValue.empty()) { | 2877 | if (!stopValue.empty()) { |
| 2879 | maxVar = getUnusedName("_max_"); | 2878 | maxVar = getUnusedName("_max_"sv); |
| 2880 | varBefore.push_back(maxVar); | 2879 | varBefore.push_back(maxVar); |
| 2881 | _buf << indent() << "local "sv << maxVar << " = "sv << stopValue << nll(nameList); | 2880 | _buf << indent() << "local "sv << maxVar << " = "sv << stopValue << nll(nameList); |
| 2882 | } | 2881 | } |
| @@ -2885,7 +2884,7 @@ private: | |||
| 2885 | if (stopValue.empty()) { | 2884 | if (stopValue.empty()) { |
| 2886 | _buf << "#"sv << listVar; | 2885 | _buf << "#"sv << listVar; |
| 2887 | } else { | 2886 | } else { |
| 2888 | _buf << maxVar << " < 0 and #"sv << listVar <<" + " << maxVar << " or "sv << maxVar; | 2887 | _buf << maxVar << " < 0 and #"sv << listVar << " + "sv << maxVar << " or "sv << maxVar; |
| 2889 | } | 2888 | } |
| 2890 | if (!stepValue.empty()) { | 2889 | if (!stepValue.empty()) { |
| 2891 | _buf << ", "sv << stepValue; | 2890 | _buf << ", "sv << stepValue; |
| @@ -2897,7 +2896,7 @@ private: | |||
| 2897 | bool newListVal = false; | 2896 | bool newListVal = false; |
| 2898 | if (listVar.empty()) { | 2897 | if (listVar.empty()) { |
| 2899 | newListVal = true; | 2898 | newListVal = true; |
| 2900 | listVar = getUnusedName("_list_"); | 2899 | listVar = getUnusedName("_list_"sv); |
| 2901 | varBefore.push_back(listVar); | 2900 | varBefore.push_back(listVar); |
| 2902 | } | 2901 | } |
| 2903 | if (!endWithSlice) { | 2902 | if (!endWithSlice) { |
| @@ -3355,7 +3354,7 @@ private: | |||
| 3355 | } | 3354 | } |
| 3356 | } | 3355 | } |
| 3357 | if (!varDefs.empty()) { | 3356 | if (!varDefs.empty()) { |
| 3358 | temp.push_back(indent() + s("local ") + join(varDefs, ", "sv) + nll(body)); | 3357 | temp.push_back(indent() + s("local "sv) + join(varDefs, ", "sv) + nll(body)); |
| 3359 | } | 3358 | } |
| 3360 | } | 3359 | } |
| 3361 | std::string parent, parentVar; | 3360 | std::string parent, parentVar; |
| @@ -3431,7 +3430,7 @@ private: | |||
| 3431 | if (extend) { | 3430 | if (extend) { |
| 3432 | _buf << indent() << "setmetatable("sv << baseVar << ", "sv << parentVar << ".__base)"sv << nll(classDecl); | 3431 | _buf << indent() << "setmetatable("sv << baseVar << ", "sv << parentVar << ".__base)"sv << nll(classDecl); |
| 3433 | } | 3432 | } |
| 3434 | _buf << indent() << classVar << " = setmetatable({" << nll(classDecl); | 3433 | _buf << indent() << classVar << " = setmetatable({"sv << nll(classDecl); |
| 3435 | if (!builtins.empty()) { | 3434 | if (!builtins.empty()) { |
| 3436 | _buf << join(builtins) << ","sv << nll(classDecl); | 3435 | _buf << join(builtins) << ","sv << nll(classDecl); |
| 3437 | } else { | 3436 | } else { |
| @@ -3472,7 +3471,7 @@ private: | |||
| 3472 | pushScope(); | 3471 | pushScope(); |
| 3473 | auto selfVar = getUnusedName("_self_"sv); | 3472 | auto selfVar = getUnusedName("_self_"sv); |
| 3474 | addToScope(selfVar); | 3473 | addToScope(selfVar); |
| 3475 | _buf << indent(1) << "local " << selfVar << " = setmetatable({}, "sv << baseVar << ")"sv << nll(classDecl); | 3474 | _buf << indent(1) << "local "sv << selfVar << " = setmetatable({}, "sv << baseVar << ")"sv << nll(classDecl); |
| 3476 | _buf << indent(1) << "cls.__init("sv << selfVar << ", ...)"sv << nll(classDecl); | 3475 | _buf << indent(1) << "cls.__init("sv << selfVar << ", ...)"sv << nll(classDecl); |
| 3477 | _buf << indent(1) << "return "sv << selfVar << nll(classDecl); | 3476 | _buf << indent(1) << "return "sv << selfVar << nll(classDecl); |
| 3478 | popScope(); | 3477 | popScope(); |
| @@ -4101,7 +4100,7 @@ private: | |||
| 4101 | auto assignList = x->new_ptr<ExpList_t>(); | 4100 | auto assignList = x->new_ptr<ExpList_t>(); |
| 4102 | assignList->exprs.push_back(exp); | 4101 | assignList->exprs.push_back(exp); |
| 4103 | auto assign = x->new_ptr<Assign_t>(); | 4102 | auto assign = x->new_ptr<Assign_t>(); |
| 4104 | assign->values.push_back(toAst<Exp_t>(s("require ") + _parser.toString(import->literal), x)); | 4103 | assign->values.push_back(toAst<Exp_t>(s("require "sv) + _parser.toString(import->literal), x)); |
| 4105 | auto assignment = x->new_ptr<ExpListAssign_t>(); | 4104 | auto assignment = x->new_ptr<ExpListAssign_t>(); |
| 4106 | assignment->expList.set(assignList); | 4105 | assignment->expList.set(assignList); |
| 4107 | assignment->action.set(assign); | 4106 | assignment->action.set(assign); |
