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; }
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; } } }
varElement* doDecList(TreeNode *p) { //printf("doDecList\n"); //printf("TreeNode->state:%s\n", p->state); //printf("TreeNode->productionRule: %d\n", p->productionRule); //这个函数会把同一语句中出现的变量串起来 switch (p->productionRule) { case 1:{ TreeNode *p1 = p->firstChild; varElement *elem1 = doDec(p1); return elem1; break; } case 2:{ TreeNode *p1 = p->firstChild; TreeNode *p3 = p1->rightBrother->rightBrother; varElement *elem1 = doDec(p1); varElement *elem2 = doDecList(p3); elem1->next = elem2; return elem1; break; } } }