-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASTNode.h
132 lines (103 loc) · 2.65 KB
/
ASTNode.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
//
// ASTNode.h
// Matrix Calculator
//
// Created by Sen on 13-4-21.
#ifndef Matrix_Calculator_ASTNode_h
#define Matrix_Calculator_ASTNode_h
#include "Global.h"
#include "Matrix.h"
enum NodeType {
DefaultType, /* maybe useful for initialization */
Expression, /* the type of one complete expression */
SimpleExp,
Term,
Factor,
SubMatrixList,
SubMatrixListRow,
SubMatrix,
ArgList,
Arg,
FuncExp,
Identifier,
Num,
VectorExp,
Colon, /* a special type of vector exp */
BracketedMatrix,
};
enum NodeSubType {
/* used for those without type */
DefaultSubType,
/* 3 derivations of exp */
ExpSimpleAssign, /* ID = xxx */
ExpPartAssign, /* ID(X,Y) = xxx */
ExpSimpleExp,
/* 4 derivations of simpleExp */
SimpleExpTerm,
SimpleExpPlus,
SimpleExpMinus,
SimpleExpPower,
/* 4 derivations of term */
TermFactor,
TermTimes,
TermDivide,
TermMod,
TermTurn,
/* 6 derivations of factor */
FactorParen,
FactorID,
FactorFuncExp,
FactorNumber,
FactorVectorExp,
FactorBracketedMatrix,
/* 2 derivations of subMatrixList */
SubMatrixListSingle,
SubMatrixListMultiple,
/* 2 derivations of subMatrixListRow (COMMA and space versions are combined) */
SubMatrixListRowSingle,
SubMatrixListRowMultiple,
/* 4 derivations of subMatrix */
SubMatrixFuncExp,
SubMatrixID,
SubMatrixVectorExp,
SubMatrixNumber,
/* 2 derivations of argList */
ArgListSingle,
ArgListMultiple,
/* 4 derivations of arg */
ArgNumber,
ArgVectorExp,
ArgID,
ArgColon,
};
typedef enum NodeType NodeType;
typedef enum NodeSubType NodeSubType;
struct ASTNode {
NodeType type; /* save type */
NodeSubType subType;
Matrix mat; /* for matrix */
Number scalarValue; /* for number type */
char identifier[IDMAXLENGTH+1]; /* save ID */
/* for vector expression */
double start;
double end;
double step;
unsigned char evaluated; /* flag showing if this node has been evaluated */
/* children */
struct ASTNode *l;
struct ASTNode *r;
/* additional child*/
struct ASTNode *a;
/* special for ArgList*/
//struct ASTNode *nextArg;
};
typedef struct ASTNode ASTNode;
/* functions for ASTNode*/
ASTNode *createASTNode(NodeType type, NodeSubType subType);
void deleteASTNode(ASTNode *node);
void evaluateASTNode(ASTNode *node);
void displayInternalInfoOfASTNode(ASTNode *node);
void traverseASTNode(ASTNode *node);
int getNumberOfArgs(ASTNode *node);
ASTNode *getNthArgFromArgList(ASTNode *node, int N);
#endif