?? zasm.cpp
字號:
LinkedList g_SymbolTable;
// ---- 指令查找表 -----------------------------------------------------------------------
InstrLookup g_InstrTable[MAX_INSTR_LOOKUP_COUNT];
// ---- 主程序API調用表
LinkedList g_HostAPICallTable;
// ---- 函數原型 -----------------------------------------------------------------------------
// ---- main -----------------------------------------------------------------------------
void PrintLogo();
void PrintUsage();
void Init();
void ShutDown();
void LoadSourceFile();
void AssembleSourceFile();
void PrintAssembleStates();
void BuildZSE();
// ---- 詞法分析 -------------------------------------------------------------------------
Token GetNextToken();
char* GetCurrLexeme();
int SkipToNextLine();
void ResetLexer();
char GetLookAheadChar();
// ---- 簡單鏈表 -------------------------------------------------------------------------
int AddFunc( char *pstrName, int iEntryPoint );
void InitLinkedList( LinkedList *pList );
void ResetLexer();
void FreeLinkedList( LinkedList *pList );
// ---- 字符串處理 -----------------------------------------------------------------------
int IsCharNumeric( char cChar );
int IsCharWhitespace( char cChar );
int IsCharDelimiter( char cChar );
int IsCharIdent( char cChar );
int IsStringInt( char *pstrString );
int IsStringFloat( char *pstrString );
int IsStringWhitespace( char *pstrString );
int IsStringIdent( char *pstrString );
void StripComments( char *pstrSourceLine );
void TrimWhitespace( char *pstrSourceLine );
// ---- 字符串表 -------------------------------------------------------------------------
int AddString( LinkedList *pList, char *pstrString );
// ---- 函數表 ---------------------------------------------------------------------------
int AddFunc( char * pstrName, int iEntryPoint );
void SetFuncInfo( char * pstrName, int iParamCount, int iLocalDataSize );
FuncNode* GetFuncByName( char *pstrName );
// ---- 符號表 ---------------------------------------------------------------------------
int AddSymbol( char *pstrIdent, int iSize, int iStackIndex, int iFuncIndex );
int GetSizeByIdent( char *pstrIdent, int iFuncIndex );
int GetStackIndexByIdent( char *pstrIdent, int iFuncIndex );
SymbolNode* GetSymbolByIdent( char *pstrIdent, int iFuncIndex );
// ---- 標簽表 ---------------------------------------------------------------------------
int AddLabel( char *pstrIdent, int iTargetIndex, int iFuncIndex );
LabelNode* GetLabelByIdent( char *pstrIdent, int iFuncIndex );
// ---- 指令 -----------------------------------------------------------------------------
void InitInstrTable();
int AddInstrLookup( char *pstrMnemonic, int iOpCode, int iOpCount );
void SetOpType( int iInstrIndex, int iOpIndex, OpTypes iOpType );
bool GetInstrByMnemonic( char *pstrMnemonic, InstrLookup *pInstr );
// ---- 出錯處理 -------------------------------------------------------------------------
void ExitOnCharExpectedError( char cChar );
void ExitOnCodeError( char *pstrErrorMsg );
void ExitOnError( char *pstrErrorMsg );
void Exit();
// ---- 函數定義 -----------------------------------------------------------------------------
/*****************************************************************************************
*
* AddNode()
*
* 向指定的簡單鏈表鏈表添加節點
*/
int AddNode( LinkedList *pList, void *pData )
{
// 創建一個新節點
LinkedListNode *pNewNode = new LinkedListNode;
// 把節點數據設定成指定的指針
pNewNode->pData = pData;
// 把下一個指針設為空
pNewNode->pNext = 0;
// 如果當前鏈表為空,則把頭指針和末指針都設成新節點
if( pList->iNodeCount == 0 )
{
pList->pHead = pList->pTail = pNewNode;
}
// 否則把它追加到鏈表的最后
else
{
pList->pTail->pNext = pNewNode;
pList->pTail = pNewNode;
}
// 增大鏈表中節點的數量
++pList->iNodeCount;
// 返回新的鏈表的大小減一,也就是新節點的索引
return pList->iNodeCount - 1;
}
/*****************************************************************************************
*
* InitLinkedList()
*
* 初始化指定鏈表
*/
void InitLinkedList( LinkedList *pList )
{
pList->pHead = 0;
pList->pTail = 0;
pList->iNodeCount = 0;
}
/*****************************************************************************************
*
* FreeLinkedList()
*
* 釋放指定鏈表
*/
void FreeLinkedList( LinkedList *pList )
{
if( !pList ) return;
if( pList->iNodeCount != 0 )
{
// 保存當前節點和下一個節點的指針
LinkedListNode *pCurrNode;
LinkedListNode *pNextNode;
// 設定當前節點為鏈表頭
pCurrNode = pList->pHead;
// 遍歷鏈表
while(true)
{
// 釋放當前節點前保存下一個節點的指針
pNextNode = pCurrNode->pNext;
// 釋放當前節點中的數據
if( pCurrNode->pData != 0 )
{
delete pCurrNode->pData;
}
// 清除當前節點
delete pCurrNode;
// 如果存在下一個節點則后移,否則退出循環
if( pNextNode != 0 )
{
pCurrNode = pNextNode;
}
else
{
break;
}
}
}
}
/*****************************************************************************************
*
* IsCharNumeric()
*
* 判斷一個字符是否是數字
*/
int IsCharNumeric( char cChar )
{
if( cChar >= '0' && cChar <= '9' )
return true;
else
return false;
}
/*****************************************************************************************
*
* IsCharWhitespace()
*
* 判斷一個字符是否是空白符
*/
int IsCharWhitespace( char cChar )
{
if( cChar == ' ' || cChar == '\t' )
return true;
else
return false;
}
/*****************************************************************************************
*
* IsCharDelimiter()
*
* 判斷一個字符是否是分隔符的部分
*/
int IsCharDelimiter( char cChar )
{
if( cChar == ':' || cChar == ',' || cChar == '"' ||
cChar == '{' || cChar == '}' ||
cChar == '[' || cChar == ']' ||
IsCharWhitespace( cChar ) || cChar == '\n' )
{
return true;
}
else
{
return false;
}
}
/*****************************************************************************************
*
* IsCharIdent()
*
* 判斷一個字符是不是有效標識符的部分
*/
int IsCharIdent( char cChar )
{
if( ( cChar >= '0' && cChar <= '9' ) ||
( cChar >= 'A' && cChar <= 'Z' ) ||
( cChar >= 'a' && cChar <= 'z' ) ||
cChar == '_' )
{
return true;
}
else
{
return false;
}
}
/*****************************************************************************************
*
* IsStringInt()
*
* 判斷一個字符串是否是整數
*/
int IsStringInt( char *pstrString )
{
if( !pstrString )
return false;
if( strlen(pstrString) == 0 )
return false;
unsigned int iCurrCharIndex;
// 判斷第一個字符是不是數字和負號以外的字符
if( !IsCharNumeric( pstrString[0] ) )
{
if( pstrString[0] != '-' )
{
return false;
}
}
// 判斷其余字符是否都是數字
for( iCurrCharIndex = 1 ;
iCurrCharIndex < strlen( pstrString );
++iCurrCharIndex )
{
if( !IsCharNumeric(pstrString[iCurrCharIndex]) )
{
return false;
}
}
return true;
}
/*****************************************************************************************
*
* IsStringFloat()
*
* 判斷一個字符串是否是浮點數
*/
int IsStringFloat( char *pstrString )
{
if( !pstrString )
return false;
if( strlen(pstrString) == 0 )
return false;
unsigned int iCurrCharIndex;
bool bRadixPointFound = false;
// ---- 判斷第一個字符是不是數字、小數點和負號以外的字符 -----------------------------
if( !IsCharNumeric( pstrString[0] ) )
{
if( pstrString[0] != '-' && pstrString[0] != '.' )
{
return false;
}
if( pstrString[0] == '.' )
{
bRadixPointFound = true;
}
}
// ---- 判斷其余字符是不是數字并且只有一個小數點 -------------------------------------
for( iCurrCharIndex = 1;
iCurrCharIndex < strlen( pstrString );
++ iCurrCharIndex )
{
if( pstrString[iCurrCharIndex] == '.' )
{
if( bRadixPointFound == true )
{
return false;
}
else
{
bRadixPointFound = true;
}
}
else if( !IsCharNumeric( pstrString[iCurrCharIndex] ) )
{
return false;
}
}
if( bRadixPointFound )
return true;
else
return false;
}
/*****************************************************************************************
*
* IsStringWhitespace()
*
* 判斷一個字符串是否是空白符
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -