Skip to content

Commit 4fd7288

Browse files
committed
chore(ast): more ast types and other improvements
1 parent 5ed004a commit 4fd7288

File tree

4 files changed

+95
-30
lines changed

4 files changed

+95
-30
lines changed

roxas/ast.cc

-12
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@ namespace roxas {
55

66
namespace ast {
77

8-
node::node(std::string const& type)
9-
: type_(std::move(type))
10-
, literal_({ literal::UNKNOWN, "unknown" })
11-
, children_({})
12-
{
13-
}
14-
15-
void node::print()
16-
{
17-
std::cout << "node type: " << type_;
18-
}
19-
208
} // namespace ast
219

2210
} // namespace roxas

roxas/ast.h

+89-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#pragma once
22

3+
#include <memory>
34
#include <optional>
45
#include <string>
56
#include <tuple>
7+
#include <variant>
68
#include <vector>
79

810
namespace roxas {
@@ -11,14 +13,24 @@ namespace ast {
1113

1214
class node;
1315
struct definition;
16+
class lvalue_node;
17+
class rvalue_node;
1418

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;
1624

17-
using AST = std::vector<ast::definition>;
25+
using ast_type = std::vector<ast::definition>;
1826

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();
2030

21-
namespace ast {
31+
private:
32+
ast_type ast_;
33+
};
2234

2335
enum class literal : int
2436
{
@@ -28,33 +40,98 @@ enum class literal : int
2840
UNKNOWN
2941
};
3042

31-
using literal_datatype = std::tuple<literal, std::string>;
32-
3343
struct definition
3444
{
3545
enum class type : int
3646
{
3747
FUNCTION,
38-
VECTOR
48+
VECTOR,
49+
UNKNOWN
3950
};
4051

41-
type type{ type::FUNCTION };
52+
type type{ type::UNKNOWN };
4253
std::vector<node> children{};
4354
};
4455

56+
using literal_type = std::tuple<literal, std::string>;
57+
4558
class node
4659
{
4760
public:
4861
node(node const&) = delete;
4962
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+
}
50129

51-
explicit node(std::string const& type);
52-
void print();
130+
void print() const override;
53131

54132
private:
55133
std::string type_;
56-
std::optional<literal_datatype> literal_;
57-
std::optional<std::vector<node>> children_;
134+
rvalue_datatype rvalue_{ type_ };
58135
};
59136

60137
} // namespace ast

roxas/parse_tree.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ std::string read_source_file(fs::path path)
5555
* @param env_path an optional absolute path to a venv directory where
5656
* dependecies are installed
5757
*/
58-
ParseTreeModuleLoader::ParseTreeModuleLoader(std::string const& module_path,
59-
std::string const& module_name,
60-
std::string const& file_path,
58+
ParseTreeModuleLoader::ParseTreeModuleLoader(std::string module_path,
59+
std::string module_name,
60+
std::string file_path,
6161
std::string const& env_path)
6262
: module_path_(std::move(module_path))
6363
, module_name_(std::move(module_name))

roxas/parse_tree.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class ParseTreeModuleLoader
4242
* @param env_path an optional absolute path to a venv directory where
4343
* dependecies are installed
4444
*/
45-
ParseTreeModuleLoader(std::string const& module_path,
46-
std::string const& module_name,
47-
std::string const& file_path,
45+
ParseTreeModuleLoader(std::string module_path,
46+
std::string module_name,
47+
std::string file_path,
4848
std::string const& env_path = "");
4949

5050
/**

0 commit comments

Comments
 (0)