-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeNode.cpp
43 lines (39 loc) · 1.49 KB
/
TreeNode.cpp
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
#include "TreeNode.h"
void StatementListNode::replaceChild(TreeNode *value, TreeNode *replacement) {
for (int i = 0; i < statements.size(); ++i) {
if (statements[i] == value) {
statements[i] = replacement;
return;
}
}
throw std::invalid_argument("No child matching value found.");
}
void OperatorNode::replaceChild(TreeNode *value, TreeNode *replacement) {
if (left == value) {
left = dynamic_cast<ExpressionNode *>(replacement);
} else if (right == value) {
right = dynamic_cast<ExpressionNode *>(replacement);
} else {
throw std::invalid_argument("No child matching value found.");
}
}
void AssignmentNode::replaceChild(TreeNode *value, TreeNode *replacement) {
if (variable == value) {
variable = dynamic_cast<VariableNode *>(replacement);
} else if (expression == value) {
expression = dynamic_cast<ExpressionNode *>(replacement);
} else {
throw std::invalid_argument("No child matching value found.");
}
}
void BranchNode::replaceChild(TreeNode *value, TreeNode *replacement) {
if (condition == value) {
condition = dynamic_cast<ExpressionNode *>(replacement);
} else if (ifTrue == value) {
ifTrue = dynamic_cast<StatementListNode *>(replacement);
} else if (ifFalse != nullptr && ifFalse == value) {
ifFalse = dynamic_cast<StatementListNode *>(replacement);
} else {
throw std::invalid_argument("No child matching value found.");
}
}