📄 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 + -