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