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

📄 xsc.cpp

📁 < Game Script Mastery>> source code
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*

    Project.

        XSC - The XtremeScript Compiler Version 0.8

    Abstract.

        Compiles XtremeScript source files (XSS) to XtremeScript assembly files (XASM) that
        can be assembled into XVM executables by the XASM assembler.

    Date Created.

        8.28.2002

    Author.

        Alex Varanese

*/

// ---- Include Files -------------------------------------------------------------------------

    #include "xsc.h"

    #include "error.h"
    #include "func_table.h"
    #include "symbol_table.h"

    #include "preprocessor.h"
    #include "lexer.h"
    #include "parser.h"
    #include "i_code.h"
    #include "code_emit.h"

// ---- Globals -------------------------------------------------------------------------------

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

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

        LinkedList g_SourceCode;                        // Source code linked list

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

        ScriptHeader g_ScriptHeader;                    // Script header data
        
    // ---- I-Code Stream ---------------------------------------------------------------------

        LinkedList g_ICodeStream;                       // I-code stream

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

        LinkedList g_FuncTable;                         // The function table

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

        LinkedList g_SymbolTable;                       // The symbol table

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

		LinkedList g_StringTable;						// The string table

    // ---- XASM Invocation -------------------------------------------------------------------

        int g_iPreserveOutputFile;                      // Preserve the assembly file?
        int g_iGenerateXSE;                             // Generate an .XSE executable?

    // ---- Expression Evaluation -------------------------------------------------------------

        int g_iTempVar0SymbolIndex,                     // Temporary variable symbol indices
            g_iTempVar1SymbolIndex;

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

    /******************************************************************************************
    *
    *   PrintLogo ()
    *
    *   Prints out logo/credits information.
    */

    void PrintLogo ()
    {
        printf ( "XSC\n" );
        printf ( "XtremeScript Compiler 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:\tXSC Source.XSS [Output.XASM] [Options]\n" );
        printf ( "\n" );
        printf ( "\t-S:Size      Sets the stack size (must be decimal integer value)\n" );
        printf ( "\t-P:Priority  Sets the thread priority: Low, Med, High or timeslice\n" );
        printf ( "\t             duration (must be decimal integer value)\n" );
        printf ( "\t-A           Preserve assembly output file\n" );
        printf ( "\t-N           Don't generate .XSE (preserves assembly output file)\n" );
        printf ( "\n" );
        printf ( "Notes:\n" );
        printf ( "\t- File extensions are not required.\n" );
        printf ( "\t- Executable name is optional; source name is used by default.\n" );
        printf ( "\n" );
    }

    /******************************************************************************************
    *
    *   VerifyFilenames ()
    *
    *   Verifies the input and output filenames.
    */

    void VerifyFilenames ( int argc, char * argv [] )
    {
        // First make a global copy of the source filename and convert it to uppercase

        strcpy ( g_pstrSourceFilename, argv [ 1 ] );
        strupr ( g_pstrSourceFilename );

        // Check for the presence of the .XASM extension and add it if it's not there

	    if ( ! strstr ( g_pstrSourceFilename, SOURCE_FILE_EXT ) )
        {
			// The extension was not found, so add it to string

			strcat ( g_pstrSourceFilename, SOURCE_FILE_EXT );
        }

        // Was an executable filename specified?

        if ( argv [ 2 ] && argv [ 2 ][ 0 ] != '-' )
        {
            // Yes, so repeat the validation process

            strcpy ( g_pstrOutputFilename, argv [ 2 ] );
            strupr ( g_pstrOutputFilename );

            // Check for the presence of the .XSE extension and add it if it's not there

	        if ( ! strstr ( g_pstrOutputFilename, OUTPUT_FILE_EXT ) )
            {
			    // The extension was not found, so add it to string

			    strcat ( g_pstrOutputFilename, OUTPUT_FILE_EXT );
            }
        }
        else
        {
            // No, so base it on the source filename

            // First locate the start of the extension, and use pointer subtraction to find the index

            int ExtOffset = strrchr ( g_pstrSourceFilename, '.' ) - g_pstrSourceFilename;
            strncpy ( g_pstrOutputFilename, g_pstrSourceFilename, ExtOffset );

            // Append null terminator

            g_pstrOutputFilename [ ExtOffset ] = '\0';

            // Append executable extension

		    strcat ( g_pstrOutputFilename, OUTPUT_FILE_EXT );
        }
    }

    /******************************************************************************************
    *
    *   ReadCmmndLineParams ()
    *
    *   Reads and verifies the command line parameters.
    */

    void ReadCmmndLineParams ( int argc, char * argv [] )
    {
        char pstrCurrOption [ 32 ];
        char pstrCurrValue [ 32 ];
        char pstrErrorMssg [ 256 ];

        for ( int iCurrOptionIndex = 0; iCurrOptionIndex < argc; ++ iCurrOptionIndex )
        {
            // Convert the argument to uppercase to keep things neat and tidy

            strupr ( argv [ iCurrOptionIndex ] );

            // Is this command line argument an option?

            if ( argv [ iCurrOptionIndex ][ 0 ] == '-' )
            {
                // Parse the option and value from the string

                int iCurrCharIndex;
                int iOptionSize;
                char cCurrChar;

                // Read the option up till the colon or the end of the string

                iCurrCharIndex = 1;
                while ( TRUE )
                {
                    cCurrChar = argv [ iCurrOptionIndex ][ iCurrCharIndex ];
                    if ( cCurrChar == ':' || cCurrChar == '\0' )
                        break;
                    else
                        pstrCurrOption [ iCurrCharIndex - 1 ] = cCurrChar;
                    ++ iCurrCharIndex;
                }
                pstrCurrOption [ iCurrCharIndex - 1 ] = '\0';

                // Read the value till the end of the string, if it has one

                if ( strstr ( argv [ iCurrOptionIndex ], ":" ) )
                {
                    ++ iCurrCharIndex;
                    iOptionSize = iCurrCharIndex;

                    pstrCurrValue [ 0 ] = '\0';
                    while ( TRUE )
                    {
                        if ( iCurrCharIndex > ( int ) strlen ( argv [ iCurrOptionIndex ] ) )
                            break;
                        else
                        {
                            cCurrChar = argv [ iCurrOptionIndex ][ iCurrCharIndex ];
                            pstrCurrValue [ iCurrCharIndex - iOptionSize ] = cCurrChar;
                        }
                        ++ iCurrCharIndex;
                    }
                    pstrCurrValue [ iCurrCharIndex - iOptionSize ] = '\0';

                    // Make sure the value is valid

                    if ( ! strlen ( pstrCurrValue ) )
                    {
                        sprintf ( pstrErrorMssg, "Invalid value for -%s option", pstrCurrOption );
                        ExitOnError ( pstrErrorMssg );
                    }
                }

                // ---- Perform the option's action

                // Set the stack size

                if ( stricmp ( pstrCurrOption, "S" ) == 0 )
                {
                    // Convert the value to an integer stack size

                    g_ScriptHeader.iStackSize = atoi ( pstrCurrValue );
                }

                // Set the priority

                else if ( stricmp ( pstrCurrOption, "P" ) == 0 )
                {
                    // ---- Determine what type of priority was specified

                    // Low rank

                    if ( stricmp ( pstrCurrValue, PRIORITY_LOW_KEYWORD ) == 0 )
                    {
                        g_ScriptHeader.iPriorityType = PRIORITY_LOW;
                    }

                    // Medium rank

                    else if ( stricmp ( pstrCurrValue, PRIORITY_MED_KEYWORD ) == 0 )
                    {
                        g_ScriptHeader.iPriorityType = PRIORITY_MED;
                    }

                    // High rank

                    else if ( stricmp ( pstrCurrValue, PRIORITY_HIGH_KEYWORD ) == 0 )
                    {
                        g_ScriptHeader.iPriorityType = PRIORITY_HIGH;
                    }

                    // User-defined timeslice

                    else
                    {
                        g_ScriptHeader.iPriorityType = PRIORITY_USER;
                        g_ScriptHeader.iUserPriority = atoi ( pstrCurrValue );
                    }
                }

                // Preserve the assembly file

                else if ( stricmp ( pstrCurrOption, "A" ) == 0 )
                {
                    g_iPreserveOutputFile = TRUE;
                }

                // Don't generate an .XSE executable

                else if ( stricmp ( pstrCurrOption, "N" ) == 0 )
                {
                    g_iGenerateXSE = FALSE;
                    g_iPreserveOutputFile = TRUE;
                }
                
                // Anything else is invalid

                else
                {
                    sprintf ( pstrErrorMssg, "Unrecognized option: \"%s\"", pstrCurrOption );
                    ExitOnError ( pstrErrorMssg );
                }
            }
        }
    }

	/******************************************************************************************
	*
	*	Init ()
	*
	*	Initializes the compiler.
	*/

	void Init ()
	{
        // ---- Initialize the script header

        g_ScriptHeader.iIsMainFuncPresent = FALSE;
        g_ScriptHeader.iStackSize = 0;
        g_ScriptHeader.iPriorityType = PRIORITY_NONE;

        // ---- Initialize the main settings

        // Mark the assembly file for deletion

        g_iPreserveOutputFile = FALSE;

        // Generate an .XSE executable

⌨️ 快捷键说明

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