📄 formulamodula.cpp
字号:
pParentModule = NULL;
if(szSource != NULL)
delete [] szSource;
szSource = NULL;
nSourceLen = 0;
varTable.RemoveAll();
nFormulaSum = 0;
nCurVarISN =VARISN_START;
nCurTempISN =TEMPISN_START;
nCurConstISN=MODUALISN_SPACE;
vRetType = VDT_VOID;
formulaTable.RemoveAll( );
nPrmSum = 0;
}
void CFormulaModule::SetSource(LPCTSTR szSou, int nL)
{
if(szSource != NULL)
delete [] szSource;
szSource = NULL;
if(nL<=0)
nSourceLen = strlen(szSou);
else
nSourceLen = nL;
if(nSourceLen>0){
szSource = new char[nSourceLen+1];
CopyMemory(szSource,szSou,nSourceLen);
szSource[nSourceLen] = 0;
}
}
LPCTSTR CFormulaModule::GetSource()
{
return szSource;
}
int CFormulaModule::GetSourceLen()
{
return nSourceLen;
}
void CFormulaModule::GetVarInPlace()
{
for(int i=0; i< nFormulaSum;i++){
PFORMULAITEM pFormula =& formulaTable[i];
switch(pFormula->nOpt){
case OP_ADD://+
case OP_SUB:// -
case OP_MUL:// *
case OP_DIV:// /
case OP_MOD:// %
case OP_EQ://==
case OP_BG://>
case OP_LT://<
case OP_EL://<=
case OP_EB://>=
case OP_NE://!=
case OP_OR://|(|)
case OP_AND://&(&)
case OP_GET_AT://[] RES PRM PRM2
pFormula->pvRes = GetVarItem(pFormula->nResult);
pFormula->pvPrm = GetVarItem(pFormula->nPrm);
pFormula->pvPrm2 = GetVarItem(pFormula->nPrm2);
break;
case OP_NOT://!
case OP_EVALUATE: //= RES PRM
pFormula->pvRes = GetVarItem(pFormula->nResult);
pFormula->pvPrm = GetVarItem(pFormula->nPrm);
break;
case OP_PUSH://
case OP_RET: //
case OP_JZ://
case OP_JNZ:// PRM
pFormula->pvPrm = GetVarItem(pFormula->nPrm);
break;
case OP_CALL_FUNC://
case OP_CALL_MODULE://RES
pFormula->pvRes = GetVarItem(pFormula->nResult);
break;
// jump operator
case OP_JE://
case OP_JNE://
case OP_JBG://
case OP_JLT://
case OP_JEB://
case OP_JEL:// PRM PRM2
pFormula->pvPrm = GetVarItem(pFormula->nPrm);
pFormula->pvPrm2 = GetVarItem(pFormula->nPrm2);
break;
case OP_JMP://
case OP_END://
break;
default:
if(GetVarItem(pFormula->nResult)!=NULL)
pFormula->pvRes = GetVarItem(pFormula->nResult);
if(GetVarItem(pFormula->nPrm)!=NULL)
pFormula->pvPrm = GetVarItem(pFormula->nPrm);
if(GetVarItem(pFormula->nPrm2)!=NULL)
pFormula->pvPrm2 = GetVarItem(pFormula->nPrm2);
break;
}
}
}
CFormulaModule & CFormulaModule::operator=(CFormulaModule & tmpFM)
{
Release();
nCurVarISN =tmpFM.nCurVarISN;
nCurTempISN =tmpFM.nCurTempISN;
nCurConstISN=tmpFM.nCurConstISN;
vRetType = tmpFM.vRetType;
nPrmSum = tmpFM.nPrmSum;
sModuleName = tmpFM.sModuleName;
nSourceLen = tmpFM.nSourceLen;
if(nSourceLen>0){
szSource = new char[nSourceLen+1];
CopyMemory(szSource,tmpFM.szSource,nSourceLen);
szSource[nSourceLen] = 0;
}
pParentModule = tmpFM.pParentModule;
POSITION pPos = tmpFM.varTable.GetHeadPosition();
while(pPos != NULL)
varTable.AddHead(tmpFM.varTable.GetNext(pPos));
nFormulaSum = tmpFM.nFormulaSum;
formulaTable.SetSize( nFormulaSum );
for(int i=0; i<nFormulaSum ;i++)
formulaTable[i] = tmpFM.formulaTable[i];
return *this;
}
PVARITEM CFormulaModule::AddVarItem(VAR_ITEM_STYLE nS)
{
CVarItem vItem;
vItem.nStyle = nS;
switch(nS){
case VIS_VARIABLE:
vItem.nISN = nCurVarISN++;
break;
case VIS_CONST:
vItem.nISN = nCurConstISN++;
break;
case VIS_TEMP:
vItem.nISN = nCurTempISN++;
break;
default:
ASSERT(0);
}
POSITION p = varTable.AddTail(vItem);
CVarItem & vI = varTable.GetAt(p);
return &vI;
}
int CFormulaModule::AddVariable(LPCTSTR szName)
{
int nLay=0;
PVARITEM pVI = GetVarItem(szName,nLay);
if(pVI != NULL)
return nLay * MODUALISN_SPACE + pVI->nISN;
pVI = AddVarItem(VIS_VARIABLE);
pVI->SetName(szName);
return pVI->nISN;
}
int CFormulaModule::AddConst(LPCTSTR szValue)
{
POSITION p = varTable.GetHeadPosition();
while(p != NULL){
CVarItem & vI = varTable.GetNext(p);
if(vI.nStyle == VIS_CONST &&
vI.GetValue().Compare(szValue)==0)
return vI.nISN;
}
PVARITEM pVI = AddVarItem(VIS_CONST);
pVI->SetValue(szValue);
return pVI->nISN;
}
int CFormulaModule::AddTemp(LPCTSTR szValue)
{
POSITION p = varTable.GetHeadPosition();
while(p != NULL){
CVarItem & vI = varTable.GetNext(p);
if(vI.nStyle == VIS_TEMP &&
vI.nISN == nCurTempISN)
{
++nCurTempISN;
vI.SetValue(szValue);
return vI.nISN;
}
}
PVARITEM pVI = AddVarItem(VIS_TEMP);
pVI->SetValue(szValue);
return pVI->nISN;
}
int CFormulaModule::GetCurTemp()
{
return nCurTempISN;
}
void CFormulaModule::LockTemp(int nIsn)
{
PVARITEM pIT = GetVarItem(nIsn);
ASSERT(pIT);
pIT->isLocked = TRUE;
}
void CFormulaModule::UnlockTemp(int nIsn)
{
PVARITEM pIT = GetVarItem(nIsn);
ASSERT(pIT);
pIT->isLocked = FALSE;
}
int CFormulaModule::UsedTemp()
{
ASSERT(nCurTempISN>=TEMPISN_START);
PVARITEM pIT = GetVarItem(nCurTempISN-1);
if(! pIT->isLocked)
return --nCurTempISN;
return nCurTempISN;
}
BOOL CFormulaModule::IsTemp(int nIsn)
{
return (nIsn>=TEMPISN_START) && (nIsn<=nCurTempISN);
}
void CFormulaModule::SetValue(int isn,LPCTSTR szValue)
{
PVARITEM pVI = GetVarItem(isn);
ASSERT(pVI);
if(pVI != NULL)
pVI->SetValue(szValue);
}
int CFormulaModule::GetVarISN(LPCTSTR szName)
{
int nLay=0;
PVARITEM pVI = GetVarItem(szName,nLay);
if(pVI !=NULL)
return nLay * MODUALISN_SPACE + pVI->nISN;
return 0;
}
int CFormulaModule::SetFloat(LPCTSTR szName,float fVal)
{
int nLay=0;
PVARITEM pVI = GetVarItem(szName,nLay);
if(pVI == NULL){
pVI = AddVarItem(VIS_VARIABLE);
pVI->SetName(szName);
}
pVI->SetFloat(fVal);
return pVI->nISN;
}
int CFormulaModule::SetVariable(LPCTSTR szName,LPCTSTR szValue)
{
int nLay=0;
PVARITEM pVI = GetVarItem(szName,nLay);
if(pVI == NULL){
pVI = AddVarItem(VIS_VARIABLE);
pVI->SetName(szName);
}
pVI->SetValue(szValue);
return pVI->nISN;
}
int CFormulaModule::SetArrayNumberVar(LPCTSTR szName,int nLen,float * lpValue)
{
int nLay=0;
PVARITEM pVI = GetVarItem(szName,nLay);
if(pVI == NULL){
pVI = AddVarItem(VIS_VARIABLE);
pVI->SetName(szName);
}
pVI->SetArrayNumber(nLen,lpValue);
return pVI->nISN;
}
int CFormulaModule::GetVariableSum()
{
return varTable.GetCount();
}
void CFormulaModule::ReplicateVarTable( VARTABLE & desTable)
{
POSITION pPos = varTable.GetHeadPosition();
while(pPos)
desTable.AddTail(varTable.GetNext(pPos));
}
PVARITEM CFormulaModule::GetVarItem(LPCTSTR szName, int & nLay)
{
nLay=0;
POSITION p = varTable.GetHeadPosition();
while(p != NULL){
CVarItem & vI = varTable.GetNext(p);
if(vI.nStyle == VIS_VARIABLE &&
vI.GetName().CompareNoCase(szName)== 0 )
return &vI;
}
// ISN + n * MODUALISN_SPACE
if(pParentModule == NULL)
return NULL;
PVARITEM pItem = pParentModule->GetVarItem(szName,nLay);
nLay++;
return pItem;
}
PVARITEM CFormulaModule::GetVarItem(int isn)
{
POSITION p = varTable.GetHeadPosition();
while(p != NULL){
CVarItem & vI = varTable.GetNext(p);
if(vI.nISN == isn)
return &vI;
}
if(pParentModule == NULL || isn < MODUALISN_SPACE)
return NULL;
PVARITEM pItem = pParentModule->GetVarItem(isn - MODUALISN_SPACE);
return pItem;
}
CString CFormulaModule::GetVarName(int isn)
{
if(isn==0 ) return CString(" ");
PVARITEM pI = GetVarItem(isn);
if(pI==NULL) return CString(" ");
return pI->GetName();
}
VAR_DATA_TYPE CFormulaModule::GetVariableType(LPCTSTR szName)
{
int nLay=0;
PVARITEM pVI = GetVarItem(szName,nLay);
if(pVI == NULL)
return VDT_VOID;
return pVI->GetType();
}
CString CFormulaModule::ListVariable(int nVarInd, LIST_VAR_TYPE nListType)
{
POSITION p = varTable.FindIndex(nVarInd);
if(p==NULL)
return CString("NULL");
VARITEM & vi = varTable.GetAt(p);
return vi.GetDescribe(nListType);
}
//--------------------------------------------------------------------------------
int CFormulaModule::GetFormulaItemSum()
{
return nFormulaSum;//formulaTable.GetCount();
}
int CFormulaModule::AddFormulaItem(SFormulaItem & fi)
{
if( nFormulaSum >= formulaTable.GetSize() )
formulaTable.SetSize(nFormulaSum+1);
formulaTable[nFormulaSum] = fi;
nFormulaSum ++;
return nFormulaSum-1;
}
PFORMULAITEM CFormulaModule::GetFormulaItem(int nInd)
{
if(nInd >= nFormulaSum )
nFormulaSum = nInd+1;
if(nFormulaSum > formulaTable.GetSize())
formulaTable.SetSize(nFormulaSum);
return &formulaTable[nInd];
}
CString CFormulaModule::GetDescribe(PFORMULAITEM pFI , LIST_FORMULA_ITEM_TYPE nListType)
{
CString sFormula;
CString sOpt;
switch(pFI->nOpt){
case OP_ADD:
sOpt = "+";
break;//30 // +
case OP_SUB:
sOpt = "-";
break;// 31 // -
case OP_MUL:
sOpt = "*";
break;// 32 // *
case OP_DIV:
sOpt = "/";
break;// 33 // /
case OP_MOD:
sOpt = "%";
break;// 32 // *
case OP_EQ:
sOpt = "==";
break;// 34 //==
case OP_BG:
sOpt = ">";
break;// 35 //>
case OP_LT:
sOpt = "<";
break;// 36 //<
case OP_EL:
sOpt = "<=";
break;// 37 //<=
case OP_EB:
sOpt = ">=";
break;// 38 //>=
case OP_NE:
sOpt = "!=";
break;// 39 //!=
case OP_OR:
sOpt = "OR";
break;// 40 //|(|)
case OP_AND:
sOpt = "AND";
break;// 41 //&(&)
case OP_NOT:
sOpt = "NOT";
break;// 42 //!
case OP_EVALUATE:
sOpt = ":=";
break;// 45 //=
case OP_POP:
sOpt = "POP";
break;// 46 //
case OP_PUSH:
sOpt = "PUSH";
break;// 47 //
case OP_RET:
sOpt = "RET";
break;// 48 //
case OP_GET_AT:
sOpt = "AT[]";
break;// 49 //
case OP_CALL_FUNC:
sOpt = "FUNC";
break;// 50 //
case OP_CALL_MODULE:
sOpt = "CALL";
break;// 51 //
// jump operator
case OP_JMP:
sOpt = "JMP";
break;// 60 //
case OP_JZ:
sOpt = "JZ";
break;// 61 //
case OP_JNZ:
sOpt = "JNZ";
break;// 62 //
case OP_JE:
sOpt = "JE";
break;// 63 //
case OP_JNE:
sOpt = "JNE";
break;// 64 //
case OP_JBG:
sOpt = "JBG";
break;// 65 //
case OP_JLT:
sOpt = "JLG";
break;// 66 //
case OP_JEB:
sOpt = "JEB";
break;// 67 //
case OP_JEL:
sOpt = "JEL";
break;// 68 //
case OP_END:
sOpt = "END";
break;// 68 //
default:
sOpt.Format("%d",pFI->nOpt);
break;
}
CString sPrm,sRes;// = GetVarName(pFI->nPrm);
if( pFI->nOpt== OP_CALL_FUNC){
PFUNCINFO pFunc = CConstDef::GetSysFunc(pFI->nPrm);
if(pFunc == NULL)
sPrm = " ";
else
sPrm = pFunc->sName;
}else
sPrm = GetVarName(pFI->nPrm);
if( pFI->nOpt>= OP_JMP && pFI->nOpt <= OP_JEL)
sRes.Format("%d",pFI->nResult);
else
sRes = GetVarName(pFI->nResult);
sFormula.Format("%s\t%s\t%s\t%s",sOpt,sRes,
sPrm,
GetVarName(pFI->nPrm2));
return sFormula;
}
CString CFormulaModule::ListFormulaItem(int nFormulaInd, LIST_FORMULA_ITEM_TYPE nListType)
{
CString sDesc("");
if(nFormulaInd>=0 && nFormulaInd<nFormulaSum)
sDesc.Format("%d:\t%s",nFormulaInd,GetDescribe(&formulaTable[nFormulaInd],nListType));
return sDesc;
}
//----------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -