diff options
Diffstat (limited to 'MoonParser/moon_ast.cpp')
| -rw-r--r-- | MoonParser/moon_ast.cpp | 34 |
1 files changed, 15 insertions, 19 deletions
diff --git a/MoonParser/moon_ast.cpp b/MoonParser/moon_ast.cpp index 3c433ae..fcbaa90 100644 --- a/MoonParser/moon_ast.cpp +++ b/MoonParser/moon_ast.cpp | |||
| @@ -156,22 +156,10 @@ public: | |||
| 156 | if (root) { | 156 | if (root) { |
| 157 | std::cout << "compiled!\n\n"; | 157 | std::cout << "compiled!\n\n"; |
| 158 | std::vector<std::string> out; | 158 | std::vector<std::string> out; |
| 159 | root->eachChild([&](ast_node* node) { | 159 | pushScope(); |
| 160 | switch (node->getId()) { | 160 | transformBlock(root->block, out); |
| 161 | case "Block"_id: | 161 | popScope(); |
| 162 | pushScope(); | 162 | std::string result = std::move(out.back()); |
| 163 | transformBody(static_cast<Body_t*>(node), out, true); | ||
| 164 | popScope(); | ||
| 165 | break; | ||
| 166 | default: break; | ||
| 167 | } | ||
| 168 | }); | ||
| 169 | std::string result; | ||
| 170 | if (out.size() == 1) { | ||
| 171 | result = std::move(out.front()); | ||
| 172 | } else if (out.size() > 1) { | ||
| 173 | result = join(out, "\n"sv); | ||
| 174 | } | ||
| 175 | std::cout << result << '\n'; | 163 | std::cout << result << '\n'; |
| 176 | } else { | 164 | } else { |
| 177 | std::cout << "compile failed!\n"; | 165 | std::cout << "compile failed!\n"; |
| @@ -807,9 +795,9 @@ private: | |||
| 807 | out.push_back(clearBuf()); | 795 | out.push_back(clearBuf()); |
| 808 | } | 796 | } |
| 809 | 797 | ||
| 810 | void transformBody(Body_t* body, std::vector<std::string>& out, bool implicitReturn = false) { | 798 | void transformCodes(ast_node* nodes, std::vector<std::string>& out, bool implicitReturn) { |
| 811 | if (implicitReturn) { | 799 | if (implicitReturn) { |
| 812 | auto last = lastStatementFrom(body); | 800 | auto last = lastStatementFrom(nodes); |
| 813 | if (ast_is<ExpList_t>(last->content)) { | 801 | if (ast_is<ExpList_t>(last->content)) { |
| 814 | auto expList = static_cast<ExpList_t*>(last->content.get()); | 802 | auto expList = static_cast<ExpList_t*>(last->content.get()); |
| 815 | auto expListLow = new_ptr<ExpListLow_t>(); | 803 | auto expListLow = new_ptr<ExpListLow_t>(); |
| @@ -821,7 +809,7 @@ private: | |||
| 821 | } | 809 | } |
| 822 | } | 810 | } |
| 823 | std::vector<std::string> temp; | 811 | std::vector<std::string> temp; |
| 824 | body->traverse([&](ast_node* node) { | 812 | nodes->traverse([&](ast_node* node) { |
| 825 | switch (node->getId()) { | 813 | switch (node->getId()) { |
| 826 | case "Statement"_id: | 814 | case "Statement"_id: |
| 827 | transformStatement(static_cast<Statement_t*>(node), temp); | 815 | transformStatement(static_cast<Statement_t*>(node), temp); |
| @@ -832,6 +820,14 @@ private: | |||
| 832 | out.push_back(join(temp)); | 820 | out.push_back(join(temp)); |
| 833 | } | 821 | } |
| 834 | 822 | ||
| 823 | void transformBody(Body_t* body, std::vector<std::string>& out, bool implicitReturn = false) { | ||
| 824 | transformCodes(body, out, implicitReturn); | ||
| 825 | } | ||
| 826 | |||
| 827 | void transformBlock(Block_t* block, std::vector<std::string>& out, bool implicitReturn = true) { | ||
| 828 | transformCodes(block, out, implicitReturn); | ||
| 829 | } | ||
| 830 | |||
| 835 | void transformReturn(Return_t* returnNode, std::vector<std::string>& out) { | 831 | void transformReturn(Return_t* returnNode, std::vector<std::string>& out) { |
| 836 | if (auto valueList = returnNode->valueList.get()) { | 832 | if (auto valueList = returnNode->valueList.get()) { |
| 837 | if (auto singleValue = singleValueFrom(valueList)) { | 833 | if (auto singleValue = singleValueFrom(valueList)) { |
