|
16 | 16 |
|
17 | 17 | #pragma once
|
18 | 18 |
|
19 |
| -#include <memory> |
20 |
| -#include <optional> |
21 |
| -#include <string> |
22 |
| -#include <tuple> |
23 |
| -#include <variant> |
24 |
| -#include <vector> |
| 19 | +#include <simdjson.h> |
25 | 20 |
|
26 | 21 | namespace roxas {
|
27 | 22 |
|
28 |
| -// Link to the grammar can be found here: |
29 |
| -// https://github.com/jahan-addison/xion/blob/master/xion/grammar.lark |
| 23 | +namespace json { |
30 | 24 |
|
31 |
| -namespace ast { |
32 |
| - |
33 |
| -struct definition; |
34 |
| -class node; |
35 |
| -class expression_node; |
36 |
| -class statement_node; |
37 |
| -class lvalue_node; |
38 |
| -class rvalue_node; |
39 |
| - |
40 |
| -enum class literal : int |
41 |
| -{ |
42 |
| - Number, |
43 |
| - Constant, |
44 |
| - String, |
45 |
| - Unknown |
46 |
| -}; |
47 |
| - |
48 |
| -using literal_node = std::tuple<literal, std::string>; |
49 |
| - |
50 |
| -} // namespace ast |
51 |
| - |
52 |
| -/** |
53 |
| - * @brief |
54 |
| - * |
55 |
| - * The AST object that holds a representation of a parse tree from |
56 |
| - * the ParseTreeModuleLoader object suitable to construct a B program. |
57 |
| - * |
58 |
| - * Constructs child nodes with recursive descent on ast data types, |
59 |
| - * prepares data for future compiler passes. |
60 |
| - * |
61 |
| - */ |
62 |
| -class Abstract_Syntax_Tree |
63 |
| -{ |
64 |
| - public: |
65 |
| - Abstract_Syntax_Tree(Abstract_Syntax_Tree const&) = delete; |
66 |
| - Abstract_Syntax_Tree& operator=(Abstract_Syntax_Tree const&) = delete; |
67 |
| - |
68 |
| - using ast_type = std::vector<ast::definition>; |
69 |
| - |
70 |
| - public: |
71 |
| - /** |
72 |
| - * @brief Construct a new Abstract_Syntax_Tree object |
73 |
| - * |
74 |
| - * @param parse_tree A parse tree from the ParseTreeModuleLoader |
75 |
| - */ |
76 |
| - explicit Abstract_Syntax_Tree(std::string_view parse_tree); |
77 |
| - ~Abstract_Syntax_Tree() = default; |
78 |
| - |
79 |
| - public: |
80 |
| - /** |
81 |
| - * @brief Get the AST |
82 |
| - * |
83 |
| - * @return ast_type the AST data structure |
84 |
| - */ |
85 |
| - const ast_type& get_ast_definitions() const; |
86 |
| - |
87 |
| - private: |
88 |
| - ast::definition construct_definition_ast_(); |
89 |
| - ast::statement_node construct_statement_node_(); |
90 |
| - ast::expression_node construct_expression_node_(); |
91 |
| - |
92 |
| - private: |
93 |
| - ast::lvalue_node construct_lvalue_node_(); |
94 |
| - ast::rvalue_node construct_rvalue_node_(); |
95 |
| - |
96 |
| - private: |
97 |
| - ast::literal_node construct_constant_ast_(); |
98 |
| - |
99 |
| - private: |
100 |
| - std::string_view parse_tree_; |
101 |
| - ast_type ast_; |
102 |
| -}; |
103 |
| - |
104 |
| -namespace ast { |
105 |
| - |
106 |
| -// The overload pattern |
107 |
| -template<class... Ts> |
108 |
| -struct overload : Ts... |
109 |
| -{ |
110 |
| - using Ts::operator()...; |
111 |
| -}; |
112 |
| -template<class... Ts> |
113 |
| -overload(Ts...) -> overload<Ts...>; |
114 |
| - |
115 |
| -/** |
116 |
| - * @brief A representation of a Definition (vector or function) in the B |
117 |
| - * language |
118 |
| - * |
119 |
| - */ |
120 |
| -struct definition |
121 |
| -{ |
122 |
| - using ptr = std::unique_ptr<node>; |
123 |
| - enum class type : int |
124 |
| - { |
125 |
| - Function, |
126 |
| - Vector, |
127 |
| - Unknown |
128 |
| - }; |
129 |
| - |
130 |
| - type type{ type::Unknown }; |
131 |
| - std::vector<ptr> children{}; |
132 |
| -}; |
133 |
| - |
134 |
| -/** |
135 |
| - * @brief |
136 |
| - * |
137 |
| - * The AST node abstract class |
138 |
| - * |
139 |
| - */ |
140 |
| -class node |
141 |
| -{ |
142 |
| - public: |
143 |
| - node(node const&) = delete; |
144 |
| - node& operator=(node const&) = delete; |
145 |
| - virtual ~node() = default; |
146 |
| - |
147 |
| - protected: |
148 |
| - node() = default; |
149 |
| - |
150 |
| - public: |
151 |
| - virtual void print() const = 0; |
152 |
| -}; |
153 |
| - |
154 |
| -/** |
155 |
| - * @brief The lvalue node |
156 |
| - * |
157 |
| - * Provides a string to its identifer |
158 |
| - * |
159 |
| - */ |
160 |
| -class lvalue_node final : public node |
161 |
| -{ |
162 |
| - public: |
163 |
| - /** |
164 |
| - * @brief Construct a new lvalue node |
165 |
| - * |
166 |
| - * @param type lvalue type, per the grammar |
167 |
| - */ |
168 |
| - explicit lvalue_node(std::string type) |
169 |
| - : identifier_(std::move(type)) |
170 |
| - { |
171 |
| - } |
172 |
| - /** |
173 |
| - * @brief The lvalue node print function |
174 |
| - * |
175 |
| - */ |
176 |
| - void print() const override; |
177 |
| - |
178 |
| - private: |
179 |
| - std::string identifier_; |
180 |
| -}; |
181 |
| - |
182 |
| -/** |
183 |
| - * @brief The rvalue node |
184 |
| - * |
185 |
| - * Holds a pointer to its data expression |
186 |
| - * |
187 |
| - */ |
188 |
| -class rvalue_node final : public node |
189 |
| -{ |
190 |
| - public: |
191 |
| - /** |
192 |
| - * @brief Construct a new rvalue node |
193 |
| - * |
194 |
| - * @param type rvalue type, per te grammar |
195 |
| - * @param node the rvalue expression |
196 |
| - */ |
197 |
| - explicit rvalue_node(std::string type, std::string rvalue) |
198 |
| - : type_(std::move(type)) |
199 |
| - , rvalue_(std::move(rvalue)) |
200 |
| - |
201 |
| - { |
202 |
| - } |
203 |
| - |
204 |
| - /** |
205 |
| - * @brief The rvalue node print function |
206 |
| - * |
207 |
| - */ |
208 |
| - void print() const override; |
209 |
| - |
210 |
| - private: |
211 |
| - std::string type_; |
212 |
| - std::string rvalue_; |
213 |
| -}; |
214 |
| - |
215 |
| -/** |
216 |
| - * @brief The expression node |
217 |
| - * |
218 |
| - * An expression can be one or many of an lvalue, or rvalue |
219 |
| - * |
220 |
| - */ |
221 |
| -class expression_node final : public node |
222 |
| -{ |
223 |
| - public: |
224 |
| - using datatype = |
225 |
| - std::vector<std::variant<std::monostate, lvalue_node, rvalue_node>>; |
226 |
| - /** |
227 |
| - * @brief Construct a new expression node |
228 |
| - * |
229 |
| - * @param type expression type, per the grammar |
230 |
| - * @param expr the lvalue or rvalue of the expression |
231 |
| - */ |
232 |
| - explicit expression_node(std::string type, datatype expr) |
233 |
| - : type_(std::move(type)) |
234 |
| - , expr_(std::move(expr)) |
235 |
| - { |
236 |
| - } |
237 |
| - |
238 |
| - /** |
239 |
| - * @brief The expression node print function |
240 |
| - * |
241 |
| - */ |
242 |
| - void print() const override; |
243 |
| - |
244 |
| - private: |
245 |
| - std::string type_; |
246 |
| - datatype expr_; |
247 |
| -}; |
248 |
| - |
249 |
| -/** |
250 |
| - * @brief The statement node |
251 |
| - * |
252 |
| - * A statement may hold construct one of a string, expression, or literal. |
253 |
| - * Note that statements may have branches (such as if statements or switch/case) |
254 |
| - * |
255 |
| - */ |
256 |
| -class statement_node final : public node |
257 |
| -{ |
258 |
| - public: |
259 |
| - using ptr = std::unique_ptr<statement_node>; |
260 |
| - using datatype = std:: |
261 |
| - variant<std::monostate, std::string, expression_node, literal_node>; |
262 |
| - /** |
263 |
| - * @brief Construct a new statement node |
264 |
| - * |
265 |
| - * @param type statement type, per the grammar |
266 |
| - * @param branches a vector of the statement branches |
267 |
| - */ |
268 |
| - explicit statement_node(std::string type, |
269 |
| - std::vector<statement_node::ptr> branches) |
270 |
| - : type_(std::move(type)) |
271 |
| - , branches_(std::move(branches)) |
272 |
| - |
273 |
| - { |
274 |
| - } |
275 |
| - |
276 |
| - /** |
277 |
| - * @brief The statement node print function |
278 |
| - * |
279 |
| - */ |
280 |
| - void print() const override; |
281 |
| - |
282 |
| - private: |
283 |
| - std::string type_; |
284 |
| - std::vector<statement_node::ptr> branches_; |
285 |
| - datatype data_; |
286 |
| -}; |
287 |
| - |
288 |
| -} // namespace ast |
| 25 | +} // namespace json |
289 | 26 | } // namespace roxas
|
0 commit comments