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

📄 mainfrm.cpp

📁 PL0的编译系统,使用MFC编写界面,采用浮动窗口来仿VC的环境
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "My1.h"
#include "MainFrm.h"
#include <fstream.h>
//#include "My1View.h"
#include "Code.h"
#include "Error.h"
#include "Splash.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

class CMy1View;
#include "Table.h"
/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_WM_INITMENU()
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_COMMAND(ID_SHOW, OnShow)
	ON_COMMAND(IDM_EXECUTE, OnExecute)
	ON_COMMAND(IDM_COMPILE, OnCompile)
	ON_COMMAND(ID_SAVE, OnSave)
	ON_WM_CLOSE()
	ON_COMMAND(ID_SHOWRESULT, OnShowresult)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

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

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	mCompiler=NULL;
	mCurrentFile[0]='\0';
	mCompileSuccess=0;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	CSplashWnd::ShowSplashScreen(this);
	Sleep(2100);
	SetIcon(AfxGetApp()->LoadIcon(IDR_MAINFRAME),false);	

	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);
	
	OnViewShort();

	m_CtrlBar.SetFlag(1);
	if(!m_CtrlBar.Create("Function View",this, CSize(200,200),TRUE,123))
	{
		TRACE0("Failed to create mybar\n");
		return -1;
		// fail to create
	}
	m_CtrlBar.SetBarStyle(m_CtrlBar.GetBarStyle()|
		CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
	m_CtrlBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_CtrlBar,AFX_IDW_DOCKBAR_LEFT);

	m_CtrlBar1.SetFlag(0);
	if(!m_CtrlBar1.Create("Output Window",this, CSize(200,100),TRUE,123))
	{
		TRACE0("Failed to create mybar\n");
		return -1;
		// fail to create
	}
	m_CtrlBar1.SetBarStyle(m_CtrlBar1.GetBarStyle()|
		CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
	m_CtrlBar1.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_CtrlBar1,AFX_IDW_DOCKBAR_BOTTOM);

	
	// CG: The following line was added by the Splash Screen component.	//CSplashWnd::ShowSplashScreen(this);	
	CenterWindow(this);	
	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

	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::OnViewShort()
{
	m_wndToolBar.SetButtons(NULL,14);
	m_wndToolBar.SetButtonInfo(0,ID_FILE_NEW,TBBS_BUTTON,0);
	m_wndToolBar.SetButtonInfo(1,ID_FILE_OPEN,TBBS_BUTTON,1);
	m_wndToolBar.SetButtonInfo(2,ID_FILE_SAVE,TBBS_BUTTON,2);
	m_wndToolBar.SetButtonInfo(3,ID_SEPARATOR,TBBS_SEPARATOR,15);
	m_wndToolBar.SetButtonInfo(4,ID_EDIT_CUT,TBBS_BUTTON,4);
	m_wndToolBar.SetButtonInfo(5,ID_EDIT_COPY,TBBS_BUTTON,5);
	m_wndToolBar.SetButtonInfo(6,ID_EDIT_PASTE,TBBS_BUTTON,6);
	m_wndToolBar.SetButtonInfo(7,ID_SEPARATOR,TBBS_SEPARATOR,15);
	m_wndToolBar.SetButtonInfo(8,ID_FILE_PRINT,TBBS_BUTTON,7);
	m_wndToolBar.SetButtonInfo(9,ID_APP_ABOUT,TBBS_BUTTON,8);
	m_wndToolBar.SetButtonInfo(10,ID_SEPARATOR,TBBS_SEPARATOR,15);
	m_wndToolBar.SetButtonInfo(11,IDM_COMPILE,TBBS_BUTTON,9);
	m_wndToolBar.SetButtonInfo(12,ID_SHOWRESULT,TBBS_BUTTON,10);
	m_wndToolBar.SetButtonInfo(13,IDM_EXECUTE,TBBS_BUTTON,11);
	m_wndToolBar.Invalidate();
	AfxGetApp()->OnIdle(-1);
}
void CMainFrame::OnShow() 
{
	// TODO: Add your command handler code here

	
}

void CMainFrame::OnExecute() 
{
	// TODO: Add your command handler code here
	OnCompile();
	if(mCompileSuccess)
		mCompiler->Interpret();
}

void CMainFrame::OnCompile() 
{
	// TODO: Add your command handler code here	

	OnSave();	
	mCompiler=new CCompiler(mCurrentFile,this);	

	this->m_CtrlBar.m_Function.DeleteAllItems();
	HTREEITEM root = this->m_CtrlBar.m_Function.InsertItem("Wordspace",0,0); 
	HTREEITEM subroot1=this->m_CtrlBar.m_Function.InsertItem("VarDeclare",1,1,root); 
	HTREEITEM subroot2=this->m_CtrlBar.m_Function.InsertItem("ConstDeclare",1,1,root); 

	m_CtrlBar1.m_Output.FmtLines(true);
	m_CtrlBar1.m_Output.SetFocus();
	m_CtrlBar1.m_Output.SetSel(0,-1,false);	
	m_CtrlBar1.m_Output.Clear();
	m_CtrlBar1.m_Output.ReplaceSel("Compiling......																							\r\n",false);
	//Sleep(500);
	//m_CtrlBar1.m_Output.SetFocus();	
	mCompiler->Compile();
	
	
	if (mCompiler->ErrorNumber())
		{
		if(mCompiler->mError)
			{
			m_CtrlBar1.m_Output.ReplaceSel("\r\n",false);
			mCompileSuccess=0;
			for(int i=0;i<mCompiler->mError->num;i++)
				{
				char buf[100];
				sprintf(buf,"**** line(%d) : error %d: ",mCompiler->mError->lines[i],mCompiler->mError->errors[i]);
				m_CtrlBar1.m_Output.ReplaceSel(buf,false);
				switch (mCompiler->mError->errors[i])
					{
					case 1:m_CtrlBar1.m_Output.ReplaceSel("常数说明中的\"=\"写成\":=\"!																			 \r\n",false);break;
					case 2:m_CtrlBar1.m_Output.ReplaceSel("常数说明中的\"=\"后应是数字!																			\r\n",false);break;
					case 3:m_CtrlBar1.m_Output.ReplaceSel("常数说明中标识符后应是\"=\"!																			\r\n",false);break;
					case 4:m_CtrlBar1.m_Output.ReplaceSel("const, var, procedure 后应是数字!																		\r\n",false);break;
					case 5:m_CtrlBar1.m_Output.ReplaceSel("漏掉了\',\' 或\';\'!																				\r\n",false);break;
					case 6:m_CtrlBar1.m_Output.ReplaceSel("过程说明后的符号不正确(应是语句开始符,或过程定义符!																\r\n",false);break;
					case 7:m_CtrlBar1.m_Output.ReplaceSel("应是语句开始符!																					\r\n",false);break;
					case 8:m_CtrlBar1.m_Output.ReplaceSel("程序体内的语句部分的后跟符不正确!																		\r\n",false);break;
					case 9:m_CtrlBar1.m_Output.ReplaceSel("程序结尾丢了句号\'.\'!																				\r\n",false);break;
					case 10:m_CtrlBar1.m_Output.ReplaceSel("语句之间漏了\';\'!																				\r\n",false);break;
					case 11:m_CtrlBar1.m_Output.ReplaceSel("标识符未说明!																					\r\n",false);break;
					case 12:m_CtrlBar1.m_Output.ReplaceSel("赋值语句中, 赋值号左部标识符属性应是变量!																	\r\n",false);break;
					case 13:m_CtrlBar1.m_Output.ReplaceSel("赋值语句左部标识符后应是赋值号\':=\'!																		\r\n",false);break;
					case 14:m_CtrlBar1.m_Output.ReplaceSel("call 后应为标识符!																				\r\n",false);break;
					case 15:m_CtrlBar1.m_Output.ReplaceSel("call 后标识符属性应为过程!																			\r\n",false);break;
					case 16:m_CtrlBar1.m_Output.ReplaceSel("条件语句中丢了\'then\'!																				\r\n",false);break;
					case 17:m_CtrlBar1.m_Output.ReplaceSel("丢了\'end\' 或\';\'!																				\r\n",false);break;
					case 18:m_CtrlBar1.m_Output.ReplaceSel("while 型循环语句中丢了\'do\'!																			\r\n",false);break;
					case 19:m_CtrlBar1.m_Output.ReplaceSel("语句后的符号不正确!																				\r\n",false);break;
					case 20:m_CtrlBar1.m_Output.ReplaceSel("应为关系运算符!																					\r\n",false);break;
					case 21:m_CtrlBar1.m_Output.ReplaceSel("表达式内标识符属性不能是过程!																			\r\n",false);break;
					case 22:m_CtrlBar1.m_Output.ReplaceSel("表达式中漏掉右括号\'(\'!																			\r\n",false);break;
					case 23:m_CtrlBar1.m_Output.ReplaceSel("因子后的非法符号!																				\r\n",false);break;
					case 24:m_CtrlBar1.m_Output.ReplaceSel("表达式的开始符不能是此符号!																			\r\n",false);break;
					case 25:m_CtrlBar1.m_Output.ReplaceSel("repeat 型循环语句中没有until!																			\r\n",false);break;
					case 30:m_CtrlBar1.m_Output.ReplaceSel("数位太长!																					\r\n",false);break;
					case 31:m_CtrlBar1.m_Output.ReplaceSel("数越界!																						\r\n",false);break;
					case 32:m_CtrlBar1.m_Output.ReplaceSel("read语句括号中的标识符不是变量!																			\r\n",false);break;
					}
				}
			}
		}
	else{
		m_CtrlBar1.m_Output.ReplaceSel("\r\n",false);
		CString s;
		GetWindowText(s);	
		char buf[100];	
		int length=s.GetLength();
		for(int i=0;i<length;i++)
			{
			char c=s.GetAt(i);
			if(c!='-')
				buf[i]=c;
			else{
				buf[i]='\0';
				break;
				}
			}		
		m_CtrlBar1.m_Output.ReplaceSel(buf,false);
		m_CtrlBar1.m_Output.ReplaceSel(".exe - 0 warning(s), 0 error(s)																					\r\n",false);
		mCompileSuccess=1;	
		}	
	fclose(mCompiler->mFile);
}

void CMainFrame::OnSave() 
{
	// TODO: Add your command handler code here
	this->GetActiveDocument()->GetDocTemplate()->SaveAllModified();

}
void CMainFrame::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	DeleteFile("c://MyData.txt");	
	CFrameWnd::OnClose();
}

void CMainFrame::OnInitMenu(CMenu* pMenu)
{
	
   CFrameWnd::OnInitMenu(pMenu);
  
	// CG: This block added by 'Tip of the Day' component.
	{
		// TODO: This code adds the "Tip of the Day" menu item
		// on the fly.  It may be removed after adding the menu
		// item to all applicable menu items using the resource
		// editor.

		// Add Tip of the Day menu item on the fly!
		static CMenu* pSubMenu = NULL;

		CString strHelp; strHelp.LoadString(CG_IDS_TIPOFTHEDAYHELP);
		CString strMenu;
		int nMenuCount = pMenu->GetMenuItemCount();
		BOOL bFound = FALSE;
		for (int i=0; i < nMenuCount; i++) 
			{
			pMenu->GetMenuString(i, strMenu, MF_BYPOSITION);
			if (strMenu == strHelp)
				{ 
				pSubMenu = pMenu->GetSubMenu(i);
				bFound = TRUE;				
				ASSERT(pSubMenu != NULL);
				}
			}

		CString strTipMenu;
		strTipMenu.LoadString(CG_IDS_TIPOFTHEDAYMENU);
		if (!bFound)
			{
			// Help menu is not available. Please add it!
			if (pSubMenu == NULL) 
				{
				// The same pop-up menu is shared between mainfrm and frame 
				// with the doc.
				static CMenu popUpMenu;
				pSubMenu = &popUpMenu;
				pSubMenu->CreatePopupMenu();
				pSubMenu->InsertMenu(0, MF_STRING|MF_BYPOSITION, 
					CG_IDS_TIPOFTHEDAY, strTipMenu);
				} 
			pMenu->AppendMenu(MF_STRING|MF_BYPOSITION|MF_ENABLED|MF_POPUP, 
				(UINT)pSubMenu->m_hMenu, strHelp);
			DrawMenuBar();
			} 
		else{      
			// Check to see if the Tip of the Day menu has already been added.
			pSubMenu->GetMenuString(0, strMenu, MF_BYPOSITION);

			if (strMenu != strTipMenu) 
				{
				// Tip of the Day submenu has not been added to the 
				// first position, so add it.
				pSubMenu->InsertMenu(0, MF_BYPOSITION);  // Separator
				pSubMenu->InsertMenu(0, MF_STRING|MF_BYPOSITION, 
					CG_IDS_TIPOFTHEDAY, strTipMenu);
				}
			}
	}
}


void CMainFrame::ActivateFrame(int nCmdShow) 
{
	// TODO: Add your specialized code here and/or call the base class
	OnInitMenu(AfxGetMainWnd()->GetMenu());
	CFrameWnd::ActivateFrame(nCmdShow);
}

void CMainFrame::OnShowresult() 
{
	// TODO: Add your command handler code here
	fstream outfile;
	char buf[50];
	int x,i;
	
	x=mCompiler->mCode->cx;
	outfile.open("c://WordData.txt",ios::out);
	outfile<<"代码如下:"<<endl;
	for(i=0;i<x;i++)
		{
		switch(mCompiler->mCode->code[i].f)
		{
		case lit:
			sprintf(buf,"lit\t%d\t%d\n",mCompiler->mCode->code[i].l,mCompiler->mCode->code[i].a);
			break;
		case lod:
			sprintf(buf,"lit\t%d\t%d\n",mCompiler->mCode->code[i].l,mCompiler->mCode->code[i].a);
			break;
		case sto:
			sprintf(buf,"lit\t%d\t%d\n",mCompiler->mCode->code[i].l,mCompiler->mCode->code[i].a);
			break;
		case cal:
			sprintf(buf,"lit\t%d\t%d\n",mCompiler->mCode->code[i].l,mCompiler->mCode->code[i].a);
			break;
		case intint:
			sprintf(buf,"lit\t%d\t%d\n",mCompiler->mCode->code[i].l,mCompiler->mCode->code[i].a);
			break;
		case jmp:
			sprintf(buf,"lit\t%d\t%d\n",mCompiler->mCode->code[i].l,mCompiler->mCode->code[i].a);
			break;
		case jpc:
			sprintf(buf,"lit\t%d\t%d\n",mCompiler->mCode->code[i].l,mCompiler->mCode->code[i].a);
			break;
		case opr:
			sprintf(buf,"lit\t%d\t%d\n",mCompiler->mCode->code[i].l,mCompiler->mCode->code[i].a);
			break;
		}
		outfile<<buf;
		}	
	outfile<<"语法分析表如下:"<<endl;
	for(i=1;i<=mCompiler->mTable->TableIndex();i++)
		{
		sprintf(buf,"%s-----\t%d\t%d\t%d\n",mCompiler->mTable->table[i].name,mCompiler->mTable->table[i].kind,mCompiler->mTable->table[i].val,mCompiler->mTable->table[i].level);
		outfile<<buf;
		}
	outfile.close();
	dlg.DoModal();
}

⌨️ 快捷键说明

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