executecode.cpp

来自「我的简易编译器终于在花了近20个工作日后完成了。按照设计是做成一个Formula」· C++ 代码 · 共 823 行 · 第 1/2 页

CPP
823
字号
// ExecuteCode.cpp: implementation of the CExecuteCode class.
//
//////////////////////////////////////////////////////////////////////
/*
 * Generated by MyEclipse Struts
 * 
 * Written by Yang Huaisheng 
 * Homepage: http://codefan.spaces.live.com
 * version 0.01
 * create at 2006-04-30
 * 
 *  Distribute freely, except: don't remove my name from the source or
 *  documentation (don't take credit for my work), mark your changes (don't
 *  get me blamed for your possible bugs), don't alter or remove this
 *  notice.
 *  No warrantee of any kind, express or implied, is included with this
 *  software; use at your own risk, responsibility for damages (if any) to
 *  anyone resulting from the use of this software rests entirely with the
 *  user.
 * 
 *  Send bug reports, bug fixes, enhancements, requests, flames, etc. to
 *     codefan@hotmial.com
 *  
 */

#include "stdafx.h"
#include "..\HEADER\ExecuteCode.h"
#include "..\..\Formule\Calc\RegularOpt.h"
#include "..\..\..\String\StringOpt.h"
#include <math.h>

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// CVarStack::Construction/Destruction
//////////////////////////////////////////////////////////////////////

CVarStack::CVarStack()
{
	m_nDept=0;
}

CVarStack::~CVarStack()
{

}

int CVarStack::GetDept()
{
	return m_nDept;
}

void CVarStack::Push(PVARITEM & pVI)
{
	if( m_nDept >= m_Stack.GetSize() ) 
		m_Stack.SetSize(m_nDept+1);
	m_Stack[m_nDept] = pVI;
	m_nDept++;
}

PVARITEM CVarStack::Pop()
{
	ASSERT(m_nDept>0);
	m_nDept--;
	return m_Stack[m_nDept];
}

PVARITEM CVarStack::GetTop()
{
	//ASSERT(m_nDept>0);
	if(m_nDept ==0)
		return NULL;
	return m_Stack[m_nDept-1];
}

void CVarStack::Clear()
{
	m_nDept = 0;
}

BOOL CVarStack::IsEmpty()
{
	return m_nDept == 0;
}

PVARITEM CVarStack::operator[](int nInd)
{
	if(m_nDept+nInd+1 >= m_Stack.GetSize())
		return NULL;
	return m_Stack[m_nDept+nInd+1];
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CExecuteCode::CExecuteCode()
{
	m_bHasRet = FALSE;
	m_pFormulaModula = NULL;
	m_CurFormulaPos=0;
}

CExecuteCode::~CExecuteCode()
{

}

PVARITEM CExecuteCode::GetVarItem(int isn)
{
	//从当前变量表中查找 m_CurVarTable
	if(isn < MODUALISN_SPACE){
		POSITION p = m_CurVarTable.GetHeadPosition();
		while(p != NULL){
			CVarItem & vI = m_CurVarTable.GetNext(p);
			if(vI.nISN == isn)
				return &vI;
		}
		return NULL;
	//如果是其父类某块的变量,复制一个副本到m_UsedParentVarTable 中,并返回副本的地址
	}else{
		POSITION p = m_UsedParentVarTable.GetHeadPosition();
		while(p != NULL){
			CVarItem & vI = m_UsedParentVarTable.GetNext(p);
			if(vI.nISN == isn)
				return &vI;
		}
		// 如果不在已使用的列表中
		PVARITEM pItem = m_pFormulaModula->GetVarItem(isn);
		if(pItem==NULL) return NULL;
		CVarItem tmpItem;
		tmpItem.Clone(*pItem);// = *pItem;//
		tmpItem.nISN = isn;
		
		p = m_UsedParentVarTable.AddTail(tmpItem);
		CVarItem & vI = m_UsedParentVarTable.GetAt(p);
		return &vI;	
	}
	//这样做的目的是不破坏系统的可重入性
	return NULL;
}

BOOL CExecuteCode::RunCurFormula2()//if RET then END
{
	PFORMULAITEM pFormula = m_pFormulaModula->GetFormulaItem(m_CurFormulaPos);
	
	ASSERT(pFormula);
	PVARITEM pvRes,pvPrm,pvPrm2;// CExecuteCode::GetVarItem(int isn)
	pvRes = pFormula->pvRes;
	pvPrm = pFormula->pvPrm;
	pvPrm2= pFormula->pvPrm2;

	BOOL isEnd=FALSE;

	switch(pFormula->nOpt){
	case OP_ADD://+
		pvRes->fVal = pvPrm->fVal + pvPrm2->fVal;
		break;

	case OP_SUB:// -
		pvRes->fVal = pvPrm->fVal - pvPrm2->fVal;
		break;

	case OP_MUL:// *
		pvRes->fVal = pvPrm->fVal * pvPrm2->fVal;
		break;

	case OP_DIV:// /
		pvRes->fVal = pvPrm->fVal / pvPrm2->fVal;
		break;

	case OP_EVALUATE: //=
		pvRes->Clone(*pvPrm) ;
		break;

	case OP_GET_AT://[]
		ASSERT(pvPrm && pvPrm2);
		pvRes->fVal = pvPrm->GetFloat(pvPrm2->fVal) ;//1.3);//
		break;

	case OP_RET:
		if(pvPrm != NULL){
//			m_RetVar.SetFloat(pvPrm->fVal);
			m_RetVar.vType = VDT_NUMBER;
			m_RetVar.fVal = pvPrm->fVal;
//			m_RetVar.Clone( *pvPrm);
			m_bHasRet = TRUE;
		}
	case OP_END://
		isEnd = TRUE;
		break;
	}

	m_CurFormulaPos++;
	return isEnd;
}


BOOL CExecuteCode::RunCurFormula()//if RET then END
{
	PFORMULAITEM pFormula = m_pFormulaModula->GetFormulaItem(m_CurFormulaPos);
	ASSERT(pFormula);
	PVARITEM pvRes,pvPrm,pvPrm2;// CExecuteCode::GetVarItem(int isn)
	if(m_RunModule){
		pvRes = pFormula->pvRes;
		pvPrm = pFormula->pvPrm;
		pvPrm2= pFormula->pvPrm2;
	}else{
		pvRes = GetVarItem(pFormula->nResult);
		pvPrm = GetVarItem(pFormula->nPrm);
		pvPrm2= GetVarItem(pFormula->nPrm2);
	}

	BOOL isEnd=FALSE;
	switch(pFormula->nOpt){
	case OP_ADD://+
		pvRes->Clone(*pvPrm + *pvPrm2);
		//*pvRes = *pvPrm + *pvPrm2
		break;
	case OP_SUB:// -
		pvRes->Clone(*pvPrm - *pvPrm2);
		break;
	case OP_MUL:// *
		pvRes->Clone(*pvPrm * *pvPrm2);
		break;
	case OP_DIV:// /
		pvRes->Clone(*pvPrm / *pvPrm2);
		break;

	case OP_MOD:// %
		if(pvPrm2->ToFloat()==0.f)
			pvRes->SetFloat(0.f);
		else
			pvRes->SetFloat(pvPrm->ToFloat() -
			              int ( pvPrm->ToFloat() /  pvPrm2->ToFloat()) *  pvPrm2->ToFloat() );
		break;

	case OP_EQ://==
		if(*pvPrm == *pvPrm2)
			pvRes->SetFloat(1);
		else
			pvRes->SetFloat(0);
		break;
	case OP_BG://>
		if(*pvPrm > *pvPrm2)
			pvRes->SetFloat(1);
		else
			pvRes->SetFloat(0);
		break;
	case OP_LT://<
		if(*pvPrm < *pvPrm2)
			pvRes->SetFloat(1);
		else
			pvRes->SetFloat(0);
		break;
	case OP_EL://<=
		if(*pvPrm <= *pvPrm2)
			pvRes->SetFloat(1);
		else
			pvRes->SetFloat(0);
		break;
	case OP_EB://>=
		if(*pvPrm >= *pvPrm2)
			pvRes->SetFloat(1);
		else
			pvRes->SetFloat(0);
		break;
	case OP_NE://!=
		if(*pvPrm != *pvPrm2)
			pvRes->SetFloat(1);
		else
			pvRes->SetFloat(0);
		break;
	case OP_OR://|(|)	
		if(*pvPrm || *pvPrm2)
			pvRes->SetFloat(1);
		else
			pvRes->SetFloat(0);
		break;
	case OP_AND://&(&)
		if(*pvPrm && *pvPrm2)
			pvRes->SetFloat(1);
		else
			pvRes->SetFloat(0);
		break;
	case OP_NOT://!	
		if(! *pvPrm )
			pvRes->SetFloat(1);
		else
			pvRes->SetFloat(0);
		break;

	case OP_EVALUATE: //=
		pvRes->Clone(*pvPrm) ;
		break;

	//case OP_POP:// 	
	case OP_PUSH:// 	
		m_RuntimeStack.Push(pvPrm);
		break;
	case OP_GET_AT://[]
		ASSERT(pvPrm && pvPrm2);
		pvRes->SetFloat( pvPrm->GetFloat(pvPrm2->GetFloat()) );
		break;
	case OP_CALL_FUNC://
		CallSysFunc();
		break;
	case OP_CALL_MODULE://
		break;
	case OP_RET:
		if(pvPrm != NULL){
			m_RetVar.Clone( *pvPrm);
			m_bHasRet = TRUE;
		}
//		isEnd = TRUE;
//		break;
	case OP_END://
		isEnd = TRUE;
		break;
// jump operator
	case OP_JMP://
		m_CurFormulaPos = pFormula->nResult;
		return FALSE;
	case OP_JZ://
		if(! pvPrm->ToBool()) {
			m_CurFormulaPos = pFormula->nResult;
			return FALSE;
		}
		break;
	case OP_JNZ://
		if( pvPrm->ToBool()) 
		{
			m_CurFormulaPos = pFormula->nResult;
			return FALSE;
		}
		break;
	case OP_JE://
		if(*pvPrm == *pvPrm2){ 
			m_CurFormulaPos = pFormula->nResult;
			return FALSE;
		}
		break;
	case OP_JNE://
		if(*pvPrm != *pvPrm2){ 
			m_CurFormulaPos = pFormula->nResult;
			return FALSE;
		}
		break;
	case OP_JBG://
		if(*pvPrm > *pvPrm2){ 
			m_CurFormulaPos = pFormula->nResult;
			return FALSE;
		}
		break;
	case OP_JLT://
		if(*pvPrm < *pvPrm2){ 
			m_CurFormulaPos = pFormula->nResult;
			return FALSE;
		}
		break;
	case OP_JEB://
		if(*pvPrm >= *pvPrm2){ 
			m_CurFormulaPos = pFormula->nResult;
			return FALSE;
		}
		break;
	case OP_JEL://
		if(*pvPrm <= *pvPrm2){ 
			m_CurFormulaPos = pFormula->nResult;
			return FALSE;
		}
		break;
	}

	m_CurFormulaPos++;
	return isEnd;
}

void CExecuteCode::CallSysFunc()
{
	PFORMULAITEM pFormula = m_pFormulaModula->GetFormulaItem(m_CurFormulaPos);
	PVARITEM pvRes,pPrm,pPrm2;
	int i,nPrmSum=0;
	while(m_RuntimeStack.Pop()!=NULL)
		nPrmSum++;
	
	int nFuncID = pFormula->nPrm;
	if(m_RunModule){
		pvRes = pFormula->pvRes;
	}else{
		pvRes = GetVarItem(pFormula->nResult);
	}

	ASSERT(pvRes);

	switch(nFuncID){
	case FUNC_PRINT		://1
		ASSERT(nPrmSum>=1);
		pPrm = m_RuntimeStack[0];
		m_slOutput.AddTail(pPrm->ToString()); 
		break;	
	case FUNC_AVE		://-1
		{
			int nDataCount=0;
			float fDataSum=0.f;
			for(i=0;i<nPrmSum;i++){
				fDataSum += m_RuntimeStack[i]->SumFloat();
				nDataCount += m_RuntimeStack[i]->GetCount();
			}

⌨️ 快捷键说明

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