diff options
| -rw-r--r-- | MoonParser/ast.cpp | 26 | ||||
| -rw-r--r-- | MoonParser/ast.hpp | 8 | ||||
| -rw-r--r-- | MoonParser/moon_ast.cpp | 34 | ||||
| -rw-r--r-- | MoonParser/parser.hpp | 6 |
4 files changed, 24 insertions, 50 deletions
diff --git a/MoonParser/ast.cpp b/MoonParser/ast.cpp index dbf5d17..852593b 100644 --- a/MoonParser/ast.cpp +++ b/MoonParser/ast.cpp | |||
| @@ -14,12 +14,10 @@ traversal ast_node::traverse(const std::function<traversal (ast_node*)>& func) { | |||
| 14 | return func(this); | 14 | return func(this); |
| 15 | } | 15 | } |
| 16 | 16 | ||
| 17 | ast_node* ast_node::getByPath(std::initializer_list<std::size_t>) { | 17 | ast_node* ast_node::getByPath(std::initializer_list<size_t>) { |
| 18 | return nullptr; | 18 | return nullptr; |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | void ast_node::eachChild(const std::function<void (ast_node*)>&) { } | ||
| 22 | |||
| 23 | bool ast_node::visitChild(const std::function<bool (ast_node*)>&) { | 21 | bool ast_node::visitChild(const std::function<bool (ast_node*)>&) { |
| 24 | return false; | 22 | return false; |
| 25 | } | 23 | } |
| @@ -70,13 +68,14 @@ traversal ast_container::traverse(const std::function<traversal (ast_node*)>& fu | |||
| 70 | return traversal::Continue; | 68 | return traversal::Continue; |
| 71 | } | 69 | } |
| 72 | 70 | ||
| 73 | ast_node* ast_container::getByPath(std::initializer_list<std::size_t> paths) { | 71 | ast_node* ast_container::getByPath(std::initializer_list<size_t> paths) { |
| 74 | ast_node* current = this; | 72 | ast_node* current = this; |
| 75 | auto it = paths.begin(); | 73 | auto it = paths.begin(); |
| 76 | while (it != paths.end()) { | 74 | while (it != paths.end()) { |
| 77 | ast_node* findNode = nullptr; | 75 | ast_node* findNode = nullptr; |
| 76 | size_t id = *it; | ||
| 78 | current->visitChild([&](ast_node* node) { | 77 | current->visitChild([&](ast_node* node) { |
| 79 | if (node->getId() == *it) { | 78 | if (node->getId() == id) { |
| 80 | findNode = node; | 79 | findNode = node; |
| 81 | return true; | 80 | return true; |
| 82 | } | 81 | } |
| @@ -93,23 +92,6 @@ ast_node* ast_container::getByPath(std::initializer_list<std::size_t> paths) { | |||
| 93 | return current; | 92 | return current; |
| 94 | } | 93 | } |
| 95 | 94 | ||
| 96 | void ast_container::eachChild(const std::function<void (ast_node*)>& func) { | ||
| 97 | const auto& members = this->members(); | ||
| 98 | for (auto member : members) { | ||
| 99 | if (_ast_ptr* ptr = ast_cast<_ast_ptr>(member)) { | ||
| 100 | if (ptr->get()) { | ||
| 101 | func(ptr->get()); | ||
| 102 | } | ||
| 103 | } else if (_ast_list* list = ast_cast<_ast_list>(member)) { | ||
| 104 | for (auto obj : list->objects()) { | ||
| 105 | if (obj) { | ||
| 106 | func(obj); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | bool ast_container::visitChild(const std::function<bool (ast_node*)>& func) { | 95 | bool ast_container::visitChild(const std::function<bool (ast_node*)>& func) { |
| 114 | const auto& members = this->members(); | 96 | const auto& members = this->members(); |
| 115 | for (auto member : members) { | 97 | for (auto member : members) { |
diff --git a/MoonParser/ast.hpp b/MoonParser/ast.hpp index 982eea8..d239eac 100644 --- a/MoonParser/ast.hpp +++ b/MoonParser/ast.hpp | |||
| @@ -64,9 +64,7 @@ public: | |||
| 64 | */ | 64 | */ |
| 65 | virtual traversal traverse(const std::function<traversal (ast_node*)>& func); | 65 | virtual traversal traverse(const std::function<traversal (ast_node*)>& func); |
| 66 | 66 | ||
| 67 | virtual ast_node* getByPath(std::initializer_list<std::size_t> paths); | 67 | virtual ast_node* getByPath(std::initializer_list<size_t> paths); |
| 68 | |||
| 69 | virtual void eachChild(const std::function<void (ast_node*)>& func); | ||
| 70 | 68 | ||
| 71 | virtual bool visitChild(const std::function<bool (ast_node*)>& func); | 69 | virtual bool visitChild(const std::function<bool (ast_node*)>& func); |
| 72 | 70 | ||
| @@ -135,12 +133,10 @@ public: | |||
| 135 | */ | 133 | */ |
| 136 | virtual void construct(ast_stack& st) override; | 134 | virtual void construct(ast_stack& st) override; |
| 137 | 135 | ||
| 138 | virtual ast_node* getByPath(std::initializer_list<std::size_t> paths) override; | 136 | virtual ast_node* getByPath(std::initializer_list<size_t> paths) override; |
| 139 | 137 | ||
| 140 | virtual traversal traverse(const std::function<traversal (ast_node*)>& func) override; | 138 | virtual traversal traverse(const std::function<traversal (ast_node*)>& func) override; |
| 141 | 139 | ||
| 142 | virtual void eachChild(const std::function<void (ast_node*)>& func) override; | ||
| 143 | |||
| 144 | virtual bool visitChild(const std::function<bool (ast_node*)>& func) override; | 140 | virtual bool visitChild(const std::function<bool (ast_node*)>& func) override; |
| 145 | 141 | ||
| 146 | virtual ast_node* getChild(int index) const override; | 142 | virtual ast_node* getChild(int index) const override; |
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)) { |
diff --git a/MoonParser/parser.hpp b/MoonParser/parser.hpp index ae83215..a84b9a3 100644 --- a/MoonParser/parser.hpp +++ b/MoonParser/parser.hpp | |||
| @@ -17,15 +17,15 @@ | |||
| 17 | #include <locale> | 17 | #include <locale> |
| 18 | 18 | ||
| 19 | // const str hash helper functions | 19 | // const str hash helper functions |
| 20 | inline constexpr std::size_t hash(char const* input) | 20 | inline constexpr size_t hash(char const* input) |
| 21 | { | 21 | { |
| 22 | return *input ? *input + 33ull * hash(input + 1) : 5381; | 22 | return *input ? *input + 33ull * hash(input + 1) : 5381; |
| 23 | } | 23 | } |
| 24 | inline std::size_t hash(const char* input, int size, int index) | 24 | inline size_t hash(const char* input, int size, int index) |
| 25 | { | 25 | { |
| 26 | return index < size ? input[index] + 33ull * hash(input, size, index + 1) : 5381; | 26 | return index < size ? input[index] + 33ull * hash(input, size, index + 1) : 5381; |
| 27 | } | 27 | } |
| 28 | inline std::size_t constexpr operator"" _id(const char* s, size_t) | 28 | inline size_t constexpr operator"" _id(const char* s, size_t) |
| 29 | { | 29 | { |
| 30 | return hash(s); | 30 | return hash(s); |
| 31 | } | 31 | } |
