-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathGramTreeNode.h
538 lines (430 loc) · 10.5 KB
/
GramTreeNode.h
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#pragma once
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <list>
#include <cassert>
#include <string.h>
using std::string;
using std::stack;
using std::vector;
using std::list;
#include "Scanner.h"
#include "Value.h"
#include "Function.h"
class SubroutineBodyNode;
class SubroutineDecNode;
class CompondStatement;
class ClassTreeNode;
class Visitor;
class SyntaxTreeNodeBase
{
public:
static const int Child_Num_Const = 5;
enum NodeKind {
None,
SUBROUTINE_DEC_K,
BASIC_TYPE_K,
CLASS_TYPE_K,
NULL_K,
PARAM_K,
VAR_DEC_K,
ARRAY_K,
VAR_K,
IF_STATEMENT_K,
WHILE_STATEMENT_K,
CALL_EXPRESSION_K,
RETURN_STATEMENT_K,
CALL_STATEMENT_K,
BOOL_EXPRESSION_K,
FUNCTION_CALL_K,
COMPARE_K,
OPERATION_K,
BOOL_K,
NAMELIST_K, //声明变量
ASSIGN_K,
SUBROUTINE_BODY_K,
BOOL_CONST_K,
NEGATIVE_K,
INT_CONST_K,
CHAR_CONST_K,
STRING_CONST_K,
KEY_WORD_CONST,
THIS_K,
TABLE_DEFINE_K,
TABLE_NAME_FIELD_K,
TABLE_INDEX_FIELD_K,
TABLE_ARRAY_FIELD_K,
FUNCTION_STM_K,
BLOCK_K,
CHUNK_K,
FOR_K
};
public:
SyntaxTreeNodeBase(int nK = None);
SyntaxTreeNodeBase(Scanner::Token t) {
_token = t;
}
~SyntaxTreeNodeBase(){}
protected:
SyntaxTreeNodeBase* _child[Child_Num_Const];
SyntaxTreeNodeBase* _pParent;
SyntaxTreeNodeBase* _pNext;
NodeKind _nodeKind;
short _childIndex; //子类的索引
short _siblings; //同等节点的个数
Scanner::Token _token;
public:
void setToken(Scanner::Token& t) {
_token = t;
}
Scanner::Token getToken() {
return _token;
}
const string getLexeme() {
return _token.lexeme;
}
void setLexeme(string& str) {
_token.lexeme = str;
}
unsigned int getRow() {
return _token.row;
}
void setNodeKind(int kind) {
_nodeKind = (NodeKind)kind;
}
NodeKind getNodeKind() {
return _nodeKind;
}
short getChildIndex() {
return _childIndex;
}
void setNextNode(SyntaxTreeNodeBase* node) {
if (node) {
node->_childIndex = _childIndex; //这么理解,父亲有多个老婆,每个老婆有0到多个儿子,生母是同一个的孩子编号一样
}
_pNext = node;
}
short getSiblings() {
return _siblings;
}
SyntaxTreeNodeBase* getNextNode() {
return _pNext;
}
SyntaxTreeNodeBase* getParentNode() {
return _pParent;
}
void setParentNode(SyntaxTreeNodeBase* node) { //也要把亲兄弟指定父类
auto cur = this;
while (cur) {
cur->_pParent = node;
cur = cur->getNextNode();
}
}
void addChild(SyntaxTreeNodeBase* pChild, int ind);
SyntaxTreeNodeBase* getChildByIndex(int ind) {
if (ind >= 0 && ind < Child_Num_Const) {
return _child[ind];
}
return nullptr;
}
virtual void accept(Visitor* visitor, void* data );
virtual string getName() { return ""; }
virtual string getSignName() { return ""; }
virtual SyntaxTreeNodeBase* getChildByTag(string name) { return nullptr; }
virtual SyntaxTreeNodeBase* clone();
virtual void clear(bool next);
};
typedef SyntaxTreeNodeBase TreeNode;
class TreeNodeList
{
TreeNode* _head;
TreeNode* _cur;
public:
TreeNodeList() {
_head = _cur = nullptr;
}
void Push(TreeNode* node);
TreeNode* getHeadNode() {
return _head;
}
TreeNode* getCurNode() {
return _cur;
}
TreeNode* joinBy(TreeNodeList* node2); //合并,第二个接上第一个,返回第一个的头
static TreeNode* getCurNode(TreeNode* node);
};
class ChunkNode : public SyntaxTreeNodeBase {
public:
ChunkNode() : SyntaxTreeNodeBase() {
_nodeKind = CHUNK_K;
}
virtual ~ChunkNode(){}
void accept(Visitor* visitor, void* data);
};
class BlockNode : public SyntaxTreeNodeBase {
public:
BlockNode() : SyntaxTreeNodeBase() {
_nodeKind = BLOCK_K;
}
virtual ~BlockNode(){}
void accept(Visitor* visitor, void* data);
};
class IdentifierNode : public SyntaxTreeNodeBase {
public:
IdentifierNode(Scanner::Token& t) : SyntaxTreeNodeBase() {
_token = t;
_val = nullptr;
}
virtual ~IdentifierNode(){}
void accept(Visitor* visitor, void* data);
Value* getVal();
private:
Value* _val;
};
class Terminator : public SyntaxTreeNodeBase {
public:
enum TermType {
TERM_NIL,
TERM_FALSE,
TERM_TRUE,
TERM_NUMBER,
TERM_STRING,
};
Terminator(Scanner::Token& t, TermType tt) : SyntaxTreeNodeBase() {
_token = t;
_type = tt;
_val = nullptr;
}
virtual ~Terminator(){}
void accept(Visitor* visitor, void* data);
Value* getVal();
private:
TermType _type;
Value* _val;
};
class UnaryExpression : public SyntaxTreeNodeBase {
public:
UnaryExpression(Scanner::Token& t) : SyntaxTreeNodeBase() {
_token = t;
}
virtual ~UnaryExpression(){}
void accept(Visitor* visitor, void* data);
};
class VarDecNode : public SyntaxTreeNodeBase { //变量声明节点
public:
VarDecNode() : SyntaxTreeNodeBase() {
_nodeKind = VAR_DEC_K;
}
enum EVarDec {
VarDec_Type = 0,
VarDec_Name
};
virtual ~VarDecNode(){}
SyntaxTreeNodeBase* getVarDecType() { return _child[VarDecNode::VarDec_Type]; } //变量的声明
SyntaxTreeNodeBase* getVarDecName() { return _child[VarDecNode::VarDec_Name]; } //变量的名字
};
class ParamNode : public VarDecNode { //形参节点
public:
ParamNode() : VarDecNode() {
_nodeKind = PARAM_K;
}
virtual ~ParamNode(){}
};
class LocalNameListStatement : public SyntaxTreeNodeBase { //声明语句
public:
LocalNameListStatement() : SyntaxTreeNodeBase() {
_nodeKind = NAMELIST_K;
}
virtual ~LocalNameListStatement() {}
enum LocalStatement {
NameList = 0,
ExpList
};
SyntaxTreeNodeBase* getNameList() { return getChildByIndex(NameList); };
SyntaxTreeNodeBase* getExpList() { return getChildByIndex(ExpList); };
void accept(Visitor* visitor, void* data);
};
class AssignStatement : public SyntaxTreeNodeBase {
public:
AssignStatement() : SyntaxTreeNodeBase() {
_nodeKind = ASSIGN_K;
}
virtual ~AssignStatement(){}
enum AStatement {
AssignLetf = 0,
AssignRight
};
virtual SyntaxTreeNodeBase* getChildByTag(string name) override;
SyntaxTreeNodeBase* getAssginLeft() { return _child[AssignLetf]; }
SyntaxTreeNodeBase* getAssginRight() { return _child[AssignRight]; }
void accept(Visitor* visitor, void* data);
};
class OperateStatement : public SyntaxTreeNodeBase {
public:
enum OperateType {
Plus,
Minus,
Multi,
Div
};
OperateStatement(OperateType type) : SyntaxTreeNodeBase() {
_nodeKind = OPERATION_K;
_opType = type;
}
virtual ~OperateStatement(){}
SyntaxTreeNodeBase* getTermLeft() { return _child[0]; }
SyntaxTreeNodeBase* getTermRight() { return _child[1]; }
void accept(Visitor* visitor, void* data);
OperateType _opType;
};
class TableDefine : public SyntaxTreeNodeBase { //talbe节点
public:
TableDefine() : SyntaxTreeNodeBase() {
_nodeKind = TABLE_DEFINE_K;
}
virtual ~TableDefine(){}
void accept(Visitor* visitor, void* data);
private:
TreeNodeList _fieldList;
public:
void push_filed(TreeNode* node) {
_fieldList.Push(node);
}
TreeNode* getField() {
return _fieldList.getHeadNode();
}
};
class TabMemberAccessor : public SyntaxTreeNodeBase { //table.member
public:
TabMemberAccessor() : SyntaxTreeNodeBase() {
}
virtual ~TabMemberAccessor(){}
void accept(Visitor* visitor, void* data);
};
class TabIndexAccessor : public SyntaxTreeNodeBase { //table[index]
public:
TabIndexAccessor() : SyntaxTreeNodeBase() {
}
virtual ~TabIndexAccessor(){}
void accept(Visitor* visitor, void* data);
};
//t = {1,2,[3]=3, d=5} 1,2是TableArrayFiled,[3]=7是TableIndexField,d=5是TableNameField
class TableIndexField : public SyntaxTreeNodeBase {
public:
TableIndexField() : SyntaxTreeNodeBase() {
_nodeKind = TABLE_INDEX_FIELD_K;
}
virtual ~TableIndexField(){}
void accept(Visitor* visitor, void* data);
};
class TableNameField : public SyntaxTreeNodeBase {
public:
TableNameField() : SyntaxTreeNodeBase() {
_nodeKind = TABLE_NAME_FIELD_K;
}
virtual ~TableNameField(){}
void accept(Visitor* visitor, void* data);
};
class TableArrayFiled : public SyntaxTreeNodeBase {
public:
TableArrayFiled() : SyntaxTreeNodeBase() {
_nodeKind = TABLE_ARRAY_FIELD_K;
}
virtual ~TableArrayFiled(){}
void accept(Visitor* visitor, void* data);
};
class NormalCallFunciton : public SyntaxTreeNodeBase {
public:
NormalCallFunciton() : SyntaxTreeNodeBase() {
_nodeKind = FUNCTION_CALL_K;
_needRetNum = 1; //一般需要的返回值是1个
}
virtual ~NormalCallFunciton(){}
SyntaxTreeNodeBase* getCaller() { return _child[0]; }
SyntaxTreeNodeBase* getParamList() { return _child[1]; }
virtual void accept(Visitor* visitor, void* data);
int _needRetNum; //需要返回值的个数
};
class FunctionStatement : public SyntaxTreeNodeBase {
public:
FunctionStatement() : SyntaxTreeNodeBase() {
_nodeKind = FUNCTION_STM_K;
}
virtual ~FunctionStatement(){}
enum FuncStmt {
EFuncName,
EFuncParams,
EFuncBody,
EFunRet
};
virtual void accept(Visitor* visitor, void* data);
SyntaxTreeNodeBase* getFuncName() { return _child[EFuncName]; }
SyntaxTreeNodeBase* getFuncParams() { return _child[EFuncParams]; }
SyntaxTreeNodeBase* getFuncBody() { return _child[EFuncBody]; }
SyntaxTreeNodeBase* getFuncRet() { return _child[EFunRet]; }
void setGlobal(bool b) { _bGlobal = b; }
bool getGlobal(){ return _bGlobal; }
private:
bool _bGlobal;
};
class ReturnStatement : public SyntaxTreeNodeBase {
public:
ReturnStatement() : SyntaxTreeNodeBase() {
_nodeKind = RETURN_STATEMENT_K;
}
~ReturnStatement(){}
virtual void accept(Visitor* visitor, void* data);
};
class IfStatement : public SyntaxTreeNodeBase {
public:
IfStatement() : SyntaxTreeNodeBase() {
_nodeKind = IF_STATEMENT_K;
}
enum eIfStmt {
ECompare,
EIf,
EElseOrEnd
};
virtual void accept(Visitor* visitor, void* data);
};
class CompareStatement : public SyntaxTreeNodeBase {
public:
CompareStatement() : SyntaxTreeNodeBase() {
_nodeKind = COMPARE_K;
}
enum eCmp {
ECmpLef,
ECmpRight
};
virtual void accept(Visitor* visitor, void* data);
};
class NumericForStatement : public SyntaxTreeNodeBase
{
public:
NumericForStatement() : SyntaxTreeNodeBase() {
_nodeKind = SyntaxTreeNodeBase::FOR_K;
}
void accept(Visitor* visitor, void* data);
enum eFor {
EStart, EEnd, EStep, EBlock
};
private:
};
class GenericForStatement : public SyntaxTreeNodeBase
{
public:
GenericForStatement() : SyntaxTreeNodeBase() {
_nodeKind = SyntaxTreeNodeBase::FOR_K;
}
void accept(Visitor* visitor, void* data);
private:
};
class BreakStatement : public SyntaxTreeNodeBase
{
public:
BreakStatement() :SyntaxTreeNodeBase() {
}
void accept(Visitor* visitor, void* data);
};