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

📄 semantic.cpp

📁 编译器:用于实现一个简单的编译器的语法分析功能
💻 CPP
字号:
#include "semantics.h"

extern double Parameter,
			Origin_x,Origin_y,
			Scale_x,Scale_y,
			Rot_angle;
double GetExprValue(struct ExprNode *root);
void DrawPixel(unsigned long x ,unsigned long y);
void DrawLoop(double Start,
			  double End,
			  double Step,
			  struct ExprNode *HorPtr,
			  struct ExprNode *VerPtr);
void DelExprTree(struct ExprNode *root);

static void Errmsg(char *string);

static CalcCoord(struct ExprNode *Hor_Exp,
				 struct ExprNode *Ver_Exp,
				 double &Hor_x,
				 double &Ver_y);
//--------------------------------------
void Errmsg(char *string) 
{
	exit(1);
};
//-----------------------------------
static CalcCoord(struct ExprNode *Hor_Exp,
				 struct ExprNode *Ver_Exp,
				 double &Hor_x,
				 double &Ver_y)
{
	double HorCord,VerCord,Hor_tmp;

	HorCord=GetExprValue(Hor_Exp);
	VerCord=GetExprValue(Ver_Exp);
	//
	HorCord*=Scale_x;
	VerCord*=Scale_y;
	//
	Hor_tmp=HorCord * cos(Rot_angle)+VerCord * sin(Rot_angle);
	VerCord=VerCord * cos(Rot_angle)-VerCord * sin(Rot_angle);
	HorCord=Hor_tmp;
	//
	HorCord+=Origin_x;
	VerCord+=Origin_y;
	//
	Hor_x=HorCord;
	Ver_y=VerCord;
}
//------------循环绘制点坐标
void DrawLoop(double Start,
			  double End,
			  double Step,
			  struct ExprNode *HorPtr,
			  struct ExprNode *VerPtr)
{
	extern double Parameter;
	double x,y;
	for(Parameter=Start;Parameter<=End;Parameter+=Step)
	{
		CalcCoord(HorPtr,VerPtr,x,y);
		DrawPixel((unsigned long) x,(unsigned long) y);
	}

}
//=------------------------计算表达式的值-------------------
double GetExprValue(struct ExprNode *root)
{
	if (root==NULL)
		return 0.0;
	switch (root->OpCode )
	{
	case PLUS:
		return GetExprValue(root->Content .CaseOperator.Left)+GetExprValue(root->Content .CaseOperator .Right);
	case MINUS:
		return GetExprValue(root->Content .CaseOperator.Left)-GetExprValue(root->Content .CaseOperator .Right);
	case DIV:
		return GetExprValue(root->Content .CaseOperator.Left)/GetExprValue(root->Content .CaseOperator .Right);
	case MUL:
		return GetExprValue(root->Content .CaseOperator.Left)*GetExprValue(root->Content .CaseOperator .Right);;
	case POWER:
		return pow(GetExprValue(root->Content .CaseOperator.Left),GetExprValue(root->Content .CaseOperator .Right));
	case FUNC: 
		return (*root->Content.CaseFunc .MathFuncPtr )(GetExprValue(root->Content .CaseFunc .Child));
	case CONST_ID:
		return root->Content .CaseConst ;
	case T:
		return *(root->Content .CaseParmPtr );
	default:
		return 0.0;
	}
}
//--------------------删除一棵语法树----------------------------
void DelExprTree(struct ExprNode *root)
{
	if (root==NULL)
		return;
	switch (root->OpCode)
	{
	case PLUS:
	case MINUS:
	case MUL:
	case DIV:
	case POWER:
		DelExprTree(root->Content .CaseOperator .Left);
		DelExprTree(root->Content .CaseOperator .Right);
		break;
	case FUNC:
		DelExprTree(root->Content .CaseFunc .Child );
		break;
	default:
		break;
	}
	delete(root);
}
#ifdef _BC_COMPILER
int InGraaphMode=0;
int InitGraph(void)
{
	int gd=DETECT,gm;
	if(InGraphMode)
		return 0;
	registerbgidrkver(EGAVGA_driver);
	initgraph(&gd,&gm,"");
	setcolor(-1);
	InGraphMode=1;
	return 1;

}

void CloseGraph(void)
{
	if(!InGraphMode)
		return;
	getch();
	closegraph();
	InGraphMode=0;


}
#endif
//-----------------绘制一个点---------------------------
void DrawPixel(unsigned long x,unsigned long y)
{
#ifdef _VC_COMPILER
	SetPixel(hDC,x,y,black);
#endif;

#ifdef _BC_COMPILER
	putpixel(x,y,white);
#endif
}

#ifdef _VC_COMPILER
#include"semantic.h"
#define MAX_CHARS 200
HDC hDC;

char SrcFilePath[MAX_CHARS];
static char Name="Compiler";

//-----------初始化窗口函数声明
static bool PrepareWindow(HINSTANCE,HINSTANCE,int);
 
//---------检查源程序文件是否合法函数声明
static bool CheckSrcFile(LPSTR);

//-----窗口消息处理函数声明
static LRESULT CALLBACK WndProc(HWND,UNIT,WPARAM,LPARAM);

//------window程序主函数
int APIENTRY Winmain(HINSTANCE hInstance,
					 HINSTANCE hPreInstance,
					 LPSTR ilCmdLine,
					 int nCmdShow)
{
	strcpy(SrcFilePath,lpCmdLine);
	
	if(PrepareWindow(hInstance,hPrevInstance,nCmdShow)!=true)
	{
		MessageBox(NULL,"窗口初始化失败!","错误",MB_OK);
		return 1;
	}
	
	
	
    if(!CheckSrcFile(lpCmdLine)) return 1;
	
	
    Parser(SrcFilePath);
	
	MSG Msg;
	while(GetMessage(&Msg,NULL,0,0))
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	
	return Msg.wParam;
}


#endif

⌨️ 快捷键说明

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