Skip to content

Commit efad2cc

Browse files
committed
chore(ast): substantial progress on ast types
1 parent ca3d744 commit efad2cc

File tree

2 files changed

+206
-24
lines changed

2 files changed

+206
-24
lines changed

roxas/ast.cc

+63-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,74 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
#include <iostream>
1718
#include <roxas/ast.h>
1819

1920
namespace roxas {
2021

21-
namespace ast {
22+
/**
23+
* @brief Construct a new Abstract_Syntax_Tree object
24+
*
25+
* @param parse_tree A parse tree from the ParseTreeModuleLoader
26+
*/
27+
Abstract_Syntax_Tree::Abstract_Syntax_Tree(std::string_view parse_tree)
28+
: parse_tree_(parse_tree)
29+
{
30+
}
31+
32+
/**
33+
* @brief Get the AST
34+
*
35+
* @return ast_type the AST data structure
36+
*/
37+
38+
Abstract_Syntax_Tree::ast_type& Abstract_Syntax_Tree::get_ast()
39+
{
40+
return ast_;
41+
}
42+
43+
/**
44+
* @brief The rvalue node print function
45+
*
46+
*/
47+
void ast::rvalue_node::print() const
48+
{
49+
rvalue_->print();
50+
}
51+
52+
/**
53+
* @brief The lvalue node print function
54+
*
55+
*/
56+
void ast::lvalue_node::print() const
57+
{
58+
std::cout << "lvalue: " << identifier_ << std::endl;
59+
}
2260

23-
} // namespace ast
61+
/**
62+
* @brief The expression node print function
63+
*
64+
*/
65+
void ast::expression_node::print() const
66+
{
67+
for (auto const& item : expr_) {
68+
std::visit(
69+
ast::overload{
70+
[](std::monostate) {},
71+
[&](ast::lvalue_node const&) { std::get<1>(item).print(); },
72+
[&](ast::rvalue_node const&) { std::get<2>(item).print(); } },
73+
item);
74+
}
75+
}
76+
77+
/**
78+
* @brief The statement node print function
79+
*
80+
*/
81+
void ast::statement_node::print() const
82+
{
83+
// @TODO
84+
}
2485

2586
} // namespace roxas

roxas/ast.h

+143-22
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
#pragma once
1718

1819
#include <memory>
@@ -24,13 +25,26 @@
2425

2526
namespace roxas {
2627

28+
// Link to the grammar can be found here:
29+
// https://github.com/jahan-addison/xion/blob/master/xion/grammar.lark
30+
2731
namespace ast {
2832

2933
class node;
3034
struct definition;
3135
class lvalue_node;
36+
class rvalue_node;
3237
class expression_node;
3338

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+
*/
3448
class Abstract_Syntax_Tree
3549
{
3650
public:
@@ -40,13 +54,38 @@ class Abstract_Syntax_Tree
4054
using ast_type = std::vector<ast::definition>;
4155

4256
public:
57+
/**
58+
* @brief Construct a new Abstract_Syntax_Tree object
59+
*
60+
* @param parse_tree A parse tree from the ParseTreeModuleLoader
61+
*/
4362
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();
4572

4673
private:
74+
std::string_view parse_tree_;
4775
ast_type ast_;
4876
};
4977

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+
5089
enum class literal : int
5190
{
5291
Number,
@@ -55,8 +94,14 @@ enum class literal : int
5594
Unknown
5695
};
5796

97+
/**
98+
* @brief A representation of a Definition (vector or function) in the B
99+
* language
100+
*
101+
*/
58102
struct definition
59103
{
104+
using ptr = std::unique_ptr<node>;
60105
enum class type : int
61106
{
62107
Function,
@@ -65,11 +110,17 @@ struct definition
65110
};
66111

67112
type type{ type::Unknown };
68-
std::vector<node> children{};
113+
std::vector<ptr> children{};
69114
};
70115

71116
using literal_type = std::tuple<literal, std::string>;
72117

118+
/**
119+
* @brief
120+
*
121+
* The AST node abstract class
122+
*
123+
*/
73124
class node
74125
{
75126
public:
@@ -84,69 +135,139 @@ class node
84135
virtual void print() const = 0;
85136
};
86137

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
88145
{
89146
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))
93154
{
94155
}
95-
156+
/**
157+
* @brief The lvalue node print function
158+
*
159+
*/
96160
void print() const override;
97161

98162
private:
99-
std::string type_;
100-
expr_ptr rvalue_;
163+
std::string identifier_;
101164
};
102165

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
104173
{
105174
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))
108186
{
109187
}
110188

189+
/**
190+
* @brief The expression node print function
191+
*
192+
*/
111193
void print() const override;
112194

113195
private:
114-
std::string identifier_;
196+
std::string type_;
197+
datatype expr_;
115198
};
116199

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
118207
{
119208
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)
123217
: type_(std::move(type))
218+
, rvalue_(std::move(node_ptr))
219+
124220
{
125221
}
126222

223+
/**
224+
* @brief The rvalue node print function
225+
*
226+
*/
127227
void print() const override;
128228

129229
private:
130230
std::string type_;
131-
expr_datatype expr_;
231+
ptr rvalue_;
132232
};
133233

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+
*/
134241
class statement_node final : public node
135242
{
136243
public:
137-
using statement_datatype = std::
244+
using ptr = std::unique_ptr<statement_node>;
245+
using datatype = std::
138246
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)
140255
: type_(std::move(type))
256+
, branches_(std::move(branches))
257+
141258
{
142259
}
143260

261+
/**
262+
* @brief The statement node print function
263+
*
264+
*/
144265
void print() const override;
145266

146267
private:
147268
std::string type_;
148-
statement_datatype data_{};
149-
std::vector<node> children_{};
269+
std::vector<statement_node::ptr> branches_;
270+
datatype data_;
150271
};
151272

152273
} // namespace ast

0 commit comments

Comments
 (0)