⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser_tab.c

📁 一个面向对像语言的编译器
💻 C
📖 第 1 页 / 共 5 页
字号:
							}

							//设置类成员函数的offset
							if(flag)
							{
								tScope=scopes->GetCurrentScope();

								//先查找该函数是否已经声明过
								Declaration *temp=tScope->Lookup(yyvsp[-3].identifier);
								if(temp!=NULL)
								{
									//如果已经声明过,将现在定义的函数的offset设置为前面声明的offset
									if(temp->IsFunctionDecl())
									{
										int offset=temp->GetOffset();
										d->SetOffset(offset);
										temp->MakeFunDefined();
									}
								}
								else
									cg->SetMethodOffset(d);
							}
							
							//将标示函数是否已经定义过的标识置为有效
							d->MakeFunDefined();

							yyval.decl=d;
							yyval.decl=d;
							scopes->Declare(d);

							scopes->PushScope(new Scope(d)); 
							cg->InitParaOffset();
						
							if(flag)
							{
								//如果是类中的函数定义,就将this作为第一个形参
								mythis=new Declaration(Declaration::Variable, "this", d->GetScope()->GetClassType());
								cg->SetParaOffset(mythis);
								scopes->Declare(mythis);
							}		
									
							DeclList *paraList=yyvsp[-1].declList;
							int paraNum=paraList->NumElements();
							
							//设置个形参的offset
							for(int i=0; i<paraNum; i++)
							{
								Declaration *tDecl=paraList->Nth(i);
								cg->SetParaOffset(tDecl);
							}
							cg->GenBeginFunc(d);
							cg->InitLocalOffset();
							
							char *temp=cg->NewLabel();
							cg->PushRetLabel(temp);
						;
    break;}
case 32:
#line 479 "parser.y"
{ 
							Declaration *funDecl;
							funDecl=yyvsp[-1].decl;

							//设置函数local域的stack size
							funDecl->SetStackFrameSize(cg->GetStackFrameSize(LocalStackSize));
							scopes->PopScope();
							
							cg->GenLabel(cg->GetRetTopLabel());	
							cg->GenEndFunc(funDecl);
							cg->PopRetLabel(1);
						;
    break;}
case 33:
#line 494 "parser.y"
{
						 scopes->PushScope(new Scope(Scope::Local)); 
						;
    break;}
case 34:
#line 498 "parser.y"
{ scopes->PopScope(); ;
    break;}
case 37:
#line 506 "parser.y"
{;
    break;}
case 38:
#line 507 "parser.y"
{;
    break;}
case 39:
#line 508 "parser.y"
{;
    break;}
case 40:
#line 509 "parser.y"
{;
    break;}
case 41:
#line 510 "parser.y"
{;
    break;}
case 42:
#line 511 "parser.y"
{;
    break;}
case 43:
#line 512 "parser.y"
{;
    break;}
case 44:
#line 513 "parser.y"
{;
    break;}
case 45:
#line 514 "parser.y"
{;
    break;}
case 46:
#line 518 "parser.y"
{
							Declaration *debug1, *debug2;
							debug1 = yyvsp[-2].decl, debug2 = yyvsp[0].decl;
							
							//LValue是地址的情况
							if(yyvsp[-2].decl->IsAddr())
							{
								if(yyvsp[0].decl->IsAddr())
								{
									cg->GenAssign(yyvsp[-2].decl, yyvsp[0].decl);
								}
								else
								{
									cg->GenStore(yyvsp[-2].decl,yyvsp[0].decl);
								}
							}
							//LValue不是地址的情况
							else
							{
								if(yyvsp[0].decl->IsAddr())
								{
									Declaration *decl=cg->GenLoad(yyvsp[0].decl, yyvsp[0].decl->GetType());
									cg->GenAssign(yyvsp[-2].decl, decl);
								}
								else
									cg->GenAssign(yyvsp[-2].decl,yyvsp[0].decl);
							}
						;
    break;}
case 47:
#line 547 "parser.y"
{
								
						;
    break;}
case 48:
#line 552 "parser.y"
{;
    break;}
case 49:
#line 556 "parser.y"
{
							Declaration *temp=yyvsp[-1].decl;
							yyval.decl=yyvsp[-1].decl;
						;
    break;}
case 50:
#line 561 "parser.y"
{yyval.decl=NULL;;
    break;}
case 51:
#line 565 "parser.y"
{
							//调用semantic.cpp中的函数处理LValue
							yyval.decl=DealWithLValue(yyvsp[-1].decl, yyvsp[0].identifier);							
						;
    break;}
case 52:
#line 570 "parser.y"
{
							//调用semantic.cpp中的函数处理数组引用的情况
							yyval.decl=DealWithArray(yyvsp[-3].decl,yyvsp[-1].decl);
						;
    break;}
case 53:
#line 577 "parser.y"
{
							//调用semantic.cpp中的函数处理函数调用
							yyval.decl=DealWithCall(yyvsp[-4].decl, yyvsp[-3].identifier, yyvsp[-1].declList);
						;
    break;}
case 54:
#line 584 "parser.y"
{
							if(yyvsp[0].decl->IsAddr())
							{
								Declaration *decl=cg->GenLoad(yyvsp[0].decl, yyvsp[0].decl->GetType());
								yyval.decl=decl;
							}
							else
								yyval.decl=yyvsp[0].decl;
						;
    break;}
case 55:
#line 595 "parser.y"
{
							yyval.decl=yyvsp[0].decl;
						;
    break;}
case 56:
#line 598 "parser.y"
{yyval.decl = yyvsp[0].decl;;
    break;}
case 57:
#line 600 "parser.y"
{yyval.decl=cg->GenBinaryOp("+", yyvsp[-2].decl, yyvsp[0].decl, yyvsp[-2].decl->GetType());;
    break;}
case 58:
#line 602 "parser.y"
{yyval.decl=cg->GenBinaryOp("-", yyvsp[-2].decl, yyvsp[0].decl, yyvsp[-2].decl->GetType());;
    break;}
case 59:
#line 604 "parser.y"
{yyval.decl=cg->GenBinaryOp("/", yyvsp[-2].decl, yyvsp[0].decl, yyvsp[-2].decl->GetType());;
    break;}
case 60:
#line 606 "parser.y"
{yyval.decl=cg->GenBinaryOp("*", yyvsp[-2].decl, yyvsp[0].decl, yyvsp[-2].decl->GetType());;
    break;}
case 61:
#line 608 "parser.y"
{yyval.decl=cg->GenBinaryOp("%", yyvsp[-2].decl, yyvsp[0].decl, Type::intType);
    break;}
case 62:
#line 610 "parser.y"
{

							Declaration *dec1=yyvsp[-2].decl, *dec2=yyvsp[0].decl;
							//整形数的比较
							if(yyvsp[-2].decl->GetType()->IsEquivalentTo(Type::intType))
								yyval.decl=cg->GenBinaryOp("==", yyvsp[-2].decl, yyvsp[0].decl, Type::boolType);
							
							//字符串的比较
							if(yyvsp[-2].decl->GetType()->IsEquivalentTo(Type::stringType))
							{
								cg->GenPushParam(yyvsp[0].decl);
								cg->GenPushParam(yyvsp[-2].decl);
								yyval.decl=cg->GenLCall("_StringEqual", VarSize*2, Type::boolType);
							}
							
							//类类型的比较
							if((yyvsp[-2].decl->GetType()->IsClassType())&&(yyvsp[0].decl->GetType()->IsClassType()))
							{
								yyval.decl=cg->GenBinaryOp("==", yyvsp[-2].decl, yyvsp[0].decl, Type::boolType);
							}	
						;
    break;}
case 63:
#line 632 "parser.y"
{
							Declaration *dec1, *dec2;
							//处理int型数
							if(yyvsp[-2].decl->GetType()->IsEquivalentTo(Type::intType))
							{
								dec1=cg->GenBinaryOp("<", yyvsp[-2].decl, yyvsp[0].decl, Type::boolType);
								dec2=cg->GenBinaryOp("<", yyvsp[0].decl, yyvsp[-2].decl, Type::boolType);
								yyval.decl=cg->GenBinaryOp("||", dec1, dec2, Type::boolType);
							}

							//处理string类型
							if(yyvsp[-2].decl->GetType()->IsEquivalentTo(Type::stringType))
							{
								cg->GenPushParam(yyvsp[0].decl);
								cg->GenPushParam(yyvsp[-2].decl);
								dec1=cg->GenLCall("_StringEqual", VarSize*2, Type::boolType);
								dec2=cg->GenLoadConstant(1, Type::intType);
								yyval.decl=cg->GenBinaryOp("-", dec2, dec1, Type::boolType);
							}
							//处理类类型

							if((yyvsp[-2].decl->GetType()->IsClassType())&&(yyvsp[0].decl->GetType()->IsClassType()))
							{
								dec1=cg->GenBinaryOp("==", yyvsp[-2].decl, yyvsp[0].decl, Type::boolType);
								dec2=cg->GenLoadConstant(1, Type::intType);
								yyval.decl=cg->GenBinaryOp("-", dec2, dec1, Type::boolType);
							}	

						;
    break;}
case 64:
#line 663 "parser.y"
{yyval.decl=cg->GenBinaryOp("<", yyvsp[-2].decl, yyvsp[0].decl, Type::boolType);;
    break;}
case 65:
#line 666 "parser.y"
{yyval.decl=cg->GenBinaryOp("<", yyvsp[0].decl, yyvsp[-2].decl, Type::boolType);;
    break;}
case 66:
#line 668 "parser.y"
{
							Declaration *dec1, *dec2;
							dec1=cg->GenBinaryOp("<", yyvsp[-2].decl, yyvsp[0].decl, Type::boolType);
							dec2=cg->GenBinaryOp("==", yyvsp[-2].decl, yyvsp[0].decl, Type::boolType);
							yyval.decl=cg->GenBinaryOp("||", dec1, dec2, Type::boolType);
						;
    break;}
case 67:
#line 675 "parser.y"
{
							Declaration *dec1, *dec2;
							dec1=cg->GenBinaryOp("<", yyvsp[0].decl, yyvsp[-2].decl, Type::boolType);
							dec2=cg->GenBinaryOp("==", yyvsp[-2].decl, yyvsp[0].decl, Type::boolType);
							yyval.decl=cg->GenBinaryOp("||", dec1, dec2, Type::boolType);
						;
    break;}
case 68:
#line 682 "parser.y"
{yyval.decl=cg->GenBinaryOp("&&", yyvsp[-2].decl, yyvsp[0].decl, Type::boolType);;
    break;}
case 69:
#line 684 "parser.y"
{yyval.decl=cg->GenBinaryOp("||", yyvsp[-2].decl, yyvsp[0].decl, Type::boolType);;
    break;}
case 70:
#line 686 "parser.y"
{yyval.decl = yyvsp[-1].decl;;
    break;}
case 71:
#line 688 "parser.y"
{
							Declaration *dec;
							dec=cg->GenLoadConstant(0, Type::intType);
							yyval.decl=cg->GenBinaryOp("-", dec, yyvsp[0].decl, Type::intType);
						;
    break;}
case 72:
#line 694 "parser.y"
{
							Declaration *dec;
							dec=cg->GenLoadConstant(0, Type::boolType);
							yyval.decl=cg->GenBinaryOp("==", dec, yyvsp[0].decl, Type::boolType);
						;
    break;}
case 73:
#line 700 "parser.y"
{
							yyval.decl=cg->GenBuiltInCall(ReadInteger, Type::intType);
						;
    break;}
case 74:
#line 704 "parser.y"
{
							yyval.decl=cg->GenBuiltInCall(ReadLine, Type::stringType);
						;
    break;}
case 75:
#line 708 "parser.y"
{
							yyval.decl=scopes->Lookup("this");
						;
    break;}
case 76:
#line 712 "parser.y"
{
							Declaration *dec1, *dec2, *dec3;
							dec1=scopes->Lookup(yyvsp[-1].identifier);
							dec2=cg->GenLoadConstant(dec1->GetStackFrameSize(), Type::intType);
							dec3=cg->GenBuiltInCall(Alloc, dec1->GetType(), dec2);
							cg->GenStore(dec3, cg->GenLoadLabel(yyvsp[-1].identifier));
							yyval.decl=dec3;
						;
    break;}
case 77:
#line 721 "parser.y"
{
							//数组的分配过程按ppt中的过程写
							Declaration *dec1, *dec2, *dec3, *dec4, *dec5, *dec6, *dec7;
							yyval.label=cg->NewLabel();
							char *debug =yyval.label;
							dec1=cg->GenLoadConstant(0, Type::intType);
							dec2=cg->GenBinaryOp("<", yyvsp[-3].decl, dec1, Type::boolType);
							
							
							cg->GenIfZ(dec2, yyval.label);

							dec3=cg->GenLoadConstant(err_arr_neg_size);
							cg->GenPushParam(dec3);
							cg->GenLCall("_PrintString",VarSize,Type::voidType);
							cg->GenLCall("_Halt", 0, Type::voidType);

							cg->GenLabel(yyval.label);
							
							dec4=cg->GenLoadConstant(VarSize, Type::intType);
							dec5=cg->GenBinaryOp("*", dec4, yyvsp[-3].decl, Type::intType);
							dec6=cg->GenBinaryOp("+", dec4, dec5, Type::intType);
							dec7=cg->GenBuiltInCall(Alloc,Type::NewArrayType(yyvsp[-1].type),dec6);
							
							cg->GenStore(dec7, yyvsp[-3].decl);
							yyval.decl=dec7;
							//$$->ValidateAddr();
						;
    break;}
case 78:
#line 751 "parser.y"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -