Skip to content

Commit 5ed004a

Browse files
committed
feat(ast): initial ast and node definitions
1 parent af835f9 commit 5ed004a

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

cppcheck.suppress

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ missingIncludeSystem
1313
unusedStructMember
1414
unmatchedSuppression
1515

16+
// Ignore debug methods
17+
unusedFunction
18+
1619
// libpython uses this a lot, unfortunately
1720
cstyleCast
1821

roxas/ast.cc

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
#include <roxas/ast.h>
3+
4+
namespace roxas {
5+
6+
namespace ast {
7+
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+
20+
} // namespace ast
21+
22+
} // namespace roxas

roxas/ast.h

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#pragma once
2+
3+
#include <optional>
4+
#include <string>
5+
#include <tuple>
6+
#include <vector>
7+
8+
namespace roxas {
9+
10+
namespace ast {
11+
12+
class node;
13+
struct definition;
14+
15+
} // namespace ast
16+
17+
using AST = std::vector<ast::definition>;
18+
19+
AST parse_tree_to_ast(std::string const& parse_tree);
20+
21+
namespace ast {
22+
23+
enum class literal : int
24+
{
25+
NUMBER,
26+
CONSTANT,
27+
STRING,
28+
UNKNOWN
29+
};
30+
31+
using literal_datatype = std::tuple<literal, std::string>;
32+
33+
struct definition
34+
{
35+
enum class type : int
36+
{
37+
FUNCTION,
38+
VECTOR
39+
};
40+
41+
type type{ type::FUNCTION };
42+
std::vector<node> children{};
43+
};
44+
45+
class node
46+
{
47+
public:
48+
node(node const&) = delete;
49+
node& operator=(node const&) = delete;
50+
51+
explicit node(std::string const& type);
52+
void print();
53+
54+
private:
55+
std::string type_;
56+
std::optional<literal_datatype> literal_;
57+
std::optional<std::vector<node>> children_;
58+
};
59+
60+
} // namespace ast
61+
} // namespace roxas

roxas/main.cc

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <cstdlib>
1818
#include <iostream>
19+
#include <roxas/ast.h>
1920
#include <roxas/parse_tree.h>
2021

2122
int main(int argc, const char* argv[])

0 commit comments

Comments
 (0)