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

📄 mainfrm.cpp

📁 PASCAL编译器
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "pascalcompiler.h"

#include "MainFrm.h"
#include "PascalcompilerDoc.h"
#include "PascalcompilerView.h"
#include "example.h"
#include "DialogView1.h"
extern CPascalcompilerApp theApp;


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_LONERROR, OnLonerror)
	ON_COMMAND(ID_COMPILE, OnCompile)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	keyword[0] = "begin";
	keyword[1] = "do";
	keyword[2] = "else";
	keyword[3] = "end";
	keyword[4] = "if";
	keyword[5] = "then";
	keyword[6] = "var";
	keyword[7] = "while";
	KEYWORD[0] = "BIGIN";
	KEYWORD[1] = "DO";
	KEYWORD[2] = "ELSE";
	KEYWORD[3] = "END";
	KEYWORD[4] = "IF";
	KEYWORD[5] = "THEN";
	KEYWORD[6] = "VAR";
	KEYWORD[7] = "WHILE";
	fenjies[0] = ",";
	fenjies[1] = ";";
	fenjies[2] = "(";
	fenjies[3] = ")";
	fenjies[4] = ".";
	fenjies[5] = ":=";
	alus[0]    = "+";
	alus[1]    = "-";
	alus[2]    = "*";
	alus[3]    = "/";
	related[0] = "<";
	related[1] = "<=";
	related[2] = "=";
	related[3] = ">";
	related[4] = ">=";
	related[5] = "<>";
	markrow = 1;
	markerror = "";
	markerror1 = "";
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndToolBar);

	AnimateWindow(GetSafeHwnd(),1000,AW_SLIDE|AW_HOR_POSITIVE);
	return 0;
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	//cs.dwExStyle |= WS_EX_TOPMOST;
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnCompile() 
{
	// TODO: Add your command handler code here
	Cexample exa;
	exa.DoModal();
}
///////////////////////////////////////////
//                词法分析               //
///////////////////////////////////////////
void CMainFrame::OnLonerror()
{
	// TODO: Add your command handler code here
	show = "";
	CString getwordstr;
	CMainFrame *pFrame = (CMainFrame *)theApp.GetMainWnd();
	pFrame->m_Splitter.SetActivePane(0,0);
	CPascalcompilerView *pView = (CPascalcompilerView *)pFrame->GetActiveView();
	CEdit &edit=pView->GetEditCtrl();
	edit.GetWindowText(str);
	edit.GetWindowText(str1);
	if(str!="")
	{
		////////////////////词法分析//////////////////
		do
		{
			str = CMainFrame::DeleteNull(str);
			if(str != "")
			{
			getwordstr = CMainFrame::GetWord(str,getwordstr);
			if((getwordstr.GetAt(0)>='A'&&getwordstr.GetAt(0)<='Z')||
				(getwordstr.GetAt(0)>='a'&&getwordstr.GetAt(0)<='z'))
			{
				CMainFrame::anykw(getwordstr);
				show += mark;
				show += "\r";
				show += "\n";
				show += "  ";
			}
			else
			{
				if(getwordstr.GetAt(0)>='0'&&getwordstr.GetAt(0)<='9')
				{ 
					CMainFrame::changshuanyle(getwordstr);
					show += mark;
					show += "\r";
					show += "\n";
					show += "  ";
				}
				else
				{
					CMainFrame::qitaany(getwordstr);
					show += mark;
					show += "\r";
					show += "\n";
					show += "  ";
				}
			}
			}
			else
			{
				break;
			}
		}
		while(true);
		/////////////////////////////////////////////////////
		pFrame->m_Splitter.SetActivePane(1,0);
		CPascalcompilerView *pView1 = (CPascalcompilerView *)pFrame->GetActiveView();
		CEdit &edit1=pView1->GetEditCtrl();

		pFrame->m_Splitter.SetActivePane(0,1);
		CPascalcompilerView *pView2 = (CPascalcompilerView *)pFrame->GetActiveView();
		CEdit &edit2=pView2->GetEditCtrl();

		edit2.SetWindowText(show);
		CMainFrame::grammaranyle(str1);////////////语法分析
		if(markerror == "" && markerror1 == "")
		{
			edit1.SetWindowText("没有错误");
		}
		else
		{
			edit1.SetWindowText(markerror+markerror1);
		}
		
		markrow = 1;
		markerror = "";
	}
	else
	{
		MessageBox("你还没有输入程序");
		pFrame->m_Splitter.SetActivePane(1,0);
		CPascalcompilerView *pView1 = (CPascalcompilerView *)pFrame->GetActiveView();
		CEdit &edit1=pView1->GetEditCtrl();
		edit1.SetWindowText(markerror);
		pFrame->m_Splitter.SetActivePane(0,1);
		CPascalcompilerView *pView2 = (CPascalcompilerView *)pFrame->GetActiveView();
		CEdit &edit2=pView2->GetEditCtrl();
		edit2.SetWindowText(show);
	}
	
}

//////////////////删除多余的空格和回车//////////////////////
CString CMainFrame::DeleteNull(CString delestr)
{
	while (true)
	{
		int j = delestr.GetLength();
		for(int i = 0;i < delestr.GetLength();i++)
		{
			if(delestr[0] == 0x20 || delestr[0] == 0x0A || delestr[0] == 0x0D)
			{
				if(delestr[0] == 0x0A)
				{
					markrow++;//////////记录行
					delestr.Delete(0);
				}
				else
				{
					delestr.Delete(0);
				}
			}
			else
			{
				return delestr;
			}
		}
		if(i == j)
		{
			return delestr;
		}
	}		
}

/////////////////////////获得一个字进行判断/////////////////////
CString CMainFrame::GetWord(CString Wordstr, CString GetWordstr)
{
	CString no_use("@#$%^&~`(){},. ;\n+:=*/-<>\"\r");
	GetWordstr = Wordstr;
	CString temp = Wordstr;
	temp.GetBufferSetLength(2);
	if(temp == ":=" ||temp == "<=" ||temp == ">=" || temp == "<>")
	{
		GetWordstr.GetBufferSetLength(2);
		str.Delete(0);str.Delete(0);
	}
	else
	{
		temp = "";
		int j = GetWordstr.FindOneOf(no_use);
		if(j == 0)
		{
			GetWordstr.GetBufferSetLength(1);
			str.Delete(0);
		}
		else if(j == -1)
		{
			GetWordstr = "";
			str = "";
		}
		else
		{
			GetWordstr.GetBufferSetLength(j);
			for(int m = 0;m <= j-1;m++)
			{
				str.Delete(0);
			}
		}
	}
	return GetWordstr;
}

////////关键字和标识符判断/////////////
CString CMainFrame::anykw(CString Gw)
{
	for(int i = 0;i < 8;i++)
	{
		if(keyword[i] == Gw || KEYWORD[i] == Gw)
		{
			mark =Gw+"   是关键字";
			return mark;
		}
		if(Gw.GetLength() == 1)
		{
			mark = Gw+"   是标识符";
			return mark;
		}
	}
	mark = Gw;
	mark += "  是工程或函数名吗?";
	return mark;
}
/////////////////////常数判断//////////////////////
CString CMainFrame::changshuanyle(CString Gw)
{
	mark = Gw;
	mark += "  是常数";
	return mark;
}
////////分界符,关系符,运算符的判断////////////
CString CMainFrame::qitaany(CString Gw)
{
	for(int i = 0;i <6;i++)
	{
		if(fenjies[i] == Gw)
		{
			mark = Gw+"  是分界符";
			return mark;
		}
	}
	for(int i1 = 0;i1 < 6;i1++)
	{
		if(related[i1] == Gw)
		{
			mark = Gw+"  是关系符";
			return mark;
		}
	}
	for(int i2 = 0;i2 < 4;i2++)
	{
		if(alus[i2] == Gw)
		{
			mark = Gw+"  是运算符";
			return mark;
		}
	}
	mark = Gw;
	mark += "  是错误的符号";
	CString kk;
	kk.Format("%i",markrow);
	kk = "第" + kk;
	kk = kk + "行:   ";
	markerror += kk;
	markerror += mark;
	markerror += "\r";
	markerror += "\n";
	return mark;
}


BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext *pContext)
{
	CRect cr;
	GetClientRect(&cr);
	CSize paneSize(2*cr.Width()/3,2*cr.Height()/3);
	CSize paneSize1(2*cr.Width()/3,cr.Height()/3);
	CSize paneSize2(cr.Width()/3,2*cr.Height()/3);
	CSize paneSize3(cr.Width()/3,cr.Height()/3);
	m_Splitter.CreateStatic(this,2,2);
	m_Splitter.CreateView(0,0,RUNTIME_CLASS(CPascalcompilerView),
		paneSize,pContext);
	m_Splitter.CreateView(1,0,RUNTIME_CLASS(CPascalcompilerView),
		paneSize1,pContext);
	m_Splitter.CreateView(0,1,RUNTIME_CLASS(CPascalcompilerView),
		paneSize2,pContext);
	m_Splitter.CreateView(1,1,RUNTIME_CLASS(CDialogView1),
		paneSize3,pContext);
	return TRUE;
}

void CMainFrame::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	AnimateWindow(GetSafeHwnd(),1000,AW_HIDE|AW_HOR_POSITIVE);
	CFrameWnd::OnClose();
}
//////////////////////////////////////////////
////            语法分析                  ////
//////////////////////////////////////////////
void CMainFrame::grammaranyle(CString anystr)
{
	markerror1 = "";
	CString befor,begend;
	befor = anystr;
	begend = anystr;
	if(begend.Find(".")==-1)
	{
		markerror1 += "在程序的最后少了一个 '.'";
		markerror1 += "\r";
		markerror1 += "\n";
	}
	int i = befor.Find("begin");
	if(i == -1)
	{
		markerror1 += "没有函数体或少了一个 'begin'";
		markerror1 += "\r";
		markerror1 += "\n";
	}
	else
	{
	befor.GetBufferSetLength(i);
	////////////////////////////////////////////////
	//判断函数开始前的一些定义每一行后面是否有分号//
	////////////////////////////////////////////////
	while(true)
	{
		int cout = befor.GetLength();
		for(int n = 0;n < cout;n++)
		{
			if(befor[0] == 0x20 || befor[0] == 0x0A || befor[0] == 0x0D)
			{
				befor.Delete(0);
			}
			else
			{
				break;
			}
		}
		if(befor != "")
		{
			int j = befor.Find(0x0D);
			if(befor.GetAt(j-1)==';')
			{
				for(int m = 0;m <= j+1;m++)
				{
					befor.Delete(0);
				}
			}
			else
			{
				markerror1 += befor.GetAt(j-1);
				markerror1 += "后面少了一个 ';'";
				markerror1 += "\r";
				markerror1 += "\n";
				for(int m = 0;m <= j+1;m++)
				{
					befor.Delete(0);
				}
			}
		}
		else
		{
			break;
		}
	}
	////////////////////////////////////////
	///判断程序中的begin和end是否成对出现///
	///////////end后面是否有分号////////////
	for(int k = 0;k <i;k++)
	{
		begend.Delete(0);
	}
	while(begend != "")
	{
		for(int l = 0;l < 5;l++)
		{
			begend.Delete(0);
		}
		int end = begend.Find("begin");
		if(end != -1)
		{
			int end1 = end;
			while(end)
			{
				end--;
				if(begend.GetAt(end) == 'd')
				{
					if(begend.GetAt(end - 1) == 'n'
						&&begend.GetAt(end - 2) == 'e')
					{
						if(begend.GetAt(end+1) != ';')
						{
							markerror1 += "'end '后面少了一个 ';'";
							markerror1 += "\r";
							markerror1 += "\n";
							break;
						}
						else
						{
							break;
						}
					}
				}
			}
			if(end == 0)
			{
				markerror1 += "在'begin'前少了一个'end'";
				markerror1 += "\r";
				markerror1 += "\n";
			}
			for(int u = 0;u < end1;u++)
			{
				begend.Delete(0);
			}
		}
		else
		{
			end = begend.Find("end");
			if(end == -1)
			{
				markerror1 += "在程序的最后少了一个 'end'";
				markerror1 += "\r";
				markerror1 += "\n";
			}
			begend = "";
		}
	}
	///////////////////////////////////////////////////////
	}
}

⌨️ 快捷键说明

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