Exemplo n.º 1
0
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;
			   }
	}
}
Exemplo n.º 2
0
void doStmt(TreeNode *p) {
	//printf("doStmt\n");
	//printf("TreeNode->state:%s\n", p->state);
	//printf("TreeNode->productionRule: %d\n", p->productionRule);
	switch (p->productionRule) {
		case 1:{
			TreeNode *p1 = p->firstChild;
			doExp(p1);
			break;
			   }
		case 2:{
			TreeNode *p1 = p->firstChild;
			doCompSt(p1);
			break;
			   }
		case 3:	{//这块之后要做函数返回类型检查
			TreeNode *p2 = p->firstChild->rightBrother;
			doExp(p2);
			break;
				}
		case 4:	{
			//这块可以判断一下p3代表的exp是不是int。不过实验讲义假设一定是
			//后两个同理
			TreeNode *p3 = p->firstChild->rightBrother->rightBrother;
			TreeNode *p5 = p3->rightBrother->rightBrother;
			doExp(p3);
			doStmt(p5);
			break;
				}
		case 5:{
			TreeNode *p3 = p->firstChild->rightBrother->rightBrother;
			TreeNode *p5 = p3->rightBrother->rightBrother;
			TreeNode *p7 = p5->rightBrother->rightBrother;
			doExp(p3);
			doStmt(p5);
			doStmt(p7);
			break;
			   }
		case 6:{
			TreeNode *p3 = p->firstChild->rightBrother->rightBrother;
			TreeNode *p5 = p3->rightBrother->rightBrother;
			doExp(p3);
			doStmt(p5);
			break;
			   }
	}
}
Exemplo n.º 3
0
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;
			   }
	}
}
Exemplo n.º 4
0
void doStmt(TreeNode *p) {
	//printf("doStmt\n");
	//printf("TreeNode->state:%s\n", p->state);
	//printf("TreeNode->productionRule: %d\n", p->productionRule);
	switch (p->productionRule) {
		case 1:{
			TreeNode *p1 = p->firstChild;
			doExp(p1);
			break;
			   }
		case 2:{
			TreeNode *p1 =	 p->firstChild;
			doCompSt(p1);
			break;
			   }
		case 3:	{
			//printf("in return\n");
			TreeNode *p2 = p->firstChild->rightBrother;
			Type type = doExp(p2);
			if (!type_equal(type, funcTableHeader->type)) {	//直接和函数表中第一项比较,一定是最近的函数
				printf("Error 8 at line %d: function return unexpected type\n", p->line);
			}
			//printf("out return\n");
			break;
				}
		case 4:	{
			//这块可以判断一下p3代表的exp是不是int。不过实验讲义假设一定是
			//后两个同理
			TreeNode *p3 = p->firstChild->rightBrother->rightBrother;
			TreeNode *p5 = p3->rightBrother->rightBrother;
			doExp(p3);
			doStmt(p5);
			break;
				}
		case 5:{
			TreeNode *p3 = p->firstChild->rightBrother->rightBrother;
			TreeNode *p5 = p3->rightBrother->rightBrother;
			TreeNode *p7 = p5->rightBrother->rightBrother;
			doExp(p3);
			doStmt(p5);
			doStmt(p7);
			break;
			   }
		case 6:{
			TreeNode *p3 = p->firstChild->rightBrother->rightBrother;
			TreeNode *p5 = p3->rightBrother->rightBrother;
			loopNum++;
			doExp(p3);
			doStmt(p5);
			loopNum--;
			break;
			   }
		case 7: {
			if (loopNum == 0)
				printf("Error 18 at line %d: use 'break' out of a loop\n", p->line);
			break;
			}
		case 8: {
			if (loopNum == 0)
				printf("Error 19 at line %d: use 'continue' out of a loop\n", p->line);
			break;
			}
		case 9:{
			//the for loop,待完善,应该是中间那个要做个type检查。
			TreeNode *p3 = p->firstChild->rightBrother->rightBrother;
			TreeNode *p5 = p3->rightBrother->rightBrother;
			TreeNode *p7 = p5->rightBrother->rightBrother;
			TreeNode *p9 = p7->rightBrother->rightBrother;
			loopNum++;
			doOptExp(p3);
			doExp(p5);
			doOptExp(p7);
			doStmt(p9);
			loopNum--;
			   }
	}
}