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

📄 pascalcompiler.cpp

📁 C++ mfc 源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		}
		if (NextToken()!=')')
			throw error(SET_EXPECTED, CString(")"));
	}
	else
	{
		PushBack();
		if (simb.m_ListaPar.GetCount () != 0)
				throw error(SET_GENERAL, CString("Some parameters expected"));
	}
	type.m_nAtribLValue = LV_VALUE;
	type.m_nAtribTip = simb.m_nType ;
}


/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.8 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::InstrWhile()
{
	if (NextToken()!=TT_KW_WHILE)
		throw error(SET_EXPECTED, CString("while"));
	Conditie();
	if (NextToken()!=TT_KW_DO)
		throw error(SET_EXPECTED, CString("do"));
	Instr();
}


/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.8 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::Conditie()
{
	int val;
	while (1)
	{
		if (NextToken()!=TT_KW_NOT)
			PushBack();
		ExprLogica();
		val = NextToken();
		if (val != TT_KW_AND && val != TT_KW_OR)
		{
			PushBack();
			break;
		}
	}
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


void CPascalCompiler::ExprLogica()
{
	ExprType type;
	Expr(type);
	int val = NextToken();
	switch (val)
	{
	case '=':break;
	case '<':break;
	case '>':break;
	case TT_GTE:break;
	case TT_LWE:break;
	case TT_NE:break;
	default: throw error(SET_EXPECTED, CString("logical operator"));;
	}
	Expr(type);
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


void CPascalCompiler::InstrRepeat()
{
	if (NextToken()!=TT_KW_REPEAT)
		throw error(SET_EXPECTED, CString("repeat"));
	Instr();
	if (NextToken()!=TT_KW_UNTIL)
		throw error(SET_EXPECTED, CString("until"));
	Conditie();
}


/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::InstrIf()
{
	if (NextToken()!=TT_KW_IF)
		throw error(SET_EXPECTED, CString("if"));
	Conditie();
	if (NextToken()!=TT_KW_THEN)
		throw error(SET_EXPECTED, CString("then"));
	Instr();
	if (NextToken()==TT_KW_ELSE)
	{
		Instr();
	}
	else PushBack();
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


void CPascalCompiler::InstrFor()
{
	if (NextToken()!=TT_KW_FOR)
		throw error(SET_EXPECTED, CString("for"));
	ExprType type;
	Variabila(type);
	if (type.m_nAtribLValue!=LV_ADDRESS)
		throw error(SET_GENERAL, CString("Variable expected"));
	if (type.m_nAtribTip != ET_INTEGER)
		throw error(SET_GENERAL, CString("Variable of integer type expected"));
	if (NextToken()!=TT_IS)
		throw error(SET_EXPECTED, CString(":="));
	Expr(type);
	if (type.m_nAtribTip != ET_INTEGER)
		throw error(SET_GENERAL, CString("The For value must be of integer type"));
	int val = NextToken();
	if (val==TT_KW_TO)
	{
		// varianta To
		;
	}
	else
		if (val==TT_KW_DOWNTO)
		{
			// varianta DownTo
			;
		}
		else throw error(SET_EXPECTED, CString("to or downto"));;
	Expr(type);
	if (type.m_nAtribTip != ET_INTEGER)
		throw error(SET_GENERAL, CString("The For value must be of integer type"));
	if (NextToken() == TT_KW_STEP)
	{
		Expr(type);
		if (type.m_nAtribTip != ET_INTEGER)
			throw error(SET_GENERAL, CString("The For value must be of integer type"));
	}
	else PushBack();
	if (NextToken()!=TT_KW_DO)
		throw error(SET_EXPECTED, CString("do"));
	Instr();
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


void CPascalCompiler::InstrCase()
{
	if (NextToken() != TT_KW_CASE)
		throw error(SET_EXPECTED, CString("case"));
	ExprType type;
	Expr(type);
	if (type.m_nAtribTip != ET_INTEGER)
		throw error(SET_GENERAL, CString("Case requires Integer"));

	if (NextToken() != TT_KW_OF)
		throw error(SET_EXPECTED, CString("of"));
	ListaAltern();
	if (NextToken() != TT_KW_END)
		throw error(SET_EXPECTED, CString("end"));
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


void CPascalCompiler::ListaAltern()
{
	while (1)
	{
		while (1)
		{
			int val = NextToken();
			if (val!=TT_INTEGER || val != TT_REAL)
				throw error(SET_EXPECTED, CString("number"));
			if (NextToken()!=',')
			{
				PushBack();
				break;
			}
		}
		if (NextToken()!=':')
			throw error(SET_EXPECTED, CString(":"));
		Instr();
		if (NextToken()!=';')
			{
				PushBack();
				break;
			}
	}
	if (NextToken() == TT_KW_OTHERWISE)
	{
		Instr();
	}
	else
		PushBack();

}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


void CPascalCompiler::ApelProcedura()
{
	if (NextToken()!= TT_WORD)
		throw error(SET_EXPECTED, CString("identifier"));
	Symbol simb;
	RetreaveSymbol(GetStrValue(),simb);
	POSITION pos = simb.m_ListaPar.GetHeadPosition ();
	if (NextToken()=='(')
	{
		while (1)
		{
			if (pos == NULL)
				throw error(SET_GENERAL, CString("Too many parameters"));
			Param par = simb.m_ListaPar .GetNext(pos);
			ExprType type;
			Expr(type);
			if (type.m_nAtribTip != par.m_nType )
				throw error(SET_GENERAL, CString("Invalid Parameter type"));
			if (par.m_nTransmisie == LV_ADDRESS && type.m_nAtribLValue== LV_VALUE )
				throw error(SET_GENERAL, CString("Invalid Parameter, must be a variable"));
			if (NextToken()!=',')
			{
				PushBack();
				if (pos != NULL)
					throw error(SET_GENERAL, CString("Too few parameters"));
				break;
			}
		}
		if (NextToken()!=')')
			throw error(SET_EXPECTED, CString(")"));
	}
	else
	{
		PushBack();
		if (simb.m_ListaPar.GetCount () != 0)
			throw error(SET_GENERAL, CString("Some parameters expected"));
	}
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


void CPascalCompiler::InstrPrint()
{
	if (NextToken()!=TT_KW_PRINT)
		throw error(SET_EXPECTED, CString("print"));
	if (NextToken()!='(')
		throw error(SET_EXPECTED, CString("("));
	while (1)
	{
		if (NextToken()!=TT_STRING)
		{
			PushBack();
			ExprType type;
			Expr(type);
		}
		if (NextToken()!=',')
		{
			PushBack();
			break;
		}
	}
	if (NextToken()!=')')
		throw error(SET_EXPECTED, CString(")"));
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


void CPascalCompiler::InstrRead()
{
	if (NextToken()!=TT_KW_READ)
		throw error(SET_EXPECTED, CString("read"));
	if (NextToken()!='(')
		throw error(SET_EXPECTED, CString("("));
	while (1)
	{
		ExprType type;
		Variabila(type);
		if (NextToken()!=',')
		{
			PushBack();
			break;
		}
	}
	if (NextToken()!=')')
		throw error(SET_EXPECTED, CString(")"));
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.13 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

CString error::GetErrStr()
{
	CString estr;
	switch (m_nType)
	{
	case SET_GENERAL: estr.Format("Syntax ERROR, %s",m_sData);break;
	case SET_EXPECTED: estr.Format("%s expected",m_sData);break;
	case SET_INVALID_OP_TYPES: estr.Format("Invalid operation between different types, %s",m_sData);break;
	case SET_INVALID_OP: estr.Format("Invalid operation , %s",m_sData);break;
	default: estr = _T("Syntax ERROR");
	}
	return estr;
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.29 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


CString CPascalCompiler::GetErrStr(error &e)
{
	CString str;
	str.Format("%s  at : %d", e.GetErrStr(), LineNo ());	
	return str;
}



/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.29 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


SymbolTableCollection::SymbolTableCollection()
{
}


/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.29 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


SymbolTableCollection::~SymbolTableCollection()
{
	POSITION pos = GetHeadPosition();
	while (pos!=NULL)
	{
		SymbolTable *table = GetNext(pos);
		delete table;
	}
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.29 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


void SymbolTableCollection::IncreaseLevel()
{
	SymbolTable *ntable = new SymbolTable();
	ntable->InitHashTable( MAX_SYMB );
	AddHead(ntable);
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.29 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


void SymbolTableCollection::DecreaseLevel()
{
	SymbolTable *ntable = GetHead();
	delete ntable;
	RemoveHead();
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.29 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void SymbolTableCollection::InsertSymbol(Symbol &s)
{
	SymbolTable *ntable = GetHead();
	(*ntable)[s.m_sName] = s;
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.29 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


BOOL SymbolTableCollection::RetreaveSymbolCL(CString& name, Symbol &simb)
{
	SymbolTable *ntable = GetHead();
	return ntable->Lookup(name,simb);
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.29 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


BOOL CPascalCompiler::RetreaveSymbol(CString &name, Symbol &simb)
{
	return m_SymbTableCollection.RetreaveSymbolAL (name,simb);
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.30 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


void CPascalCompiler::IncreaseLevel()
{
	m_SymbTableCollection.IncreaseLevel ();	
	m_nVNivel++;
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.30 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::DecreaseLevel()
{
	m_SymbTableCollection.DecreaseLevel ();	
	m_nVNivel--;
}

BOOL SymbolTableCollection::RetreaveSymbolAL(CString &name, Symbol &simb)
{
	SymbolTable *ntable = NULL;
	POSITION pos = GetHeadPosition();
	while (pos != NULL)
	{
		ntable = GetNext(pos);
		if (ntable->Lookup(name,simb))
			return TRUE;
	}
	return FALSE;
}

⌨️ 快捷键说明

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