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

📄 mainfrm.cpp

📁 本学期编译课程需要实现L语言
💻 CPP
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "Lojo.h"

#include "MainFrm.h"

#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_CIFA, OnCifa)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_YUFA, OnYufa)
	//}}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
	
}

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

	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.x=0;
	cs.y=0;
	cs.cx=::GetSystemMetrics(SM_CXSCREEN);
	cs.cy=::GetSystemMetrics(SM_CYSCREEN)-30;


	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


BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (!m_wndSplitter.CreateStatic(this, 1, 2))    //创建1行2列的切分窗口
	{
		TRACE0("Failed to CreateStaticSplitter\n");
		return FALSE;
	}

	CRect rc;				//获得客户区大小
	GetClientRect(rc);
	int x=rc.Width()/2;
	int y=rc.Height()/2;

	if (!m_wndSplitter.CreateView(0, 0, pContext->m_pNewViewClass/*RUNTIME_CLASS(CSourceEditView)*/, CSize(x,y), pContext))
	{
		TRACE0("Failed to create second pane\n");
		return FALSE;
	} //创建第1个视图

	if (!m_wndSplitter.CreateView(0, 1,	pContext->m_pNewViewClass, CSize(x,y), pContext))
	{
		TRACE0("Failed to create first pane\n");
		return FALSE;
	} //创建第2个视图


	m_pEditSrc=&((CEditView*)m_wndSplitter.GetPane(0,0))->GetEditCtrl();  //获得CEditView视图中的编辑框控件的指针
	m_pEditOut=&((CEditView*)m_wndSplitter.GetPane(0,1))->GetEditCtrl();

	


/*--------------------------------设置字体--------------------------*/
	m_fonta.CreateFont(14, 0,0,0,FW_NORMAL, 0,0,0,
		DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
		DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Verdana");
	
	m_fontb.CreateFont(12, 0,0,0,FW_NORMAL, 0,0,0,
		DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
		DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "fixedsys");
	
	m_pEditOut->SetFont(&m_fonta);
	m_pEditSrc->SetFont(&m_fontb); 
/*--------------------------------设置字体--------------------------*/
	return true;
}

void CMainFrame::OnCifa() 
{
	// TODO: Add your command handler code here
	m_pEditOut->SetWindowText("\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\t正在进行词法分析,稍等....");
 	CWaitCursor cur;

	CString str, strTemp;
	m_pEditSrc->GetWindowText(strTemp);
	if(strTemp.IsEmpty())
	{
		m_pEditOut->SetWindowText("\r\n\r\n\r\n\r\n\r\n\r\n\r\n请在左边编辑源程序或打开一个源程序文件!\r\n\r\n然后再进行词法分析");
		return;
	}
	

	char buf[10000];
	int sz=m_pEditSrc->GetWindowText(buf,10000);   //获得源代码
 
	::strcpy(m_cfenxi.m_str,buf);

	m_cfenxi.CiFaFenXi();   //进行词法分析

	CIFA** p=m_cfenxi.m_cifa;

	int s=0;
	int a=p[s]->nType;
	while ((p[s]->nType)>=0)
		s++;
	char (*ep)[100];
	ep=m_cfenxi.m_szErrMsg;
	//result to : p,s,ep


	int ErrorCount=0;
	str.Format("\r\n\t\t词法分析结果\r\n\r\n\r\n输出格式:二元式序列(类型,值)单词\r\n\r\n");
	
	for (int i=0;i<s;i++)
	{
		if (p[i]->nType>0)
		{   
			int toTP=p[i]->nType;
			if(toTP==1 || toTP==2 || toTP==3) toTP=toTP;
			else if(toTP==4||toTP==5) toTP=4;
			else if(toTP>=6&&toTP<=26) toTP=5;
			else if(toTP>=27&&toTP<=40) toTP=6;
			else if(toTP>=41&&toTP<=50) toTP=7;

			strTemp.Format("(%s, %d)\t%s\r\n",m_cfenxi.m_szTP[toTP],p[i]->nValue,p[i]->szText);
			str+=strTemp;
		}
		else
		{
			ErrorCount++;
			if (p[i]->nValue==2 && (p[i]->szText[0]>126 || p[i]->szText[0]<32))
			{
				strTemp.Format("错误号:%d %s 【 无法显示该字符! 】\r\n",	p[i]->nValue,ep[p[i]->nValue]);
				str+=strTemp;
			}
			else
			{
				strTemp.Format("错误号:%d %s 【 %s 】\r\n",p[i]->nValue,ep[p[i]->nValue],p[i]->szText);
				str+=strTemp;
			}
		}
	}
	strTemp.Format("\r\n 发现 %d 个错误!\r\n",ErrorCount);
	str+=strTemp;

	m_pEditOut->SetWindowText(str);
}


void CMainFrame::OnFileOpen() 
{
	// TODO: Add your command handler code here
	CFileDialog *lpf;
	CStdioFile	sf;
	CString		strTemp, strOut;
	lpf=new CFileDialog(TRUE,NULL,NULL,NULL,NULL);
	if(lpf->DoModal()==IDOK)
	{
		CString sz;
		sz=lpf->GetFileName();
		
		if(sf.Open(sz,CFile::modeRead))
		{
			
			while(sf.ReadString(strTemp))
			{
				strOut+=strTemp;
				strOut+="\r\n";
			}	
		
			m_pEditSrc->SetWindowText(strOut);
			sf.Close();
		}
	}	
	delete lpf;	
}

void CMainFrame::OnYufa() 
{
	CWaitCursor cur;

	CString str,strTemp;
	m_pEditSrc->GetWindowText(strTemp);
	if(strTemp.IsEmpty())
	{
		m_pEditOut->SetWindowText("\r\n\r\n\r\n\r\n\r\n\r\n\r\n请在左边编辑源程序或打开一个源程序文件!\r\n\r\n然后再进行语法分析");
		return;
	}

/*
	char buf[30003];           
	int sz=m_pEditSrc->GetWindowText(buf,30000);
	buf[sz]=0;
	::strcpy(m_cfenxi.m_str,buf);

	m_cfenxi.CiFaFenXi();   */

	m_yfenxi.init(m_cfenxi);

	CIFA** cp=m_yfenxi.m_cifa;

	int s=0;
	while ((cp[s]->nType)>=0)
		s++;
	char (*ep)[100];
	ep=m_yfenxi.m_szErrMsg;

	for (int i=0;i<s;i++)
		if (cp[i]->nType==0)
			break;

	m_yfenxi.YuFaFenXi();   //语法分析

	//result to : cp,s,ep

	m_pEditOut->SetWindowText("");


 	str.Format("\r\n\t\t语法分析结果\r\n\r\n\r\n");


	if (m_yfenxi.m_nErrNo==0)
	{
		strTemp.Format("源程序语法正确!\r\n");
		str+=strTemp;
	}
	else
	{
		strTemp.Format("语法上有错误!!\r\n");
		str+=strTemp;

		m_pEditSrc->SetSel(cp[m_yfenxi.m_nErrAddr]->nAddr,
			cp[m_yfenxi.m_nErrAddr]->nAddr
			+::strlen(cp[m_yfenxi.m_nErrAddr]->szText));

		strTemp.Format("\r\n错误号:%d %s\r\n",
			m_yfenxi.m_nErrNo,
			ep[m_yfenxi.m_nErrNo]);

		str+=strTemp;
	}

	m_pEditOut->SetWindowText(str);
	
}

⌨️ 快捷键说明

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