📄 zscpascaleditordoc.cpp
字号:
// ZSCPascalEditorDoc.cpp : implementation of the CZSCPascalEditorDoc class
//
#include "stdafx.h"
#include "resource.h"
#include <Tlhelp32.h>
#include <winperf.h>
#include <shlguid.h>
#include <shlobj.h>
#include <tchar.h>
#include "ZSCPascalEditorDoc.h"
#include "ZSCPascalEditorView.h"
#include "ZSCPascal.h"
#include "StringTokenizer.h"
#include "PascalAnalyzer.h"
#include "PascalCompiler.h"
#include "ResultDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CZSCPascalEditorDoc
IMPLEMENT_DYNCREATE(CZSCPascalEditorDoc, CDocument)
BEGIN_MESSAGE_MAP(CZSCPascalEditorDoc, CDocument)
//{{AFX_MSG_MAP(CZSCPascalEditorDoc)
ON_COMMAND(ID_COMPILER_LEXICALANALYZER, OnCompilerLexicalanalyzer)
ON_COMMAND(ID_COMPILER_SINTSEMANALYZER, OnCompilerSintsemanalyzer)
ON_COMMAND(ID_COMPILER_COMPILERUN, OnCompilerCompilerun)
ON_COMMAND(ID_COMPILER_COMPILE, OnCompilerCompile)
ON_COMMAND(ID_COMPILER_TRACE, OnCompilerTrace)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//////////////////////////////////////////////
// We use the application in this file
extern CZSCPascalApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CZSCPascalEditorDoc construction/destruction
CZSCPascalEditorDoc::CZSCPascalEditorDoc()
{
// TODO: add one-time construction code here
memset(&m_lf, 0, sizeof(m_lf));
m_lf.lfWeight = FW_NORMAL;
m_lf.lfCharSet = ANSI_CHARSET;
m_lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
m_lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
m_lf.lfQuality = DEFAULT_QUALITY;
m_lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
strcpy(m_lf.lfFaceName, "Courier");
}
CZSCPascalEditorDoc::~CZSCPascalEditorDoc()
{
}
BOOL CZSCPascalEditorDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
((CCrystalEditView*)m_viewList.GetHead())->SetWindowText("");
m_TextBuffer.InitNew();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CZSCPascalEditorDoc serialization
void CZSCPascalEditorDoc::Serialize(CArchive& ar)
{
CCrystalEditView* pView = (CCrystalEditView*)m_viewList.GetHead();
ASSERT_VALID(pView);
ASSERT_KINDOF(CCrystalEditView, pView);
//Write/Read raw data from file to/from RichEditControl
if (ar.IsStoring())
{
} else
{
}
ASSERT_VALID(this);
}
/////////////////////////////////////////////////////////////////////////////
// CZSCPascalEditorDoc diagnostics
#ifdef _DEBUG
void CZSCPascalEditorDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CZSCPascalEditorDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CZSCPascalEditorDoc commands
BOOL CZSCPascalEditorDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
return m_TextBuffer.LoadFromFile(lpszPathName);
}
BOOL CZSCPascalEditorDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
m_TextBuffer.SaveToFile(lpszPathName);
return TRUE; // Note - we didn't call inherited member!
}
void CZSCPascalEditorDoc::DeleteContents()
{
// TODO: Add your specialized code here and/or call the base class
CDocument::DeleteContents();
m_TextBuffer.FreeAll();
}
void CZSCPascalEditorDoc::OnCompilerLexicalanalyzer()
{
// TODO: Add your command handler code here
CZSCPascalEditorView* pView = (CZSCPascalEditorView*)m_viewList.GetHead();
ASSERT_VALID(pView);
ASSERT_KINDOF(CZSCPascalEditorView, pView);
CString str;
if ((m_TextBuffer.GetLineCount () > 0) && (m_TextBuffer.GetLineLength(m_TextBuffer.GetLineCount ()-1) >0))
m_TextBuffer.GetText(0,0,m_TextBuffer.GetLineCount ()-1,
m_TextBuffer.GetLineLength(m_TextBuffer.GetLineCount ()-1),str);
else str = "";
CPascalLexAnalyzer *analizer = new CPascalLexAnalyzer(str);
CString text;
int val;
do
{
val = analizer->NextToken();
CString str;
str.Format("Atom Code :%d - Value :%s \r\n", val, analizer->GetStrValue());
text += str;
}
while (val != TT_EOF);
// Creating the result file
CString sResultFileName;
sResultFileName = GetPathName();
sResultFileName = sResultFileName.Left(sResultFileName.GetLength()-3) + "rez";
CFile file(sResultFileName,CFile::modeCreate | CFile::modeWrite);
file.Write((const char *) text,text.GetLength());
file.Close();
// Open the result File
theApp.OpenDocumentFile(sResultFileName);
delete analizer;
}
void CZSCPascalEditorDoc::OnCompilerSintsemanalyzer()
{
// TODO: Add your command handler code here
CZSCPascalEditorView* pView = (CZSCPascalEditorView*)m_viewList.GetHead();
ASSERT_VALID(pView);
ASSERT_KINDOF(CZSCPascalEditorView, pView);
CString str;
if ((m_TextBuffer.GetLineCount () > 0) && (m_TextBuffer.GetLineLength(m_TextBuffer.GetLineCount ()-1) >0))
m_TextBuffer.GetText(0,0,m_TextBuffer.GetLineCount ()-1,
m_TextBuffer.GetLineLength(m_TextBuffer.GetLineCount ()-1),str);
else str = "";
CPascalCompiler *analizer = new CPascalCompiler(str);
try
{
analizer->ProgramSursa ();
}
catch(error &e)
{
CString str = analizer->GetErrStr (e);
AfxMessageBox(str);
pView->SetSelection (CPoint(0,analizer->LineNo()-1), CPoint(m_TextBuffer.GetLineLength(analizer->LineNo()-1),analizer->LineNo()-1));
delete analizer;
return;
}
delete analizer;
AfxMessageBox("Evaluarea sintactica a avut success");
}
///////////////////////////////////////////////////////////////
// Programmer : Zoly Farkas
// Creation Date : 6/4/99 6:38:07 PM
// Function name : Wait
// Description : Wait for the process to terminate
// Return type : DWORD
// Argument : HANDLE hProcess
///////////////////////////////////////////////////////////////
DWORD Wait(HANDLE hProcess)
{
return WaitForSingleObject(hProcess, INFINITE);
}
///////////////////////////////////////////////////////////////
// Programmer : Zoly Farkas
// Creation Date : 6/4/99 9:42:42 AM
// Function name : Execute
// Description : This function spawns a new process, and executes it
// Return type : DWORD
// Argument : const CString & strCmd
// Argument : const CString & strArgs
// Argument : const CString & strDir
// Argument : BOOL bWait
///////////////////////////////////////////////////////////////
DWORD Execute(const CString & strCmd, const CString & strArgs, const CString & strDir, BOOL bWait)
{
TRACE3("ProcessMgr::Execute(%s,%s,%s)\n",LPCTSTR(strCmd),LPCTSTR(strArgs),LPCTSTR(strDir));
CString strPath = strCmd;
// first check, which program to use to start <strCmd>
TCHAR szExe[1024];
if( int(FindExecutable(
strPath, // pointer to filename
strDir.IsEmpty() ? 0 : LPCTSTR(strDir), // pointer to default directory
szExe // result-buffer
)) <= 32 ) {
return DWORD(-1);
}
CString strCmdLine;
if( strPath.FindOneOf(TEXT(" \t")) >= 0 )
strCmdLine = "\"" + strPath + "\"" ;
else
strCmdLine = strPath;
if( !strArgs.IsEmpty() )
strCmdLine += " " + strArgs;
STARTUPINFO StartupInfo;
ZeroMemory(&StartupInfo, sizeof(STARTUPINFO));
StartupInfo.cb = sizeof(STARTUPINFO);
PROCESS_INFORMATION ProcessInfo;
DWORD dwRetVal = DWORD(-1);
BOOL bStat = CreateProcess(
szExe, // pointer to name of executable module
LPTSTR(LPCTSTR(strCmdLine)),// pointer to command line string
0, // pointer to process security attributes
0, // pointer to thread security attributes
TRUE, // handle inheritance flag
0, // creation flags
0, // pointer to new environment block
strDir.IsEmpty() ? 0 : LPCTSTR(strDir),
&StartupInfo, // pointer to STARTUPINFO
&ProcessInfo // pointer to PROCESS_INFORMATION
);
if( bStat ) {
if( bWait ) {
::WaitForInputIdle(ProcessInfo.hProcess, INFINITE);
dwRetVal = Wait(ProcessInfo.hProcess);
} else {
// before we return to the caller, we wait for the currently
// started application until it is ready to work.
::WaitForInputIdle(ProcessInfo.hProcess, INFINITE);
dwRetVal = ProcessInfo.dwProcessId;
}
::CloseHandle(ProcessInfo.hThread);
}
return dwRetVal;
}
///////////////////////////////////////////////////////////////
// Programmer : Zoly Farkas
// Creation Date : 5/25/99 4:13:51 PM
// Function name : CZSCPascalEditorDoc::OnCompilerCompilerun
// Description : THis is the response to the compile&run option
// Return type : void
///////////////////////////////////////////////////////////////
void CZSCPascalEditorDoc::OnCompilerCompilerun()
{
// TODO: Add your command handler code here
// Compiling the programm
OnCompilerCompile();
CString sResultFileName;
sResultFileName = GetPathName();
sResultFileName = sResultFileName.Left(sResultFileName.GetLength()-3) + "bin";
// now launching the virtual machine
Execute(CString("vmpascal.exe"), sResultFileName, "./",FALSE);
}
///////////////////////////////////////////////////////////////
// Programmer : Zoly Farkas
// Creation Date : 6/3/99 4:42:11 PM
// Function name : CZSCPascalEditorDoc::OnCompilerCompile
// Description : This function compiles the programm
// Return type : void
///////////////////////////////////////////////////////////////
void CZSCPascalEditorDoc::OnCompilerCompile()
{
// TODO: Add your command handler code here
// TODO: Add your command handler code here
CZSCPascalEditorView* pView = (CZSCPascalEditorView*)m_viewList.GetHead();
ASSERT_VALID(pView);
ASSERT_KINDOF(CZSCPascalEditorView, pView);
CString str;
if ((m_TextBuffer.GetLineCount () > 0) && (m_TextBuffer.GetLineLength(m_TextBuffer.GetLineCount ()-1) >0))
m_TextBuffer.GetText(0,0,m_TextBuffer.GetLineCount ()-1,
m_TextBuffer.GetLineLength(m_TextBuffer.GetLineCount ()-1),str);
else
str = "";
CPascalCompiler *compiler = new CPascalCompiler(str);
compiler->GenerateASMSource (TRUE); // Temporary, in the future will depend on a programm option
try
{
compiler->Compile ();
}
catch(error &e)
{
CString str = compiler->GetErrStr (e);
AfxMessageBox(str);
pView->SetSelection (CPoint(0,compiler->LineNo()-1), CPoint(m_TextBuffer.GetLineLength(compiler->LineNo()-1),compiler->LineNo()-1));
delete compiler;
return;
}
// save the Results to a file
CString sResultFileName;
sResultFileName = GetPathName();
sResultFileName = sResultFileName.Left(sResultFileName.GetLength()-3) + "asm";
CFile file(sResultFileName,CFile::modeCreate | CFile::modeWrite);
file.Write((const char *) *compiler->GetASMCode(),(compiler->GetASMCode())->GetLength());
file.Close();
// show the file if the options are set or no
// temporary the programm will load the file anywai until the options stuff is made
theApp.OpenDocumentFile(sResultFileName);
// Delete the compiler stuff
sResultFileName = GetPathName();
sResultFileName = sResultFileName.Left(sResultFileName.GetLength()-3) + "bin";
CFile file2(sResultFileName,CFile::modeCreate | CFile::modeWrite);
BinHeader header;
header.signature[0] = 'Z';
header.signature[1] = 'S';
header.signature[2] = 'C';
header.version = 100;
header.headerSize =sizeof(header);
header.entryPoint = compiler->m_EntryPoint;
header.codeSize = compiler->m_Code->GetCurCodePos();
file2.Write((const char *) &header,header.headerSize );
file2.Write((const char *) (compiler->m_Code->GetCode()), compiler->m_Code->GetCurCodePos());
file2.Close();
delete compiler;
}
///////////////////////////////////////////////////////////////
// Programmer : Zoly Farkas
// Creation Date : 6/3/99 4:43:20 PM
// Function name : CZSCPascalEditorDoc::OnCompilerTrace
// Description : This option runs the programm in trace mode, step by step
// Return type : void
///////////////////////////////////////////////////////////////
void CZSCPascalEditorDoc::OnCompilerTrace()
{
// TODO: Add your command handler code here
OnCompilerCompile();
CString sFileName;
sFileName = GetPathName();
sFileName = sFileName.Left(sFileName.GetLength()-3) + "bin";
// Now creating the Virtual Machine
CVirtualMachine Machine;
// Set the Trap Flag for debugging mode
Machine.SetTrap (TRUE);
CFile file(sFileName,CFile::modeRead);
BinHeader header;
// Next part must be updated for newer version headers
file.Read( &header,sizeof(header));
Machine.LoadMemoryContent (0,&file,header.codeSize);
file.Close();
Machine.Start(header.entryPoint,header.codeSize +100);
do
{
;
}
while (Machine.Continue () == MS_ACTIVE) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -