Ejemplo n.º 1
0
/*
================
CSyntaxRichEditCtrl::OnToolTipNotify
================
*/
BOOL CSyntaxRichEditCtrl::OnToolTipNotify( UINT id, NMHDR *pNMHDR, LRESULT *pResult ) {
	TOOLTIPTEXTA* pTTTA = (TOOLTIPTEXTA*)pNMHDR;
	TOOLTIPTEXTW* pTTTW = (TOOLTIPTEXTW*)pNMHDR;

	*pResult = 0;

	idStr name;

	if ( GetNameForMousePosition( name ) ) {
		CString toolTip;

		if ( GetToolTip == NULL || !GetToolTip( name, toolTip ) ) {

			int keyWordIndex = FindKeyWord( name, name.Length() );

			if ( keyWordIndex != -1 && keyWords[keyWordIndex].description[0] != '\0' ) {
				toolTip = keyWords[keyWordIndex].description;
			} else {
				toolTip = name.c_str();
			}
		}

		AFX_MODULE_THREAD_STATE *state = AfxGetModuleThreadState();

		// set max tool tip width to enable multi-line tool tips using "\r\n" for line breaks
		state->m_pToolTip->SetMaxTipWidth( 500 );

		// set the number of milliseconds after which the tool tip automatically disappears
		state->m_pToolTip->SetDelayTime( TTDT_AUTOPOP, 5000 + toolTip.GetLength() * 50 );

#ifndef _UNICODE
		if( pNMHDR->code == TTN_NEEDTEXTA ) {
			delete m_pchTip;
			m_pchTip = new TCHAR[toolTip.GetLength() + 2];
			lstrcpyn( m_pchTip, toolTip, toolTip.GetLength() + 1 );
			pTTTW->lpszText = (WCHAR*)m_pchTip;
		} else {
			delete m_pwchTip;
			m_pwchTip = new WCHAR[toolTip.GetLength() + 2];
			_mbstowcsz( m_pwchTip, toolTip, toolTip.GetLength() + 1 );
			pTTTW->lpszText = (WCHAR*)m_pwchTip;
		}
#else
		if( pNMHDR->code == TTN_NEEDTEXTA ) {
			delete m_pchTip;
			m_pchTip = new TCHAR[toolTip.GetLength() + 2];
			_wcstombsz( m_pchTip, toolTip, toolTip.GetLength() + 1 );
			pTTTA->lpszText = (LPTSTR)m_pchTip;
		} else {
			delete m_pwchTip;
			m_pwchTip = new WCHAR[toolTip.GetLength() + 2];
			lstrcpyn( m_pwchTip, toolTip, toolTip.GetLength() + 1 );
			pTTTA->lpszText = (LPTSTR) m_pwchTip;
		}
#endif

		return TRUE;
	}
	return FALSE;
}
Ejemplo n.º 2
0
void Symbol::SetCount() {
	count = 0;
	string line;
	ifstream fin(file.c_str());

	if(fin == NULL) {
		cout << "Can not find \"" << file << "\"." << endl;
	}
	else {
		while(getline(fin, line)) {
			istringstream fin_word(line);
			string word = "";
			string remain = "";
			int pos = 0;

			while(fin_word >> word) {
				while(word != "") {
					if((pos = Check(word)) != -1) {
						if(pos != 0) {
							remain = word.substr(pos);
							word = word.substr(0, pos);
						}
						else {
							remain = word.substr(pos + length(word));
							word = word.substr(0, length(word));
						}
					}

					if(FindKeyWord(word)) {
						count = count + 1;
					}

					pos = 0;
					word = remain;
					remain = "";
				}
			}
		}
	}

	fin.close();
}
Ejemplo n.º 3
0
INT32 GisLexAwkt::GetToken()
{
    // Save start position of the previous symbol
    m_cprev = m_cc;

    // Save previous token
    m_lastToken = m_token;

    // Get next not blank character
    m_ch = find_nonblank();

    // Stream is over
    if (m_ch == '\0')
    {
        m_token = GisToken_End;
        m_prevToken = m_token;
        return m_token;
    }

    // Is it keyword ?
    if (iswalpha(m_ch))
    {
        wchar_t     word[maxCharLength];

        // Get keyword
        getword(word, sizeof(word)/sizeof(wchar_t));

        // Is it a valid keyword for this grammar?
        INT32   tktmp;
        if ((tktmp = FindKeyWord(word, g_aAwktWords, AWKTKEYWORDCOUNT)) != GisToken_Undefined)
        {
            m_token = tktmp;
        }
        else
        {
            m_token = GisToken_Undefined;
        }
    }

    // Is it a numeric constant?
    else if (iswdigit(m_ch))
    {
        // m_token is real or integer number
        getnumber(false);
    }

    // negative values
    else if (m_ch == '-')
    {
        m_ch = if_getch();
        m_ch = find_nonblank();
        getnumber(true);
    }

    // comma
    else if (m_ch == ',')
    {
        m_token = GisToken_Comma;
        m_ch = if_getch();

    }
    // left parenthesis
    else if (m_ch == '(')
    {
        m_token = GisToken_LeftParenthesis;
        m_ch = if_getch();

    }
    // right parenthesis
    else if (m_ch == ')')
    {
        m_token = GisToken_RightParenthesis;
        m_ch = if_getch();
    }

    // something bad
    else
    {
        m_token = GisToken_Error;
        m_ch = if_getch();
    }

    m_prevToken = m_token;
    return m_token;
}
Ejemplo n.º 4
0
//Read in a general basis set from a GAMESS log file
BasisSet * BasisSet::ParseGAMESSBasisSet(BufferFile * Buffer, long NumAtoms, const mpAtom * Atoms) {
	long NumShells=0, NextAtom=0, ShellStart=0,
		fShellStart=0, BasisEndPos, nTotFuncs=0, LinePos, EndPos, iatom, items;
	char	LineText[kMaxLineLength+1];
	Buffer->GetLine(LineText);
	
	Buffer->LocateKeyWord("CONTRACTION COEFFICIENT", 21);
	Buffer->SkipnLines(2);
	long StartPos = Buffer->GetFilePos();
		//go to the end of the basis and get the total # of shells to dimension mem
		//output was changed in June, 2000
	bool ShellsFound = Buffer->LocateKeyWord("TOTAL NUMBER OF BASIS SET SHELLS", 32);
	if (!ShellsFound) ShellsFound = Buffer->LocateKeyWord("TOTAL NUMBER OF SHELLS", 22);
	if (ShellsFound) {
		BasisEndPos = Buffer->GetFilePos();
		Buffer->GetLine(LineText);
		LinePos = FindKeyWord(LineText, "=", 1) + 1;
		sscanf(&(LineText[LinePos]),"%ld",&NumShells);
	} else return NULL;
	if (NumShells<=0) return NULL;	//invalid number of shells
	if (NumAtoms <= 0) return NULL;
	BasisSet * Basis = new BasisSet(NumAtoms, NumShells);
	if (Basis == NULL) return NULL;//No memory!

	Buffer->SetFilePos(StartPos);
	Buffer->SkipnLines(2);	//advance to the first primitive line
//Note the atom label is skipped since it is just a general text string and the
//Basis set is always given in the same order as the coordinates
	//The ishell loop could be a do-while structure, but this ensures
	//that I don't go off the end of the shell array
	for (long ishell=0; ishell<NumShells; ishell++) {
		float	fExp, fCoef1, fCoef2, fCoef3, scrap;
		char	ShellText[10];
		long	fShell, scrapl, NumPrims, CoefsperPrim;
		short	ShellType;
		BasisShell	foo;

		StartPos = Buffer->GetFilePos();
		EndPos = Buffer->FindBlankLine();
		NumPrims = Buffer->GetNumLines(EndPos-StartPos);
		Buffer->GetLine(LineText);
		items = sscanf(LineText, "%ld%5s",&fShell, ShellText);
		if (items != 2) throw DataError();
		CoefsperPrim = 1;
		switch (ShellText[0]) {
			case 'S':
				ShellType=SShell;
			break;
			case 'P':
				ShellType=PShell;
			break;
			case 'D':
				ShellType=DShell;
			break;
			case 'F':
				ShellType=FShell;
			break;
			case 'G':
				ShellType=GShell;
			break;
			case 'H':
				ShellType=HShell;
				break;
			case 'I':
				ShellType=IShell;
				break;
			case 'L':
				ShellType=LShell;
				CoefsperPrim = 2;
			break;
			default:
				wxString msg;
				msg.Printf(_("Unknown shell type encountered, shell label = %s"), ShellText);
				wxLogMessage(msg);
				throw DataError();
		}
		Basis->Shells.push_back(foo);
		Basis->Shells[ishell].NumPrims = NumPrims;
		Basis->Shells[ishell].ShellType = ShellType;
		std::vector<float> & Exponent = Basis->Shells[ishell].Exponent;
		std::vector<float> & NormCoef = Basis->Shells[ishell].NormCoef;
		std::vector<float> & InputCoef = Basis->Shells[ishell].InputCoef;
		Exponent.reserve(NumPrims);
		if (ShellType >= 0) {
			NormCoef.reserve(NumPrims);
			InputCoef.reserve(NumPrims);
		} else {
			NormCoef.reserve(2*NumPrims);
			InputCoef.reserve(2*NumPrims);
		}
		bool OldStyle = false;
		if (FindKeyWord(LineText, "(", 1) != -1) OldStyle=true;
		for (long iprim=0; iprim<NumPrims; iprim++) {
			InputCoef.push_back(0.0);
			NormCoef.push_back(0.0);
			if (ShellType<0) {
				InputCoef.push_back(0.0);
				NormCoef.push_back(0.0);
			}
			Exponent.push_back(0.0);
		}
		for (long iprim=0; iprim<NumPrims; iprim++) {
			if (ShellType>=0) {
				if (OldStyle) {
					items = sscanf(LineText, "%ld%5s%ld%f%f (%f)",&fShell, ShellText,
						&scrapl, &fExp, &fCoef1, &scrap);
					if (items != 6) throw DataError();
					InputCoef[iprim] = scrap;
				} else {
					items = sscanf(LineText, "%ld%5s%ld%f%f",&fShell, ShellText,
						&scrapl, &fExp, &fCoef1);
					if (items != 5) throw DataError();
					InputCoef[iprim] = fCoef1;
				}
			} else {
				if (OldStyle) {
					items = sscanf(LineText, "%ld%5s%ld%f%f (%f)%f (%f)",&fShell, ShellText,
						&scrapl, &fExp, &fCoef1,&scrap,&fCoef2,&fCoef3);
					NormCoef[iprim+NumPrims] = fCoef2;
					InputCoef[iprim+NumPrims] = fCoef3;
					InputCoef[iprim] = scrap;
					if (items != 8) throw DataError();
				} else {
					items = sscanf(LineText, "%ld%5s%ld%f%f %f",&fShell, ShellText,
						&scrapl, &fExp, &fCoef1,&fCoef2);
					NormCoef[iprim+NumPrims] = fCoef2;	//This is not normalized, but we take care of that later
					InputCoef[iprim+NumPrims] = fCoef2;
					InputCoef[iprim] = fCoef1;
					if (items != 6) throw DataError();
				}
			}
			Exponent[iprim] = fExp;
			NormCoef[iprim] = fCoef1;
//Coefficients are already normalized when read in from in front of parenthesis
//so this code is unneeded but might be useful in the future
	/*		ExpNorm = sqrt(sqrt(fExp));
			ExpNorm2 = ExpNorm*ExpNorm;
			ExpNorm *= ExpNorm2;
				//Normalization constant is: [fExp^(n+3/2) 2^(2n+3/2)/(pi^3/2 n!!)]^1/2
				//This factor is correct for functions of the form xx, xxx, etc but is off
				//for the xy, xxy, etc functions which must be multiplied by sqrt3, sqrt5 etc later
			switch (ShellType) {
				case -1:	//L shells
					NormCoef[iprim+NumPrims] = fCoef2*PNorm*ExpNorm*ExpNorm2;
				case 0:		//S shell
					NormCoef[iprim] *= SNorm*ExpNorm;
				break;
				case 1:		//P shell
					NormCoef[iprim] *= PNorm*ExpNorm*ExpNorm2;
				break;
				case 2:		//D shell
					NormCoef[iprim] *= DNorm*fExp*ExpNorm;
				break;
				case 3:		//F shell
					NormCoef[iprim] *= FNorm*fExp*ExpNorm*ExpNorm2;
				break;
				case 4:		//G shell
					NormCoef[iprim] *= GNorm*fExp*fExp*ExpNorm;
				break;
			}*/
			Buffer->GetLine(LineText);
		}
		StartPos = Buffer->GetFilePos();
		Buffer->GetLine(LineText);
		Buffer->SetFilePos(StartPos);
		long iscanerr = sscanf(LineText, "%ld %5s %ld %f (%f) ",&scrapl, ShellText,
			&scrapl, &fExp, &scrap);
		nTotFuncs += Basis->Shells[ishell].GetNumFuncs(false);
		Basis->NumShells++;
		if (iscanerr<3) {//Found next atom label so tidy up the current atom
			Buffer->SkipnLines(2);	//Skip Atom label and blank line
			long nGotShells = ishell-ShellStart+1;
			long nMappedAtoms = (fShell-fShellStart-nGotShells)/nGotShells + 1;
			Basis->NumFuncs += nMappedAtoms*nTotFuncs;
			//skip over any MM atoms since they aren't included in the basis
			while (Atoms[NextAtom].IsSIMOMMAtom() && (NextAtom < NumAtoms)) NextAtom++;
			nTotFuncs = 0;	//reset the function counter for the next atom
			for (iatom=0; iatom<nMappedAtoms; iatom++) {
				Basis->BasisMap[2*NextAtom] = ShellStart;
				Basis->BasisMap[2*NextAtom+1] = ishell;
				NextAtom++;
			}
			ShellStart = ishell+1;
			fShellStart = fShell;
		}
		if (fShell >= NumShells) break;
	}
	return Basis;
}	/*ParseGAMESSBasisSet*/
Ejemplo n.º 5
0
int  CBolanStack::PushScript(const char * ExpStr,CVariableList* VariableList,CFactionList * FactionList)
{
	char temp[MAX_STRING_LENGTH+1];
	Identifier TempID;

	CBolanStack TempStack;
	CBolan Bolan;
	int i;
	bool IsInStr;

	CEasyBuffer Buffer((int)strlen(ExpStr)+1);
	char * ExpBuff=(char *)Buffer.GetBuffer();


	i=0;
	IsInStr=false;
	while(*ExpStr)
	{
		if(*ExpStr=='"') IsInStr=!IsInStr;

		if(IsInStr)
			ExpBuff[i++]=(*ExpStr++);
		else
			ExpBuff[i++]=toupper(*ExpStr++);		

	}
	ExpBuff[i]=0;
	//CharUpper(ExpBuff);
	ExpStr=ExpBuff;
	while(*ExpStr)
	{
		Bolan.Clear();
		if(*ExpStr==' '||*ExpStr=='\r'||*ExpStr=='\n'||*ExpStr=='	')
		{
			//空格忽略
			ExpStr++;
		}
		else if(*ExpStr=='/'&&*(ExpStr+1)=='/')
		{
			while((*ExpStr!='\r')&&(*ExpStr!='\n')&&(*ExpStr))
			{
				ExpStr++;
			}
		}
		else if(*ExpStr=='"')//字符串
		{
			i=0;
			ExpStr++;
			while(*ExpStr!='"')
			{
				if(i>MAX_STRING_LENGTH)
					return 1001;
				if(*ExpStr==0)
					return 1002;
				temp[i++]=*ExpStr++;
			}
			temp[i]=0;
			ExpStr++;

			Bolan.type=BOLAN_TYPE_VALUE;
			Bolan.ValueType=VALUE_TYPE_STRING;
			Bolan.StrValue=temp;
			Bolan.level=0;
			Push(&Bolan);
		}
		else if((*ExpStr>='0'&&*ExpStr<='9')||*ExpStr=='.')	//数字
		{
			i=0;
			while((*ExpStr>='0'&&*ExpStr<='9')||*ExpStr=='.')
			{
				temp[i++]=*ExpStr++;
			}
			temp[i]=0;
			Bolan.type=BOLAN_TYPE_VALUE;
			Bolan.ValueType=VALUE_TYPE_NUMBER;
			Bolan.value=atof(temp);
			Bolan.level=0;
			Push(&Bolan);
		}
		else if(*ExpStr==':')		//跳转标识
		{
			i=0;
			ExpStr++;
			while(CanMakeIdentifier(*ExpStr))
			{
				if(i>MAX_IDENTIFIER_LENGTH)
					return 1003;
				TempID[i++]=*ExpStr++;
			}
			TempID[i]=0;			
			Bolan.type=BOLAN_TYPE_IDENTIFIER;
			Bolan.level=0;				
			Bolan.StrValue=TempID;
			Push(&Bolan);			
		}
		else if(*ExpStr==';'||*ExpStr=='\r'||*ExpStr=='\n')//行结束符
		{
			//弹出所有操作符
			while(TempStack.GetTop()!=NULL)
			{				
				Push(TempStack.Pop());
			}
			Bolan.type=BOLAN_TYPE_KEYWORD;
			Bolan.level=0;
			Bolan.index=KW_LINEEND;
			Push(&Bolan);
			//除去多余的行结束符
			while(*ExpStr==' '||*ExpStr==';'||*ExpStr=='\r'||*ExpStr=='\n')
				ExpStr++;
		}
		else if(*ExpStr>='A'&&*ExpStr<='Z')//标识符
		{
			i=0;
			while(CanMakeIdentifier(*ExpStr))
			{
				if(i>MAX_IDENTIFIER_LENGTH)
					return 1003;
				temp[i++]=*ExpStr++;
			}
			temp[i]=0;	
			if(_stricmp(temp,"AND")==0)
			{
				while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->level>15)
				{				
					Push(TempStack.Pop());
				}
				Bolan.type=BOLAN_TYPE_OPERATOR;
				Bolan.level=15;
				Bolan.index=OPERATOR_AND;
				TempStack.Push(&Bolan);			
			}
			else if(_stricmp(temp,"OR")==0)
			{
				while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->level>15)
				{				
					Push(TempStack.Pop());
				}
				Bolan.type=BOLAN_TYPE_OPERATOR;
				Bolan.level=15;
				Bolan.index=OPERATOR_OR;
				TempStack.Push(&Bolan);			
			}
			else if(_stricmp(temp,"NOT")==0)
			{
				while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->level>16)
				{				
					Push(TempStack.Pop());
				}
				Bolan.type=BOLAN_TYPE_OPERATOR;
				Bolan.level=16;
				Bolan.index=OPERATOR_NOT;
				TempStack.Push(&Bolan);			
			}
			else
			{
				int KeyWord=FindKeyWord(temp);
				if(KeyWord>=0)//关键字
				{
					//弹出所有操作符
					while(TempStack.GetTop()!=NULL)
					{				
						Push(TempStack.Pop());
					}

					Bolan.type=BOLAN_TYPE_KEYWORD;
					Bolan.level=0;
					Bolan.index=KeyWord;
					Push(&Bolan);

				}
				else
				{
					//函数
					CFaction * pFaction=FactionList->GetObject(temp);
					if(pFaction)
					{		
						while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->level>50)
						{				
							Push(TempStack.Pop());
						}
						Bolan.type=BOLAN_TYPE_FUNCTION;
						Bolan.level=50;
						Bolan.index=pFaction->ID;
						TempStack.Push(&Bolan);					
					}
					else
					{		
						//其他的标识符作为变量					
						Bolan.type=BOLAN_TYPE_VARIABLE;
						Bolan.level=0;							
						Bolan.StrValue=temp;
						CVariable * pVar=VariableList->GetObject(Bolan.StrValue);
						if(pVar)
							Bolan.index=pVar->ID;
						else
							Bolan.index=0;
						Push(&Bolan);					
					}
				}
			}
		}
		else if(*ExpStr=='(')
		{			
			Bolan.type=BOLAN_TYPE_OPERATOR;
			Bolan.level=0;
			Bolan.index=OPERATOR_LP;
			TempStack.Push(&Bolan);
			ExpStr++;			
		}
		else if(*ExpStr==')')
		{
			while(TempStack.GetTop()!=NULL&&(TempStack.GetTop()->type!=BOLAN_TYPE_OPERATOR||TempStack.GetTop()->index!=OPERATOR_LP))
			{				
				Push(TempStack.Pop());
			}			
			TempStack.Pop();
			ExpStr++;
		}
		else if(*ExpStr=='=')
		{	
			if(*(ExpStr+1)=='=')
			{
				while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->level>20)
				{				
					Push(TempStack.Pop());
				}		
				Bolan.type=BOLAN_TYPE_OPERATOR;
				Bolan.level=20;
				Bolan.index=OPERATOR_EQU;	
				TempStack.Push(&Bolan);
				ExpStr+=2;
			}
			else
			{
				while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->level>10)
				{				
					Push(TempStack.Pop());
				}		
				Bolan.type=BOLAN_TYPE_OPERATOR;
				Bolan.level=10;
				Bolan.index=OPERATOR_EVA;	
				TempStack.Push(&Bolan);
				ExpStr++;
			}
		}
		else if(*ExpStr=='>')
		{			
			if(*(ExpStr+1)=='=')
			{
				while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->level>20)
				{				
					Push(TempStack.Pop());
				}	
				Bolan.type=BOLAN_TYPE_OPERATOR;
				Bolan.level=20;
				Bolan.index=OPERATOR_MORE_EQU;	
				TempStack.Push(&Bolan);
				ExpStr+=2;	
			}
			else
			{
				while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->level>20)
				{				
					Push(TempStack.Pop());
				}	
				Bolan.type=BOLAN_TYPE_OPERATOR;
				Bolan.level=20;
				Bolan.index=OPERATOR_MORE;	
				TempStack.Push(&Bolan);
				ExpStr++;				
			}
		}
		else if(*ExpStr=='<')
		{			
			if(*(ExpStr+1)=='=')
			{
				while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->level>20)
				{				
					Push(TempStack.Pop());
				}	
				Bolan.type=BOLAN_TYPE_OPERATOR;
				Bolan.level=20;
				Bolan.index=OPERATOR_LESS_EQU;		
				TempStack.Push(&Bolan);
				ExpStr+=2;	
			}
			else if(*(ExpStr+1)=='>')
			{
				while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->level>20)
				{				
					Push(TempStack.Pop());
				}	
				Bolan.type=BOLAN_TYPE_OPERATOR;
				Bolan.level=20;
				Bolan.index=OPERATOR_DIFF;		
				TempStack.Push(&Bolan);
				ExpStr+=2;	
			}
			else
			{
				while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->level>20)
				{				
					Push(TempStack.Pop());
				}	
				Bolan.type=BOLAN_TYPE_OPERATOR;
				Bolan.level=20;
				Bolan.index=OPERATOR_LESS;			
				TempStack.Push(&Bolan);
				ExpStr++;				
			}			

		}
		else if(*ExpStr=='+'||*ExpStr=='-')
		{
			if(*ExpStr=='-'&&(ExpStr==ExpBuff||*(ExpStr-1)=='('))
			{
				Bolan.type=BOLAN_TYPE_OPERATOR;
				Bolan.index=OPERATOR_NEG;
				Bolan.level=60;
				TempStack.Push(&Bolan);
				ExpStr++;
			}
			else
			{
				while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->level>30)
				{				
					Push(TempStack.Pop());
				}		
				Bolan.type=BOLAN_TYPE_OPERATOR;
				Bolan.level=30;
				if(*ExpStr=='+') Bolan.index=OPERATOR_ADD;
				else Bolan.index=OPERATOR_SUB;	
				TempStack.Push(&Bolan);
				ExpStr++;				
			}
		}
		else if(*ExpStr=='*'||*ExpStr=='/')
		{
			while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->level>40)
			{				
				Push(TempStack.Pop());
			}				
			Bolan.type=BOLAN_TYPE_OPERATOR;
			Bolan.level=40;
			if(*ExpStr=='*') Bolan.index=OPERATOR_MUL;
			else Bolan.index=OPERATOR_DIV;	
			TempStack.Push(&Bolan);
			ExpStr++;
		}
		else if(*ExpStr==',')
		{
			while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->level>0)
			{				
				Push(TempStack.Pop());
			}	
			ExpStr++;
		}		
		else
		{
			return 1005;				//非法字符
		}		
	}
	while(TempStack.GetTop()!=NULL)
	{				
		Push(TempStack.Pop());
	}
	return 0;
}
Ejemplo n.º 6
0
int CExpressionCaculator::ExecScript(CBolanStack& ScriptList,CBolan& ExpResult,int & pos,bool AutoDeclareVariable)
{

	int ReturnCode;
	int temp;
	bool IsInIF=true;
	bool IsAfterInterrupt=false;
	int OldPos;
	int RecentControlPos,SavedPos;

	if(AutoDeclareVariable)
	{
		ReturnCode=DealVariableDefine(pos,ScriptList);
		if(ReturnCode)
			return ReturnCode;
	}
	//处理中断恢复
	if(m_CurInterrupt>0)
	{
		RecentControlPos=m_InterruptDataBuff[m_CurInterrupt].RecentControlPos;
		ReturnCode=CaculateBolanExpression(ScriptList,ExpResult,pos,RecentControlPos);
		if(ReturnCode!=0)
			return ReturnCode;
		if(RecentControlPos>0)
		{
			IsAfterInterrupt=true;
			SavedPos=pos;
			pos=RecentControlPos;
		}		
	}

	while(pos<ScriptList.GetSize())
	{
		OldPos=pos;
		switch(ScriptList.GetAt(pos)->type)
		{
		case BOLAN_TYPE_VALUE:
		case BOLAN_TYPE_OPERATOR:
		case BOLAN_TYPE_FUNCTION:		
		case BOLAN_TYPE_VARIABLE:
			ReturnCode=CaculateBolanExpression(ScriptList,ExpResult,pos,-1);
			if(ReturnCode!=0)
				return ReturnCode;
			break;
		case BOLAN_TYPE_KEYWORD:
			switch((int)(ScriptList.GetAt(pos)->index))
			{			
			case KW_END:
				pos++;
				return 0;
			case KW_IF:			
				pos++;
				if(IsAfterInterrupt)
				{
					pos=SavedPos;
					IsAfterInterrupt=false;
				}
				else
				{
					ReturnCode=CaculateBolanExpression(ScriptList,ExpResult,pos,OldPos);
					if(ReturnCode!=0)
						return ReturnCode;
				}				
				if(ExpResult.value==0)
				{
					temp=FindCoupleIF(ScriptList,KW_THEN,KW_IF,KW_ENDIF,pos,KW_ENDIF);
					if(temp<0)
						return 2003;

					temp=FindCoupleIF(ScriptList,KW_ELSEIF,KW_IF,KW_ENDIF,pos,KW_ENDIF);
					if(temp<0)
						temp=FindCoupleIF(ScriptList,KW_ELSE,KW_IF,KW_ENDIF,pos,KW_ENDIF);
					if(temp<0)
						temp=FindCoupleIF(ScriptList,KW_ENDIF,KW_IF,KW_ENDIF,pos,-1);
					if(temp<0)
						return 2001;
					pos=temp;
					IsInIF=false;
				}
				else
					IsInIF=true;
				break;
			case KW_THEN:
				pos++;
				break;
			case KW_ELSE:
				pos++;
				if(IsInIF)
				{
					temp=FindCoupleIF(ScriptList,KW_ENDIF,KW_IF,KW_ENDIF,pos+1,-1);
					if(temp<0)
						return 2002;
					pos=temp+1;
					IsInIF=false;
				}				
				break;
			case KW_ELSEIF:
				pos++;
				if(IsInIF)
				{					
					temp=FindCoupleIF(ScriptList,KW_ENDIF,KW_IF,KW_ENDIF,pos,-1);
					if(temp<0)
						return 2003;
					pos=temp+1;
					IsInIF=false;
				}
				else
				{	
					if(IsAfterInterrupt)
					{
						pos=SavedPos;
						IsAfterInterrupt=false;
					}
					else
					{
						ReturnCode=CaculateBolanExpression(ScriptList,ExpResult,pos,OldPos);
						if(ReturnCode!=0)
							return ReturnCode;
					}
					if(ExpResult.value==0)
					{
						temp=FindCoupleIF(ScriptList,KW_THEN,KW_IF,KW_ENDIF,pos,KW_ENDIF);
						if(temp<0)
							return 2003;

						temp=FindCoupleIF(ScriptList,KW_ELSEIF,KW_IF,KW_ENDIF,pos,KW_ENDIF);
						if(temp<0)
							temp=FindCoupleIF(ScriptList,KW_ELSE,KW_IF,KW_ENDIF,pos,KW_ENDIF);
						if(temp<0)
							temp=FindCoupleIF(ScriptList,KW_ENDIF,KW_IF,KW_ENDIF,pos,-1);
						if(temp<0)
							return 2004;
						pos=temp;
						IsInIF=false;
					}
					else
						IsInIF=true;
				}
				break;
			case KW_ENDIF:
				IsInIF=false;
				pos++;
				break;
			case KW_WHILE:				
				pos++;			
				if(IsAfterInterrupt)
				{
					pos=SavedPos;
					IsAfterInterrupt=false;
				}
				else
				{
					ReturnCode=CaculateBolanExpression(ScriptList,ExpResult,pos,OldPos);
					if(ReturnCode!=0)
						return ReturnCode;
				}
				if(ExpResult.value==0)
				{
					temp=FindCoupleKeyWord(ScriptList,KW_ENDWHILE,KW_WHILE,pos);
					if(temp<0)
						return 2010;
					pos=temp+1;					
				}				
				break;
			case KW_DO:
				pos++;
				break;
			case KW_ENDWHILE:
				if(pos==0)
					return 2014;
				temp=FindCoupleKeyWordReverse(ScriptList,KW_WHILE,KW_ENDWHILE,pos-1);
				if(temp<0)
					return 2011;
				pos=temp;
				break;
			case KW_BREAK:
				temp=FindKeyWord(ScriptList,KW_ENDWHILE,pos,-1);
				if(temp<0)
					return 2012;
				pos=temp+1;				
				break;
			case KW_CONTINUE:
				temp=FindKeyWordReverse(ScriptList,KW_WHILE,pos,-1);
				if(temp<0)
					return 2013;
				pos=temp;
				break;
			case KW_GOTO:
				if(ScriptList.GetAt(pos+1)==NULL)
					return 2014;
				if(ScriptList.GetAt(pos+1)->type!=BOLAN_TYPE_IDENTIFIER)
					return 2014;
				temp=ScriptList.FindIdentifier(0,ScriptList.GetAt(pos+1)->StrValue);
				if(temp<0)
					return 2015;
				pos=temp+1;				
				break;
			case KW_IDHEADER:
				pos++;
				break;
			case KW_LINEEND:
				pos++;				
				break;
			case KW_DECLARE:
				pos++;				
				break;
			case KW_NUMBER:
			case KW_STRING:			
				pos+=2;
				break;
			default:
				pos++;
			}
			break;
		case BOLAN_TYPE_IDENTIFIER:
			pos++;
			//if(pos>0)
			//	if(ScriptList.GetAt(pos-1)->type==BOLAN_TYPE_KEYWORD&&ScriptList.GetAt(pos-1)->index==(POSITION)KW_GOTO)

			//	{
			//		temp=FindIdentifier(ScriptList,ScriptList.GetAt(pos)->StrValue);
			//		if(temp>=0)
			//		{
			//			pos=temp+1;
			//		}
			//	}
			break;
		default:
			pos++;
		}
	}
	return 0;
}
/*
================
CSyntaxRichEditCtrl::HighlightSyntax

  Update the syntax highlighting for the given character range.
================
*/
void CSyntaxRichEditCtrl::HighlightSyntax(int startCharIndex, int endCharIndex)
{
	int c, t, line, charIndex, textLength, syntaxStart, keyWordLength, keyWordIndex;
	const char *keyWord;
	CHARRANGE visRange;
	CString text;

	// get text length
	GetTextRange(0, GetTextLength(), text);
	textLength = text.GetLength();

	// make sure the indexes are within bounds
	if (startCharIndex < 0) {
		startCharIndex = 0;
	}

	if (endCharIndex < 0) {
		endCharIndex = textLength - 1;
	} else if (endCharIndex >= textLength) {
		endCharIndex = textLength - 1;
	}

	// move the start index to the beginning of the line
	for (; startCharIndex > 0; startCharIndex--) {
		if (idStr::CharIsNewLine(text[startCharIndex-1])) {
			break;
		}
	}

	// move the end index to the end of the line
	for (; endCharIndex < textLength - 1; endCharIndex++) {
		if (idStr::CharIsNewLine(text[endCharIndex+1])) {
			break;
		}
	}

	// get the visible char range
	visRange = GetVisibleRange();

	// never update beyond the visible range
	if (startCharIndex < visRange.cpMin) {
		SetColor(startCharIndex, visRange.cpMin - 1, SRE_COLOR_BLACK, INVALID_BACK_COLOR, false);
		startCharIndex = visRange.cpMin;
	}

	if (visRange.cpMax < endCharIndex) {
		SetColor(visRange.cpMax, endCharIndex, SRE_COLOR_BLACK, INVALID_BACK_COLOR, false);
		endCharIndex = visRange.cpMax;

		if (endCharIndex >= textLength) {
			endCharIndex = textLength - 1;
		}
	}

	// test if the start index is inside a multi-line comment
	if (startCharIndex > 0) {
		// multi-line comments have a slightly different background color
		if (GetBackColor(startCharIndex-1) == MULTILINE_COMMENT_BACK_COLOR) {
			for (; startCharIndex > 0; startCharIndex--) {
				if (text[startCharIndex] == '/' && text[startCharIndex+1] == '*') {
					break;
				}
			}
		}
	}

	// test if the end index is inside a multi-line comment
	if (endCharIndex < textLength - 1) {
		// multi-line comments have a slightly different background color
		if (GetBackColor(endCharIndex+1) == MULTILINE_COMMENT_BACK_COLOR) {
			for (endCharIndex++; endCharIndex < textLength - 1; endCharIndex++) {
				if (text[endCharIndex-1] == '*' && text[endCharIndex] == '/') {
					break;
				}
			}
		}
	}

	line = 0;
	stringColorLine = -1;
	stringColorIndex = 0;

	// set the default color
	SetDefaultFont(startCharIndex, endCharIndex + 1);

	// syntax based colors
	for (charIndex = startCharIndex; charIndex <= endCharIndex; charIndex++) {

		t = charType[text[charIndex]];

		switch (t) {
			case CT_WHITESPACE: {
				if (idStr::CharIsNewLine(text[charIndex])) {
					line++;
				}

				break;
			}
			case CT_COMMENT: {
				c = text[charIndex+1];

				if (c == '/') {
					// single line comment
					syntaxStart = charIndex;

					for (charIndex += 2; charIndex < textLength; charIndex++) {
						if (idStr::CharIsNewLine(text[charIndex])) {
							break;
						}
					}

					SetColor(syntaxStart, charIndex + 1, singleLineCommentColor, DEFAULT_BACK_COLOR, false);
				} else if (c == '*') {
					// multi-line comment
					syntaxStart = charIndex;

					for (charIndex += 2; charIndex < textLength; charIndex++) {
						if (text[charIndex] == '*' && text[charIndex+1] == '/') {
							break;
						}
					}

					charIndex++;
					SetColor(syntaxStart, charIndex + 1, multiLineCommentColor, MULTILINE_COMMENT_BACK_COLOR, false);
				}

				break;
			}
			case CT_STRING: {
				if (line != stringColorLine) {
					stringColorLine = line;
					stringColorIndex = 0;
				}

				syntaxStart = charIndex;

				for (charIndex++; charIndex < textLength; charIndex++) {
					c = text[charIndex];

					if (charType[c] == CT_STRING && text[charIndex-1] != '\\') {
						break;
					}

					if (idStr::CharIsNewLine(c)) {
						line++;
						break;
					}
				}

				SetColor(syntaxStart, charIndex + 1, stringColor[stringColorIndex], DEFAULT_BACK_COLOR, false);
				stringColorIndex ^= 1;
				break;
			}
			case CT_LITERAL: {
				syntaxStart = charIndex;

				for (charIndex++; charIndex < textLength; charIndex++) {
					c = text[charIndex];

					if (charType[c] == CT_LITERAL && text[charIndex-1] != '\\') {
						break;
					}

					if (idStr::CharIsNewLine(c)) {
						line++;
						break;
					}
				}

				SetColor(syntaxStart, charIndex + 1, literalColor, DEFAULT_BACK_COLOR, false);
				break;
			}
			case CT_NUMBER: {
				break;
			}
			case CT_NAME: {
				syntaxStart = charIndex;
				keyWord = ((const char *)text) + charIndex;

				for (charIndex++; charIndex < textLength; charIndex++) {
					c = text[charIndex];
					t = charType[c];

					if (t != CT_NAME && t != CT_NUMBER) {
						// allow path names
						if (!allowPathNames || (c != '/' && c != '\\' && c != '.')) {
							break;
						}
					}
				}

				keyWordLength = charIndex - syntaxStart;
				keyWordIndex = FindKeyWord(keyWord, keyWordLength);

				if (keyWordIndex != -1) {
					SetColor(syntaxStart, syntaxStart + keyWordLength, keyWordColors[keyWordIndex], DEFAULT_BACK_COLOR, false);
				}

				break;
			}
			case CT_PUNCTUATION: {
				break;
			}
		}
	}

	// show braced section
	BracedSectionShow();
}
Ejemplo n.º 8
0
int  CESBolanStack::PushScript(LPCTSTR ExpStr,CESVariableList* pVariableList,CESFunctionList * pFunctionList,int& LastLine)
{
	TCHAR TempID[MAX_IDENTIFIER_LENGTH];

	CESBolanStack TempStack;
	CESBolanStack OrginScript;
	ES_BOLAN Bolan;
	
	bool IsInStr;
	LastLine=1;

	CEasyBuffer Buffer(((UINT)_tcslen(ExpStr)+1)*sizeof(TCHAR));
	CEasyBuffer TempBuffer(((UINT)_tcslen(ExpStr)+1)*sizeof(TCHAR));

	TCHAR * ExpBuff=(TCHAR *)Buffer.GetBuffer();
	TCHAR * TempBuff=(TCHAR *)TempBuffer.GetBuffer();


	UINT Len=0;
	IsInStr=false;
	while(*ExpStr)
	{
		if(*ExpStr=='"') IsInStr=!IsInStr;

		if(IsInStr)
			ExpBuff[Len++]=(*ExpStr++);
		else
			ExpBuff[Len++]=toupper(*ExpStr++);		

	}
	ExpBuff[Len]=0;
	//CharUpper(ExpBuff);
	ExpStr=ExpBuff;

	//先提取原始布兰式
	while(*ExpStr)
	{
		Bolan.Clear();
		Bolan.Line=LastLine;
		if(*ExpStr==' '||*ExpStr=='\r'||*ExpStr=='\n'||*ExpStr=='	')
		{
			//空格、空行忽略
			if(*ExpStr=='\n')
				LastLine++;
			ExpStr++;
		}
		else if(*ExpStr=='/'&&*(ExpStr+1)=='/')
		{
			while((*ExpStr!='\r')&&(*ExpStr!='\n')&&(*ExpStr))
			{
				if(*ExpStr=='\n')
					LastLine++;
				ExpStr++;
			}
		}
		else if(*ExpStr=='"')//字符串
		{
			UINT i=0;
			ExpStr++;
			while(*ExpStr!='"')
			{
				//if(i>MAX_STRING_LENGTH)
				//	return 1001;
				if(*ExpStr==0)
					return 1002;
				TempBuff[i++]=*ExpStr++;
				if(*ExpStr=='"'&&*(ExpStr+1)=='"')
				{
					TempBuff[i++]=*ExpStr++;
					ExpStr++;
				}
			}
			TempBuff[i]=0;
			ExpStr++;

			Bolan.Type=BOLAN_TYPE_VALUE;
			Bolan.ValueType=VALUE_TYPE_STRING;
			Bolan.StrValue=TempBuff;
			Bolan.Level=0;
			OrginScript.Push(&Bolan);
		}
		else if((*ExpStr>='0'&&*ExpStr<='9')||*ExpStr=='.')	//数字
		{
			UINT i=0;
			while((*ExpStr>='0'&&*ExpStr<='9')||*ExpStr=='.'||*ExpStr=='F'||*ExpStr=='D'||*ExpStr=='I'||*ExpStr=='L'||*ExpStr=='E'||
				*ExpStr=='A'||*ExpStr=='B'||*ExpStr=='C'||*ExpStr=='X')
			{
				TempBuff[i++]=*ExpStr++;
			}
			TempBuff[i]=0;
			Bolan.Type=BOLAN_TYPE_VALUE;			
			Bolan.Level=0;
			StrToNumber(TempBuff,Bolan);
			OrginScript.Push(&Bolan);
		}
		else if(*ExpStr==':')		//跳转标识
		{
			UINT i=0;
			ExpStr++;
			while(CanMakeIdentifier(*ExpStr))
			{
				if(i>MAX_IDENTIFIER_LENGTH)
					return 1003;
				TempID[i++]=*ExpStr++;
			}
			TempID[i]=0;			
			Bolan.Type=BOLAN_TYPE_IDENTIFIER;
			Bolan.Index=IDENTIFIER_TYPE_JUMP_DEFINE;
			Bolan.Level=0;				
			Bolan.StrValue=TempID;
			OrginScript.Push(&Bolan);			
		}
		else if(*ExpStr==';'||*ExpStr=='\r'||*ExpStr=='\n')//行结束符
		{			
			Bolan.Type=BOLAN_TYPE_KEYWORD;
			Bolan.Level=0;
			Bolan.Index=KW_LINEEND;
			OrginScript.Push(&Bolan);
			//除去多余的行结束符
			while(*ExpStr==' '||*ExpStr==';'||*ExpStr=='\r'||*ExpStr=='\n')
			{
				if(*ExpStr=='\n')
					LastLine++;
				ExpStr++;
			}
		}
		else if(*ExpStr>='A'&&*ExpStr<='Z')//标识符
		{
			UINT i=0;
			while(CanMakeIdentifier(*ExpStr))
			{
				if(i>MAX_IDENTIFIER_LENGTH)
					return 1003;
				TempBuff[i++]=*ExpStr++;
			}
			TempBuff[i]=0;	
			if(_tcsicmp(TempBuff,_T("AND"))==0)
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=15;
				Bolan.Index=OPERATOR_AND;
				OrginScript.Push(&Bolan);			
			}
			else if(_tcsicmp(TempBuff,_T("OR"))==0)
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=15;
				Bolan.Index=OPERATOR_OR;
				OrginScript.Push(&Bolan);			
			}
			else if(_tcsicmp(TempBuff,_T("NOT"))==0)
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=16;
				Bolan.Index=OPERATOR_NOT;
				OrginScript.Push(&Bolan);			
			}
			else
			{
				int KeyWord=FindKeyWord(TempBuff);
				if(KeyWord>=0)//关键字
				{
					Bolan.Type=BOLAN_TYPE_KEYWORD;
					Bolan.Level=0;
					Bolan.Index=KeyWord;
					OrginScript.Push(&Bolan);

				}
				else
				{		
					Bolan.Type=BOLAN_TYPE_IDENTIFIER;
					Bolan.Index=IDENTIFIER_TYPE_UNKNOW;
					Bolan.Level=0;				
					Bolan.StrValue=TempBuff;
					OrginScript.Push(&Bolan);					
				}
			}
		}
		else if(*ExpStr=='(')
		{			
			Bolan.Type=BOLAN_TYPE_OPERATOR;
			Bolan.Level=0;
			Bolan.Index=OPERATOR_LP;
			OrginScript.Push(&Bolan);
			ExpStr++;			
		}
		else if(*ExpStr==')')
		{
			Bolan.Type=BOLAN_TYPE_OPERATOR;
			Bolan.Level=0;
			Bolan.Index=OPERATOR_RP;
			OrginScript.Push(&Bolan);
			ExpStr++;
		}
		else if(*ExpStr=='=')
		{	
			if(*(ExpStr+1)=='=')
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=20;
				Bolan.Index=OPERATOR_EQU;	
				OrginScript.Push(&Bolan);
				ExpStr+=2;
			}
			else
			{					
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=10;
				Bolan.Index=OPERATOR_EVA;	
				OrginScript.Push(&Bolan);
				ExpStr++;
			}
		}
		else if(*ExpStr=='>')
		{			
			if(*(ExpStr+1)=='=')
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=20;
				Bolan.Index=OPERATOR_MORE_EQU;	
				OrginScript.Push(&Bolan);
				ExpStr+=2;	
			}
			else
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=20;
				Bolan.Index=OPERATOR_MORE;	
				OrginScript.Push(&Bolan);
				ExpStr++;				
			}
		}
		else if(*ExpStr=='<')
		{			
			if(*(ExpStr+1)=='=')
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=20;
				Bolan.Index=OPERATOR_LESS_EQU;		
				OrginScript.Push(&Bolan);
				ExpStr+=2;	
			}
			else if(*(ExpStr+1)=='>')
			{				
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=20;
				Bolan.Index=OPERATOR_DIFF;		
				OrginScript.Push(&Bolan);
				ExpStr+=2;	
			}
			else
			{					
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=20;
				Bolan.Index=OPERATOR_LESS;			
				OrginScript.Push(&Bolan);
				ExpStr++;				
			}			

		}
		else if(*ExpStr=='+'||*ExpStr=='-')
		{
			//if(*ExpStr=='-'&&(ExpStr==ExpBuff||*(ExpStr-1)=='('))
			//{				
			//	Bolan.Type=BOLAN_TYPE_OPERATOR;
			//	Bolan.Index=OPERATOR_NEG;
			//	Bolan.Level=50;
			//	OrginScript.Push(&Bolan);
			//	ExpStr++;
			//}
			//else
			{						
				Bolan.Type=BOLAN_TYPE_OPERATOR;
				Bolan.Level=30;
				if(*ExpStr=='+') Bolan.Index=OPERATOR_ADD;
				else Bolan.Index=OPERATOR_SUB;	
				OrginScript.Push(&Bolan);
				ExpStr++;				
			}
		}
		else if(*ExpStr=='*'||*ExpStr=='/')
		{						
			Bolan.Type=BOLAN_TYPE_OPERATOR;
			Bolan.Level=40;
			if(*ExpStr=='*') Bolan.Index=OPERATOR_MUL;
			else Bolan.Index=OPERATOR_DIV;	
			OrginScript.Push(&Bolan);
			ExpStr++;
		}
		else if(*ExpStr==',')
		{
			Bolan.Type=BOLAN_TYPE_OPERATOR;
			Bolan.Index=OPERATOR_COMMA;
			Bolan.Level=0;
			OrginScript.Push(&Bolan);			
			ExpStr++;
		}		
		else
		{
			return 1005;				//非法字符
		}		
	}

	CESThread ESThread;

	ESThread.SetScript(&OrginScript);
	ESThread.SetVariableList(pVariableList);
	ESThread.SetFunctionList(pFunctionList);

	int RetCode=DealIdentifiers(&ESThread);
	LastLine=ESThread.GetLastLine();
	if(RetCode)
		return RetCode;

	

	
	for(UINT i=0;i<OrginScript.GetSize();i++)
	{
		ES_BOLAN * pBolan=OrginScript.GetAt(i);
		LastLine=pBolan->Line;
		switch(pBolan->Type)
		{
		case BOLAN_TYPE_VALUE:
			Push(pBolan);
			break;
		case BOLAN_TYPE_OPERATOR:
			if(pBolan->Index==OPERATOR_RP)
			{
				while(TempStack.GetTop()!=NULL&&(TempStack.GetTop()->Type!=BOLAN_TYPE_OPERATOR||TempStack.GetTop()->Index!=OPERATOR_LP))
				{				
					ES_BOLAN * pTemp=TempStack.Pop();					
					Push(pTemp);
				}			
				if(TempStack.GetTop()==NULL||TempStack.GetTop()->Type!=BOLAN_TYPE_OPERATOR||TempStack.GetTop()->Index!=OPERATOR_LP)
					return 2021;
				TempStack.Pop();
			}
			else if(pBolan->Index==OPERATOR_LP)
			{
				TempStack.Push(pBolan);
			}			
			else if(pBolan->Index==OPERATOR_NOP)
			{

			}
			else if(pBolan->Index==OPERATOR_COMMA)
			{
				while(TempStack.GetTop()!=NULL&&(TempStack.GetTop()->Type!=BOLAN_TYPE_OPERATOR||TempStack.GetTop()->Index!=OPERATOR_LP))
				{				
					ES_BOLAN * pTemp=TempStack.Pop();					
					Push(pTemp);
				}	
			}
			else
			{
				while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->Level>=pBolan->Level)
				{		
					ES_BOLAN * pTemp=TempStack.Pop();				
					Push(pTemp);
				}	
				TempStack.Push(pBolan);
			}
			break;
		case BOLAN_TYPE_FUNCTION:
			while(TempStack.GetTop()!=NULL&&TempStack.GetTop()->Level>=pBolan->Level)
			{		
				ES_BOLAN * pTemp=TempStack.Pop();				
				Push(pTemp);
			}		
			TempStack.Push(pBolan);
			break;
		case BOLAN_TYPE_VARIABLE:
			Push(pBolan);
			break;
		case BOLAN_TYPE_KEYWORD:
			while(TempStack.GetTop()!=NULL)
			{		
				ES_BOLAN * pTemp=TempStack.Pop();			
				Push(pTemp);
			}	
			switch(pBolan->Index)
			{
			case KW_EXIT:
			case KW_IF:			
			case KW_THEN:
			case KW_ELSE:
			case KW_ELSEIF:				
			case KW_ENDIF:
			case KW_WHILE:				
			case KW_DO:				
			case KW_ENDWHILE:				
			case KW_BREAK:				
			case KW_CONTINUE:				
			case KW_GOTO:
				Push(pBolan);
				break;
			case KW_LINEEND:				
			case KW_INT:
			case KW_INT64:
			case KW_FLOAT:
			case KW_DOUBLE:
			case KW_STRING:				
				break;
			case KW_FUNCTION:				
			case KW_ENDFUN:				
				Push(pBolan);
				break;
			};			
			break;
		case BOLAN_TYPE_IDENTIFIER:
			while(TempStack.GetTop()!=NULL)
			{		
				ES_BOLAN * pTemp=TempStack.Pop();				
				Push(pTemp);
			}
			if(pBolan->Index!=IDENTIFIER_TYPE_VARIABLE_DEFINE)
				Push(pBolan);
			break;
		}
	}
	while(TempStack.GetTop()!=NULL)
	{		
		ES_BOLAN * pTemp=TempStack.Pop();
		if(pTemp->Type==BOLAN_TYPE_OPERATOR&&pTemp->Index==OPERATOR_LP)
			return 2021;
		Push(pTemp);
	}

	//设置各种控制语句
	ES_FUNCTION * pCurFunction=NULL;	

	for(UINT i=0;i<GetSize();i++)
	{
		ES_BOLAN * pBolan=GetAt(i);
		LastLine=pBolan->Line;
		if(pBolan->Type==BOLAN_TYPE_FUNCTION)
		{
			pBolan->Type=BOLAN_TYPE_OPERATOR;
			pBolan->Level=pBolan->Index;
			pBolan->Index=OPERATOR_CALL;
		}
		else if(pBolan->Type==BOLAN_TYPE_KEYWORD)
		{
			switch(pBolan->Index)
			{
			case KW_EXIT:
				pBolan->Type=BOLAN_TYPE_OPERATOR;
				pBolan->Index=OPERATOR_EXIT;
				break;
			case KW_IF:
			case KW_ELSEIF:
				{
					int EndPos=FindCoupleKeyWord(KW_ENDIF,KW_IF,KW_ENDIF,i+1);
					if(EndPos<0)
						return 2001;
					if(pBolan->Index==KW_ELSEIF)
					{
						pBolan->Type=BOLAN_TYPE_OPERATOR;
						pBolan->Index=OPERATOR_JMP;
						pBolan->Level=EndPos+1;
					}
					int Pos=GetNextKeyWord(i+1);
					if(Pos<0)
						return 2001;
					ES_BOLAN * pThenBolan=GetAt(Pos);
					if(pThenBolan->Type!=BOLAN_TYPE_KEYWORD&&pThenBolan->Index!=KW_THEN)
						return 2001;
					int ElseIfPos=FindCoupleKeyWord(KW_ELSEIF,KW_IF,KW_ENDIF,i+1,EndPos+1);
					int ElsePos=FindCoupleKeyWord(KW_ELSE,KW_IF,KW_ENDIF,i+1,EndPos+1);
					pThenBolan->Type=BOLAN_TYPE_OPERATOR;
					pThenBolan->Index=OPERATOR_JZ;
					if(ElseIfPos>=0)
						pThenBolan->Level=ElseIfPos+1;
					else if(ElsePos>=0)
						pThenBolan->Level=ElsePos+1;
					else
						pThenBolan->Level=EndPos+1;
				}					
				break;
			case KW_ELSE:
				{
					int EndPos=FindCoupleKeyWord(KW_ENDIF,KW_IF,KW_ENDIF,i+1);
					if(EndPos<0)
						return 2001;
					pBolan->Type=BOLAN_TYPE_OPERATOR;
					pBolan->Index=OPERATOR_JMP;
					pBolan->Level=EndPos+1;
				}
				break;
			case KW_WHILE:
				{
					int Pos=GetNextKeyWord(i+1);
					if(Pos<0)
						return 2011;
					ES_BOLAN * pDoBolan=GetAt(Pos);
					if(pDoBolan->Type!=BOLAN_TYPE_KEYWORD||pDoBolan->Index!=KW_DO)
						return 2011;
					Pos=FindCoupleKeyWord(KW_ENDWHILE,KW_WHILE,KW_ENDWHILE,i+1);
					if(Pos<0)
						return 2011;
					ES_BOLAN * pEndBolan=GetAt(Pos);
					pDoBolan->Type=BOLAN_TYPE_OPERATOR;
					pDoBolan->Index=OPERATOR_JZ;
					pDoBolan->Level=Pos+1;
					pEndBolan->Type=BOLAN_TYPE_OPERATOR;
					pEndBolan->Index=OPERATOR_JMP;
					pEndBolan->Level=i+1;
				}
				break;			
			case KW_ENDWHILE:
				{
					return 2011;
					//int Pos=FindCoupleKeyWordReverse(KW_WHILE,KW_WHILE,KW_ENDWHILE,i-1);
					//if(Pos<0)
					//	return 2011;
					//pBolan->Type=BOLAN_TYPE_OPERATOR;
					//pBolan->Index=OPERATOR_JMP;
					//pBolan->Level=Pos+1;
				}
				break;
			case KW_BREAK:
				{
					int Pos=FindCoupleKeyWord(KW_ENDWHILE,KW_WHILE,KW_ENDWHILE,i);
					if(Pos<0)
						return 2012;
					pBolan->Type=BOLAN_TYPE_OPERATOR;
					pBolan->Index=OPERATOR_JMP;
					pBolan->Level=Pos+1;
				}
				break;
			case KW_CONTINUE:
				{
					int Pos=FindCoupleKeyWordReverse(KW_WHILE,KW_WHILE,KW_ENDWHILE,i);
					if(Pos<0)
						return 2013;
					pBolan->Type=BOLAN_TYPE_OPERATOR;
					pBolan->Index=OPERATOR_JMP;
					pBolan->Level=Pos+1;
				}
				break;
			case KW_GOTO:
				{
					ES_BOLAN *pIDBolan=GetAt(i+1);
					if(pIDBolan==NULL)
						return 2014;
					if(pIDBolan->Type!=BOLAN_TYPE_IDENTIFIER)
						return 2014;
					int Pos=FindIdentifier(0,pIDBolan->StrValue);
					if(Pos<0)
						return 2015;
					pBolan->Type=BOLAN_TYPE_OPERATOR;
					pBolan->Index=OPERATOR_JMP;
					pBolan->Level=Pos+1;
				}
				break;
			case KW_FUNCTION:
				{
					int Pos=FindKeyWord(KW_ENDFUN,i,-1);
					if(Pos<0)
						return 2016;
					pBolan->Type=BOLAN_TYPE_OPERATOR;
					pBolan->Index=OPERATOR_JMP_FUNC;
					pBolan->Level=Pos+1;

					
				}
				break;
			case KW_ENDFUN:
				{					
					if(pCurFunction==NULL)
						return 2019;					
					pCurFunction->FunEndPos=i+1;
					pBolan->Type=BOLAN_TYPE_OPERATOR;
					pBolan->Index=OPERATOR_RET;
				}
				break;				
			}
		}
		else if(pBolan->Type==BOLAN_TYPE_IDENTIFIER&&pBolan->Index==IDENTIFIER_TYPE_FUNCTION_DEFINE)
		{
			pCurFunction=pFunctionList->FindFunction(pBolan->StrValue);
			if(pCurFunction==NULL)
			{
				return 2019;
			}
			if(pCurFunction->Type!=FUNCTION_TYPE_SCRIPT)
			{
				return 2019;
			}
			pCurFunction->FunStartPos=i;

			//反转函数参数顺序
			RetCode=ReverseFunctionParam(this,i+1,pCurFunction->ParaCount);
			if(RetCode)
				return RetCode;
		}
	}

	//删除多余的布兰式
	for(int i=(int)GetSize()-1;i>=0;i--)
	{
		ES_BOLAN * pBolan=GetAt(i);
		if(pBolan->Type!=BOLAN_TYPE_VALUE&&
			pBolan->Type!=BOLAN_TYPE_OPERATOR&&
			pBolan->Type!=BOLAN_TYPE_VARIABLE&&(pBolan->Type!=BOLAN_TYPE_KEYWORD||pBolan->Index!=KW_FUNCTION))
		{
			DeleteBolan(this,pFunctionList,i);
		}
	}

	//进行变量绑定

	ESThread.SetScript(this);
	RetCode=DoVariableBind(&ESThread,LastLine);
	if(RetCode)
		return RetCode;

	return 0;
	
}
Ejemplo n.º 9
0
SymbolTable::SymbolTable(string file_name) {
	file = file_name;

	ifstream fin(file.c_str());
	ofstream fout("data/symbol_table.txt");
	Symbol symbol = Symbol(file);
	Table table[ symbol.GetCount() ];

	string line;
	int count = 0;
	int level_max = 0;
	/*------temp save for table------*/
	int level = 0;
	string symbol_name = "";
	string type_name = "";
	bool array_bool = false;
	bool function_bool = false;

	Origin();

	while(getline(fin, line)) {
		istringstream fin_word(line);
		string word = "";
		string remain = "";
		int pos = 0;

		while(fin_word >> word) {
			while(word != "") {
				if((pos = Check(word)) != -1) {
					if(pos != 0) {
						remain = word.substr(pos);
						word = word.substr(0, pos);
					}
					else {
						remain = word.substr(pos + length(word));
						word = word.substr(0, length(word));
					}
				}

				/*-----save in table------*/
				if( FindKeyWord(word) ) {
					type_name = word;
				}
				else {
					if(type_name != "") {
						if(word == ";" || word == "," || word == ")" || word == "=" || word == "(" || word == "[") {
							if(word == ";" || word == "," || word == ")" || word == "=") {
								array_bool = false;
								function_bool = false;
							}
							else if(word == "(") {
								level = 0;
								array_bool = false;
								function_bool = true;
							}
							else if(word == "[") {
								array_bool = true;
								function_bool = false;
							}

							table[count].Set(level, symbol_name, type_name, array_bool, function_bool);
							count = count + 1;
							type_name = "";
						}
						else {
							symbol_name = word;
						}
					}
				}

				if(word == "{" || word == "(") {
					level = level + 1;
				}
				else if(word == ")") {
					level = level - 1;
				}

				if(level > level_max) {
					level_max = level;
				}
				/*------------------------*/

				pos = 0;
				word = remain;
				remain = "";
			}
		}
	}

	int level_temp = 0;
	while(level_temp <= level_max) {
		for(int i = 0; i < (sizeof(table)/sizeof(*table)); i++) {
			if(table[i].GetLevel() == level_temp) {
				if(table[i].GetLevel() == table[i - 1].GetLevel() + 1) {
					scope = scope + 1;
				}
					table[i].SetScope(scope);
			}
		}

		level_temp = level_temp + 1;
	}

	//sort table
	for(int i = 0; i < (sizeof(table)/sizeof(*table)); i++) {
		for(int j = i + 1; j < (sizeof(table)/sizeof(*table)); j++) {
			if(table[j].GetScope() < table[i].GetScope()) {
				Table temp = Table();
				temp.Set(table[j].GetLevel(), table[j].GetSymbol(), table[j].GetType(), table[j].GetArray(), table[j].GetFunction());
				temp.SetScope(table[j].GetScope());

				for(int k = j; k >= i + 1; k--) {
					table[k].Set(table[k - 1].GetLevel(), table[k - 1].GetSymbol(), table[k - 1].GetType(), table[k - 1].GetArray(), table[k - 1].GetFunction());
					table[k].SetScope(table[k - 1].GetScope());
				}

				table[i].Set(temp.GetLevel(), temp.GetSymbol(), temp.GetType(), temp.GetArray(), temp.GetFunction());
				table[i].SetScope(temp.GetScope());
			}
		}
	}

	int temp = 0;
	for(int i = 0; i < (sizeof(table)/sizeof(*table)); i++) {
		if(temp != table[i].GetScope()) {
			temp = table[i].GetScope();
			fout << endl;
		}

		fout << table[i].GetScope() << " ";
		fout << table[i].GetSymbol() << " ";
		fout << table[i].GetType() << " ";
		fout << (table[i].GetArray() ? "true" : "false") << " ";
		fout << (table[i].GetFunction() ? "true" : "false") << " ";
		fout << endl;
	}

	fin.close();
	fout.close();
}