diff options
Diffstat (limited to 'src/yuescript/ast.hpp')
-rw-r--r-- | src/yuescript/ast.hpp | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/src/yuescript/ast.hpp b/src/yuescript/ast.hpp index f59b50f..c78d26c 100644 --- a/src/yuescript/ast.hpp +++ b/src/yuescript/ast.hpp | |||
@@ -98,16 +98,18 @@ public: | |||
98 | using select_last_t = typename select_last<Ts...>::type; | 98 | using select_last_t = typename select_last<Ts...>::type; |
99 | 99 | ||
100 | template <class... Args> | 100 | template <class... Args> |
101 | select_last_t<Args...>* getByPath() { | 101 | select_last_t<Args...>* get_by_path() { |
102 | int types[] = {id<Args>()...}; | 102 | int types[] = {id<Args>()...}; |
103 | return static_cast<select_last_t<Args...>*>(getByTypeIds(std::begin(types), std::end(types))); | 103 | return static_cast<select_last_t<Args...>*>(get_by_type_ids(std::begin(types), std::end(types))); |
104 | } | 104 | } |
105 | 105 | ||
106 | virtual bool visitChild(const std::function<bool(ast_node*)>& func); | 106 | virtual bool visit_child(const std::function<bool(ast_node*)>& func); |
107 | 107 | ||
108 | virtual int getId() const = 0; | 108 | virtual int get_id() const = 0; |
109 | 109 | ||
110 | virtual const std::string_view getName() const = 0; | 110 | virtual const std::string_view get_name() const = 0; |
111 | |||
112 | virtual std::string to_string(void*) const { return {}; } | ||
111 | 113 | ||
112 | template <class T> | 114 | template <class T> |
113 | inline ast_ptr<false, T> new_ptr() const { | 115 | inline ast_ptr<false, T> new_ptr() const { |
@@ -121,7 +123,7 @@ public: | |||
121 | 123 | ||
122 | private: | 124 | private: |
123 | int _ref; | 125 | int _ref; |
124 | ast_node* getByTypeIds(int* begin, int* end); | 126 | ast_node* get_by_type_ids(int* begin, int* end); |
125 | }; | 127 | }; |
126 | 128 | ||
127 | template <class T> | 129 | template <class T> |
@@ -130,12 +132,12 @@ id() { return 0; } | |||
130 | 132 | ||
131 | template <class T> | 133 | template <class T> |
132 | T* ast_cast(ast_node* node) { | 134 | T* ast_cast(ast_node* node) { |
133 | return node && id<T>() == node->getId() ? static_cast<T*>(node) : nullptr; | 135 | return node && id<T>() == node->get_id() ? static_cast<T*>(node) : nullptr; |
134 | } | 136 | } |
135 | 137 | ||
136 | template <class T> | 138 | template <class T> |
137 | T* ast_to(ast_node* node) { | 139 | T* ast_to(ast_node* node) { |
138 | assert(node->getId() == id<T>()); | 140 | assert(node->get_id() == id<T>()); |
139 | return static_cast<T*>(node); | 141 | return static_cast<T*>(node); |
140 | } | 142 | } |
141 | 143 | ||
@@ -143,7 +145,7 @@ template <class... Args> | |||
143 | bool ast_is(ast_node* node) { | 145 | bool ast_is(ast_node* node) { |
144 | if (!node) return false; | 146 | if (!node) return false; |
145 | bool result = false; | 147 | bool result = false; |
146 | int i = node->getId(); | 148 | int i = node->get_id(); |
147 | using swallow = bool[]; | 149 | using swallow = bool[]; |
148 | (void)swallow{result || (result = id<Args>() == i)...}; | 150 | (void)swallow{result || (result = id<Args>() == i)...}; |
149 | return result; | 151 | return result; |
@@ -181,7 +183,7 @@ public: | |||
181 | 183 | ||
182 | virtual traversal traverse(const std::function<traversal(ast_node*)>& func) override; | 184 | virtual traversal traverse(const std::function<traversal(ast_node*)>& func) override; |
183 | 185 | ||
184 | virtual bool visitChild(const std::function<bool(ast_node*)>& func) override; | 186 | virtual bool visit_child(const std::function<bool(ast_node*)>& func) override; |
185 | 187 | ||
186 | private: | 188 | private: |
187 | ast_member_vector m_members; | 189 | ast_member_vector m_members; |
@@ -235,13 +237,13 @@ public: | |||
235 | 237 | ||
236 | template <class T> | 238 | template <class T> |
237 | T* to() const { | 239 | T* to() const { |
238 | assert(m_ptr && m_ptr->getId() == id<T>()); | 240 | assert(m_ptr && m_ptr->get_id() == id<T>()); |
239 | return static_cast<T*>(m_ptr); | 241 | return static_cast<T*>(m_ptr); |
240 | } | 242 | } |
241 | 243 | ||
242 | template <class T> | 244 | template <class T> |
243 | bool is() const { | 245 | bool is() const { |
244 | return m_ptr && m_ptr->getId() == id<T>(); | 246 | return m_ptr && m_ptr->get_id() == id<T>(); |
245 | } | 247 | } |
246 | 248 | ||
247 | void set(ast_node* node) { | 249 | void set(ast_node* node) { |
@@ -333,7 +335,7 @@ public: | |||
333 | 335 | ||
334 | private: | 336 | private: |
335 | virtual bool accept(ast_node* node) override { | 337 | virtual bool accept(ast_node* node) override { |
336 | return node && (std::is_same<ast_node, T>() || id<T>() == node->getId()); | 338 | return node && (std::is_same<ast_node, T>() || id<T>() == node->get_id()); |
337 | } | 339 | } |
338 | }; | 340 | }; |
339 | 341 | ||
@@ -380,7 +382,7 @@ private: | |||
380 | if (!node) return false; | 382 | if (!node) return false; |
381 | using swallow = bool[]; | 383 | using swallow = bool[]; |
382 | bool result = false; | 384 | bool result = false; |
383 | (void)swallow{result || (result = id<Args>() == node->getId())...}; | 385 | (void)swallow{result || (result = id<Args>() == node->get_id())...}; |
384 | return result; | 386 | return result; |
385 | } | 387 | } |
386 | }; | 388 | }; |
@@ -515,7 +517,7 @@ public: | |||
515 | 517 | ||
516 | private: | 518 | private: |
517 | virtual bool accept(ast_node* node) override { | 519 | virtual bool accept(ast_node* node) override { |
518 | return node && (std::is_same<ast_node, T>() || id<T>() == node->getId()); | 520 | return node && (std::is_same<ast_node, T>() || id<T>() == node->get_id()); |
519 | } | 521 | } |
520 | }; | 522 | }; |
521 | 523 | ||
@@ -557,7 +559,7 @@ private: | |||
557 | if (!node) return false; | 559 | if (!node) return false; |
558 | using swallow = bool[]; | 560 | using swallow = bool[]; |
559 | bool result = false; | 561 | bool result = false; |
560 | (void)swallow{result || (result = id<Args>() == node->getId())...}; | 562 | (void)swallow{result || (result = id<Args>() == node->get_id())...}; |
561 | return result; | 563 | return result; |
562 | } | 564 | } |
563 | }; | 565 | }; |