-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherr.cpp
724 lines (654 loc) · 19 KB
/
err.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
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
#include "err.hpp"
rec::rec(type nodetype, int line_num) : t(nodetype), line_num(line_num){
// cout<<this->name<<endl;
}
rec::rec(type t):t(t){
};
rec::rec(type t,char* name):t(t),name(string(name)){
};
rec::rec(type t,string name):t(t),name(name){};
rec::rec(const rec& other) : t(other.t), line_num(other.line_num), name(other.name), val(nullptr) {
if (other.val==&other)
{ val=this;
return;
}
// 复制 val
if (other.val != nullptr) {
val = new rec(*other.val);
}
// 复制 recs
for (const auto& child : other.recs) {
recs.push_back(new rec(*child));
}
}
rec::rec(){};
map m;
std::unordered_map<type, std::string> typeToStringMap = {
{intval, "intval"},
{charval, "charval"},
{floatval, "floatval"},
{fun, "fun"},
{arr, "arr"},
{biop, "biop"},
{siop, "siop"},
{struCt, "struCt"},
{intvar, "intvar"},
{floatvar, "floatvar"},
{charvar, "charvar"},
{structvar, "structvar"},
{var, "var"},
{def, "def"},
{usfun, "usfun"},
{ussiop, "ussiop"},
{usbiop, "usbiop"},
{usassign, "usassign"},
{usarr, "usarr"},
{usstruct, "usstruct"},
{noact, "noact"},
{deffun, "deffun"},
{checkreturn, "checkreturn"}
};
string rec::toString() const{
{
std::string result = "Type: " + typeToStringMap[t] + ", Line Number: " + std::to_string(line_num)
+ ", Name: " + name ;
if (this==val)
{ return result;
}
else{
if (val != nullptr) {
result += ", Value: (" + val->toString()+")";
}
result += " Child Nodes: [";
for (const auto& subRec : recs) {
result += subRec->toString() + ", ";
}
result += "]";
return result;
}
}
}
rec* find(string name, map& m){
auto it = m.checkMap.find(name);
if (it != m.checkMap.end()) {
return it->second;
} else {
it = m.checkMap.find("fun "+name);
if (it != m.checkMap.end()) {
return it->second;
}
}return nullptr;
}
void rec::link(int nodes_num, ...){
va_list nodes;
va_start(nodes, nodes_num);
for (int i = 0; i < nodes_num; i++)
{ rec *node = (rec *)va_arg(nodes, rec*);
this->recs.push_back(node);
}
va_end(nodes);
}
bool check (const rec& first, const rec& second) {
// cout<<first.name<<endl<<second.name<<endl;
// cout<<first.t<<endl<<second.t<<endl;
if (&first==&second)
{
return true;
}
// cout<<(first.t==intvar||first.t==floatvar)<<(second.t==intvar||second.t==floatvar)<<endl;
// if ((first.t==intvar||first.t==floatvar)&&(second.t==intvar||second.t==floatvar))
// {
// return true;
// } else
if (((first.t==second.t)&&(first.t!=structvar)))
{
return true;
}
if (((first.t != second.t) || (first.name != second.name))) {
return false;
}
// 比较 val
if ((first.val == nullptr && second.val != nullptr) || (first.val != nullptr && second.val == nullptr)) {
return false;
}
if (first.val != nullptr && second.val != nullptr && !check(*first.val, *second.val)) {
return false;
}
// 比较 recs
if (first.recs.size() != second.recs.size()) {
return false;
}
for (size_t i = 0; i < first.recs.size(); ++i) {
if (!check(*first.recs[i], *second.recs[i])) {
return false;
}
}
return true;
}
rec* ac(rec* check){
for (rec* subRec : check->recs) {
ac(subRec);
}
// cout<<typeToStringMap[check->t]<<endl;
deal(check,m);
return nullptr;
}
void checkback(rec* fun,rec* Compst){
std::queue<rec*> bfsQueue;
bfsQueue.push(Compst); // 将根节点入队
while (!bfsQueue.empty()) {
rec* currentNode = bfsQueue.front();
bfsQueue.pop();
if (currentNode->name=="Return")
{
rec* re=currentNode->recs[0];
// cout<<fun->toString()<<endl;
if ((re->t==usbiop)||(re->t==usfun)||(re->t==usstruct))
{
if (re->val==nullptr)
{
return;
}
if (re->name=="Exp")
{
rec* v=find(re->val->name,m);
if (v)
{
re=v;
}
}
if (!check(*re->val,*fun->val))
{
err(returnunmatch,re->val);
}
}
else if (re->t==usarr)
{
// cout<<re->toString()<<endl;
}
else if (re->t==ussiop)
{
if (!check(*re->recs[0],*fun->val))
{
err(returnunmatch,re->val);
}
}
else if (re->val==re)
{
if (!check(*re->val,*fun->val))
{
err(returnunmatch,re);
}
}
else{
rec* r=find(re->name,m);
if (!r)
{
err(varnodef,re);
}else{
if (!check(*r->val,*fun->val))
{
err(returnunmatch,re);
}
}
}
}
for (rec* subRec : currentNode->recs) {
bfsQueue.push(subRec);
}
}
}
void checktassign(rec* left,rec* right){
// cout<<left->toString()<<endl;
// cout<<right->toString()<<endl;
rec* l=find(left->name,m);
// if (right->t==arr&&right->recs.size()==0)
// {
// err(equnmatch,right);
// }
if (!l)
{
if (left->val==left)
{
err(rvalleft,left);
return;
}
err(varnodef,left);
}else{
rec* r=find(right->name,m);
if (right->val==right)
{
if (!check(*l->val,*right->val))
{
err(equnmatch,left);
}
}
else{
if (!r)
{
if (!check(*l->val,*right))
{
err(varnodef,right);
}
} else{
if (!check(*l->val,*r->val))
{
err(equnmatch,left);
}
}
}
}
}
rec* checkbiop(rec* left, rec* right){
// cout<<"我在比较"<<endl;
// cout<<left->toString()<<endl;
// cout<<right->toString()<<endl;
rec* l=find(left->name,m);
if (!l&&left->val!=left)
{
err(varnodef,left);
}else{
rec* r=find(right->name,m);
if (right->val==right)
{
if (!check(*l->val,*right->val))
{
err(opunmatch,left);
}
}else{
if (!r)
{
err(varnodef,right);
} else{
if (!check(*l->val,*r->val))
{
err(opunmatch,left);
}
}
}
}
// if (right->val==right)
// {
// rec* res=new rec(*left);
// res->line_num=left->line_num;
// res->val=right;
// return res;
// }
return new rec(*right);
}
rec* checkusfun(rec* func,rec* args){
rec* f=find(func->name,m);
if (!f){
err(funnodef,func);
}else{
if (f->t!=fun)
{
err(notafun,func);
return nullptr;
}
if (!args&&f->recs.size()==0){
rec* res=new rec(*f->val);
res->line_num=func->line_num;
return res;}
else if (args&&f->recs.size()==0)
{
err(argunmatch,func,0,args->recs.size());
}else if (f->recs.size()!=0&&!args)
{
err(argunmatch,func,f->recs[0]->recs.size(),0);
}else{
rec* fargs=f->recs[0];
// cout<<fargs->toString()<<endl;
// cout<<args->toString()<<endl;
if (args->recs.size()!=fargs->recs.size()){
err(argunmatch,func,f->recs[0]->recs.size(),args->recs.size());
return nullptr;
}
// for (int a=0;a<fargs->recs.size();a++){
// rec* r=args->recs[a];
// if (!find(r->name,m)&&r->val!=r)
// {
// err(varnodef,r);
// }
// rec* fi=find(r->name,m);
// if (fi)
// {
// r=fi;
// }
// if (!check(*r,*f->recs[a]))
// {
// err(argtypeunmatch,func);
// }
// }
}
rec* res=new rec(*f->val);
res->line_num=func->line_num;
return res;
}
return nullptr;
}
rec* checkusstruct(rec* stru,rec* mem){
// cout<<stru->toString()<<endl;
// cout<<mem->toString()<<endl;
rec* st=find(stru->name,m);
if (!st){
err(varnodef,stru);
}else{
if(st->val->t!=structvar){
err(dotnostuct,st);
}else{
rec* stname=st->val;//get apple
rec* struc=stname->val;//get struct inside def
rec* smem=stname->recs[0];//get link node
for (rec* r:smem->recs)
{
rec* m=r->recs[0];
if (m->name==mem->name)
{
m->line_num=stru->line_num;
// cout<<"返回"<<endl;
// cout<<m->toString()<<endl;
return m;
}
}
err(structnohas,mem);
}
}
return nullptr;
}
rec* checkusarr(rec* arra, rec* index){
rec* res=nullptr;
if (arra->t!=arr)
{
rec* ar=find(arra->name,m);
if (!ar){
err(varnodef,arra);
}
else{
if (ar->t!=arr)
{
err(notanarr,arra);
}
else{
res=new rec(*ar);
res->line_num=arra->line_num;
res->recs.erase(res->recs.begin());
}
}
}
else if (arra->t==arr&&arra->recs.size()==0)
{
err(notanarr,arra);
return nullptr;
}
else{
res=new rec(*arra);
res->line_num=arra->line_num;
res->recs.erase(res->recs.begin());
}
rec* ind=find(index->name,m);
if (ind)
{
if (ind->val->t!=intvar)
{
err(indexnoint,index);
}
}else{
if (index->t==usfun)
{
index=index->val;
}
if (index->t!=intvar)
{
err(indexnoint,index);
}
}
return res;
}
rec* deal(rec* todeal, map& m){
// cout<<todeal->toString()<<endl;
if (todeal->t==usstruct)
{
rec* str=todeal->recs[0];
rec* mem=todeal->recs[1];
rec* s=str;
rec* member=mem;
if (s->t==usstruct)
{
if (s->val==nullptr)
{
err(equnmatch,todeal);
return nullptr;
}
s = find(s->val->name,m);
if (!s)
{
err(varnodef,str);
}else{
s->line_num=str->line_num;
}
}else{
s = find(s->name,m);
if (!s)
{
err(varnodef,str);
return nullptr;
}else{
s->line_num=str->line_num;
}
}
todeal->val=checkusstruct(s,member);
}
if (todeal->t==checkreturn){
rec* fun=todeal->recs[0];
rec* Compst=todeal->recs[1];
// cout<<"我放了一下结点进入函数返回检查"<<endl;
// cout<<fun->toString()<<endl;
// cout<<Compst->toString()<<endl;
checkback(fun,Compst);
}
if (todeal->t==usassign){
rec* left=todeal->recs[0];
rec* right=todeal->recs[1];
rec* l=left;
rec* r=right;
// cout<<"左边是"<<endl;
// cout<<l->toString()<<endl;
// cout<<"右边是"<<endl;
// cout<<r->toString()<<endl;
if ((left->t==usbiop)||(left->t==usfun)||(left->t==usstruct)||(left->t==usarr))
{
l=left->val;
if (left->val==nullptr)
{
err(equnmatch,todeal);
return nullptr;
}
}
if ((right->t==usbiop)||(right->t==usfun)||(right->t==usstruct)||(right->t==usarr))
{
if (right->val==nullptr)
{
err(equnmatch,todeal);
return nullptr;
}
r=right->val;
}
checktassign(l,r);
}
if (todeal->t==usbiop)
{
rec* left=todeal->recs[0];
if (left->name=="Exp")
{
left=left->val;
if (left->val==nullptr&&todeal->line_num!=0)
{
err(equnmatch,todeal);
return nullptr;
}
}
rec* right=todeal->recs[2];
todeal->val=checkbiop(left,right);
}
if (todeal->t==usfun)
{
rec* fun=todeal->recs[0];
rec* arg=nullptr;
if (todeal->recs.size()==2)
{
arg=todeal->recs[1];
}
todeal->val=checkusfun(fun,arg);
// cout<<"goback"<<endl;
// checkmap();
}
if (todeal->t==usarr)
{
if (todeal->val!=nullptr)
{
return nullptr;
}
// cout<<todeal->toString()<<endl;
rec* arr=todeal->recs[0];
rec* index=todeal->recs[1];
rec* d;
if (arr->t==usarr)
{
arr=arr->val;
}
if (index->t==usarr)
{
index=index->val;
}
todeal->val= checkusarr(arr,index);
}
return nullptr;
}
void define (rec* type,rec* node){
// cout<<type->t<<" "<<node->t<<endl;
// cout<<"start to define"<<endl;
// cout<<type->name<<endl;
if (node->t==structvar){
node->val=new rec(*type);
auto it = m.checkMap.find(node->name);
if (it != m.checkMap.end()) {
err(structredef,node);
} else {
m.checkMap[node->name]=node;
}
}
if (node->t==fun)
{
node->val=new rec(*type);
auto it = m.checkMap.find("fun "+node->name);
if (it != m.checkMap.end()) {
err(funredef,node);
} else {
m.checkMap["fun "+node->name]=node;
}
}//如果是函数,其rec存的为输入参数,val表示其返回类型
if (node->t==arr)
{
node->val=new rec(*type);
auto it = m.checkMap.find(node->name);
if (it != m.checkMap.end()) {
err(varredef,node);
} else {
m.checkMap[node->name]=node;
}
}//如果是数组,就把所有的成员值全部设为对应type
if (node->t==var)
{
auto it = m.checkMap.find(node->name);
rec* r=type;
if (it != m.checkMap.end()) {
err(varredef,node);
} else {
if (type->t==structvar&&type->val==nullptr)
{
r=find(type->name,m);
if (!r)
{
err(varnodef,type);
}
}
m.checkMap[node->name]=node;
node->val=new rec(*r);
// cout<<node->name<<endl;
// cout<<node->val->name<<endl;
}
} //是变量值全设为对应type
if (node->t==noact)
{
for (rec* element : node->recs) {
define(type,element);
}
}//批量定义变量
// checkmap();
}
void checkmap(){
for (auto it = m.checkMap.begin(); it != m.checkMap.end(); ++it) {
std::cout << "Key: " << it->first << ", Value: " << it->second << std::endl;}
cout<<"finish define"<<endl;
}
void err(errtype e, rec* r,int exp,int err){
if (r->line_num==0)
{
return;
}
if (e==varnodef){
cout<<"Error type 1 at Line "<<r->line_num<<": "<<r->name<<" is used without a definition"<<endl;
}else if (e==funnodef){
cout<<"Error type 2 at Line "<<r->line_num<<": "<<r->name<<" is invoked without a definition"<<endl;
}else if (e==varredef){
cout<<"Error type 3 at Line "<<r->line_num<<": "<<r->name<<" is redefined in the same scope"<<endl;
}else if (e==funredef)
{
cout<<"Error type 4 at Line "<<r->line_num<<": \""<< r->name << "\" is redefined" <<endl;
}
else if (e==equnmatch)
{
cout<<"Error type 5 at Line "<<r->line_num<<": unmatching types appear at both sides of the assignment operator"<<endl;
}else if (e==rvalleft)
{
cout<<"Error type 6 at Line "<<r->line_num<<": rvalue appears on the left-hand side of the assignment operator"<<endl;
}else if (e==opunmatch)
{
cout<<"Error type 7 at Line "<<r->line_num<<": unmatching operand"<<endl;
}else if (e==returnunmatch)
{
cout<<"Error type 8 at Line "<<r->line_num<<": incompatiable return type \""<<r->name<<"\""<<endl;
}
else if (e==argunmatch)
{
cout<<"Error type 9 at Line "<<r->line_num<<": invalid argument number, except "<<exp<<", got "<<err<<endl;
//can add a type check
}
else if (e==notanarr)
{
cout<<"Error type 10 at Line "<<r->line_num<<": indexing on non-array variable \""<<r->name<<"\""<<endl;
}else if (e==notafun)
{
cout<<"Error type 11 at Line "<<r->line_num<<": invoking non-function variable \""<<r->name<<"\""<<endl;
}else if (e==indexnoint)
{
cout<<"Error type 12 at Line "<<r->line_num<<": indexing by non-integer "<<endl;
}else if (e==dotnostuct)
{
cout<<"Error type 13 at Line "<<r->line_num<<": accessing with non-struct variable \""<<r->name<<"\""<<endl;
}else if (e==structnohas)
{
cout<<"Error type 14 at Line "<<r->line_num<<": accessing an undefined structure member \""<<r->name<<"\""<<endl;
}else if (e=structredef)
{
cout<<"Error type 15 at Line "<<r->line_num<<": redefine the same structure type \""<<r->name<<"\""<<endl;
}
else if (e==argtypeunmatch)
{
cout<<"Error type 9 at Line "<<r->line_num<<": invalid argument type"<<endl;
}
else{
cout<<"Error type 17 at Line "<<r->line_num<<": err"<<endl;
}
}