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