📄 struct.c
字号:
return; } if(strcmp(tree->name,"return")==0){ typeCheck(tree->sibling); returnType=pop(); if(funcType!=returnType){ if( funcType==1 && returnType==2){ symnode *tmp; tmp=newLeaf("tmp"); copyNode(tree->sibling, tmp); strcpy(tree->sibling->name, "cast"); strcpy(tree->sibling->value, "int"); tree->sibling->sibling=tmp; return; } if( funcType==2 && returnType==1){ symnode *tmp; tmp=newLeaf("tmp"); copyNode(tree->sibling, tmp); strcpy(tree->sibling->name, "cast"); strcpy(tree->sibling->value, "float"); tree->sibling->sibling=tmp; return; } printf("ERROR: type mismatch\n"); printf("-parsing messege@(%d,%d): return type wrong\n",tree->line, tree->col); typeErrs++; } //printf("return type: %d\n", returnType); return; } typeCheck(tree->next); typeCheck(tree->sibling); if(strcmp(tree->name,"func")==0){ if(returnType==-1){ printf("ERROR: function error\n"); printf("-parsing messege@(%d,%d): function needs return statement\n",tree->line, tree->col); typeErrs++; } foundFunc=0; funcType = -1; returnType=-1; return; } if(strcmp(tree->name, "prntLst")==0){ pop(); return; } if(strcmp(tree->name, "cast")==0){ int type; type = pop(); if(type==10){ printf("ERROR: cast error\n"); printf("-parsing messege@(%d,%d): string can not be casted to any type\n",tree->line, tree->col); typeErrs++; } if(strcmp(tree->value, "char")==0){ if(type==2){ printf("ERROR: cast error\n"); printf("-parsing messege@(%d,%d): can not cast type float to type char\n",tree->line,tree->col); typeErrs++; } push(0.0); }else if(strcmp(tree->value, "int")==0) push(1.0); else if(strcmp(tree->value, "float")==0){ if(type==0){ printf("ERROR: cast error\n"); printf("-parsing messege@(%d,%d): can not cast type char to type float\n",tree->line,tree->col); typeErrs++; } push(2.0); }else{ printf("ERROR: cast error\n"); printf("-parsing messege@(%d,%d): type \"%s\" dose not exist\n",tree->line,tree->col, tree->value); typeErrs++; push(1.0); // push integer as default } return; } if(strcmp(tree->name, "CHAR_V")==0){ //printf("CHAR_V : %s\n", tree->value); push(0.0); return; } if(strcmp(tree->name, "INT_V")==0){ push(1.0); return; } if(strcmp(tree->name, "FLT_V")==0){ //printf("FLT_V : %s\n", tree->value); push(2.0); return; } if(strcmp(tree->name, "STRING")==0){ push(10.0); return; } if(strcmp(tree->name, "ID")==0 ){ // it is id symrec * id; id=getsym(tree->symTab->name, tree->symTab->scope); if(id==NULL) { typeErrs++; printf("ERROR: variable not declared\n"); printf("-parsing messege@(%d,%d): identifier \"%s\" not declared but first used here\n", tree->line,tree->col, tree->symTab->name); push(1.0); // if id not existed, set it as integer in case type checking broken return; } if(id->nameType==0) { // if is variable not function int type; type = id->type; //if(type!=5){ //printf("ID : %s, type: %d\n", tree->symTab->name,type); if(id->isArray==1 || type==5) { push(5); if(id->isArray==1) { arrayType=type; //array=id; } }else push(type); } return; } if(strcmp(tree->name, "offset")==0){ //printf("-------------------------- offset\n"); if(strcmp(tree->sibling->name,"exp")==0){ int a,b,type; b=pop(); a=pop(); if(b!=1){ typeErrs++; printf("ERROR: array index error\n"); printf("-parsing messege@(%d,%d): array index type should be int type\n", tree->line,tree->col); } //printf("offset: a:%d, b:%d\n",a,b); //if(tree->symTab==NULL) printf("NULL ..\n"); //type = tree->symTab->type; //type = array->type; type=arrayType; //printf("arrayType: %d\n", type);; push(type); } if(strcmp(tree->sibling->name, "member")==0){ int type,size; type=getMemberType(tree->sibling->symTab->structtype, tree->sibling->value); size=getMemberSize(tree->sibling->symTab->structtype, tree->sibling->value); if(size>1) arrayType = type; int a,b; b=pop(); //a=pop(); //AddrID=0; //printf("``````type: %d\n",type); if(type!=-1) push(type); else{ typeErrs++; printf("ERROR: structure member not declared\n"); printf("-parsing messege@(%d,%d): structure \"%s\" dose not have the member \"%s\"\n", tree->sibling->line,tree->sibling->col, tree->sibling->symTab->name,tree->sibling->value); push(1.0); // if id not existed, set it as integer in case type checking broken } return; } return; } if(strcmp(tree->name, "iDector")==0){ symnode *id; int a,b; id=tree; while(id!=NULL){ if(strcmp(id->name,"ID")==0) break; else id=id->next; } if(strcmp(tree->next->name, "dirDectr")==0){ b=pop(); //a=pop(); if(id->symTab->isArray==1 && b!=1){ typeErrs++; printf("ERROR: declaration error\n"); printf("-parsing messege@(%d,%d): only integer can be used to declare the array index \"%s\"\n", tree->line,tree->col, id->symTab->name); //push(1.0); } return; } if(strcmp(tree->next->name, "ASSIGN")==0 && id->symTab->isArray==1){ int i; for(i=0; i<id->symTab->numArray; i++){ b=pop(); if(id->symTab->type==0){ if(b==2){ printf("ERROR: initialization error\n"); printf("-parsing messege@(%d,%d): can not assign float number to a char type identifier \"%s\"\n", tree->line,tree->col, id->symTab->name); typeErrs++; } if(b==10){ printf("ERROR: initialization error\n"); printf("-parsing messege@(%d,%d): can not assign string to one element of a char type identifier \"%s\"\n", tree->line,tree->col, id->symTab->name); typeErrs++; } } if(id->symTab->type==1){ if(b==2){ printf("ERROR: initialization error\n"); printf("-parsing messege@(%d,%d): can not assign float to one element of a int type identifier \"%s\"\n", tree->line, tree->col,id->symTab->name); typeErrs++; } if(b==10){ printf("ERROR: initialization error\n"); printf("-parsing messege@(%d,%d): can not assign string to one element of a int type identifier \"%s\"\n", tree->line,tree->col, id->symTab->name); typeErrs++; } } if(id->symTab->type==2){ if(b==0){ printf("ERROR: initialization error\n"); printf("-parsing messege@(%d,%d): can not assign char number to a float type identifier \"%s\"\n", tree->line, tree->col,id->symTab->name); typeErrs++; } if(b==1){ printf("ERROR: initialization error\n"); printf("-parsing messege@(%d,%d): can not assign int number to a float type identifier \"%s\"\n", tree->line,tree->col, id->symTab->name); typeErrs++; } if(b==10){ printf("ERROR: initialization error\n"); printf("-parsing messege@(%d,%d): can not assign string to one element of a float type identifier \"%s\"\n", tree->line,tree->col, id->symTab->name); typeErrs++; } } } pop(); return; } } if(strcmp(tree->name, "OP")==0){ int a,b; b=pop(); a=pop(); if( (strcmp(tree->next->name,"CHAR_V")==0 ||strcmp(tree->next->name,"INT_V")==0 || strcmp(tree->next->name,"FLT_V")==0 ) && (strcmp(tree->sibling->name,"CHAR_V")==0 ||strcmp(tree->sibling->name,"INT_V")==0 || strcmp(tree->sibling->name,"FLT_V")==0 )){ float n,s; if(a==0 || a==1 ){ n=atoi(tree->next->value); } if(a==2 ){ int t; t=atoi(tree->next->value); n= *((float *)(&t)); } if(b==0 || b==1 ){ s=atoi(tree->sibling->value); } if(b==2 ){ int t; t=atoi(tree->sibling->value); s= *((float *)(&t)); //printf("%s s:%f\n",tree->sibling->value,f); } if(strcmp(tree->value,"+")==0) n=n+s; if(strcmp(tree->value,"-")==0) n=n-s; if(strcmp(tree->value,"*")==0) n=n*s; if(strcmp(tree->value,"/")==0) n=n/s; if( (strcmp(tree->value, "%")==0) || (strcmp(tree->value, "<<")==0) || (strcmp(tree->value, ">>")==0) || (strcmp(tree->value, "&")==0) || (strcmp(tree->value, "^")==0) || (strcmp(tree->value, "|")==0) ){ if(a!=1 || b!=1){ typeErrs++; printf("ERROR: type mismatch\n"); printf("-parsing messege@(%d,%d): operands for \"%s\" must be integers\n", tree->line,tree->col, tree->value); push(1.0); return; } int l,r; l=n; r=s; if(strcmp(tree->value,"%")==0) l=l%r; if(strcmp(tree->value,"<<")==0) l=l<<r; if(strcmp(tree->value,">>")==0) l=l>>r; if(strcmp(tree->value,"&")==0) l=l&r; if(strcmp(tree->value,"^")==0) l=l^r; if(strcmp(tree->value,"|")==0) l=l|r; n=l; } char val[80]; if(a==2.0 || b==2.0){ sprintf(val, "%d", *((unsigned int *)(&n))); strcpy(tree->name,"FLT_V"); strcpy(tree->value, val); push(2.0); }else { sprintf(val, "%d",(int)n); strcpy(tree->name,"INT_V"); strcpy(tree->value,val); push(1.0); } tree->next=NULL; tree->sibling=NULL; return; } //printf(" (%d,%d)(%s)op a:%d, b:%d\n",tree->line,tree->col,tree->value,a,b); if(a==5 || b==5){ printf("ERROR: type mismatch\n"); printf("-parsing messege@(%d,%d): array or structure name can not involve calculation\n", tree->line,tree->col); typeErrs++; push(1); return; } if(a!=1|| b!=1){ if( (strcmp(tree->value, "%")==0) || (strcmp(tree->value, "<<")==0) || (strcmp(tree->value, ">>")==0) || (strcmp(tree->value, "&")==0) || (strcmp(tree->value, "^")==0) || (strcmp(tree->value, "|")==0) ){ typeErrs++; printf("ERROR: type mismatch\n"); printf("-parsing messege@(%d,%d): operands for \"%s\" must be integers\n", tree->line,tree->col, tree->value); push(1.0); return; } } if( (a==0 && b==2) || (a==2 && b==0) || a==10 || b==10){ typeErrs++; printf("ERROR: type mismatch\n"); printf("-parsing messege@(%d,%d): operands not match for \"%s\" operation\n", tree->line,tree->col, tree->value); push(1.0); return; } if(a==2 || b ==2) push(2.0); else push(1.0); return; } if(strcmp(tree->name, "CMP")==0){ int a,b; b=pop(); a=pop(); if(a==5 || b==5){ printf("ERROR: type mismatch\n"); printf("-parsing messege@(%d,%d): array or structure name can not involve comparison\n", tree->line,tree->col); typeErrs++; push(a); return; } if(a==10 || b==10){ printf("ERROR: type mismatch\n"); printf("-parsing messege@(%d,%d): string can only be used in initializing char array\n", tree->line,tree->col); typeErrs++; } push(1.0); return; } if(strcmp(tree->name, "ASSIGN")==0){ if( !(tree->sibling->next!=NULL && strcmp(tree->sibling->next->name, "initLst")==0) ){ float a,b; b=pop(); a=pop(); //printf("assign: a:%f b:%f\n", a,b); if(a==5 && b==10){ if(arrayType!=0){ printf("ERROR: type mismatch\n"); printf("-parsing messege@(%d,%d): only char type array can use string to initialise\n", tree->line,tree->col); typeErrs++; } return; } if( b==5){ printf("ERROR: type mismatch\n"); printf("-parsing messege@(%d,%d): array or structure name can not involve assignment\n", tree->line,tree->col); typeErrs++; push(a); return; } if( a==5){ printf("ERROR: type mismatch\n"); printf("-parsing messege@(%d,%d): array or structure name can not involve assignment\n", tree->line,tree->col); typeErrs++; push(a); return; } if(a!=5 && b==10){ //if(strcmp(tree->sibling->name,"init")!=0){ printf("ERROR: type mismatch\n"); printf("-parsing messege@(%d,%d): string can only be used in initializing char array\n", tree->line,tree->col); typeErrs++; //} push(a); return; } if(a!=1 || b!=1){ if( (strcmp(tree->value, "%=")==0) || (strcmp(tree->value, "<<=")==0) || (strcmp(tree->value, ">>=")==0) || (strcmp(tree->value, "&=")==0) || (strcmp(tree->value, "^=")==0) || (strcmp(tree->value, "|=")==0) ){ typeErrs++; printf("ERROR: type mismatch\n"); printf("-parsing messege@(%d,%d): operands for \"%s\" must be integer or int type variable(s)\n", tree->line,tree->col, tree->value); push(1.0); return; } } if(a==0 && b==2){ printf("ERROR: type mismatch\n"); printf("-parsing messege@(%d,%d): can not assign float type value to a char type variable\n", tree->line,tree->col); typeErrs++; push(a); return; } if(a==2 && b==0){ printf("ERROR: type mismatch\n"); printf("-parsing messege@(%d,%d): can not assign char type value to a float type variable\n", tree->line,tree->col); typeErrs++; push(a); return; } push(a); return; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -