-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodetype.cpp
488 lines (426 loc) · 12.1 KB
/
nodetype.cpp
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
#include "nodetype.h"
#include "ast.h"
bool copyHelp::isLiteral(int type) //warn:是否为字面量,添加新的字面量要进行修改
{
return (type==Num||type==String||type==Arr||type==Matrix||type==Vector||type==Null); //暂且先把Null安排上
}
bool copyHelp::isLiteral(BasicNode* node)
{
return copyHelp::isLiteral(node->getType());
}
bool isNotAssignable(BasicNode* val) //warn:是否不可赋值给变量,支持新的节点类型要进行修改
{
return (val->getType()==Pro||val->getType()==Fun||val->getType()==If||val->getType()==While);
//fix:目前暂不支持函数指针,因为函数实体的变量表示还没设计好
}
BasicNode::~BasicNode()
{
copyHelp::delTree(this);
}
BasicNode* BasicNode::operator +(BasicNode& rhs)
{
BasicNode* retn = new FunNode(record::globalScope.functionList["+"]);
retn->addNode(this);
retn->addNode(&rhs);
return retn;
}
BasicNode* BasicNode::operator -(BasicNode& rhs)
{
BasicNode* retn = new FunNode(record::globalScope.functionList["-"]);
retn->addNode(this);
retn->addNode(&rhs);
return retn;
}
BasicNode* BasicNode::operator *(BasicNode& rhs)
{
BasicNode* retn = new FunNode(record::globalScope.functionList["*"]);
retn->addNode(this);
retn->addNode(&rhs);
return retn;
}
BasicNode* BasicNode::operator /(BasicNode& rhs)
{
BasicNode* retn = new FunNode(record::globalScope.functionList["/"]);
retn->addNode(this);
retn->addNode(&rhs);
return retn;
}
BasicNode* BasicNode::operator ^(BasicNode& rhs)
{
BasicNode* retn = new FunNode(record::globalScope.functionList["^"]);
retn->addNode(this);
retn->addNode(&rhs);
return retn;
}
VarNode::VarNode(int valtype)
{
this->valtype=valtype;
if(valtype!=-1)
this->typeRestrictFlag=true;
}
VarNode::~VarNode()
{
if((!this->isEmpty())&&this->ownershipFlag)
delete this->val;
//然后BasicNode析构
}
void assignmentChecking(BasicNode *val,bool thisTypeRestrictFlag,int thisvaltype)
{
if(isNotAssignable(val))
throw cannotAssignedExcep();
if(thisTypeRestrictFlag&&val->getType()!=thisvaltype)
throw Excep("Mismatch between assignment type and variable type");
}
void VarNode::setVal(BasicNode* val)
{
assignmentChecking(val,this->typeRestrictFlag,this->getType());
this->clearVal();
//warn:理论上讲按照目前的设计,变量不应作为具有所有权的值(因为所有权在运行时域),但在此暂不进行检查。如果进行检查,直接在此处添加
this->valtype=val->getType();
this->ownershipFlag=true;
this->val=val;
}
void VarNode::setBorrowVal(BasicNode *val)
{
assignmentChecking(val,this->typeRestrictFlag,this->getType());
this->clearVal();
this->valtype=val->getType();
this->ownershipFlag=false;
this->val=val;
}
void VarNode::setVarVal(VarNode *node)
{
if(node->isEmpty())
throw unassignedAssignedExcep();
this->clearVal();
BasicNode* oriVal=node->eval();
//目前策略为:字面量进行拷贝(有所有权),变量作为无所有权指针传递
if(copyHelp::isLiteral(oriVal))
this->setVal(copyHelp::copyVal(oriVal));
if(oriVal->getType()==Var)
this->setBorrowVal(oriVal);
//fix:支持函数指针之后还需要在此处进行添加
}
BasicNode* VarNode::eval()
{
if(this->isEmpty())
this->giveupEval = true;
else
this->giveupEval = false;
if(this->giveupEval)
return copyHelp::copyNode(this);
else
{
if(copyHelp::isLiteral(this->val))
return copyHelp::copyVal(this->val);
else
return this->val;
}
//注意,多级指针也只会解包一次。不过从返回值基本无法判断返回的是自身还是自身的变量指针值,所以先前需要getValType进行判断
}
void VarNode::clearVal()
{
//调用前应进行是否为空的检查,否则有所有权的情况下会delete nullptr
if(this->ownershipFlag)
{
delete this->val;
this->val=nullptr;
}
}
VarRefNode::VarRefNode(int valtype)
{
this->valtype=valtype;
if(valtype!=-1)
this->typeRestrictFlag=true;
}
VarRefNode::~VarRefNode()
{
if(this->ownershipFlag&&this->isbind()) //一般来讲应该不会出现在绑定(函数调用期间)就释放实体的情况
delete this->val;
}
void VarRefNode::unbind()
{
//调用前应进行是否为空的检查,否则有所有权的情况下会delete nullptr
if(this->ownershipFlag)
delete this->val;
}
BasicNode* VarRefNode::eval()
{
if(!this->isbind())
throw VarRefUnbindExcep();
return this->val;
}
void VarRefNode::setVal(BasicNode* val)
{
this->valtype=val->getType();
this->ownershipFlag=true;
this->val=val;
}
void VarRefNode::setBorrowVal(BasicNode *val)
{
if(val->getType()==Var&&dynamic_cast<Variable*>(val)->isEmpty()) //按目前情况,传过来的只可能是Var,第一个条件有点多余
throw unassignedAssignedExcep();
this->valtype=val->getType();
this->ownershipFlag=false;
this->val=val;
}
void VarRefNode::bind(BasicNode *val)
{
assignmentChecking(val,this->typeRestrictFlag,this->getType());
//目前策略为:字面量进行拷贝(有所有权),变量作为无所有权指针传递
if(copyHelp::isLiteral(val))
this->setVal(copyHelp::copyVal(val));
if(val->getType()==Var)
this->setBorrowVal(val);
//fix:支持函数指针之后还需要在此处进行添加
}
void FunNode::addNode(BasicNode *node)
{
if(!this->funEntity->isVLP()) //支持变长参数就先不进行参数个数检查
if(this->sonNode.size()+1>this->funEntity->getParnum())
throw parameterNumExceedingExcep();
this->sonNode.push_back(node);
}
BasicNode* FunNode::eval()
{
this->giveupEval=false;
vector<BasicNode*> var;
BasicNode* result;
for(auto i:sonNode){
var.push_back(i->eval());
if(i->getType() == Fun && ((FunNode*)i)->giveupEval)
this->giveupEval = true;
if(i->getType() == Var && ((VarNode*)i)->giveupEval)
this->giveupEval = true;
}
if(!this->giveupEval){
result = this->getEntity()->eval(var);
}
else {
result = new FunNode(this->getEntity());
for(auto i:var)
result->addNode(i);
}
return result;
}
#ifdef PARTEVAL
bool isNotGiveupEval(BasicNode* node)
{
if(!(node->getType()==Fun&&dynamic_cast<FunNode*>(node)->giveupEval))
if(!(node->getType()==If&&dynamic_cast<IfNode*>(node)->giveupEval))
if(!(node->getType()==While&&dynamic_cast<WhileNode*>(node)->giveupEval))
//warn:支持新的控制流节点后要在此处添加
return true;
return false;
}
#endif
BasicNode* Function::eval(vector<BasicNode*> &sonNode)
{
//函数求值
if(this->iscanBE) //基础求值模式
{
if(this->canBEfun(sonNode)) //参数合法
return this->BEfun(sonNode);
else
throw callCheckMismatchExcep(TypeMisMatch);
}
else //不能基础求值就是正常有函数体Pro的
{
this->bindArgument(sonNode); //子节点绑定到实参
BasicNode* execpro=copyHelp::copyNode(this->body); //执行复制那个,防止函数体求值时被破坏
BasicNode* result=execpro->eval();
delete execpro;
this->unbindArgument();
return result;
}
}
Function::~Function()
{
delete this->body;
for(VarReference* i:this->argumentList)
delete i;
}
void Function::addArgument(VarReference *var)
{
if(!this->isVLP()) //支持变长参数就先不进行参数个数检查
if(this->argumentList.size()+1>this->getParnum())
throw argumentNumExceedingExcep();
this->argumentList.push_back(var);
}
void Function::unbindArgument()
{
for(VarReference* i:this->argumentList)
i->unbind();
}
void Function::bindArgument(vector<BasicNode *> &sonNode)
{
if(sonNode.size()!=this->argumentList.size())
throw callCheckMismatchExcep(NumberMismatch);
for(unsigned int i=0;i<this->argumentList.size();i++)
{
VarReference* formalpar=this->argumentList.at(i);
formalpar->bind(sonNode.at(i));
}
}
BasicNode* ProNode::eval()
{
vector<BasicNode*>&body=this->sonNode;
for(unsigned int i=0;i<body.size();i++) //最后一个可能是返回值,先留着后面单独处理
{
BasicNode* t = body.at(i)->eval();
if(body.at(i)->isRet())
return t;
delete t;
}
return new nullNode();
}
BasicNode* conditionalControlNode::evalCondition()
{
#ifdef PARTEVAL
this->giveupEval=false;
#endif
BasicNode* recon;
#ifdef PARTEVAL
try
{
#endif
recon=this->condition->eval();
#ifdef PARTEVAL
}
catch(unassignedEvalExcep) //condition直接就是个符号变量,放弃求值返回自身
{throw Excep("conditionalControlNode return");}
#endif
#ifdef PARTEVAL
if(recon->getType()==Fun&&dynamic_cast<FunNode*>(recon)->giveupEval) //是一个函数里面有放弃求值的变量
{
this->giveupEval=true; //本控制流节点也放弃求值
throw Excep("conditionalControlNode return");
}
#endif
return recon;
}
BasicNode* IfNode::eval()
{
BasicNode* recon;
#ifdef PARTEVAL
try
{
#endif
recon=this->evalCondition();
#ifdef PARTEVAL
}
catch(string e)
{
if(e=="conditionalControlNode return")
return this; //放弃求值,直接返回
else
throw e;
}
#endif
if(recon->getType()!=Num)
throw Excep("IfNode condition value's type mismatch");
BasicNode* result;
if(dynamic_cast<NumNode*>(recon)->getNum()==0)
{//这里判断false
if(this->falsePro!= nullptr)
result=this->falsePro->eval();
}
else
result=this->truePro->eval();
delete recon;
return result;
}
IfNode::~IfNode()
{
delete this->condition;
delete this->truePro;
delete this->falsePro;
}
BasicNode* WhileNode::eval()
{
BasicNode* execpro;
while(1)
{
BasicNode* recon;
#ifdef PARTEVAL
try
{
#endif
recon=this->evalCondition();
#ifdef PARTEVAL
}
catch(string e)
{
if(e=="conditionalControlNode return")
return this;
else
throw e;
}
#endif
if(recon->getType()!=Num)
throw Excep("WhileNode condition value's type mismatch");
if(dynamic_cast<NumNode*>(recon)->getNum()==1) //为真继续循环
{
execpro=copyHelp::copyNode(this->body);
execpro->eval();
delete execpro;
}
else
break;
}
return new nullNode();
}
WhileNode::~WhileNode()
{
delete this->condition;
delete this->body;
}
void ArrNode::clearArray()
{
//ArrayNode所有权由域持有,内含变量所有权由类本身持有,因此所有elm全部释放
for(BasicNode* node:this->allelm)
delete node;
}
ArrNode::ArrNode(int valtype, int len)
{
this->valtype=valtype;
this->len=len;
if(valtype!=-1) //开启严格求值
this->typeRestrictFlag=true;
//非定长数组会自动全部初始化
if(len!=-1)
{
for(unsigned int i=0;i<len;i++)
this->allelm.push_back(new VarNode(valtype));
}
}
VarNode* ArrNode::addElm(int valtype)
{
if(!this->isVLA())
throw Excep("non-VLA Arr cannot add Elm");
if(this->typeRestrictFlag)
{
if(valtype!=this->valtype)
throw Excep("Element type does not match array type");
}
VarNode* node=new VarNode(valtype);
this->allelm.push_back(node);
return node;
}
VarNode* ArrNode::addElm(VarNode *var)
{
if(!this->isVLA())
throw Excep("non-VLA Arr cannot add Elm");
if(this->typeRestrictFlag)
{
if(valtype!=this->valtype)
throw Excep("Element type does not match array type");
}
this->allelm.push_back(var);
return var;
}
void ArrNode::delElm(unsigned int sub)
{
vector<VarNode*>::iterator it=this->allelm.begin()+sub;
this->allelm.erase(it);
}