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

📄 pascalcompiler.h

📁 袖珍pascal编译器,适用语言VC & C++ Visual Basic
💻 H
字号:
#if !defined(AFX_PASCALSINTANALYZER_H__5D1CA609_A57E_11D2_8AB9_00002145DF63__INCLUDED_)
#define AFX_PASCALSINTANALYZER_H__5D1CA609_A57E_11D2_8AB9_00002145DF63__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "PascalAnalyzer.h"
#include "VirtualMachine.h"
#include "defines.h"
// PascalSintAnalyzer.h: interface for the CPascalCompiler class.

//////////////////////////////////////////////////////////////////////
// Structure that stores expression informations


struct ExprInfo
{
	int     m_nAtribTip;	//  type

	int     m_nAtribLValue;	//	value type 
	
	union	// Value
	{
		CHAR     m_cAttribValue;
		INTEGER     m_nAttribValue;

		REAL m_dAttribValue;
	};


	ExprInfo ()	// initializations
	{
		m_nAtribTip = 0;
		m_nAtribLValue = 0;
	}
};

// String List 
typedef CList < CString, CString & > StringList;

// Type Info
// This structure stores the type info for the basic types

struct TypeInfo
{
	int     m_nClass;
	int     m_nType;
	int     m_nIndMin;
	int     m_nIndMax;
	int     m_nSize;

	StringList m_VarList;


	TypeInfo & operator = (TypeInfo &info)
	{
		m_nClass = info.m_nClass;
		m_nType = info.m_nType;
		m_nIndMin = info.m_nIndMin;
		m_nIndMax = info.m_nIndMax;
		m_nSize = info.m_nSize;
		m_VarList.RemoveAll( );
		m_VarList.AddTail( &info.m_VarList );
	}


	TypeInfo (TypeInfo &info)
	{
		*this = info;
	}


	TypeInfo ()
	{
		m_nSize = 0;
		m_nClass = 0;
		m_nType = 0;
		m_nIndMin = 0;
		m_nIndMax = 0;
	}
};



// Const List
typedef CList < ExprInfo, ExprInfo & > ConstList;

// Parameters structure
struct Param
{
	int     m_nType;
	int     m_nTransmisie;
};

// Parameter List
typedef CList < Param, Param & > ParamList;

// THE SYMBOL TABLE

// Date symbol

struct Symbol
{
	CString     m_sName;	// symbol name

	int     m_nClass;		// CT constants

	int     m_nType;		// ST constants

	REGISTER     m_Val;		// a pointer to a value in the constant list

	int     m_nAdrel;		// rel addr

	int     m_nDeplRec;		// record displacement

	int     m_nNivel;		// the current level

	int     m_nNrPar;		// parameter number

	int     m_nDimVar;		// Number of variables

	int     m_nAdrStart;	// starting address (for functions/procedures)

	ParamList m_ListaPar;	// parameter list

	int     m_nIndMin;		// inferior index for tables

	int     m_nIndMax;		// maximal index for tables

	StringList m_ListaRec;	// Record List

	int     m_nIncDom;		

	CString     m_sRelated;	// used for diferent related symbols


	Symbol & operator = (Symbol &simb)
	{
		m_ListaPar.RemoveAll( );
		m_ListaPar.AddTail( &simb.m_ListaPar );
		m_ListaRec.RemoveAll( );
		m_ListaRec.AddTail( &simb.m_ListaRec );
		m_nAdrel = simb.m_nAdrel;
		m_nAdrStart = simb.m_nAdrStart;
		m_nClass = simb.m_nClass;
		m_nDeplRec = simb.m_nDeplRec;
		m_nDimVar = simb.m_nDimVar;
		m_nIncDom = simb.m_nIncDom;
		m_nIndMax = simb.m_nIndMax;
		m_nIndMin = simb.m_nIndMin;
		m_nNivel = simb.m_nNivel;
		m_nNrPar = simb.m_nNrPar;
		m_sName = simb.m_sName;
		m_nType = simb.m_nType;
		m_Val = simb.m_Val;
		m_sRelated = simb.m_sRelated;
		return*this;
	}

	// copy constructor


	Symbol (Symbol &simb)
	{
		*this = simb;
	}

	// default constructor


	Symbol ()
	{
		m_sName = _T( "" );
		m_nClass = 0;
		m_nType = 0;
		m_Val = NULL;
		m_nAdrel = -1;
		m_nDeplRec = -1;
		m_nNivel = -1;
		m_nNrPar = -1;
		m_nDimVar = 0;
		m_nAdrStart = -1;
		m_nIndMin = 0;
		m_nIndMax = 0;
		m_nIncDom = 0;
	}
};

// symbol list
typedef CList < Symbol, Symbol & > SymbolList;

// Symbol Table
class SymbolTable : public CMap<CString, LPCSTR, Symbol*, Symbol*>
{
public:


	virtual ~SymbolTable ()
	{
		POSITION     pos = GetStartPosition( );

		while ( pos != NULL )
		{
			Symbol * simb;

			CString     key;
			GetNextAssoc( pos, key, simb );
			delete simb;
		}
	}
};

// Symbol table collection

class SymbolTableCollection : protected CList<SymbolTable*, SymbolTable*>
{
public:
	BOOL RetreaveSymbolAL( CString &name, Symbol *&simb );
	BOOL RetreaveSymbolCL( CString &name, Symbol *&simb );
	void InsertSymbol( Symbol *s );
	void DecreaseLevel( );
	void IncreaseLevel( );
	SymbolTableCollection( );
	virtual ~SymbolTableCollection( );
};

// Compilation error 
struct error
{
	int     m_nType;
	CString     m_sData;


	error (int type, CString &str)
	{
		m_nType = type;
		m_sData = str;
	}
	CString GetErrStr( );
};

// The code structure, 
// this structure incapsulates the generated code & Asm source

///////////////////////////////////////////////////////////////
// Programmer		: Zoly Farkas
// Creation Date	: 6/2/99 11:44:40 AM
// Class name	    : Code
// Parents          : none
// Description	    : This class encapsulates the generated code
//                  : 
///////////////////////////////////////////////////////////////


class Code
{
	friend class Record;
	// Generated Code Table

	BYTE     m_pbCodeTable[CODE_BLOCKSIZE];

	// code table Length

	int     m_nCodeLength;
	CString     m_sASMCode;

	// the asm code generated

	BOOL     m_bGenASM;
public:
	Code ()
	{
		m_nCodeLength = 0;
		m_sASMCode = _T( "" );
		m_bGenASM = TRUE;
	}
	
	
	void InsertLabel( CString &str );
	void SetAddress( int place, REGISTER value );
	void AddComment( CString str );
	CString *GetASMCode( );
	void GenerateCode( BYTE code, OFFSET offset );
	void GenerateCode( BYTE code, BYTE p1, REGISTER p2 );
	void GenerateCode( BYTE code, StackEntry &Param );
	void GenerateCode( BYTE code, BYTE param );
	void GenerateCode( BYTE code, REGISTER Param );
	void GenerateCode( BYTE code, REGISTER, CString &str );
	void GenerateCode( BYTE code );
	int GetCurCodePos()
	{return m_nCodeLength;}
	BYTE *GetCode()
	{return m_pbCodeTable;}
};



// Recording structure

class Record
{
	Code * m_Code;

	int     m_nCTStart;

	// Code Table values
	int     m_nCTLength;

	int     m_nASMStart;
	int     m_nASMLength;
public:
	void Start(Code *pCode)
	{
		m_Code = pCode;
		m_nCTStart = pCode->m_nCodeLength;
		m_nASMStart = pCode->m_sASMCode.GetLength();
		m_nASMLength =0;
		m_nCTLength = 0;
	}
	
	void Stop(Code *pCode)
	{
		m_nCTLength = pCode->m_nCodeLength - m_nCTStart;
		m_nASMLength = pCode->m_sASMCode.GetLength() - m_nASMStart;
	}

	void Paste(Code *pCode)
	{
	// Insert into the code table 
	for (int i = 0; i<m_nCTLength ; i++)
		pCode->m_pbCodeTable[pCode->m_nCodeLength++] = m_Code->m_pbCodeTable[m_nCTStart + i];
	// Insert into the source code
	pCode->m_sASMCode += m_Code->m_sASMCode.Mid(m_nASMStart , m_nASMLength );
	}
};


// Record List
typedef CList < Record * , Record * > RecordList;


/////////////////////////////////////////////////////////////
// and now some stuff for storing the places for addresses for different functions

struct AddressPlace
{
	int m_nPosition;
	CString m_sSimb;
};

typedef CList<AddressPlace,AddressPlace&> AddressList;



///////////////////////////////////////////////////////////////
// Programmer		: Zoly Farkas
// Creation Date	: 5/25/99 3:39:02 PM
// Class name	    : CPascalCompiler :
// Parents          : public CPascalLexAnalyzer
// Description	    : This is the compiler class
//                  : and it inherits the lexical analyzer
///////////////////////////////////////////////////////////////
class CPascalCompiler : public CPascalLexAnalyzer
{
public:
	void ProgramSursa( void );
	// throws exception
	CString GetErrStr( error &e );
private:


	// Code generation functions
	void InsertLabel (CString &str)
	{
		if ( m_Code )
			m_Code->InsertLabel( str );
	}


	void GenerateCode (BYTE code, OFFSET offset)
	{
		if ( m_Code )
			m_Code->GenerateCode( code, offset );
	}


	void GenerateCode (BYTE code, BYTE p1, REGISTER p2)
	{
		if ( m_Code )
			m_Code->GenerateCode( code, p1, p2 );
	}


	void GenerateCode (BYTE code, StackEntry &Param)
	{
		if ( m_Code )
			m_Code->GenerateCode( code, Param );
	}


	void GenerateCode (BYTE code, BYTE param)
	{
		if ( m_Code )
			m_Code->GenerateCode( code, param );
	}


	void GenerateCode (BYTE code, REGISTER Param)
	{
		if ( m_Code )
			m_Code->GenerateCode( code, Param );
	}


	void GenerateCode (BYTE code, REGISTER Param, CString &str)
	{
		if ( m_Code )
			m_Code->GenerateCode( code, Param, str );
	}


	void GenerateCode (BYTE code)
	{
		if ( m_Code )
			m_Code->GenerateCode( code );
	}
	void InstrRead( );
	void InstrPrint( );
	void ApelProcedura( );
	void ListaAltern( ExprInfo &Info );
	void InstrCase( );
	void InstrFor( );
	void InstrIf( );
	void InstrRepeat( );
	void ExprLogica( );
	void Conditie( );
	void InstrWhile( );
	void ApelFunc( ExprInfo &type );
	void Factor( ExprInfo &type );
	void Termen( ExprInfo &type );
	void Expr( ExprInfo &type );
	void Variabila( ExprInfo &type );
	void VariabilaAdr( ExprInfo &type );
	void Atribuire( void );
	void Instr( );
	void FactorStatic( ExprInfo &ct );
	void TermenStatic( ExprInfo &ct );
	void DeclCamp( TypeInfo &info );
	void TipRecord( TypeInfo &info );
	void TipTablou( TypeInfo &info );
	void DeclVar( int &varSize );
	void ExprStatica( ExprInfo &ct );
	void InstrCompusa( );
	void DeclParam( ParamList &listaParam, CString &function );
	void Parametri( ParamList &listaParam, CString &function );
	void TipSimplu( TypeInfo &info );
	void Tip( TypeInfo &info );
	void AntetFunc( ParamList &listaParam, int &type, CString &nume );
	void AntetProc( ParamList &listaParam, CString &nume );
	void ListaDeclVar( int &varSize );
	void DeclConst( );
	void ListaDeclConst( );
	void Bloc( Symbol *simb );
public:
	Code *Redirect( Code *code );
	void Compile( void );
	CString *GetASMCode( );

	// Enables/disables ASM source generation
	void GenerateASMSource( BOOL value );
	CPascalCompiler( CString str );
	virtual ~CPascalCompiler( );
private:
	int     m_nLabelCount;
	void PasteRecord( POSITION pos );
	void RemoveRecord( POSITION pos );
	void StopRecord( POSITION pos );
	POSITION StartNewRecord( );
	void AddComment( CString str );
	void DecreaseLevel( );
	void IncreaseLevel( );
	BOOL RetreaveSymbol( CString &name, Symbol *&simb );
	BOOL IsFunc( CString &str );
	BOOL IsProc( CString &str );
	void InsertSymbol( Symbol *symb );

	// Symbol Table
	SymbolTableCollection m_SymbTableCollection;


	int     m_nVNivel;	// Current level

	// The stack Size;

	int     m_nStackSize;

	// The stack Address

	REGISTER     m_SP;

	// Now we put some useful stuff, like code generation recording
	RecordList m_RecordList;

	// The relocation list, a list with the indexes of the addresses that are needed for the 
	// relocation because they are relative to the starting code of the programm
	CList<int, int> m_RelocationList;

	// List containing a position list with the Symbols 
	// used for calls to functions where the code is not parsed

	AddressList m_AdrList;

public:
	Symbol * m_pCurentBlock;

	REGISTER     m_EntryPoint;

	Code * m_Code;
};
#endif // !defined(AFX_PASCALSINTANALYZER_H__5D1CA609_A57E_11D2_8AB9_00002145DF63__INCLUDED_)

⌨️ 快捷键说明

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