📄 xasm.cpp
字号:
}
}
}
}
/******************************************************************************************
*
* IsCharWhitespace ()
*
* Returns a nonzero if the given character is whitespace, or zero otherwise.
*/
int IsCharWhitespace ( char cChar )
{
// Return true if the character is a space or tab.
if ( cChar == ' ' || cChar == '\t' )
return TRUE;
else
return FALSE;
}
/******************************************************************************************
*
* IsCharNumeric ()
*
* Returns a nonzero if the given character is numeric, or zero otherwise.
*/
int IsCharNumeric ( char cChar )
{
// Return true if the character is between 0 and 9 inclusive.
if ( cChar >= '0' && cChar <= '9' )
return TRUE;
else
return FALSE;
}
/******************************************************************************************
*
* IsCharIdentifier ()
*
* Returns a nonzero if the given character is part of a valid identifier, meaning it's an
* alphanumeric or underscore. Zero is returned otherwise.
*/
int IsCharIdent ( char cChar )
{
// Return true if the character is between 0 or 9 inclusive or is an uppercase or
// lowercase letter or underscore
if ( ( cChar >= '0' && cChar <= '9' ) ||
( cChar >= 'A' && cChar <= 'Z' ) ||
( cChar >= 'a' && cChar <= 'z' ) ||
cChar == '_' )
return TRUE;
else
return FALSE;
}
/******************************************************************************************
*
* IsCharDelimiter ()
*
* Return a nonzero if the given character is a token delimeter, and return zero otherwise
*/
int IsCharDelimiter ( char cChar )
{
// Return true if the character is a delimiter
if ( cChar == ':' || cChar == ',' || cChar == '"' ||
cChar == '[' || cChar == ']' ||
cChar == '{' || cChar == '}' ||
IsCharWhitespace ( cChar ) || cChar == '\n' )
return TRUE;
else
return FALSE;
}
/******************************************************************************************
*
* TrimWhitespace ()
*
* Trims whitespace off both sides of a string.
*/
void TrimWhitespace ( char * pstrString )
{
unsigned int iStringLength = strlen ( pstrString );
unsigned int iPadLength;
unsigned int iCurrCharIndex;
if ( iStringLength > 1 )
{
// First determine whitespace quantity on the left
for ( iCurrCharIndex = 0; iCurrCharIndex < iStringLength; ++ iCurrCharIndex )
if ( ! IsCharWhitespace ( pstrString [ iCurrCharIndex ] ) )
break;
// Slide string to the left to overwrite whitespace
iPadLength = iCurrCharIndex;
if ( iPadLength )
{
for ( iCurrCharIndex = iPadLength; iCurrCharIndex < iStringLength; ++ iCurrCharIndex )
pstrString [ iCurrCharIndex - iPadLength ] = pstrString [ iCurrCharIndex ];
for ( iCurrCharIndex = iStringLength - iPadLength; iCurrCharIndex < iStringLength; ++ iCurrCharIndex )
pstrString [ iCurrCharIndex ] = ' ';
}
// Terminate string at the start of right hand whitespace
for ( iCurrCharIndex = iStringLength - 1; iCurrCharIndex > 0; -- iCurrCharIndex )
{
if ( ! IsCharWhitespace ( pstrString [ iCurrCharIndex ] ) )
{
pstrString [ iCurrCharIndex + 1 ] = '\0';
break;
}
}
}
}
/******************************************************************************************
*
* IsStringWhitespace ()
*
* Returns a nonzero if the given string is whitespace, or zero otherwise.
*/
int IsStringWhitespace ( char * pstrString )
{
// If the string is NULL, return false
if ( ! pstrString )
return FALSE;
// If the length is zero, it's technically whitespace
if ( strlen ( pstrString ) == 0 )
return TRUE;
// Loop through each character and return false if a non-whitespace is found
for ( unsigned int iCurrCharIndex = 0; iCurrCharIndex < strlen ( pstrString ); ++ iCurrCharIndex )
if ( ! IsCharWhitespace ( pstrString [ iCurrCharIndex ] ) && pstrString [ iCurrCharIndex ] != '\n' )
return FALSE;
// Otherwise return true
return TRUE;
}
/******************************************************************************************
*
* IsStringIdentifier ()
*
* Returns a nonzero if the given string is composed entirely of valid identifier
* characters and begins with a letter or underscore. Zero is returned otherwise.
*/
int IsStringIdent ( char * pstrString )
{
// If the string is NULL return false
if ( ! pstrString )
return FALSE;
// If the length of the string is zero, it's not a valid identifier
if ( strlen ( pstrString ) == 0 )
return FALSE;
// If the first character is a number, it's not a valid identifier
if ( pstrString [ 0 ] >= '0' && pstrString [ 0 ] <= '9' )
return FALSE;
// Loop through each character and return zero upon encountering the first invalid identifier
// character
for ( unsigned int iCurrCharIndex = 0; iCurrCharIndex < strlen ( pstrString ); ++ iCurrCharIndex )
if ( ! IsCharIdent ( pstrString [ iCurrCharIndex ] ) )
return FALSE;
// Otherwise return true
return TRUE;
}
/******************************************************************************************
*
* IsStringInteger ()
*
* Returns a nonzero if the given string is composed entire of integer characters, or zero
* otherwise.
*/
int IsStringInteger ( char * pstrString )
{
// If the string is NULL, it's not an integer
if ( ! pstrString )
return FALSE;
// If the string's length is zero, it's not an integer
if ( strlen ( pstrString ) == 0 )
return FALSE;
unsigned int iCurrCharIndex;
// Loop through the string and make sure each character is a valid number or minus sign
for ( iCurrCharIndex = 0; iCurrCharIndex < strlen ( pstrString ); ++ iCurrCharIndex )
if ( ! IsCharNumeric ( pstrString [ iCurrCharIndex ] ) && ! ( pstrString [ iCurrCharIndex ] == '-' ) )
return FALSE;
// Make sure the minus sign only occured at the first character
for ( iCurrCharIndex = 1; iCurrCharIndex < strlen ( pstrString ); ++ iCurrCharIndex )
if ( pstrString [ iCurrCharIndex ] == '-' )
return FALSE;
return TRUE;
}
/******************************************************************************************
*
* IsStringFloat ()
*
* Returns a nonzero if the given string is composed entire of float characters, or zero
* otherwise.
*/
int IsStringFloat( char * pstrString )
{
if ( ! pstrString )
return FALSE;
if ( strlen ( pstrString ) == 0 )
return FALSE;
// First make sure we've got only numbers and radix points
unsigned int iCurrCharIndex;
for ( iCurrCharIndex = 0; iCurrCharIndex < strlen ( pstrString ); ++ iCurrCharIndex )
if ( ! IsCharNumeric ( pstrString [ iCurrCharIndex ] ) && ! ( pstrString [ iCurrCharIndex ] == '.' ) && ! ( pstrString [ iCurrCharIndex ] == '-' ) )
return FALSE;
// Make sure only one radix point is present
int iRadixPointFound = 0;
for ( iCurrCharIndex = 0; iCurrCharIndex < strlen ( pstrString ); ++ iCurrCharIndex )
if ( pstrString [ iCurrCharIndex ] == '.' )
if ( iRadixPointFound )
return FALSE;
else
iRadixPointFound = 1;
// Make sure the minus sign only appears in the first character
for ( iCurrCharIndex = 1; iCurrCharIndex < strlen ( pstrString ); ++ iCurrCharIndex )
if ( pstrString [ iCurrCharIndex ] == '-' )
return FALSE;
// If a radix point was found, return true; otherwise, it must be an integer so return false
if ( iRadixPointFound )
return TRUE;
else
return FALSE;
}
/******************************************************************************************
*
* PrintLogo ()
*
* Prints out logo/credits information.
*/
void PrintLogo ()
{
printf ( "XASM\n" );
printf ( "XtremeScript Assembler Version %d.%d\n", VERSION_MAJOR, VERSION_MINOR );
printf ( "Written by Alex Varanese\n" );
printf ( "\n" );
}
/******************************************************************************************
*
* PrintUsage ()
*
* Prints out usage information.
*/
void PrintUsage ()
{
printf ( "Usage:\tXASM Source.XASM [Executable.XSE]\n" );
printf ( "\n" );
printf ( "\t- File extensions are not required.\n" );
printf ( "\t- Executable name is optional; source name is used by default.\n" );
}
/******************************************************************************************
*
* LoadSourceFile ()
*
* Loads the source file into memory.
*/
void LoadSourceFile ()
{
// Open the source file in binary mode
if ( ! ( g_pSourceFile = fopen ( g_pstrSourceFilename, "rb" ) ) )
ExitOnError ( "Could not open source file" );
// Count the number of source lines
while ( ! feof ( g_pSourceFile ) )
if ( fgetc ( g_pSourceFile ) == '\n' )
++ g_iSourceCodeSize;
++ g_iSourceCodeSize;
// Close the file
fclose ( g_pSourceFile );
// Reopen the source file in ASCII mode
if ( ! ( g_pSourceFile = fopen ( g_pstrSourceFilename, "r" ) ) )
ExitOnError ( "Could not open source file" );
// Allocate an array of strings to hold each source line
if ( ! ( g_ppstrSourceCode = ( char ** ) malloc ( g_iSourceCodeSize * sizeof ( char * ) ) ) )
ExitOnError ( "Could not allocate space for source code" );
// Read the source code in from the file
for ( int iCurrLineIndex = 0; iCurrLineIndex < g_iSourceCodeSize; ++ iCurrLineIndex )
{
// Allocate space for the line
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -