-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCodeExecute.cpp
More file actions
204 lines (147 loc) · 6.05 KB
/
CodeExecute.cpp
File metadata and controls
204 lines (147 loc) · 6.05 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
//
// Created by fulongbin on 16/1/25.
//
#include "CodeExecute.h"
#include "JavaBaseType.h"
#include "JavaASTree.h"
#include "parser.hpp"
PtrValue ValueLeaf::executeCode(CodeExecuteContext &context) {
return ptrLeafValue;
}
PtrValue IdentifierASTLeaf::executeCode(CodeExecuteContext &context) {
map<string,PtrValue>::iterator it ;
for (it = context.varIdentPool.begin() ; it!=context.varIdentPool.end();it++) {
BaseInteger * baseInteger = (BaseInteger *) it->second.get();
cout << "[key : " << it->first << ", value : " << baseInteger->value << " ]" << endl;
}
PtrValue varValue = context.varIdentPool.at(idName);
if(varValue==NULL || varValue.get()==NULL){
if(context.outerContext != NULL){
varValue = context.outerContext->varIdentPool[idName];
}
}
BaseInteger *base = (BaseInteger *) varValue.get();
cout << "VariableASTLeaf map[ " << idName << " ] = " << base->value << endl;
return varValue;
}
PtrValue ReturnSentenceASTFork::executeCode(CodeExecuteContext &context) {
cout << "ReturnSentenceASTFork::executeCode" << endl;
return returnNode.executeCode(context);
}
PtrValue AssignmentASTFork::executeCode(CodeExecuteContext &context) {
PtrValue pValue = rightNode->executeCode(context);
// context.varIdentPool.insert(make_pair(leftNode.idName,pValue));
context.varIdentPool[leftNode.idName] = pValue;
BaseInteger * baseInteger = (BaseInteger *) pValue.get();
cout << "==AssignmentASTFork== insert [ " << leftNode.idName << " , " << baseInteger->value << " ]" <<endl;
map<string,PtrValue>::iterator it;
for (it = context.varIdentPool.begin() ; it!=context.varIdentPool.end();it++) {
BaseInteger * baseInteger = (BaseInteger *) it->second.get();
cout << "[key : " << it->first << ", value : " << baseInteger->value << endl;
}
return pValue;
}
PtrValue CastTypeASTFork::executeCode(CodeExecuteContext &context) {
cout << "CastTypeASTFork::executeCode" << endl;
PtrValue pValue = rightNode.executeCode(context);
return NULL;
}
PtrValue BinaryASTFork::executeCode(CodeExecuteContext &context) {
PtrValue leftValue = leftNode->executeCode(context);
PtrValue rightValue = rightNode->executeCode(context);
BaseInteger * baseInteger = new BaseInteger(0);
BaseInteger * left = (BaseInteger *)leftValue.get();
BaseInteger * right = (BaseInteger *)rightValue.get();
switch (operatorType){
case TPLUS:
baseInteger->value = left->value + right->value;
break;
case TMINUS:
baseInteger->value = left->value - right->value;
break;
case TMUL:
baseInteger->value = left->value * right->value;
break;
case TDIV:
baseInteger->value = left->value / right->value;
break;
}
cout << "BinaryASTFork::executeCode value " << baseInteger->value << endl;
PtrValue ptrResult(baseInteger);
return ptrResult;
}
PtrValue ThreeASTFork::executeCode(CodeExecuteContext &context) {
cout << "ThreeASTFork::executeCode" << endl;
BaseBoolean * pBool = (BaseBoolean *) judgeNode.executeCode(context).get();
if(pBool){
thenNode.executeCode(context);
} else{
elseNode.executeCode(context);
}
return NULL;
}
PtrValue FragmentASTree::executeCode(CodeExecuteContext &context) {
cout << "FragmentASTree::executeCode" << endl;
PtrExecuteContext ptrExecuteContext(&context);
fragmentContext.outerContext = ptrExecuteContext;
for (int i = 0; i < sentenceList.size() ; ++i) {
sentenceList[i]->executeCode(fragmentContext);
}
return NULL;
}
PtrValue IfASTFork::executeCode(CodeExecuteContext &context) {
cout << "IfASTFork::executeCode" << endl;
if(((BaseBoolean *) judgeNode.executeCode(context).get())->value){
} else{
}
return NULL;
}
PtrValue ForASTFork::executeCode(CodeExecuteContext &context) {
cout << "ForASTFork::executeCode" << endl;
CodeExecuteContext forContext = repeatNode.fragmentContext;
firstSentence.executeCode(forContext);
while (((BaseBoolean *) secondSentence.executeCode(forContext).get())->value) {
repeatNode.executeCode(forContext);
thirdSentence.executeCode(forContext);
}
return NULL;
}
PtrValue PreWhileASTFork::executeCode(CodeExecuteContext &context) {
cout << "PreWhileASTFork::executeCode" << endl;
CodeExecuteContext whileContext = repeatNode.fragmentContext;
while ( ((BaseBoolean *) judgeNode.executeCode(whileContext).get())->value ){
repeatNode.executeCode(whileContext);
}
return NULL;
}
PtrValue BackWhileASTFork::executeCode(CodeExecuteContext &context) {
cout << "BackWhileASTFork::executeCode" << endl;
CodeExecuteContext whileContext = repeatNode.fragmentContext;
do{
repeatNode.executeCode(whileContext);
}while ( ((BaseBoolean *) judgeNode.executeCode(whileContext).get())->value );
return NULL;
}
PtrValue VariableDeclarationASTFork::executeCode(CodeExecuteContext &context) {
cout << "VariableDeclarationASTFork::executeCode" << endl;
if(identifierASTLeaf.idName == "int"){
BaseInteger * baseInteger = new BaseInteger(0);
PtrValue ptr((BaseObject *)baseInteger);
context.varIdentPool.insert(map<string,PtrValue>::value_type(variableName.idName,ptr));
} else
if(identifierASTLeaf.idName == "double"){
shared_ptr<BaseDouble> ptrValue = make_shared<BaseDouble>(0.0);
PtrValue ptr((BaseObject *)ptrValue.get());
context.varIdentPool.insert(map<string,PtrValue>::value_type(variableName.idName,ptr));
} else
if(identifierASTLeaf.idName == "boolean"){
shared_ptr<BaseBoolean> ptrValue = make_shared<BaseBoolean>(false);
PtrValue ptr((BaseObject *)ptrValue.get());
context.varIdentPool.insert(map<string,PtrValue>::value_type(variableName.idName,ptr));
}
return NULL;
}
PtrValue ClassDefineASTFork::executeCode(CodeExecuteContext &context) {
cout << "ClassDefineASTFork ---> executeCode" << endl;
return NULL;
}