?? zasm.cpp
字號:
*/
int IsStringWhitespace( char *pstrString )
{
if( !pstrString )
return false;
if( strlen(pstrString) == 0 )
return false;
unsigned int iCurrCharIndex;
for( iCurrCharIndex = 0;
iCurrCharIndex < strlen(pstrString);
++ iCurrCharIndex )
{
if( !IsCharWhitespace( pstrString[iCurrCharIndex] ) )
{
return false;
}
}
return true;
}
/*****************************************************************************************
*
* IsStringIdent
*
* 判斷一個字符串是否是標識符
*/
int IsStringIdent( char *pstrString )
{
if( !pstrString )
return false;
if( strlen(pstrString) == 0 )
return false;
// ---- 判斷第一個字符是不是數字 -----------------------------------------------------
if( pstrString[0] >= '0' && pstrString[0] <= '9' )
{
return false;
}
// ---- 判斷其余字符是否都是有效標識符的部分 -----------------------------------------
unsigned int iCurrCharIndex;
for( iCurrCharIndex = 0;
iCurrCharIndex < strlen( pstrString );
++ iCurrCharIndex )
{
if( !IsCharIdent( pstrString[iCurrCharIndex] ) )
{
return false;
}
}
return true;
}
/*****************************************************************************************
*
* StripComments()
*
* 去除注釋
*/
void StripComments( char *pstrSourceLine )
{
bool bInString = false;
for( unsigned int i=0 ; i < strlen(pstrSourceLine) ; ++i )
{
if( pstrSourceLine[i] == '"' )
{
if( bInString == true )
{
bInString = false;
}
else
{
bInString = true;
}
}
if( pstrSourceLine[i] == ';' )
{
if( bInString == false )
{
pstrSourceLine[i] = '\n';
pstrSourceLine[i+1] = '\0';
break;
}
}
}
}
/*****************************************************************************************
*
* TrimWhiteSpace()
*
* 去除左右空格
*/
void TrimWhitespace( char *pstrSourceLine )
{
int iStringTotalLength; // 輸入的字符串總長
int iStringLength; // 去除空格后的字符串長度
int iLeftPose = 0; // 左邊第一個非空格字符的位置
int iRightPose = 0; // 右邊第一個非空格字符的位置
// 計算字符串總長度
iStringTotalLength = static_cast<int>( strlen(pstrSourceLine) );
// 計算左邊第一個非空格字符的位置
for( int i=0 ; i<iStringTotalLength ; ++i )
{
if( !IsCharWhitespace(pstrSourceLine[i]) )
{
iLeftPose = i;
break;
}
}
// 計算右邊第一個非空格字符的位置, 過濾換行符
for( int i=iStringTotalLength-1 ; i>=0 ; --i )
{
if( !IsCharWhitespace(pstrSourceLine[i]) )
{
iRightPose = i;
break;
}
}
// 計算去除空格后的字符串長度
iStringLength = iRightPose - iLeftPose + 1;
// 將字符串左移到開頭
if( iLeftPose != 0 )
{
for( int i=0 ; i<iStringLength ; ++ i )
{
pstrSourceLine[i] = pstrSourceLine[i+iLeftPose];
}
}
pstrSourceLine[iStringLength] = '\0';
}
/*****************************************************************************************
*
* AddString()
*
* 向指定的表添加字符串
*/
int AddString( LinkedList *pList, char *pstrString )
{
// 創建節點指針用于遍歷
LinkedListNode *pNode = pList->pHead;
// 對表中的每一個節點循環操作
for( int iCurrNode = 0 ; iCurrNode<pList->iNodeCount ; ++ iCurrNode )
{
if( strcmp( pstrString, (char*)pNode->pData ) == 0 )
{
return iCurrNode;
}
else
{
pNode = pNode->pNext;
}
}
// 字符串還沒有添加,則添加新的字符串
char *pstrStringNode = new char[strlen(pstrString)+1];
strcpy( pstrStringNode, pstrString );
return AddNode( pList, pstrStringNode );
}
/*****************************************************************************************
*
* AddFunc()
*
* 向函數表添加函數
*/
int AddFunc( char *pstrName, int iEntryPoint )
{
// 如果指定函數名已經存在,則返回一個無效索引
if( GetFuncByName( pstrName ) != 0 )
return -1;
// 創建一個函數節點
FuncNode *pNewFunc = new FuncNode;
pNewFunc->iEntryPoint = iEntryPoint;
strcpy( pNewFunc->pstrName, pstrName );
// 把函數節點添加到表中并取回索引
pNewFunc->iIndex = AddNode( &g_FuncTable, pNewFunc );
// 返回新函數的索引
return pNewFunc->iIndex;
}
/*****************************************************************************************
*
* SetFuncInfo()
*
* 設置函數表節點參數數量、局部數據大小
*/
void SetFuncInfo( char *pstrName, int iParamCount, int iLocalDataSize )
{
// 在函數表中查找指定函數
FuncNode *pFunc = GetFuncByName( pstrName );
// 設定其余信息
pFunc->iParamCount = iParamCount;
pFunc->iLocalDataSize = iLocalDataSize;
}
/*****************************************************************************************
*
* GetFuncByName()
*
* 根據函數名取得函數節點
*/
FuncNode* GetFuncByName( char *pstrName )
{
// 如果表是空的,則返回一個空指針
if( g_FuncTable.iNodeCount == 0 )
return 0;
// 創建一個指針用于鏈表的遍歷
LinkedListNode *pCurrNode = g_FuncTable.pHead;
// 遍歷鏈表直至找到匹配的結構
for( int iCurrNode = 0 ; iCurrNode < g_FuncTable.iNodeCount ; ++ iCurrNode )
{
// 將當前節點數據轉換為函數表指針
FuncNode *pCurrFunc = (FuncNode*)pCurrNode->pData;
// 檢測是否匹配
if( strcmp( pCurrFunc->pstrName, pstrName ) == 0 )
{
return pCurrFunc;
}
else
{
pCurrNode = pCurrNode->pNext;
}
}
// 沒有找到匹配的結構,返回0
return 0;
}
/*****************************************************************************************
*
* AddSymbol()
*
* 向符號表添加符號
*/
int AddSymbol( char *pstrIdent, int iSize, int iStackIndex, int iFuncIndex )
{
// 如果標簽已經存在,返回無效索引
if( GetSymbolByIdent( pstrIdent, iFuncIndex ) )
return -1;
// 創建新的符號節點
SymbolNode *pNewSymbol = new SymbolNode;
// 為新節點賦值
strcpy( pNewSymbol->pstrIdent, pstrIdent );
pNewSymbol->iSize = iSize;
pNewSymbol->iStackIndex = iStackIndex;
pNewSymbol->iFuncIndex = iFuncIndex;
// 想符號表中添加符號,并取得索引
pNewSymbol->iIndex = AddNode( &g_SymbolTable, pNewSymbol );
// 返回新符號的索引
return pNewSymbol->iIndex;
}
/*****************************************************************************************
*
* GetSymbolByIdent()
*
* 根據標識符取得符號節點指針
*/
SymbolNode* GetSymbolByIdent( char *pstrIdent, int iFuncIndex )
{
// 聲明鏈表節點指針用于符號表的遍歷
LinkedListNode *pCurrNode = g_SymbolTable.pHead;
// 遍歷符號表
for( int iCurrNode=0 ; iCurrNode<g_SymbolTable.iNodeCount ; ++ iCurrNode )
{
// 將當前節點數據轉換成符號表節點數據
SymbolNode *pCurrSymbol = (SymbolNode*)pCurrNode->pData;
// 檢測當前符號節點是否和指定符號匹配
if( strcmp( pCurrSymbol->pstrIdent, pstrIdent ) == 0 )
{
// 當前節點標識符匹配時檢測作用域是否相同或重疊(全局/局部)
if( pCurrSymbol->iFuncIndex == iFuncIndex || pCurrSymbol->iStackIndex >= 0 )
{
return pCurrSymbol;
}
}
else
{
pCurrNode = pCurrNode->pNext;
}
}
// 沒找到指定節點,返回0
return 0;
}
/*****************************************************************************************
*
* GetStackIndexByIdent()
*
* 根據標識符取得其堆棧索引
*/
int GetStackIndexByIdent( char *pstrIdent, int iFuncIndex )
{
// 取得符號
SymbolNode *pSymbol = GetSymbolByIdent( pstrIdent, iFuncIndex );
// 返回堆棧索引
return pSymbol->iStackIndex;
}
/*****************************************************************************************
*
* GetSizeByIdent()
*
* 根據標識符取得符號大小
*/
int GetSizeByIdent( char *pstrIdent, int iFuncIndex )
{
// 取得符號
SymbolNode *pSymbol = GetSymbolByIdent( pstrIdent, iFuncIndex );
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -