-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathSyntaxTree.cpp
More file actions
49 lines (39 loc) · 1.04 KB
/
Copy pathSyntaxTree.cpp
File metadata and controls
49 lines (39 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "SyntaxTree.h"
using namespace SyntaxTree;
Node::Node(Token token) { this->token = token; }
Token& Node::getToken() { return this->token; }
int Node::getNumberOfChildren() const { return children.size(); }
Node& Node::getChild(int child_id) {
if (child_id >= getNumberOfChildren()) {
throw FetlangException("Child id " + std::to_string(child_id) + " is out of bounds");
}
return children[child_id];
}
Node& Node::addChild(const Node& node) {
children.push_back(node);
return children.back();
}
Token& Node::getChildsToken(int child_id) { return getChild(child_id).getToken(); }
void Node::display(int level) const {
// display data
for (int i = 0; i < level; i++) {
std::cout << "\t";
}
if (level == 0) {
if (token.isNullToken()) {
std::cout << "{Root}" << "\n";
} else {
std::cout << "Root:" << token << "\n";
}
} else {
std::cout << token << "\n";
}
// next
for (const auto& child : children) {
child.display(level + 1);
}
}
void NodeException::display() const {
displayLineAndMessage();
node.display();
}