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

📄 scanner.cpp

📁 一个简单的图形解释语言编译程序,使用visual C++ 6.0开发
💻 CPP
字号:
#include "stdafx.h"
//#include "Lex.h"
#include "Parser.h"
//#include <windows.h>
//#include <windows.h>
//#include <wingdi.h>

#define MAX_CHARS 30
HDC hdc;
char SrcFileName[MAX_CHARS] = "test.txt";
char Name[] = "Compiler";
CParser parser;
void show();
bool CheckSrcFile(LPSTR filename);
bool PrepareWindow(HINSTANCE hInstance,HINSTANCE hPrevInstance,int nCmdShow);
LRESULT CALLBACK WndProc(HWND, UINT,WPARAM,LPARAM);
int APIENTRY WinMain(	HINSTANCE hInstance,
										HINSTANCE hPrevInstance,
										LPSTR lpCmdLine,
										int nCmdShow)
{
	
	
	if(PrepareWindow(hInstance,hPrevInstance,nCmdShow) != true)
	{
		MessageBox(NULL,"窗口初始化失败!","错误",MB_OK);
		return 0;
	}
	strcpy(SrcFileName,"test.txt");
	if(!CheckSrcFile(SrcFileName))
	{
		return 0;
	}
	
//	show();
	parser.Parser(SrcFileName,hdc);

	MSG msg;
	while(GetMessage(&msg,NULL,0,0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

void show()
{
	char FileName[100] = "TEST.TXT";
	char Name[] = "Compiler";
	CLex mylex;Token t;
	
	CParser myparser;
	printf("Hello World!\n");
	printf("Please Input Source File:\n");
	scanf("%s",FileName);
	if(!mylex.InitLex(FileName))
	{
		printf("Cannot Open Source File!\nOpen Default File:TEST.TXT\n");
		strcpy(FileName,"TEST.txt");
		mylex.InitLex(FileName);
	}
	printf("记号类别    字符串    常数值         函数指针\n");
	printf("--------------------------------------------------\n");
	while(1)
	{
		t = mylex.GetToken();
		if(t.type == NONTOKEN)
			break;
		else
		{
			printf("%4d%12s%12f%12x\n",t.type,t.lexeme,t.value,t.FuncPtr);
		}
	}
	printf("--------------------------------------------------\n");
	mylex.CloseLex();
	myparser.Parser(FileName,hdc);
	
	return ;
}

bool PrepareWindow(HINSTANCE hInstance,HINSTANCE  hPrevInstance,int nCmdShow)
{
	HWND hWnd;
	WNDCLASS W;
	memset(&W,0,sizeof(WNDCLASS));
	W.style = CS_HREDRAW | CS_VREDRAW;
	W.lpfnWndProc = WndProc;
	W.hInstance = hInstance;
	W.hCursor = LoadCursor(NULL,IDC_ARROW);
	W.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	W.lpszClassName = Name;
	RegisterClass(&W);
	
	hWnd = CreateWindow(Name,Name,
		WS_OVERLAPPEDWINDOW,10,10,740,490,NULL,NULL,hInstance,NULL);
	if(hWnd == NULL)
		return false;
	hdc = GetDC(hWnd);
	ShowWindow(hWnd,nCmdShow);
	UpdateWindow(hWnd);
	SetCursor(LoadCursor(hInstance,IDC_ARROW));
	
	return true;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
{
	switch(Message)
	{
	case WM_DESTROY:
		ReleaseDC(hWnd,hdc);
		PostQuitMessage(0);
		return 0;
		break;
//	case WM_CREATE:
	case WM_PAINT:
		PAINTSTRUCT pt;
		BeginPaint(hWnd,&pt);
		parser.Parser(SrcFileName,hdc);
		EndPaint(hWnd,&pt);
	default:
		return DefWindowProc(hWnd,Message,wParam,lParam);
	}
}
bool CheckSrcFile(LPSTR filename)
{
	FILE *file = NULL;
	if(strlen(filename) == 0)
	{
		MessageBox(NULL,"未指定源程序","错误!",MB_OK);
		return false;
	}
	if((file = fopen(filename,"r")) == NULL)
	{
		MessageBox(NULL,"打开源程序文件失败!","错误!",MB_OK);
		MessageBox(NULL,filename,"文件名",MB_OK);
		return false;
	}
	else fclose(file);
	return true;
}

⌨️ 快捷键说明

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