Type doStructSpecifier(TreeNode *p) { //待完成 //printf("doStructSpecifier\n"); //printf("production rule:%d\n", p->productionRule); switch (p->productionRule) { case 1: { TreeNode *p2 = p->firstChild->rightBrother; TreeNode *p4 = p2->rightBrother->rightBrother; char *tagname = doOptTag(p2); if (tagname != NULL) { //有标签名,把它加入结构体表中 if (searchStruct(tagname) != NULL || searchAll(tagname) != NULL) { printf("Error type 16 at line %d: struct tag %s is same as other struct tag or variable name\n", p->line, tagname); return NULL; } structTableElement *str = (structTableElement *)malloc(sizeof(structTableElement)); str->name = tagname; str->type = (Type)malloc(sizeof(struct Type_)); str->type->kind = STRUCTURE; str->type->u.var = doDefList(p4, 1); insertStruct(str); return str->type; } else { Type temptype = (Type)malloc(sizeof(struct Type_)); temptype->kind = STRUCTURE; temptype->u.var = doDefList(p4, 1); //showStructMember(temptype); return temptype; } break; } case 2: { //Tag要在之前定义过才能有效,去结构体表中查找 TreeNode *p2 = p->firstChild->rightBrother; char *tagname = doTag(p2); structTableElement *str = searchStruct(tagname); if (str == NULL) { printf("Error type 17 at line %d: struct tag %s undefined\n", p->line, tagname); return NULL; } else { //查找到,就将表中的type返回 //showStructMember(str->type); return str->type; } break; } } }
void doCompSt(TreeNode *p) { //printf("doCompSt\n"); //printf("TreeNode->state:%s\n", p->state); into_a_layer(); TreeNode *p2 = p->firstChild->rightBrother; TreeNode *p3 = p2->rightBrother; doDefList(p2, 0); doStmtList(p3); out_of_a_layer(); }
void doDefList(TreeNode *p) { //printf("doDefList\n"); //printf("TreeNode->state:%s\n", p->state); switch (p->productionRule) { case 1:{ TreeNode *p1 = p->firstChild; TreeNode *p2 = p1->rightBrother; doDef(p1); doDefList(p2); break; } case 2: break; } }
varElement* doDefList(TreeNode *p, int ifStruct) { //printf("doDefList\n"); //printf("TreeNode->state:%s\n", p->state); switch (p->productionRule) { case 1:{ TreeNode *p1 = p->firstChild; TreeNode *p2 = p1->rightBrother; varElement *ele1 = doDef(p1, ifStruct); varElement *ele2 = doDefList(p2, ifStruct); if (ifStruct == 1) { //是结构体的成员处理 while (ele1->next != NULL) { //找到这个Def定义出的的最后一个varElement ele1 = ele1->next; } ele1->next = ele2; //和DefList的varElement链串联起来,返回 return ele1; } break; } case 2:{ return NULL; break; } } }