📄 compilecode.cpp
字号:
m_CurWT = WT_EVALUATE;
RETURN_ISN(nISN);
}
nISN = IsResKey(m_sCurWord);
if( nISN != 0) {
m_CurWT = WT_RESKEY;
RETURN_ISN(nISN);
}
nISN = IsSysFunc(m_sCurWord);
if( nISN != 0) {
m_CurWT = WT_SYSFUNC;
RETURN_ISN(nISN);
}
nISN = IsModule(m_sCurWord);
if( nISN != 0) {
m_CurWT = WT_MODULE;
RETURN_ISN(nISN);
}
if(m_sCurWord[0] == '"' || m_sCurWord[0] == '\''){
nISN = m_pFormulaModula->AddConst(
m_sCurWord.Mid(1,m_sCurWord.GetLength()-2) );
m_CurWT = WT_CONST;
RETURN_ISN(nISN);
}
if( CRegularOpt::IsNumber(m_sCurWord) ){
nISN = m_pFormulaModula->AddConst( m_sCurWord );
m_CurWT = WT_CONST;
RETURN_ISN(nISN);
}
// regist name sign
if( m_sCurWord[0] == '_' ||
(m_sCurWord[0] >= 'a' && m_sCurWord[0] <= 'z' ) ||
(m_sCurWord[0] >= 'A' && m_sCurWord[0] <= 'Z' ) )
{
nISN = m_pFormulaModula->AddVariable( m_sCurWord );
m_CurWT = WT_SIGN;
RETURN_ISN(nISN);
}
m_CurWT = WT_UNKNOWN;
return KEY_UNKNOWN;
}
void CCompileCode::BackOff()
{
ASSERT( m_bIsBack == FALSE );
m_bIsBack = TRUE;
}
int CCompileCode::IsSeparator(LPCTSTR sWord)
{
ASSERT(sWord);
switch(sWord[0]){
case ',': return BS_COMMA;
case ';': return BS_SEMICOLON;
case ':': return BS_COLON;
case '[': return BS_LRBRACKET;
case ']': return BS_RRBRACKET;
case '(': return BS_LBRACKET;
case ')': return BS_RBRACKET;
case '{': return BS_LFBRACKET;
case '}': return BS_RFBRACKET;
default:
break;
}
return 0;
}
int CCompileCode::IsOperator (LPCTSTR sWord)
{
int nL = strlen(sWord);
ASSERT(nL>=1);
char ch = sWord[0];
char ch2 = ' ';
if(nL>1) ch2=sWord[1];
switch(ch){
case '+':
if(ch2=='+')
return OP_INC;
if(nL == 1)
return OP_ADD;
break;
case '-':
if(ch2=='-')
return OP_DEC;
if(nL == 1)
return OP_SUB;
break;
case '*':
if(nL == 1)
return OP_MUL;
break;
case '/':
if(nL == 1)
return OP_DIV;
break;
case '=':
if(ch2=='=')
return OP_EQ;
break;
//return OP_EVALUATE;
case '>':
if(ch2=='=')
return OP_EB;
return OP_BG;
case '<':
if(ch2=='=')
return OP_EL;
return OP_LT;
case '!':
if(ch2=='=')
return OP_NE;
return OP_NOT;
case '&':
return OP_AND;
case '|':
return OP_OR;
case '%':
return OP_MOD;
default:
break;
}
return 0;
}
int CCompileCode::IsEvaluate (LPCTSTR sWord)
{
int nL = strlen(sWord);
ASSERT(nL>=1);
char ch = sWord[0];
char ch2 = ' ';
if(nL>1) ch2=sWord[1];
switch(ch){
case '+':
if(ch2=='=')
return OP_E_ADD;
break;
case '-':
if(ch2=='=')
return OP_E_SUB;
break;
case '*':
if(ch2=='=')
return OP_E_MUL;
break;
case '/':
if(ch2=='=')
return OP_E_DIV;
break;
case '=':
if(ch2!='=')
return OP_EVALUATE;
break;
//return OP_EVALUATE;
default:
break;
}
return 0;
}
int CCompileCode::IsResKey(LPCTSTR sWord)
{
for(UINT i=0; i<CConstDef::RESKEYSUM; i++)
if(strcmp(CConstDef::RESKEYS[i].sName,sWord)==0)
return CConstDef::RESKEYS[i].nIsn;
return 0;
}
int CCompileCode::IsSysFunc(LPCTSTR sWord)
{
for(UINT i=0; i<CConstDef::SYSFUNSUM; i++)
if(strcmp(CConstDef::SYSFUNCS[i].sName,sWord)==0)
return CConstDef::SYSFUNCS[i].nFuncID;
return 0;
}
int CCompileCode::IsModule(LPCTSTR sWord)
{
return 0;
}
int CCompileCode::GenFormula(int nOpt,int nResult,int nPrm ,int nPrm2)
{
int nRes = nResult;
if(m_pFormulaModula->IsTemp(nPrm))
m_pFormulaModula->UsedTemp();
if(m_pFormulaModula->IsTemp(nPrm2))
m_pFormulaModula->UsedTemp();
// if( (nOpt >= ISN_OPT_BEG && nOpt <= ISN_OPT_END) ||
// (nOpt == OP_CALL_FUNC) ||
// (nOpt == OP_CALL_MODULE)
// )
if(nRes == ISN_TEMP)
nRes = m_pFormulaModula->AddTemp("");
SFormulaItem sFI;
sFI.nOpt = nOpt;//int
sFI.nResult = nRes;//int
sFI.nPrm = nPrm;//int
sFI.nPrm2 = nPrm2;//int
m_pFormulaModula->AddFormulaItem(sFI);
return nRes;
}
void CCompileCode::FillJmpChain(int nChain,int nJMPP )
{
// for , while , if , switch
int nCurFormula = m_pFormulaModula->GetFormulaItemSum();
int nJMPPoint = nJMPP;
if( nJMPPoint < 0)
nJMPPoint = nCurFormula;
ASSERT(nJMPPoint>=0);
int preChain = nChain;
while(preChain != ISN_JMP_TAG ){
int nC = preChain;
ASSERT(nC>=0 && nC<nCurFormula);
preChain = m_pFormulaModula->GetFormulaItem(nC)->nResult;
m_pFormulaModula->GetFormulaItem(nC)->nResult = nJMPPoint;
}
}
int CCompileCode::CmpPushParameter()
{
GenFormula(OP_PUSH,ISN_EMPTY,ISN_EMPTY);
if(Advance() != BS_LBRACKET){
BackOff();
WordMissing("(");
return 0;
}
int nPrmSum = 0;
while(1){
int nItem = CmpFormula();
if(nItem == SEN_EMPTY)
BackOff();
else{
if(m_pFormulaModula->IsTemp(nItem))
m_pFormulaModula->LockTemp(nItem);
GenFormula(OP_PUSH,ISN_EMPTY,nItem);
nPrmSum++;
}
int nIsn = Advance();
if(nIsn == BS_COMMA)
continue;
if(nIsn == BS_RBRACKET)
break;
WordMissing(",\' or \')");
return nPrmSum;
}
return nPrmSum;
}
int CCompileCode::CmpCallSysFunc()
{
int nFuncID = m_nCurISN;
int nCurTmp = m_pFormulaModula->GetCurTemp();
// m_pFormulaModula->UnlockTemp(nCondIsn);
// m_pFormulaModula->UsedTemp();
int nPS = CmpPushParameter();
PFUNCINFO pFC = CConstDef::GetSysFunc(nFuncID);
if(pFC->nPrmSum != -1 && nPS < pFC->nPrmSum)
RaiseError("no enough parameters!");
int nEndTmp = m_pFormulaModula->GetCurTemp();
for(;nEndTmp>nCurTmp;nEndTmp--){
m_pFormulaModula->UnlockTemp(nEndTmp-1);
m_pFormulaModula->UsedTemp();
}
return GenFormula(OP_CALL_FUNC,ISN_TEMP,nFuncID);
}
int CCompileCode::CmpCallModule(int nRet)
{
CString sMoudle = m_sCurWord;
int nMoudle = m_pFormulaModula->AddConst(sMoudle);
int nCurTmp = m_pFormulaModula->GetCurTemp();
CmpPushParameter();
int nEndTmp = m_pFormulaModula->GetCurTemp();
for(;nEndTmp>nCurTmp;nEndTmp--){
m_pFormulaModula->UnlockTemp(nEndTmp-1);
m_pFormulaModula->UsedTemp();
}
return GenFormula(OP_CALL_MODULE,nRet,nMoudle);
}
int CCompileCode::CmpItem()
{
int nIsn = Advance();
int nTmpIsn=0,nItem;
switch(nIsn){
case BS_LBRACKET:// (
nItem = CmpFormula();
nTmpIsn = Advance();
if( nTmpIsn != BS_RBRACKET)
WordMissing(")");
return nItem;
case OP_NOT: // if
nItem = CmpItem();
return GenFormula(OP_NOT,ISN_TEMP,nItem);
case OP_INC :
// 用+=1模拟 ++ 其实这两个操作符在 C++中是有很大的区别的
// 这里的 n++ 和 ++n 没有区分
nItem = Advance();
if(m_CurWT != WT_SIGN){
RaiseError("illegal signal!");
return UNKNOWN;
}
nTmpIsn = m_pFormulaModula->AddConst("1");
GenFormula(OP_ADD,nItem,nItem,nTmpIsn);
return nItem;
case OP_DEC:
nItem = Advance();
if(m_CurWT != WT_SIGN){
RaiseError("illegal signal!");
return UNKNOWN;
}
nTmpIsn = m_pFormulaModula->AddConst("1");
GenFormula(OP_SUB,nItem,nItem,nTmpIsn);
return nItem;
default:
break;
}
switch(m_CurWT){
case WT_CONST:
return nIsn;
case WT_SIGN: // evaluate
nTmpIsn = Advance();
switch(nTmpIsn){
case BS_LRBRACKET: // array[formula]
nItem = CmpFormula();
nTmpIsn = Advance();
if( nTmpIsn != BS_RRBRACKET) {
WordMissing("]");
BackOff();
}
return GenFormula(OP_GET_AT,ISN_TEMP,nIsn,nItem);
case OP_EVALUATE:
nItem = CmpFormula();
GenFormula(OP_EVALUATE,nIsn,nItem);
return nIsn;
case OP_INC :
// 用+=1模拟 ++ 其实这两个操作符在 C++中是有很大的区别的
// 这里的 n++ 和 ++n 没有区分
nTmpIsn = m_pFormulaModula->AddConst("1");
GenFormula(OP_ADD,nIsn,nIsn,nTmpIsn);
return nIsn;
case OP_DEC:
nTmpIsn = m_pFormulaModula->AddConst("1");
GenFormula(OP_SUB,nIsn,nIsn,nTmpIsn);
return nIsn;
default:
BackOff();
}
return nIsn;
case WT_SYSFUNC: // call SysFunc
return CmpCallSysFunc();
case WT_MODULE: // call module
return CmpCallModule(ISN_TEMP);
default:
break;
}
//BackOff();
return UNKNOWN;
}
#define MAX_OPT_STACK_DEPT 10
int CCompileCode::CmpFormula()
{
int optstack[MAX_OPT_STACK_DEPT];
UINT optpri[MAX_OPT_STACK_DEPT];
int valuestack[MAX_OPT_STACK_DEPT];
int optdepth=0,valuedepth=0;
while (1){
int tmpisn = CmpItem();
if(tmpisn == UNKNOWN){
//RaiseError("UNKNOW Item!");
//BackOff();
return SEN_EMPTY;
}
valuestack[valuedepth++] = tmpisn;
int optisn = Advance();
if(m_CurWT != WT_OPERATOR) {
BackOff();
break;
}
UINT nOptPri = CConstDef::GetOptPri(optisn);
while(optdepth>0 ){
if(optpri[optdepth-1] < nOptPri) break;
int recisn = GenFormula(optstack[optdepth-1],ISN_TEMP,valuestack[valuedepth-2],valuestack[valuedepth-1]);
valuestack[valuedepth-2] = recisn;
valuedepth--;
optdepth--;
}
optpri[optdepth] = nOptPri;
optstack[optdepth++] = optisn;
}
while(optdepth>0 && valuedepth > 1){
int recisn = GenFormula(optstack[optdepth-1],ISN_TEMP,valuestack[valuedepth-2],valuestack[valuedepth-1]);
valuestack[valuedepth-2] = recisn;
valuedepth--;
optdepth--;
}
if(valuedepth == 1)
return valuestack[0];
return SEN_EMPTY;
}
void CCompileCode::CmpSignDef()
{
int nSignSum = 0;
while(1){
int nIsn = Advance();
WORD_TYPE cWT = m_CurWT;
if(cWT != WT_SIGN){
RaiseError("sign is missing!");
return;
}
nSignSum ++;
int nTmpIsn = Advance();
if( nTmpIsn == OP_EVALUATE){
nTmpIsn = CmpFormula();
GenFormula(OP_EVALUATE,nIsn,nTmpIsn);
nTmpIsn = Advance();
}
if( nTmpIsn == BS_SEMICOLON ){ // ;
if(nSignSum == 0)
RaiseError("sign is missing!");
BackOff();
return;
}
if(nTmpIsn != BS_COMMA){ // ,
WordMissing(";");
return;
}
}
}
void CCompileCode::CmpFuncDef()
{// not supported now
}
void CCompileCode::CmpIfElseSen()
{
if(Advance() != BS_LBRACKET){
WordMissing("(");
BackOff();
return ;
}
int nJmpChainElse = ISN_JMP_TAG,nJmpChainEndIf = ISN_JMP_TAG;
int boolIsn = CmpFormula();
if(Advance() != BS_RBRACKET){
WordMissing(")");
BackOff();
return ;
}
GenFormula(OP_JZ,nJmpChainElse,boolIsn);
nJmpChainElse = m_pFormulaModula->GetFormulaItemSum() - 1;
CmpSentence();
int nIsn = Advance();
if(nIsn == RES_ELSE){
GenFormula(OP_JMP,nJmpChainEndIf);
//GEN(OP_JMP,jmpchain2);
nJmpChainEndIf = m_pFormulaModula->GetFormulaItemSum() - 1;
FillJmpChain(nJmpChainElse);
CmpSentence();
FillJmpChain(nJmpChainEndIf);
}else{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -