Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
  • Loading branch information
tutuwel committed Dec 4, 2023
1 parent 10319a9 commit 78ed7ba
Show file tree
Hide file tree
Showing 33 changed files with 320 additions and 31 deletions.
122 changes: 93 additions & 29 deletions phase2/err.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ rec* ac(rec* check){
for (rec* subRec : check->recs) {
ac(subRec);
}
cout<<typeToStringMap[check->t]<<endl;
// cout<<typeToStringMap[check->t]<<endl;
deal(check,m);
return nullptr;
}
Expand All @@ -149,13 +149,30 @@ std::queue<rec*> bfsQueue;
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);
}

Expand Down Expand Up @@ -200,10 +217,14 @@ std::queue<rec*> bfsQueue;
}
}
void checktassign(rec* left,rec* right){
// cout<<"我在赋值"<<endl;
// 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)
Expand Down Expand Up @@ -231,6 +252,7 @@ void checktassign(rec* left,rec* right){
err(varnodef,right);
}


} else{

if (!check(*l->val,*r->val))
Expand Down Expand Up @@ -284,6 +306,7 @@ rec* checkbiop(rec* left, rec* right){
rec* checkusfun(rec* func,rec* args){

rec* f=find(func->name,m);

if (!f){
err(funnodef,func);
}else{
Expand All @@ -292,7 +315,7 @@ rec* checkusfun(rec* func,rec* args){
err(notafun,func);
return nullptr;
}

if (!args&&f->recs.size()==0){
rec* res=new rec(*f->val);
res->line_num=func->line_num;
Expand All @@ -305,15 +328,31 @@ rec* checkusfun(rec* func,rec* 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 (rec* r:args->recs){
if (!find(r->name,m)&&r->val!=r)
{
err(varnodef,r);
}
}

// 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;
Expand Down Expand Up @@ -356,44 +395,59 @@ rec* checkusstruct(rec* stru,rec* mem){
}
rec* checkusarr(rec* arra, rec* index){

rec* res;
rec* res=nullptr;
if (arra->t!=arr)
{
rec* ar=find(arra->name,m);
if (!ar){
err(varnodef,arra);
}else{
if (ar->t!=arr)
{
}
else{
if (ar->t!=arr)
{
err(notanarr,arra);
}}
res=new rec(*ar);
res->recs.erase(res->recs.begin());

}
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);

}else{
res=new rec(*arra);
res->line_num=arra->line_num;
res->recs.erase(res->recs.begin());
}

rec* ind=find(index->name,m);


rec* ind=find(index->name,m);

if (ind)
{
if (ind->val->t!=intvar)
{
err(indexnoint,index);
}
}else{
if (index->t!=intvar)
if (index->t==usfun)
{
index=index->val;
}
if (index->t!=intvar)
{
err(indexnoint,index);
}
}
res->line_num=arra->line_num;

return res;
}

Expand Down Expand Up @@ -470,7 +524,8 @@ rec* deal(rec* todeal, map& m){
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;
Expand All @@ -483,7 +538,7 @@ rec* deal(rec* todeal, map& m){
if (left->name=="Exp")
{
left=left->val;
if (left->val==nullptr)
if (left->val==nullptr&&todeal->line_num!=0)
{
err(equnmatch,todeal);
return nullptr;
Expand Down Expand Up @@ -604,6 +659,11 @@ void checkmap(){
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){
Expand Down Expand Up @@ -640,7 +700,7 @@ void err(errtype e, rec* r,int exp,int err){
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 \""<<r->name<<"\""<<endl;
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;
Expand All @@ -651,7 +711,11 @@ void err(errtype e, rec* r,int exp,int err){
{
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;
}
Expand Down
3 changes: 2 additions & 1 deletion phase2/err.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ enum errtype{
structnohas,
structredef,
outindex,
uswithouval
uswithouval,
argtypeunmatch
};

enum type{
Expand Down
3 changes: 3 additions & 0 deletions phase2/extra-test/test_2_s01.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Error type 8 at Line 11: incompatiable return type
Error type 8 at Line 13: incompatiable return type
Error type 12 at Line 20: indexing by non-integer
22 changes: 22 additions & 0 deletions phase2/extra-test/test_2_s01.spl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
int test_16()
{
int c = 6;
return c;
}
float aa()
{
int a = 2;
int f = 3;
int h = 4;
if (a < f && h > f) return f;
else if ( a == f || h > a) return 5.0;
else return a;
}
int test_2_s01()
{
float aaa[5];
int b = 2;
aaa[test_16()] = 2.0;
aaa[aa()] = 2.0;
return b;
}
4 changes: 4 additions & 0 deletions phase2/extra-test/test_2_s02.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Error type 10 at line 4: indexing on non-array variable
Error type 5 at line 4: unmatching type on both sides of assignment
Error type 2 at Line 5: "func" is invoked without a definition
Error type 5 at Line 5: unmatching type on both sides of assignment
8 changes: 8 additions & 0 deletions phase2/extra-test/test_2_s02.spl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
int test_2_s02()
{
int a[10];
int c = a[2][2];
a[2] = func();
}


4 changes: 4 additions & 0 deletions phase2/extra-test/test_2_s03.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Error type 1 at Line 3: "b" is used without a definition
Error type 2 at Line 6: "function" is invoked without a definition
Error type 3 at Line 10: variable "x" is redefined in the same scope
Error type 4 at Line 20: "redefined_function" is redefined
22 changes: 22 additions & 0 deletions phase2/extra-test/test_2_s03.spl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
int test_2_s03() {
// Error Type 1: a variable is used without a definition
int a = b + 123;

// Error Type 2: a function is invoked without a definition
int i = function();

// Error Type 3: a variable is redefined in the same scope
int x = 0;
int x = 1;

return 0;
}

// Error Type 4: a function is redefined
int redefined_function() {
return 123;
}

int redefined_function() {
return 456;
}
4 changes: 4 additions & 0 deletions phase2/extra-test/test_2_s04.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Error type 5 at Line 17: unmatching types appear at both sides of the assignment operator
Error type 9 at Line 20: invalid argument number for function "add"
Error type 12 at Line 24: indexing by non-integer
Error type 14 at Line 27: accessing an undefined structure member: add
28 changes: 28 additions & 0 deletions phase2/extra-test/test_2_s04.spl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
struct St {
int sss;
float ttt;
};

int add(int x, int y)
{
return x + y;
}

int test_2_s04() {
int a;
int b[5];
struct St st;

// Error Type 5 unmatching types appear at both sides of the assignment operator (=)
st.sss = 12.34;

// Error Type 9 a function’s arguments mismatch the declared parameters (either types or numbers, or both)
add(st.sss, st.ttt);

// Error Type 12 array indexing with a non-integer type expression
st.ttt = 23.45;
b[st.ttt];

// Error Type 14 accessing an undefined structure member
st.add;
}
5 changes: 5 additions & 0 deletions phase2/extra-test/test_2_s05.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Error type 7 at Line 9: unmatching operand
Error type 5 at Line 9: unmatching type on both sides of assignment
Error type 10 at line 12: indexing on non-array variable
Error type 7 at Line 12: unmatching operand
Error type 5 at Line 12: unmatching type on both sides of assignment
14 changes: 14 additions & 0 deletions phase2/extra-test/test_2_s05.spl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
struct rabbit{
int a;
char c;
};
int test_2_s05(){
struct rabbit rrr;
int ra = rrr.a;
char rc = rrr.c;
int error = ra && rc;
error = error + 1;
rrr = rrr + 1;
error = ra[1]+1;
return 0;
}
9 changes: 9 additions & 0 deletions phase2/extra-test/test_2_s06.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Error type 9 at Line 9: invalid argument type at 1, except float, got int
Error type 9 at Line 9: invalid argument type at 2, except char, got int
Error type 9 at Line 10: invalid argument type at 0, except int, got float
Error type 9 at Line 10: invalid argument type at 2, except char, got float
Error type 9 at Line 11: invalid argument type at 0, except int, got char
Error type 9 at Line 11: invalid argument type at 1, except float, got char
Error type 9 at Line 12: invalid argument number, except 3, got 6
Error type 9 at Line 13: invalid argument number, except 3, got 6
Error type 9 at Line 14: invalid argument number, except 3, got 1
Loading

0 comments on commit 78ed7ba

Please sign in to comment.