-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathGramTreeNode.cpp
More file actions
164 lines (141 loc) · 3.11 KB
/
GramTreeNode.cpp
File metadata and controls
164 lines (141 loc) · 3.11 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "Scanner.h"
#include "GramTreeNode.h"
#include "Visitor.h"
SyntaxTreeNodeBase::SyntaxTreeNodeBase(int nK)
:_pNext(nullptr), _pParent(nullptr),
_childIndex(-1), _siblings(0)
{
_nodeKind = (NodeKind)nK;
memset(&_child, 0, sizeof(SyntaxTreeNodeBase*)* Child_Num_Const);
}
void SyntaxTreeNodeBase:: addChild(SyntaxTreeNodeBase* pChild, int ind ) {
if (pChild) {
if (ind >= 0) {
_child[ind] = pChild;
}
pChild->_childIndex = ind;
int simbling = 0;
for (auto p = pChild; p != nullptr; p = p->getNextNode()) { //所有兄弟节点都要有父节点
p->_pParent = this;
simbling++;
}
pChild->_siblings = ((simbling == 0) ? 1 : simbling); //大哥存有同等兄弟节点的个数,不是所有父节点孩子的个数
}
}
SyntaxTreeNodeBase* SyntaxTreeNodeBase::clone()
{
auto node = new SyntaxTreeNodeBase;
node->_nodeKind = _nodeKind;
node->_token = _token;
node->_pParent = _pParent;
node->_childIndex = _childIndex;
node->_siblings = _siblings;
return node;
}
void SyntaxTreeNodeBase::clear(bool next)
{
for (int i = 0; i < Child_Num_Const; i++) {
if (_child[i]) {
_child[i]->clear(true);
}
}
if (_pNext && next) {
SyntaxTreeNodeBase* p = _pNext;
std::list<SyntaxTreeNodeBase*> listNode;
while (p) {
listNode.push_back(p);
p = p->getNextNode();
}
for (auto it = listNode.begin(); it != listNode.end(); ++it) {
(*it)->clear(false);
}
}
delete this;
}
SyntaxTreeNodeBase* AssignStatement::getChildByTag(string name)
{
if (name == "var_name") {
return _child[AssignLetf];
}
else if (name == "var_rval") {
return _child[AssignRight];
}
return nullptr;
}
Value* Terminator::getVal()
{
if (!_val) {
if (_type == TERM_NUMBER) {
double num = strtod(_token.lexeme.c_str(), 0);
_val = new Number(num);
}
else if (_type == TERM_TRUE){
_val = new BoolValue(true);
}
else if (_type == TERM_FALSE){
_val = new BoolValue(false);
}
else if (_type == TERM_STRING) {
_val = new String(_token.lexeme);
}
else if (_type == TERM_NIL){
_val = new Nil();
}
}
return _val;
}
Value* IdentifierNode::getVal() {
if (!_val) {
_val = new String(_token.lexeme.c_str());
}
return _val;
}
void TreeNodeList::Push(TreeNode* node)
{
if (node != nullptr) {
if (_head == nullptr) {
TreeNode* curNode = getCurNode(node);
if (curNode != node) { //要加入的节点是个链节点则要拆散一个一个的加
_head = node;
_cur = curNode;
}
else {
_head = _cur = node;
}
}
else {
TreeNode* curNode = getCurNode(node); //节点的当前节点,即最后一个节点
if (curNode != node) { //要加入的节点是个链节点则要拆散一个一个的加
_cur->setNextNode(node);
_cur = curNode;
}
else {
_cur->setNextNode(node);
_cur = node;
}
}
}
}
TreeNode* TreeNodeList::getCurNode(TreeNode* node)
{
TreeNode* curNode = nullptr;
while (node) {
curNode = node;
node = node->getNextNode();
}
return curNode;
}
TreeNode* TreeNodeList::joinBy(TreeNodeList* node2)
{
if (_cur) {
if (node2) {
_cur->setNextNode(node2->getHeadNode());
}
}
else {
if (node2) {
return node2->getHeadNode();
}
}
return _head;
}