-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaASTree.h
More file actions
295 lines (251 loc) · 8.69 KB
/
JavaASTree.h
File metadata and controls
295 lines (251 loc) · 8.69 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
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
//
// Created by fulongbin on 16/1/23.
//
#ifndef TOYJAVA_JAVAASTREE_H
#define TOYJAVA_JAVAASTREE_H
#include <iostream>
#include <vector>
#include "JavaBaseType.h"
#include "CodeExecute.h"
using namespace std;
class ASTLeaf;
class ASTFork;
class ASTree;
class VariableDeclarationASTFork;
class MethodDeclarationASTFork;
typedef vector<ASTLeaf*> LeafList;
typedef vector<ASTFork*> ForkList;
typedef vector<ASTree*> NodeList;
typedef vector<ASTree*> SentenceList;
typedef vector<VariableDeclarationASTFork*> VariableList;
typedef vector<MethodDeclarationASTFork*> NormalMethodList;
class ASTree{
public:
virtual shared_ptr<BaseObject> executeCode(CodeExecuteContext & context) {return NULL;};
};
class ASTLeaf : public ASTree{
};
class ValueLeaf : public ASTLeaf{
public:
shared_ptr<BaseObject> ptrLeafValue;
shared_ptr<BaseObject> executeCode(CodeExecuteContext & context);
ValueLeaf(){
cout << "ValueLeaf" << endl;
};
};
//关键字
class IdentifierASTLeaf : public ASTLeaf{
public:
string idName;
IdentifierASTLeaf(string & name):idName(name){
cout << "IdentifierASTLeaf -->" << name << endl;
}
shared_ptr<BaseObject> executeCode(CodeExecuteContext & context);
};
class ASTFork : public ASTree{
};
//赋值语句
class AssignmentASTFork : public ASTFork{
public:
//被赋值
IdentifierASTLeaf leftNode;
//赋值
ASTree * rightNode;
AssignmentASTFork(IdentifierASTLeaf & assigned,ASTree * node):leftNode(assigned),rightNode(node){
cout << "AssignmentASTFork --> " << assigned.idName << endl;
};
shared_ptr<BaseObject> executeCode(CodeExecuteContext & context);
};
//返回语句
class ReturnSentenceASTFork : public ASTFork{
public:
ASTree returnNode;
ReturnSentenceASTFork(ASTree & node):returnNode(node){};
shared_ptr<BaseObject> executeCode(CodeExecuteContext & context);
};
// 强转类型
class CastTypeASTFork : public ASTFork{
public:
IdentifierASTLeaf castType;
ASTree rightNode;
CastTypeASTFork(ASTFork & node,string & typeName):rightNode(node),castType(typeName){}
shared_ptr<BaseObject> executeCode(CodeExecuteContext & context);
};
//两目运算
class BinaryASTFork : public ASTFork{
protected:
int operatorType;
ASTree * leftNode;
ASTree * rightNode;
public:
BinaryASTFork(ASTree * lfNode,int opType,ASTree * rtNode)
:operatorType(opType),
leftNode(lfNode),
rightNode(rtNode){
cout << "BinaryASTFork " <<endl;
}
shared_ptr<BaseObject> executeCode(CodeExecuteContext & context);
};
//三目运算
class ThreeASTFork : public ASTFork{
protected:
ASTree judgeNode;
ASTree thenNode;
ASTree elseNode;
public:
ThreeASTFork(ASTree & judge,ASTree & then,ASTree & else_):
judgeNode(judge),thenNode(then),elseNode(else_){
}
shared_ptr<BaseObject> executeCode(CodeExecuteContext & context);
};
//语句块
class FragmentASTree : public ASTFork{
public:
NodeList sentenceList;
CodeExecuteContext fragmentContext;
FragmentASTree(){}
shared_ptr<BaseObject> executeCode(CodeExecuteContext & context);
};
//if判断句
class IfASTFork :public ASTFork{
protected:
ASTree judgeNode;
ASTree thenNode;
ASTree elseNode;
public:
IfASTFork(ASTree & judge,ASTree & then,ASTree & else_):
judgeNode(judge),thenNode(then),elseNode(else_){
}
shared_ptr<BaseObject> executeCode(CodeExecuteContext & context);
};
//for 运算
class ForASTFork : public ASTFork{
ASTree firstSentence;
ASTree secondSentence;
ASTree thirdSentence;
FragmentASTree repeatNode;
ForASTFork(ASTFork & first,ASTFork & second,ASTFork & third,FragmentASTree & repeat)
:firstSentence(first),
secondSentence(second),
thirdSentence(third),
repeatNode(repeat){}
shared_ptr<BaseObject> executeCode(CodeExecuteContext & context);
};
//while(){} 语句
class PreWhileASTFork : public ASTFork{
protected:
ASTree judgeNode;
FragmentASTree repeatNode;
public:
PreWhileASTFork(ASTree & judge,FragmentASTree & repeat):judgeNode(judge),repeatNode(repeat){
}
shared_ptr<BaseObject> executeCode(CodeExecuteContext & context);
};
//do{}while()语句
class BackWhileASTFork : public ASTFork{
protected:
ASTree judgeNode;
FragmentASTree repeatNode;
public:
BackWhileASTFork(ASTree & judge,FragmentASTree & repeat):judgeNode(judge),repeatNode(repeat){
}
shared_ptr<BaseObject> executeCode(CodeExecuteContext & context);
};
//变量定义
class VariableDeclarationASTFork : public ASTFork{
public:
//变量类型
IdentifierASTLeaf identifierASTLeaf;
//变量名
IdentifierASTLeaf variableName;
VariableDeclarationASTFork(IdentifierASTLeaf & idLeaf,IdentifierASTLeaf & varLeaf):
identifierASTLeaf(idLeaf),
variableName(varLeaf){}
shared_ptr<BaseObject> executeCode(CodeExecuteContext & context);
};
//接口方法
class InterfaceMethodASTFork : public ASTFork{
public:
//变量名
IdentifierASTLeaf methodIdentifier;
//参数列表
VariableList parameterList;
//
IdentifierASTLeaf returnIdentifier;
InterfaceMethodASTFork(IdentifierASTLeaf & methodName,IdentifierASTLeaf & returnType,VariableList & varList)
:methodIdentifier(methodName),returnIdentifier(returnType),parameterList(varList){};
};
class MethodDeclarationASTFork : public InterfaceMethodASTFork{
public:
FragmentASTree fragmentASTree;
MethodDeclarationASTFork(IdentifierASTLeaf & methodName,IdentifierASTLeaf & returnType,VariableList & varList,FragmentASTree & fragment)
:InterfaceMethodASTFork(methodName,returnType,varList),fragmentASTree(fragment){};
};
//静态方法
class StaticMethodASTFork : public MethodDeclarationASTFork{
StaticMethodASTFork(IdentifierASTLeaf & methodName,IdentifierASTLeaf & returnType,VariableList & varList,FragmentASTree & fragment):MethodDeclarationASTFork(methodName,returnType,varList,fragment){};
};
//类方法
class NormalMethodASTFork : public MethodDeclarationASTFork{
public:
NormalMethodASTFork(IdentifierASTLeaf & methodName,IdentifierASTLeaf & returnType,VariableList & varList,FragmentASTree & fragment):MethodDeclarationASTFork(methodName,returnType,varList,fragment){};
};
//接口
class InterfaceDefineASTFork : public ASTFork{
public:
IdentifierASTLeaf interfaceIdentifier;
InterfaceDefineASTFork(string & interfaceName):interfaceIdentifier(interfaceName){};
};
class ClassDeclBodyASTFork : public ASTFork{
public:
VariableList staticVariableList;
NormalMethodList staticMethodList;
VariableList variableList;
NormalMethodList methodList;
ClassDeclBodyASTFork(){
cout << "ClassDeclBodyASTFork" << endl;
}
};
//类定义
class ClassDefineASTFork : public ASTFork{
public:
IdentifierASTLeaf classIdentifier;
ClassDeclBodyASTFork classBody;
ClassDefineASTFork(IdentifierASTLeaf & identifierASTLeaf,ClassDeclBodyASTFork & body)
:classIdentifier(identifierASTLeaf),classBody(body){
cout << "ClassDefineASTFork" << endl;
}
// ClassDefineASTFork(IdentifierASTLeaf & identifierASTLeaf,IdentifierASTLeaf & super,ClassDeclBodyASTFork body)
// :classIdentifier(identifierASTLeaf),superIdentifier(super),classBody(body){}
shared_ptr<BaseObject> executeCode(CodeExecuteContext & context);
};
//调用类成员
class CallClassMemberASTFork : public ASTFork{
public:
IdentifierASTLeaf callobject;
IdentifierASTLeaf interfaceIdentifier;
CallClassMemberASTFork(IdentifierASTLeaf & memberName,IdentifierASTLeaf & varLeaf):interfaceIdentifier(memberName),callobject(varLeaf){};
CallClassMemberASTFork(IdentifierASTLeaf & memberName,string varName):interfaceIdentifier(memberName),callobject(varName){};
};
//调用属性
class CallAttributionASTFork : public CallClassMemberASTFork{
public:
IdentifierASTLeaf attributionNode;
CallAttributionASTFork(IdentifierASTLeaf & attribution,IdentifierASTLeaf & attributionName,IdentifierASTLeaf & varLeaf)
:CallClassMemberASTFork(attributionName,varLeaf),
attributionNode(attribution){};
};
//调用类方法
class CallMethodASTFork : public CallClassMemberASTFork{
public:
NodeList argsList;
CallMethodASTFork(IdentifierASTLeaf & methodNode,NodeList & nodeList,IdentifierASTLeaf & varLeaf):CallClassMemberASTFork(methodNode,varLeaf),argsList(nodeList){}
CallMethodASTFork(IdentifierASTLeaf & methodNode,NodeList & nodeList):CallClassMemberASTFork(methodNode,"this"),argsList(nodeList){}
};
//new Object()方法
class NewObjectASTFork : public ASTFork{
public:
StaticMethodASTFork constructorMethod;
NewObjectASTFork(StaticMethodASTFork & method):constructorMethod(method){};
};
#endif //TOYJAVA_JAVAASTREE_H