void doExtDef(TreeNode *p) { //printf("doExtDef\n"); //printf("TreeNode->state:%s\n", p->state); //printf("TreeNode->productionRule: %d\n", p->productionRule); switch (p->productionRule) { case 1:{ //将doExtDecList()返回的变量链表赋为doSpecifier()返回的Type,插入变量表 TreeNode *p1 = p->firstChild; TreeNode *p2 = p1->rightBrother; Type type = doSpecifier(p1); varElement *elem = doExtDecList(p2); varElement *elemn = NULL; while (elem != NULL) { Type t = elem->type; if (t->kind != BASIC) { while (t->kind == ARRAY || t->u.array.elem->kind != BASIC) { //如果是数组要找到最底部的type节点赋为Specifier传回的Type t = t->u.array.elem; } //free(t->u.array.elem); t->u.array.elem = type; //在倒数第二个节点处改变Type } else { //free(elem->type); elem->type = type; if (search(elem->name) != NULL) { //查找此层定义不为空,说明变量重复定义 printf("error type 3 at line %d: variable %s redefined.\n", p->line, elem->name); elem = elem->next; } else { elemn = elem->next; insert(elem); elem = elemn; } } } break; } case 2: { TreeNode *p1 = p->firstChild; doSpecifier(p1); break; } case 3:{ TreeNode *p1 = p->firstChild; TreeNode *p2 = p1->rightBrother; TreeNode *p3 = p2->rightBrother; Type type = doSpecifier(p1); doFunDec(type, p2); //将返回类型传进去,在doFunDec中处理函数表 doCompSt(p3); break; } } }
varElement* doDef(TreeNode *p, int ifStruct) { //printf("doDef\n"); //printf("TreeNode->state:%s\n", p->state); //将doDecList()返回的变量链表赋为doSpecifier()返回的Type,插入变量表 TreeNode *p1 = p->firstChild; TreeNode *p2 = p1->rightBrother; Type type = doSpecifier(p1); //printf("doDef begin doDecList\n"); varElement *elem = doDecList(p2); varElement *elemHead = elem; varElement *elemn = NULL; //判断初始化时类型是否匹配 //printf("type:%d\n", type->u.basic); // printf("elem:%d\n", elem->type->u.basic); if(elem->initType != NULL) { //initType不为空,说明声明的同时初始化,需要判断类型是否匹配 if (ifStruct == 1) { printf("Error type 15 at line %d: struct member initialization is denied\n", p->line); } else if(!type_equal(type, elem->initType)) { printf("Error type 7 at line %d:'=' type mismatch\n", p->line); } } //printf("z\n"); while(elem != NULL) { //printf("z2\n"); Type t = elem->type; if (t->kind != BASIC) { //printf("z3\n"); while (t->kind == ARRAY && t->u.array.elem->kind != BASIC) { //如果是数组要找到最底部的type节点赋为Specifier传回的Type //printf("in loop\n"); t = t->u.array.elem; } //printf("z4\n"); //free(t->u.array.elem); t->u.array.elem = type; //在倒数第二个节点处改变Type //printf("z5\n"); } else { //free(elem->type); elem->type = type; //printf("type in doDef %d\n", type->u.basic); } if (ifStruct != 1) { //普通变量,插入变量表 if (search(elem->name) != NULL) { //查找此层定义不为空,说明变量重复定义 printf("error type 3 at line %d: variable %s redefined.\n", p->line, elem->name); elem = elem->next; } else { elemn = elem->next; insert(elem); elem = elemn; } } else { //结构体变量,保持链表结构 elem = elem->next; } } //printf("out of doDef\n"); return elemHead; }
argElement* doParamDec(TreeNode *p) { //这边有个参数为数组的处理,考虑是否好完成 //printf("doParamDec\n"); TreeNode *p1 = p->firstChild; TreeNode *p2 = p1->rightBrother; argElement *arg = (argElement *)malloc(sizeof(argElement)); arg->type = doSpecifier(p1); arg->next = NULL; return arg; }
void doExtDef(TreeNode *p) { //printf("doExtDef\n"); //printf("TreeNode->state:%s\n", p->state); //printf("TreeNode->productionRule: %d\n", p->productionRule); switch (p->productionRule) { case 1:{ //将doExtDefList()返回的变量链表赋为doSpecifier()返回的Type,插入变量表 TreeNode *p1 = p->firstChild; TreeNode *p2 = p1->rightBrother; // printf("here!\n"); Type type = doSpecifier(p1); // printf("here!\n"); varElement *elem = doExtDecList(p2); // printf("here!\n"); varElement *elemn = NULL; while (elem != NULL) { // printf("here!\n"); elem->type = type; //相当不确定这句指针操作的正确性 elemn = elem->layer_next; insert(elem); elem = elemn; } break; } case 2: {//这条不清楚存在的意义,可以暂不管 TreeNode *p1 = p->firstChild; doSpecifier(p1); break; } case 3:{ TreeNode *p1 = p->firstChild; TreeNode *p2 = p1->rightBrother; TreeNode *p3 = p2->rightBrother; doSpecifier(p1); doFunDec(p2); doCompSt(p3); break; } } }
void doDef(TreeNode *p) { //printf("doDef\n"); //printf("TreeNode->state:%s\n", p->state); //将doDecList()返回的变量链表赋为doSpecifier()返回的Type,插入变量表 TreeNode *p1 = p->firstChild; TreeNode *p2 = p1->rightBrother; Type type = doSpecifier(p1); //printf("doDef begin doDecList\n"); varElement *elem = doDecList(p2); varElement *elemn = NULL; //判断初始化时类型是否匹配 //printf("type:%d\n", type->u.basic); // printf("elem:%d\n", elem->type->u.basic); if(elem->type != NULL) { //type不为空,说明声明的同时初始化,需要判断类型是否匹配 //printf("here\n"); if(!type_equal(type, elem->type)) { printf("Error type 3 at line %d:'=' type mismatch\n", p->line); } else { while(elem != NULL) { elem->type = type; elemn = elem->layer_next; insert(elem); elem = elemn; } } } else{ while (elem != NULL) { elem->type = type; //相当不确定这句指针操作的正确性 elemn = elem->layer_next; insert(elem); elem = elemn; } } }