www.pudn.com > Basic语言解释器.zip > Make.cpp


// Make.cpp: implementation of the CMake class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "Make.h" 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CMake::CMake(char *Prog,int Proglength) 
{ 
  proglength=Proglength; 
  prog=new char[Proglength+1]; 
  strcpy(prog,Prog); 
} 
 
CMake::~CMake() 
{ 
  
} 
 
 
CToken CMake::get_token(void)  
{ 
	//register char *temp; 
	CToken m_token; 
    m_token.token_type=0; 
	m_token.tok=0; 
	//temp=m_token.token; 
	if(*prog=='\0') 
	{ 
		m_token.tokenlength=1; 
		m_token.token=new char[m_token.tokenlength]; 
		*m_token.token='\0'; 
		m_token.tok=0; 
		m_token.token_type=FINISHED; 
		return m_token; 
	} 
	while(iswhite(*prog)) ++prog; 
     
	if(*prog=='\r') //如果是换行符 
	{ 
		m_token.tokenlength=2; 
		m_token.token=new char[m_token.tokenlength]; 
		m_token.token[0]=*prog; 
		m_token.token[1]='\0'; 
		m_token.token_type=ENTER; 
		prog++; 
		return m_token; 
	} 
	if( isdelim(*prog)) // 如果找得到运算符号标记 
	{ 
	   m_token.tokenlength=2; 
	   m_token.token=new char[m_token.tokenlength]; 
	   *m_token.token=*prog;  
	   *(m_token.token+1)='\0'; 
	   m_token.tok=0; 
	   m_token.token_type=DELIMITER; 
	   prog++; 
	   return m_token;            // 譬如 token[0]='+' token[1]='\0'; 
	} 
 
	if(*prog=='"')   // 如果是字符串 
	{ 
		prog++; 
		int i=0; 
		char token_temp[TOKEN_MAX]; 
		while(*prog!='"' && *prog!='\r' && i= 0 || c==9 || c=='\r' || c==0) 
		return 1; 
	return 0; 
} 
 
int CMake::findchar(char *str,char ch)  
{ 
	int length=strlen(str); 
	if(length>0) 
	{ 
		for(int i=0;i m_tokens; // 装下所有的token集 
	CSub m_Sub; // 一个临时的Sub 
	///* 
    for(;;)  // 依此从源代码中提取token,装到tokens中 
	{ 
		m_token=get_token(); // 得到下一个token 
		if(m_token.token_type==NONE) 
			continue; 
 
		///////////////////////////////////////////////// 
		//   如果是遇到过程定义开头的Sub 
		if(m_token.token_type==COMMAND && m_token.tok==SUB) 
		{ 
			   if(m_tokens.size()>=1) 
				if(m_tokens.at(m_tokens.size()-1).token_type==COMMAND || 
					m_tokens.at(m_tokens.size()-1).tok==END) // 如果是End Sub中的Sub 
					continue; 
				 
				m_token=get_token(); 
				while(m_token.token_type==NONE)  // 过滤NONE的token 
					m_token=get_token(); 
				 
				if(m_token.token_type==STRING) 
				{ 
					m_Sub.namelength=m_token.tokenlength; 
					m_Sub.name=new char[m_Sub.namelength]; 
					strcpy(m_Sub.name,m_token.token); 
				} 
				else 
				{ 
					printf("Sub name error!"); 
					return ; 
				} 
				m_tokens.clear(); // 清除上个sub的tokens,重新开始记录token 
           
				//下面是关于参数的处理 
				m_token=get_token(); 
				if(*m_token.token=='(') 
				{ 
					for(;;) 
					{ 
						m_token=get_token(); // 得到下一个token 
						if(m_token.token_type==NONE) 
							continue; 
						if(*m_token.token==',') 
							continue; 
						if(m_token.token_type==FINISHED) 
							break; 
						if(*m_token.token==')')  
							break; 
						m_Sub.m_argtokens.push_back(m_token); 
					} 
				}	   
				continue; 
		} 
 
		// 如果遇到End Sub 
		if(m_token.token_type==COMMAND && m_token.tok==END) 
		{ 
			m_token=get_token(); 
			while(m_token.token_type==NONE)  // 过滤NONE的token 
				m_token=get_token(); 
			if(m_token.token_type==COMMAND && m_token.tok==SUB) 
			{ 
				m_Sub.m_tokens=m_tokens; 
				m_Subs.push_back(m_Sub); 
				continue; 
			} 
		} 
		/////////////////////////////////////////////////////// 
		m_tokens.push_back(m_token); //将得到的token装到m_tokens集中 
		if(m_token.token_type==FINISHED) //如果这是最后的字符,则退出 
		  break; 
	} 
     //*/ 
}