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

📄 compiler.cpp

📁 pl/0编译器的C++实现
💻 CPP
字号:
///////////////////////////////////////////////////////////////
//	PL/0 Compiler Shell 0.14 (2003.12.30) [Source File]
//	Author:Dwing
///////////////////////////////////////////////////////////////
#include"compiler.h"		//For Shell
///////////////////////////////////////////////////////////////
HINSTANCE hinst;	//Recent Instance
OPENFILENAME ofn;	//For Open File Dialog
FILE *fp;			//For Open File
zCompile *cp;		//编译器对象指针
int asmnum=0;		//目标代码数量
int errnum=0;		//错误数量
char buf[65536];	//I/O Buffer
char str[512];		//File Name Buffer
///////////////////////////////////////////////////////////////
void _main()		//Program Entry
{
//Get Instance Handle
	hinst=GetModuleHandle(0);
//Main DialogBox
	DialogBox(hinst,MAKEINTRESOURCE(IDD_MAINDLG),0,(DLGPROC)dlgproc);
}
///////////////////////////////////////////////////////////////
//For MAIN DialogBox
BOOL CALLBACK dlgproc(HWND hdlg,UINT msg,WPARAM wparam,LPARAM lparam)
{
	int i;
	switch(msg)
	{
	case WM_INITDIALOG:
		buf[65535]=0;	//结尾清0防止溢出
		str[0]=0;		//Clear str[]
	//Set Window Text Font
		SendDlgItemMessage(hdlg,IDC_INPUT,WM_SETFONT,(DWORD)GetStockObject(ANSI_FIXED_FONT),0);
		SendDlgItemMessage(hdlg,IDC_ASM,WM_SETFONT,(DWORD)GetStockObject(SYSTEM_FIXED_FONT),0);
		SendDlgItemMessage(hdlg,IDC_ERR,WM_SETFONT,(DWORD)GetStockObject(SYSTEM_FIXED_FONT),0);
	//填充OPENFILENAME结构体
		memset(&ofn,0,sizeof(OPENFILENAME));	//Clear it First
		ofn.lStructSize=sizeof(OPENFILENAME);
		ofn.hwndOwner=hdlg; 
		ofn.hInstance=hinst; 
		ofn.lpstrFilter="所有文件(*.*)\0*.*\0";
		ofn.nMaxFile=511;		//最大文件名缓冲区长度
		ofn.lpstrFile=str;		//使用临时字符串
		ofn.Flags=OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
		ofn.lpstrTitle="打开PL/0程序文件";
		return 1;
	case WM_COMMAND:
		switch(LOWORD(wparam))
		{
		case ID_COMPILE:
			SetCursor(LoadCursor(0,IDC_WAIT));			//Please wait...
			GetDlgItemText(hdlg,IDC_INPUT,buf,65535);	//Get PL/0 Text
			cp=new zCompile;							//Let's begin to compile...
			cp->compile(buf);							//Do the most complex part
			errnum=cp->geterr(buf);						//Get Error Text
			SetDlgItemText(hdlg,IDC_ERR,buf);			//Display it
			asmnum=cp->getasm(buf);						//Get ASM Text
			SetDlgItemText(hdlg,IDC_ASM,buf);			//Display it
			delete cp;									//The end of Compiling
			SetCursor(LoadCursor(0,IDC_ARROW));			//Completed
			break;
		case ID_RUN:
			if(!asmnum)
				MessageBox(hdlg,"请先编译后再运行!","错误",MB_ICONERROR);
			else
				if(errnum)
					MessageBox(hdlg,"请先改正错误后再运行!","错误",MB_ICONERROR);
				else
				{
					fp=fopen("temp.pl0","wb");	//Write to temp file
					fprintf(fp,"%u\r\n",asmnum);
					fwrite(buf,1,strlen(buf),fp);
					fclose(fp);
					WinExec("pl0run",1);		//Call PL/0 Interpreter
				}
			break;
		case ID_OPEN:
			if(!GetOpenFileName(&ofn)) break;
			str[512]=0;				//防止溢出
			if(!(fp=fopen(ofn.lpstrFile,"rb")))
				MessageBox(hdlg,str,"无法打开文件!",MB_ICONERROR);
			else
			{
				fseek(fp,0,SEEK_END);
				if((i=ftell(fp))>65535)
					MessageBox(hdlg,"文件过长(>=64K)!","错误",MB_ICONERROR);
				else
				{
					fseek(fp,0,SEEK_SET);
					fread(buf,1,i,fp);
					buf[i]=0;			//防止溢出
					SetDlgItemText(hdlg,IDC_INPUT,buf);
				}
				fclose(fp);
			}
			break;
		case ID_ABOUT:
			DialogBox(hinst,MAKEINTRESOURCE(IDD_ABOUT),0,(DLGPROC)abtproc);
		}
		return 1;
	case WM_CLOSE:
		EndDialog(hdlg,0);
	}
	return 0;
}
///////////////////////////////////////////////////////////////
//For ABOUT DialogBox
BOOL CALLBACK abtproc(HWND hdlg,UINT msg,WPARAM wparam,LPARAM lparam)
{
	switch(msg)
	{
	case WM_LBUTTONDOWN:												//When dragging me...
		PostMessage(hdlg,WM_NCLBUTTONDOWN,2,0);
		return 1;
	case WM_COMMAND:
		if(LOWORD(wparam)==IDOK)
			EndDialog(hdlg,0);
	}
	return 0;
}
///////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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