⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xasm.cpp

📁 < Game Script Mastery>> source code
💻 CPP
📖 第 1 页 / 共 5 页
字号:
                int iStackIndex;                        // Stack index
                int iInstrIndex;                        // Instruction index
                int iFuncIndex;                         // Function index
                int iHostAPICallIndex;                  // Host API Call index
                int iReg;                               // Register code
            };
            int iOffsetIndex;                           // Index of the offset
        }
            Op;

        typedef struct _Instr                           // An instruction
        {
            int iOpcode;                                // Opcode
            int iOpCount;                               // Number of operands
            Op * pOpList;                               // Pointer to operand list
        }
            Instr;

    // ---- Function Table --------------------------------------------------------------------

        typedef struct _FuncNode                        // A function table node
        {
			int iIndex;									// Index
            char pstrName [ MAX_IDENT_SIZE ];           // Name
            int iEntryPoint;                            // Entry point
            int iParamCount;                            // Param count
            int iLocalDataSize;                         // Local data size
        }
            FuncNode;

    // ---- Label Table -----------------------------------------------------------------------

        typedef struct _LabelNode                       // A label table node
        {
			int iIndex;									// Index
            char pstrIdent [ MAX_IDENT_SIZE ];          // Identifier
            int iTargetIndex;                           // Index of the target instruction
            int iFuncIndex;                             // Function in which the label resides
        }
            LabelNode;

    // ---- Symbol Table ----------------------------------------------------------------------

        typedef struct _SymbolNode                      // A symbol table node
        {
			int iIndex;									// Index
            char pstrIdent [ MAX_IDENT_SIZE ];          // Identifier
            int iSize;                                  // Size (1 for variables, N for arrays)
            int iStackIndex;                            // The stack index to which the symbol
                                                        // points
            int iFuncIndex;                             // Function in which the symbol resides
        }
            SymbolNode;

// ---- Global Variables ----------------------------------------------------------------------

    // ---- Lexer -----------------------------------------------------------------------------

        Lexer g_Lexer;                                  // The lexer

    // ---- Source Code -----------------------------------------------------------------------

        char ** g_ppstrSourceCode = NULL;               // Pointer to dynamically allocated
                                                        // array of string pointers.
        int g_iSourceCodeSize;                          // Number of source lines

		FILE * g_pSourceFile = NULL;                    // Source code file pointer

		char g_pstrSourceFilename [ MAX_FILENAME_SIZE ],	// Source code filename
		     g_pstrExecFilename [ MAX_FILENAME_SIZE ];	// Executable filename

    // ---- Script ----------------------------------------------------------------------------

        ScriptHeader g_ScriptHeader;                    // Script header data

        int g_iIsSetStackSizeFound;                     // Has the SetStackSize directive been
                                                        // found?
        int g_iIsSetPriorityFound;                      // Has the SetPriority directive been
                                                        // found?

    // ---- Instruction Lookup Table ----------------------------------------------------------

        InstrLookup g_InstrTable [ MAX_INSTR_LOOKUP_COUNT ];    // The master instruction
                                                                // lookup table

    // ---- Assembled Instruction Stream ------------------------------------------------------

        Instr * g_pInstrStream = NULL;                  // Pointer to a dynamically allocated
                                                        // instruction stream
        int g_iInstrStreamSize;                         // The number of instructions

        int g_iCurrInstrIndex;                          // The current instruction's index

    // ---- Function Table --------------------------------------------------------------------

        LinkedList g_FuncTable;                         // The function table

    // ---- Label Table -----------------------------------------------------------------------

        LinkedList g_LabelTable;                        // The label table

    // ---- Symbol Table ----------------------------------------------------------------------

        LinkedList g_SymbolTable;                       // The symbol table

	// ---- String Table ----------------------------------------------------------------------

		LinkedList g_StringTable;						// The string table

	// ---- Host API Call Table ---------------------------------------------------------------

		LinkedList g_HostAPICallTable;					// The host API call table

// ---- Function Prototypes -------------------------------------------------------------------

    // ---- Linked Lists ----------------------------------------------------------------------

        void InitLinkedList ( LinkedList * pList );
        void FreeLinkedList ( LinkedList * pList );
        int AddNode ( LinkedList * pList, void * pData );
        void StripComments ( char * pstrSourceLine );

    // ---- String Processing -----------------------------------------------------------------

        int IsCharWhitespace ( char cChar );
        int IsCharNumeric ( char cChar );
        int IsCharIdent ( char cChar );
        int IsCharDelimiter ( char cChar );
        void TrimWhitespace ( char * pstrString );
        int IsStringWhitespace ( char * pstrString );
        int IsStringIdent ( char * pstrString );
        int IsStringInteger ( char * pstrString );
        int IsStringFloat( char * pstrString );

    // ---- Misc ------------------------------------------------------------------------------

        void PrintLogo ();
        void PrintUsage ();

    // ---- Main ------------------------------------------------------------------------------

        void Init ();
        void ShutDown ();

        void LoadSourceFile ();
        void AssmblSourceFile ();
        void PrintAssmblStats ();
        void BuildXSE ();

        void Exit ();
        void ExitOnError ( char * pstrErrorMssg );
        void ExitOnCodeError ( char * pstrErrorMssg );
        void ExitOnCharExpectedError ( char cChar );

    // ---- Lexical Analysis ------------------------------------------------------------------

        void ResetLexer ();
        Token GetNextToken ();
        char * GetCurrLexeme ();
        char GetLookAheadChar ();
        int SkipToNextLine ();

    // ---- Instructions ----------------------------------------------------------------------

        int GetInstrByMnemonic ( char * pstrMnemonic, InstrLookup * pInstr );
        void InitInstrTable ();
        int AddInstrLookup ( char * pstrMnemonic, int iOpcode, int iOpCount );
        void SetOpType ( int iInstrIndex, int iOpIndex, OpTypes iOpType );

    // ---- Tables ----------------------------------------------------------------------------

        int AddString ( LinkedList * pList, char * pstrString );

        int AddFunc ( char * pstrName, int iEntryPoint );
        FuncNode * GetFuncByName ( char * pstrName );
        void SetFuncInfo ( char * pstrName, int iParamCount, int iLocalDataSize );

        int AddLabel ( char * pstrIdent, int iTargetIndex, int iFuncIndex );
        LabelNode * GetLabelByIdent ( char * pstrIdent, int iFuncIndex );

        int AddSymbol ( char * pstrIdent, int iSize, int iStackIndex, int iFuncIndex );
        SymbolNode * GetSymbolByIdent ( char * pstrIdent, int iFuncIndex );
        int GetStackIndexByIdent ( char * pstrIdent, int iFuncIndex );
        int GetSizeByIdent ( char * pstrIdent, int iFuncIndex );

// ---- Functions -----------------------------------------------------------------------------

    /******************************************************************************************
    *
    *   InitLinkedList ()
    *
    *   Initializes a linked list.
    */

    void InitLinkedList ( LinkedList * pList )
    {
        // Set both the head and tail pointers to null

        pList->pHead = NULL;
        pList->pTail = NULL;

        // Set the node count to zero, since the list is currently empty

        pList->iNodeCount = 0;
    }

    /******************************************************************************************
    *
    *   FreeLinkedList ()
    *
    *   Frees a linked list.
    */

    void FreeLinkedList ( LinkedList * pList )
    {
		// If the list is empty, exit

		if ( ! pList )
			return;

        // If the list is not empty, free each node

        if ( pList->iNodeCount )
        {
            // Create a pointer to hold each current node and the next node

            LinkedListNode * pCurrNode,
                           * pNextNode;

            // Set the current node to the head of the list

            pCurrNode = pList->pHead;

            // Traverse the list

            while ( TRUE )
            {
                // Save the pointer to the next node before freeing the current one

                pNextNode = pCurrNode->pNext;

                // Clear the current node's data

                if ( pCurrNode->pData )
					free ( pCurrNode->pData );

                // Clear the node itself

                if ( pCurrNode )
					free ( pCurrNode );

                // Move to the next node if it exists; otherwise, exit the loop

                if ( pNextNode )
					pCurrNode = pNextNode;
				else
					break;
            }
        }
    }

    /******************************************************************************************
    *
    *   AddNode ()
    *
    *   Adds a node to a linked list and returns its index.
    */

    int AddNode ( LinkedList * pList, void * pData )
    {
        // Create a new node

        LinkedListNode * pNewNode = ( LinkedListNode * ) malloc ( sizeof ( LinkedListNode ) );

        // Set the node's data to the specified pointer

        pNewNode->pData = pData;

        // Set the next pointer to NULL, since nothing will lie beyond it

        pNewNode->pNext = NULL;

        // If the list is currently empty, set both the head and tail pointers to the new node

        if ( ! pList->iNodeCount )
        {
            // Point the head and tail of the list at the node

            pList->pHead = pNewNode;
            pList->pTail = pNewNode;
        }

        // Otherwise append it to the list and update the tail pointer

        else
        {
            // Alter the tail's next pointer to point to the new node

            pList->pTail->pNext = pNewNode;

            // Update the list's tail pointer

            pList->pTail = pNewNode;
        }

        // Increment the node count

        ++ pList->iNodeCount;

        // Return the new size of the linked list - 1, which is the node's index

        return pList->iNodeCount - 1;
    }

	/******************************************************************************************
	*
	*	StripComments ()
	*
	*	Strips the comments from a given line of source code, ignoring comment symbols found
	*	inside strings. The new string is shortended to the index of the comment symbol
	*	character.
	*/

	void StripComments ( char * pstrSourceLine )
	{
		unsigned int iCurrCharIndex;
		int iInString;

		// Scan through the source line and terminate the string at the first semicolon

		iInString = 0;
		for ( iCurrCharIndex = 0; iCurrCharIndex < strlen ( pstrSourceLine ) - 1; ++ iCurrCharIndex )
		{
			// Look out for strings; they can contain semicolons

			if ( pstrSourceLine [ iCurrCharIndex ] == '"' )
				if ( iInString )
					iInString = 0;
				else
					iInString = 1;

			// If a non-string semicolon is found, terminate the string at it's position

			if ( pstrSourceLine [ iCurrCharIndex ] == ';' )
			{
				if ( ! iInString )
				{
					pstrSourceLine [ iCurrCharIndex ] = '\n';
					pstrSourceLine [ iCurrCharIndex + 1 ] = '\0';
					break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -