1
1
#pragma once
2
2
3
+ #include < memory>
3
4
#include < optional>
4
5
#include < string>
5
6
#include < tuple>
7
+ #include < variant>
6
8
#include < vector>
7
9
8
10
namespace roxas {
@@ -11,14 +13,24 @@ namespace ast {
11
13
12
14
class node ;
13
15
struct definition ;
16
+ class lvalue_node ;
17
+ class rvalue_node ;
14
18
15
- } // namespace ast
19
+ class Abstract_Syntax_Tree
20
+ {
21
+ public:
22
+ Abstract_Syntax_Tree (Abstract_Syntax_Tree const &) = delete ;
23
+ Abstract_Syntax_Tree& operator =(Abstract_Syntax_Tree const &) = delete ;
16
24
17
- using AST = std::vector<ast::definition>;
25
+ using ast_type = std::vector<ast::definition>;
18
26
19
- AST parse_tree_to_ast (std::string const & parse_tree);
27
+ public:
28
+ explicit Abstract_Syntax_Tree (std::string_view parse_tree);
29
+ ~Abstract_Syntax_Tree ();
20
30
21
- namespace ast {
31
+ private:
32
+ ast_type ast_;
33
+ };
22
34
23
35
enum class literal : int
24
36
{
@@ -28,33 +40,98 @@ enum class literal : int
28
40
UNKNOWN
29
41
};
30
42
31
- using literal_datatype = std::tuple<literal, std::string>;
32
-
33
43
struct definition
34
44
{
35
45
enum class type : int
36
46
{
37
47
FUNCTION,
38
- VECTOR
48
+ VECTOR,
49
+ UNKNOWN
39
50
};
40
51
41
- type type{ type::FUNCTION };
52
+ type type{ type::UNKNOWN };
42
53
std::vector<node> children{};
43
54
};
44
55
56
+ using literal_type = std::tuple<literal, std::string>;
57
+
45
58
class node
46
59
{
47
60
public:
48
61
node (node const &) = delete ;
49
62
node& operator =(node const &) = delete ;
63
+ virtual ~node () = default ;
64
+
65
+ protected:
66
+ node () = default ;
67
+
68
+ public:
69
+ virtual void print () const = 0;
70
+ };
71
+
72
+ class expression_node final : public node
73
+ {
74
+ public:
75
+ using expression_datatype =
76
+ std::vector<std::variant<std::monostate, lvalue_node, rvalue_node>>;
77
+ explicit expression_node (std::string type)
78
+ : type_(std::move(type))
79
+ {
80
+ }
81
+
82
+ void print () const override ;
83
+
84
+ private:
85
+ std::string type_;
86
+ expression_datatype expr_;
87
+ };
88
+
89
+ class statement_node final : public node
90
+ {
91
+ public:
92
+ using statement_datatype = std::
93
+ variant<std::monostate, std::string, expression_node, literal_type>;
94
+ explicit statement_node (std::string type)
95
+ : type_(std::move(type))
96
+ {
97
+ }
98
+
99
+ void print () const override ;
100
+
101
+ private:
102
+ std::string type_;
103
+ statement_datatype data_;
104
+ std::vector<node> children_;
105
+ };
106
+
107
+ class lvalue_node final : public node
108
+ {
109
+ public:
110
+ explicit lvalue_node (std::string type = " unknown" )
111
+ : identifier_(std::move(type))
112
+ {
113
+ }
114
+
115
+ void print () const override ;
116
+
117
+ private:
118
+ std::string identifier_;
119
+ };
120
+
121
+ class rvalue_node final : public node
122
+ {
123
+ public:
124
+ using rvalue_datatype = expression_node;
125
+ explicit rvalue_node (std::string type = " unknown" )
126
+ : type_(std::move(type))
127
+ {
128
+ }
50
129
51
- explicit node (std::string const & type);
52
- void print ();
130
+ void print () const override ;
53
131
54
132
private:
55
133
std::string type_;
56
- std::optional<literal_datatype> literal_;
57
- std::optional<std::vector<node>> children_;
134
+ rvalue_datatype rvalue_{ type_ };
58
135
};
59
136
60
137
} // namespace ast
0 commit comments