?? mainfrm.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 + -