📄 compilecode.cpp
字号:
// CompileCode.cpp: implementation of the CCompileCode 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\CompileCode.h"
#include "..\Interface\ConstDef.h"
#include "..\..\Formule\Calc\RegularOpt.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// CSZGetWord:: Construction/Destruction
//////////////////////////////////////////////////////////////////////
#define MAKE_CONTINUE_TAG(n) (-4 - (n))
#define IS_CONTINUE_TAG(n) ((n) <= -4)
#define GET_CONTINUE_TAG(n) (4 - (n))
CSZGetWord::CSZGetWord()
{
m_szSource = NULL;
m_nSourceLen = 0;
m_nCmpPos = 0;
m_bAcceptOpt = FALSE;
m_nCurLine = 0;
m_nCurLineBeginPos = 0;
}
CSZGetWord::~CSZGetWord()
{
}
inline int CSZGetWord::GetCurPos()
{
return m_nCmpPos;
}
inline bool CSZGetWord::IsEnd()
{
return m_nCmpPos>=m_nSourceLen;
}
inline void CSZGetWord::NewLine()
{
m_nCurLine ++;
m_nCurLineBeginPos = m_nCmpPos;
}
inline int CSZGetWord::GetCurLine()
{
return m_nCurLine;
}
inline int CSZGetWord::GetCurPosInLine()
{
return m_nCmpPos - m_nCurLineBeginPos;
}
void CSZGetWord::Reset()
{
m_nCmpPos=0;
m_nCurLine = 0;
m_nCurLineBeginPos = 0;
}
void CSZGetWord::SetSource(LPCTSTR szSou, int nL)
{
Reset();
m_nCmpPos = 0;
m_bAcceptOpt = FALSE;
m_szSource = szSou;
if(nL<0)
m_nSourceLen = strlen(szSou);
else
m_nSourceLen = nL;
}
void CSZGetWord::TrimSpace()
{
while((m_nCmpPos < m_nSourceLen ) &&
(
(m_szSource[m_nCmpPos]==' ') ||
(m_szSource[m_nCmpPos]==9) ||
(m_szSource[m_nCmpPos]==13) ||
(m_szSource[m_nCmpPos]==10)
)
)
{
if(m_szSource[m_nCmpPos] == 13)
NewLine();
m_nCmpPos++;
}
}
void CSZGetWord::TrimComment()
{
if(m_nCmpPos > m_nSourceLen-1) return;
char ch = m_szSource[m_nCmpPos];
char ch2 = m_szSource[m_nCmpPos+1];
// 判断是否是注释语句
while((ch == '/') && (ch2 == '/' || ch2 == '*')) {
m_nCmpPos+=2;
if( ch2 == '/'){ //单行注释
m_nCmpPos++;
while( ( m_nCmpPos <m_nSourceLen ) && (m_szSource[m_nCmpPos] !=13))
m_nCmpPos++;
NewLine();
m_nCmpPos++;
}else if(ch2 == '*')
{ // 多行注释
m_nCmpPos++;
while( ( m_nCmpPos <m_nSourceLen-1 ) &&
(
(m_szSource[m_nCmpPos] !='*') ||
(m_szSource[m_nCmpPos+1] !='/')
)
)
{
if(m_szSource[m_nCmpPos] == 13)
NewLine();
m_nCmpPos++;
}
m_nCmpPos+=2;
}
TrimSpace();
if(m_nCmpPos > m_nSourceLen-1) return;
ch = m_szSource[m_nCmpPos];
ch2 = m_szSource[m_nCmpPos+1];
}
}
CString CSZGetWord::GetAWord()
{
TrimSpace();
TrimComment();
if( m_nCmpPos >= m_nSourceLen)
return CString("");// KEY_UNKNOW;//m_iPreIsn = KEY_UNKNOW;
char ch = m_szSource[m_nCmpPos++];
int optenable = m_bAcceptOpt;
m_bAcceptOpt = FALSE;
CString sCurWord(ch);
switch(ch){
case '.':
if(m_szSource[m_nCmpPos] == '.' && m_szSource[m_nCmpPos+1] == '.'){
m_nCmpPos += 2;
sCurWord = "...";
return sCurWord;
}
break;//m_iPreIsn = BS_DOT ; return BS_DOT ;
case ',':
case ';':
case ':':
case '[':
case ']':
case '(':
case ')':
case '{':
case '}':
case '@':
case '%':
case '#':
return sCurWord;
case '*':
case '/':
case '=':
case '>':
case '<':
case '!':
if(m_szSource[m_nCmpPos] == '=') {
m_nCmpPos ++;
sCurWord += "=";
}
return sCurWord;
case '+':
if(m_szSource[m_nCmpPos] == '=') {
m_nCmpPos ++;
sCurWord += "=";
return sCurWord;
}
if(m_szSource[m_nCmpPos] == '+') {
m_nCmpPos ++;
sCurWord += "+";
m_bAcceptOpt = TRUE;
return sCurWord;
}
if( optenable )
return sCurWord;
return sCurWord;
case '-':
if(m_szSource[m_nCmpPos] == '=') {
m_nCmpPos ++;
sCurWord += "=";
return sCurWord;
}
if(m_szSource[m_nCmpPos] == '-') {
m_nCmpPos ++;
sCurWord += "-";
m_bAcceptOpt = TRUE;
return sCurWord;
}
if( optenable )
return sCurWord; //{ m_iPreIsn = OP_ADD ; return OP_ADD ;}
break;
case '&':
if(m_szSource[m_nCmpPos] == '&') {
m_nCmpPos ++;
sCurWord += "&";
}
return sCurWord;
case '|':
if(m_szSource[m_nCmpPos] == '|') {
m_nCmpPos ++;
sCurWord += "|";
}
return sCurWord;
default:
break;
}
m_bAcceptOpt = TRUE;
if(((ch>='a')&&(ch<='z'))||((ch>='A')&&(ch<='Z'))||(ch=='_')){
ch = m_szSource[m_nCmpPos++];
while( (m_nCmpPos<m_nSourceLen) &&
( ((ch>='a')&&(ch<='z')) ||((ch>='A')&&(ch<='Z')) || (ch=='_') ||
((ch>='0')&&(ch<='9')) ) )
{
sCurWord += ch;
ch = m_szSource[m_nCmpPos++];
}
m_nCmpPos--;
return sCurWord;//return GEN_SIGN;
}
bool bHasDot = ch == '.';
if(((ch>='0')&&(ch<='9'))||(ch=='.')||(ch=='+')||(ch=='-')){ // const
ch = m_szSource[m_nCmpPos++];
if((ch=='+')||(ch=='-')){
while((m_nCmpPos<m_nSourceLen) &&
(ch == ' ' || ch == 9) )
{
ch = m_szSource[m_nCmpPos++];
}
}
while( (m_nCmpPos<m_nSourceLen) &&( (ch=='.') || ((ch>='0')&&(ch<='9')) ) )
{
if(ch=='.'){
if(bHasDot)
break;
bHasDot = true;
}
sCurWord += ch;
ch = m_szSource[m_nCmpPos++];
}
m_nCmpPos--;
return sCurWord;//m_iPreIsn = GEN_CTNUM;return GEN_CTNUM;
}
if(ch == '"'){ //String
for( ch = m_szSource[m_nCmpPos++];
( ch != '"')&&(m_nCmpPos < m_nSourceLen) ;
ch = m_szSource[m_nCmpPos++])
{
if(ch == 13){
NewLine();
break;
}
if(ch == '\\' && m_nCmpPos < m_nSourceLen ){
ch = m_szSource[m_nCmpPos++];
switch(ch){
case 'r':
case 'R':
ch = 13;
break;
case 'n':
case 'N':
ch = 10;
break;
case 't':
case 'T':
ch = 9;
break;
}
// 分析转义符
}
sCurWord += ch;
}
sCurWord += '"';
return sCurWord;
}
if(ch == '\''){ //String
for( ch = m_szSource[m_nCmpPos++];
( ch != '\'') && (m_nCmpPos < m_nSourceLen) ;
ch = m_szSource[m_nCmpPos++] )
{
if(ch == 13){
NewLine();
break;
}
if(ch == '\\' && m_nCmpPos < m_nSourceLen ){
ch = m_szSource[m_nCmpPos++];
switch(ch){
case 'r':
case 'R':
ch = 13;
break;
case 'n':
case 'N':
ch = 10;
break;
case 't':
case 'T':
ch = 9;
break;
}
// 分析转义符
}
sCurWord += ch;
}
sCurWord += '\'';
return sCurWord;
}
m_bAcceptOpt = optenable;
return sCurWord;
}
//////////////////////////////////////////////////////////////////////
// CChainStack::Construction/Destruction
//////////////////////////////////////////////////////////////////////
CChainStack::CChainStack()
{
m_nDept=0;
}
CChainStack::~CChainStack()
{
}
int CChainStack::GetDept()
{
return m_nDept;
}
void CChainStack::Push(SJMPChain & SC)
{
if( m_nDept >= m_Stack.GetSize() )
m_Stack.SetSize(m_nDept+1);
m_Stack[m_nDept] = SC;
m_nDept++;
}
SJMPChain CChainStack::Pop()
{
ASSERT(m_nDept>0);
m_nDept--;
return m_Stack[m_nDept];
}
PJMPCHAIN CChainStack::GetTop()
{
//ASSERT(m_nDept>0);
if(m_nDept ==0)
return NULL;
return &m_Stack[m_nDept-1];
}
PJMPCHAIN CChainStack::GetTopLoopChain()
{
for(int i=m_nDept-1;i>=0; i--)
if(m_Stack[i].SEN_TYPE == RES_WHILE ||
m_Stack[i].SEN_TYPE == RES_FOR
)
return &m_Stack[i];
return NULL;
}
void CChainStack::Clear()
{
m_nDept = 0;
}
BOOL CChainStack::IsEmpty()
{
return m_nDept == 0;
}
//////////////////////////////////////////////////////////////////////
// CCompileCode::Construction/Destruction
//////////////////////////////////////////////////////////////////////
CCompileCode::CCompileCode()
{
m_sCurWord = "";
m_nCurISN = 0;
m_bIsBack = FALSE;
m_CurWT = WT_UNKNOWN;
m_pFormulaModula = NULL;
}
CCompileCode::~CCompileCode()
{
}
void CCompileCode::Reset()
{
m_sCurWord = "";
m_nCurISN = 0;
m_bIsBack = FALSE;
m_CurWT = WT_UNKNOWN;
m_slErrorMsgs.RemoveAll();
m_JMPChain.Clear();
m_pFormulaModula->Reset();
m_SourceCode.SetSource(m_pFormulaModula->GetSource(),m_pFormulaModula->GetSourceLen());
}
void CCompileCode::RaiseWarning(LPCTSTR sMsg)
{
CString sStr;
sStr.Format("Line %d Warning: %s",m_SourceCode.GetCurLine(),sMsg);
m_slErrorMsgs.AddTail(sStr);
}
void CCompileCode::RaiseError(LPCTSTR sMsg)
{
CString sStr;
sStr.Format("Line %d Error: %s",m_SourceCode.GetCurLine(),sMsg);
m_slErrorMsgs.AddTail(sStr);
}
void CCompileCode::WordMissing(LPCTSTR sWord)
{
CString sStr;
sStr.Format("missing \'%s\', but \'%s\' appears!",sWord,m_sCurWord );
RaiseError(sStr);
}
void CCompileCode::WordRedefined(LPCTSTR sWord)
{
CString sStr;
sStr.Format("%s is redefined!",sWord);
RaiseError(sStr);
}
#define RETURN_ISN(isn) m_nCurISN = isn; \
return isn;
int CCompileCode::Advance()
{
if(m_bIsBack){ // BackWord
ASSERT(m_nCurISN);
m_bIsBack = FALSE;
return m_nCurISN;
}
m_sCurWord = m_SourceCode.GetAWord();
if( m_sCurWord=="")
return KEY_UNKNOWN;
int nISN = IsSeparator(m_sCurWord);
if( nISN != 0) {
m_CurWT = WT_SEPARATOR;
RETURN_ISN(nISN);
}
nISN = IsOperator(m_sCurWord);
if( nISN != 0) {
m_CurWT = WT_OPERATOR;
RETURN_ISN(nISN);
}
nISN = IsEvaluate(m_sCurWord);
if( nISN != 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -