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

📄 compilerdlg.cpp

📁 一个很不错的可视化词法分析器 用C++写的
💻 CPP
字号:
// compilerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "compiler.h"
#include "compilerDlg.h"
#include "cifafenxi.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	virtual void CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType = adjustBorder);
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCompilerDlg dialog

CCompilerDlg::CCompilerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCompilerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCompilerDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCompilerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCompilerDlg)
	DDX_Control(pDX, IDC_EDIT2, m_edit2);
	DDX_Control(pDX, IDC_EDIT1, m_edit1);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCompilerDlg, CDialog)
	//{{AFX_MSG_MAP(CCompilerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_EXIT_MENU, OnExitMenu)
	ON_COMMAND(ID_OPEN_MENU, OnOpenMenu)
	ON_COMMAND(ID_LEXICAL_MENU, OnLexicalMenu)
	ON_COMMAND(ID_NEW_MENU, OnNewMenu)
	ON_COMMAND(ID_SAVE_MENU, OnSaveMenu)
	ON_COMMAND(ID_SAVEAS_MENU, OnSaveasMenu)
	ON_COMMAND(IDC_ABOUT_MENU, OnAboutMenu)
	ON_COMMAND(IDC_CLOSE_MENU, OnCloseMenu)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCompilerDlg message handlers

BOOL CCompilerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCompilerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCompilerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCompilerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCompilerDlg::OnExitMenu() 
{
	// TODO: Add your command handler code here
	OnOK();
}

void CCompilerDlg::OnOpenMenu() 
{
	
	if (m_edit1.GetModify())
	{
		switch (AfxMessageBox("要保存修改吗?!",MB_YESNOCANCEL|MB_ICONQUESTION))
		{
		case IDYES:
			OnSaveMenu();
			break;
		case IDCANCEL:
			return;
		default:
			;
		}
	}
	CFileDialog fdlg(TRUE,"hl",NULL,OFN_FILEMUSTEXIST,
		"HL 源程序文件(*.hl)|*.hl|所有文件(*.*)|*.*||",this);
    fdlg.m_ofn.lpstrTitle="打开HL源程序文件";
	if (fdlg.DoModal()==IDOK)
	{
		m_strCurFile=fdlg.GetPathName();
		CFile f;
		f.Open(m_strCurFile,CFile::modeRead);
		char buf[30001];
		int sz=f.Read(buf,30000);
		buf[sz]=0;
		m_edit1.SetWindowText(buf);
		m_edit1.SetSel(0,0);
		f.Close();
		char a[255];
		::strcpy(a,"HL 编译器  当前源文件:");
		::strcat(a,m_strCurFile.GetBuffer(0));
		SetWindowText(a);
		m_edit1.SetModify(FALSE);
	}	
}

void CCompilerDlg::OnNewMenu() 
{
	// TODO: Add your command handler code here
	
		if (m_edit1.GetModify())
	{
		switch (AfxMessageBox("要保存修改吗?!",MB_YESNOCANCEL|MB_ICONQUESTION))
		{
		case IDYES:
			OnSaveMenu();///////////////////////////////////////////////
			break;
		case IDCANCEL:
			return;
		default:
			;
		}
	}
    m_strCurFile="";
	m_edit1.SetSel(0,-1);
	m_edit1.Clear();
	m_edit1.EmptyUndoBuffer();
	SetWindowText("HL 编译器  当前源文件:无标题");
    m_edit1.SetModify(FALSE);
}

void CCompilerDlg::OnSaveMenu() 
{
	// TODO: Add your command handler code here
	
	if (m_strCurFile!="")
	{
		char buf[30001];
		int sz=m_edit1.GetWindowText(buf,30000);
		CFile f;
	    f.Open(m_strCurFile,CFile::modeWrite|CFile::modeCreate);
	    f.Write(buf,sz);
	    f.Close();
		char a[255];
		::strcpy(a,"HL 编译器  当前源文件:");
		::strcat(a,m_strCurFile.GetBuffer(0));
		SetWindowText(a);
		m_edit1.SetModify(FALSE);
	}
	else
	{
		OnSaveasMenu();
	}
}

void CCompilerDlg::OnSaveasMenu() 
{
	// TODO: Add your command handler code here
	CFileDialog fdlg(FALSE,"hl",NULL,OFN_OVERWRITEPROMPT,
		"HL 源程序文件(*.hl)|*.hl|所有文件(*.*)|*.*||",this);
	fdlg.m_ofn.lpstrTitle="保存PL/0源程序文件";
	if (fdlg.DoModal()==IDOK)
	{
		m_strCurFile=fdlg.GetPathName();
		char buf[30001];
		int sz=m_edit1.GetWindowText(buf,30000);
		CFile f;
	    f.Open(m_strCurFile,CFile::modeWrite|CFile::modeCreate);
	    f.Write(buf,sz);
	    f.Close();
		m_edit1.SetModify(FALSE);
		char a[255];
		::strcpy(a,"HL 编译器  当前源文件:");
		::strcat(a,m_strCurFile.GetBuffer(0));
		SetWindowText(a);
	}
}


void CCompilerDlg::OnLexicalMenu() 
{
	// TODO: Add your command handler code here
	
	char buf[30003];
	int sz=m_edit1.GetWindowText(buf,30000);
	buf[sz]=0;
	::strcpy(m_Compiler.GetTable()->SourseBuffer,buf);
	m_Compiler.CiFaFengXi();
	CifaResult * * p=m_Compiler.GetTable()->LexicalDuad;
	int s=0;
	int a=p[s]->type;
	while ((p[s]->type)>=0)
		s++;
	char (*ep)[100];
	ep=m_Compiler.GetTable()->ErrorMessageTable;
	//result to : p,s,ep
	m_edit2.SetWindowText("");
	CString str;
	int ErrorCount=0;
	str.Format("输出格式:二元式序列(类型,值)单词\r\n\r\n");
	m_edit2.ReplaceSel(str.GetBuffer(0));
	for (int i=0;i<s;i++)
	{
		if (p[i]->type>0)
		{
			str.Format("(%d,%d)\t%s\r\n",p[i]->type,p[i]->value,p[i]->text);
			m_edit2.ReplaceSel(str.GetBuffer(0));
		}
		else
		{
			ErrorCount++;
			if (p[i]->value==2 && (p[i]->text[0]>126 || p[i]->text[0]<32))
			{
				str.Format("错误号:%d %s 【 无法显示该字符! 】\r\n",
					p[i]->value,ep[p[i]->value]);
			    m_edit2.ReplaceSel(str.GetBuffer(0));
			}
			else
			{
				str.Format("错误号:%d %s 【 %s 】\r\n",
					p[i]->value,ep[p[i]->value],p[i]->text);
				m_edit2.ReplaceSel(str.GetBuffer(0));
			}
		}
	}
	str.Format("\r\n 发现 %d 个错误!\r\n",ErrorCount);
	m_edit2.ReplaceSel(str.GetBuffer(0));

}
	
void CCompilerDlg::OnGrammarMenu() 
{
	// TODO: Add your command handler code here
}

void CCompilerDlg::OnYuyiMenu() 
{
	// TODO: Add your command handler code here
}

void CCompilerDlg::OnMidcodeMenu() 
{
	// TODO: Add your command handler code here
}

void CCompilerDlg::OnCodeMenu() 
{
	// TODO: Add your command handler code here
}


void CAboutDlg::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType) 
{
	// TODO: Add your specialized code here and/or call the base class	
	CDialog::CalcWindowRect(lpClientRect, nAdjustType);
}

void CCompilerDlg::OnAboutMenu() 
{
	// TODO: Add your command handler code here
	CAboutDlg dlg;
	dlg.DoModal();
}


void CCompilerDlg::OnCloseMenu() 
{
	// TODO: Add your command handler code here
	if (m_edit1.GetModify())
	{
		switch (AfxMessageBox("要保存修改吗?!",MB_YESNOCANCEL|MB_ICONQUESTION))
		{
		case IDYES:
			OnSaveMenu();
			break;
		case IDCANCEL:
			return;
		default:
			;
		}
	}
	if (IDNO==AfxMessageBox("真的要退出吗?",MB_YESNO|MB_ICONQUESTION))
	{
		return;
	}
	EndDialog(IDCANCEL);
	CDialog::OnOK();
}

⌨️ 快捷键说明

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