// Default constructor. Inits values.
 public OpCodeGenerator()
 {
     warningCount        = 0;
     errorCount          = 0;
     opCodeDataBytes     = null;
     outputOpCodesToFile = false;
     tempOpCodeData      = null;
     tempTables          = null;
     opCodeBytes         = 0;
 }
Esempio n. 2
0
 // Default constructor. Must be created with necessary components.
 public OpCodeGenParam(StringBuilder opCodes, byte[] opCodeDataBytes, DynamicBranchTreeNode <SymbolHashTable> curSymbTable
                       , OpCodeGenTempTables tables)
 {
     this.opCodes         = opCodes;
     this.opCodeDataBytes = opCodeDataBytes;
     this.tables          = tables;
     this.curSymbTable    = curSymbTable;
     this.initVars        = 0;
     curByte           = 0;
     curSymbTableIndex = 0;
     curBlockID        = 0;
     curHeapID         = 50;
     firstBlock        = true;
     insertBytes       = new ArrayList(20);
 }
        // Generates op codes from ast.
        public ProcessReturnValue GenerateOpCodes(ASTNode rootASTNode, DynamicBranchTreeNode <SymbolHashTable> rootSymbolTableNode)
        {
            // Inits
            ProcessReturnValue ret = ProcessReturnValue.PRV_NONE;
            OpCodeGenParam     param;
            bool error = false;

            // Create temp tables
            tempTables     = new OpCodeGenTempTables();
            tempOpCodeData = new StringBuilder(250);

            // Create new byte array
            opCodeDataBytes = new byte[256];

            // Reset error and warning count
            errorCount   = 0;
            warningCount = 0;

            // Create op code gen param
            param = new OpCodeGenParam(tempOpCodeData, opCodeDataBytes, rootSymbolTableNode, tempTables);

            // Send message
            SendGeneralMessage("Starting code generation phase...");

            // Send message
            SendGeneralMessage("Generating op codes...");

            // Gen op codes
            try
            {
                rootASTNode.GenOpCodes(param);


                // Add final 00
                param.AddBytes(0x00);
            }


            // Catch index out of range exption
            // on over 256 byte error
            catch (IndexOutOfRangeException ex)
            {
                SendError("Program ran over 256 bytes.");

                // Send message
                SendGeneralMessage("Op code generation complete.");

                error = true;
            }

            // Send message
            SendGeneralMessage("Op code generation complete.");

            // If not errors
            if (!error)
            {
                // Send message
                SendGeneralMessage("Inserting memory locations...");

                // Fill in memory locations and jump sizes
                programData = FillInTempOpCodeValues(param);

                // Send message
                SendGeneralMessage("Completed inserting memory locations.");

                // Send message
                SendGeneralMessage("Code generation phase complete.");

                // Send message
                SendGeneralMessage("Compilation complete.");

                // Set op code total bytes
                opCodeBytes = programData.totalBytes;
            }
            // Else send error message
            else
            {
                // Send message
                SendGeneralMessage("Compilation completed with errors.");
            }

            //Determine return value
            if (ErrorCount > 0)
            {
                ret = ProcessReturnValue.PRV_ERRORS;
            }
            else if (WarningCount > 0)
            {
                ret = ProcessReturnValue.PRV_WARNINGS;
            }
            else
            {
                ret = ProcessReturnValue.PRV_OK;
            }

            //Return code
            return(ret);
        }