-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeStructs.hpp
54 lines (49 loc) · 1.24 KB
/
NodeStructs.hpp
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
50
51
52
53
54
#pragma once
#include <string>
#include <vector>
#include "TreePrinter.hpp"
struct Node
{
Node(std::string name, int precendence): name(name), precendence(precendence), indent(0) {}
virtual void visit(TreePrinter* tp) = 0;
virtual bool isValue() = 0;
virtual bool isOperation() = 0;
virtual void increaseIndent() = 0;
std::string name;
int precendence;
int indent;
virtual ~Node() {}
};
struct ValueNode: public Node
{
ValueNode(std::string name, int precendence): Node(name, precendence) {}
bool isValue() {return true; }
bool isOperation() {return false; }
void increaseIndent() { indent++;}
void visit(TreePrinter *tp)
{
tp->printValueNode(this);
}
};
struct OperationNode: public Node
{
OperationNode(std::string name, int precendence): Node(name, precendence) {}
bool isValue() {return false; }
bool isOperation() {return true; }
void increaseIndent()
{
indent++;
for (auto& node: childNodes)
node->increaseIndent();
}
void visit(TreePrinter *tp)
{
tp->printOperationNode(this);
}
virtual ~OperationNode()
{
for (auto& node: childNodes)
delete node;
}
std::vector<Node*> childNodes;
};