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

📄 pascalcompiler.h

📁 C++ mfc 源代码
💻 H
字号:
// PascalSintAnalyzer.h: interface for the CPascalCompiler class.
//
//////////////////////////////////////////////////////////////////////

#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"


#define NERROR	throw CPascalCompiler::error(-1,CString(""))
#define MAX_SYMB 1000

// Constante cu dimensiunea diverselor tipuri elementare

#define SC_REAL	4
#define SC_INT	2
#define SC_CHAR 1


// Constante de eroare

#define SET_GENERAL		-1
#define SET_EXPECTED	-10
#define	SET_INVALID_OP_TYPES	-11
#define SET_INVALID_OP	-12

// Constante de clasa symbol

#define CT_PROCEDURE	1
#define CT_FUNCTION		2
#define CT_VAR_SIMP		3
#define CT_VAR_RECORD	4
#define CT_VAR_RECORD_FIELD	5
#define CT_VAR_ARRAY	6
#define CT_PARAM_VAL	7
#define CT_PARAM_ADR	8
#define CT_CONST		9
#define CT_PROGNAME		10

// Constante tip simplu

#define ST_INTEGER	1
#define ST_REAL		2
#define ST_CHAR		3

// EXPRESION TYPE CONSTANTS 

#define ET_INTEGER	1
#define ET_REAL		2
#define ET_STRING	3


// LValue types

#define LV_ADDRESS	1
#define LV_VALUE	2



// Lista cu symbolurile din tabela de simboluri

typedef CList<CString, CString&> StringList;

// date rezultat expresie statica

struct ExpStat
{
	int m_nType;
	double m_dVal;
	int m_nVal;
	CString m_sVal;
};

// Informatii despre un tip

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






// Lista constante
typedef CList<ExpStat,ExpStat&> ConstList;


// Date parametru

struct Param
{
	int m_nType;
	int m_nTransmisie;	// LV_Constants
};


// Lista parametrii

typedef CList<Param,Param&> ParamList;






// si acum sa ne declaram tabela de simboluri

// Date symbol

struct Symbol
{
	CString m_sName;	// symbol name
	int m_nClass;		// CT constants
	int m_nType;	// ST constants
	POSITION m_Val;		// a pointer to a value in the constant list
	int m_nAdrel;		// rel addr
	int m_nDeplRec;		// deplasamentul in record
	int m_nNivel;		// nivelul de imbricare
	int m_nNrPar;		// numarul de parametrii
	int m_nDimVar;		// nnumarul de locatii necesar memorarii variabilelor
	int m_nAdrStart;	// adresa de start
	ParamList m_ListaPar;	// lista de parametrii
	int m_nIndMin;		// indice inferior la tablouri
	int m_nIndMax;		// indice superior la tablouri
	// LISTA_REC
	StringList m_ListaRec;
	int m_nIncDom;		// valoarea curenta a indicelui din tabela de cod
	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 ;
		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;
	}
};

// Lista de simboluri

typedef CList<Symbol, Symbol&> SymbolList;

// Tabela de simboluri

typedef CMap<CString,LPCSTR,Symbol,Symbol&> SymbolTable;


// Colectie de tabela de simboluri

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


//eroare de compilare

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


// Structura pentru determinarea tipurilor in expresii


struct ExprType
{
	int m_nAtribTip;	// tipul atributului
	int m_nAtribLValue;	// adresa sau Valoate
	ExprType()
	{
		m_nAtribTip = 0;
		m_nAtribLValue = 0;
	}
};




////////////////////////////////////////////////////////////////////////////////////
// Super clasa compilator Pascal


class CPascalCompiler : public CPascalLexAnalyzer  
{
public:
	CString GetErrStr(error& e);
	void InstrRead();
	void InstrPrint();
	void ApelProcedura();
	void ListaAltern();
	void InstrCase();
	void InstrFor();
	void InstrIf();
	void InstrRepeat();
	void ExprLogica();
	void Conditie();
	void InstrWhile();
	void ApelFunc(ExprType &type);
	void Factor(ExprType &type);
	void Termen(ExprType& type);
	void Expr(ExprType& type);
	void Variabila(ExprType &type);
	void Atribuire(void);
	void Instr();
	void FactorStatic(ExpStat &ct);
	void TermenStatic(ExpStat &ct);
	void DeclCamp(TypeInfo &info);
	void TipRecord(TypeInfo& info);
	void TipTablou(TypeInfo& info);
	void DeclVar();
	void ExprStatica(ExpStat &ct);
	void InstrCompusa();
	void DeclParam(ParamList &listaParam);
	void Parametri(ParamList &listaParam);
	void TipSimplu(TypeInfo& info);
	void Tip(TypeInfo &info);
	void AntetFunc(ParamList &listaParam, int &type, CString &nume);
	void AntetProc(ParamList &listaParam, CString &nume);
	void ListaDeclVar();
	void DeclConst();
	void ListaDeclConst();
	void Bloc(void);
	void ProgramSursa(void);		// throws exception
	CPascalCompiler(CString str);
	virtual ~CPascalCompiler();
private:
	void DecreaseLevel();
	void IncreaseLevel();
	BOOL RetreaveSymbol(CString &name, Symbol &simb);
	BOOL IsFunc(CString &str);
	BOOL IsProc(CString& str);
	void InsertSymbol(Symbol &symb);
	
	// Tabela de simboluri
	SymbolTableCollection m_SymbTableCollection;

	// variables:
	int m_nICod;	// indice in tabela de cod
	int m_nVNivel;	// nivelul curent de imbricare
	int m_nVAdrel;	// valoarea curenta a deplasamentului unei var locale sau a unui parametru
	int m_nIdeplas;	// valoarea curenta a deplasamentului unui camp dintr-o structura
	int m_nVDimv;	// dimensiunea spatiului alocat variabilelor dintr-un bloc
	int m_nVNrPar;	// numarul de parametrii a unei functii/ proceduri
	// Aici mai trebuie introdusa o stiva
	
	// Lista de constante (in documentatie e tabela de constante)
	ConstList m_ListaConstante;	// lista de constante
};

#endif // !defined(AFX_PASCALSINTANALYZER_H__5D1CA609_A57E_11D2_8AB9_00002145DF63__INCLUDED_)

⌨️ 快捷键说明

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