Esempio n. 1
0
        //Output symbol table
        private void OutputSymbolTable()
        {
            //Inits
            DynamicBranchTreeNode <HashSet <SymbolTableEntry> > curNode = compiler.Parser.SymbolTableRootNode;

            //Begin tree control update
            treeViewSymbolTable.BeginUpdate();

            //Clear items
            treeViewSymbolTable.Nodes.Clear();

            //Reset counters
            ResetSymbolTableCounters();

            //Output symbol table to tree controll
            treeViewSymbolTable.Nodes.Add(String.Format("Block {0}", block));
            OutputSymbolTableEntry(curNode, treeViewSymbolTable.Nodes[0]);

            //End tree control update
            treeViewSymbolTable.EndUpdate();

            //Update stats (counted in recursive function to save a pass through nodes )
            labelTotalVarsValue.Text    = symtblVars.ToString();
            labelTotalIntsValue.Text    = symtblInts.ToString();
            labelTotalSringsValue.Text  = symtblStrings.ToString();
            labelTotalBooleanValue.Text = symtblBools.ToString();
            labelMaxMemoryValue.Text    = symtblMemory.ToString();
        }
Esempio n. 2
0
        //Recursive output tree view child nodes
        private void OutputCSTNode(DynamicBranchTreeNode <CSTValue> CSTNode, TreeNode treeNode)
        {
            //Inits
            int children = 0;
            DynamicBranchTreeNode <CSTValue> childNode = null;
            TreeNode parentTreeNode = null;

            //Get number of children

            children = CSTNode.NodeCount;



            //Cycle through children and add nodes
            for (int i = 0; i < children; i++)
            {
                //Get child node
                childNode = CSTNode.GetChild(i);

                //Add node
                parentTreeNode = treeNode.Nodes.Add(childNode.Data.ToString());

                //Add nodes children
                OutputCSTNode(childNode, parentTreeNode);
            }
        }
Esempio n. 3
0
        private void OutputSymbolTableEntry(DynamicBranchTreeNode <HashSet <SymbolTableEntry> > entry, TreeNode treeNode)
        {
            //Inits
            HashSet <SymbolTableEntry> hashSet = entry.Data;
            TreeNode newNode = null;

            //Output hash set
            foreach (SymbolTableEntry symEntry in hashSet)
            {
                //Add each entry to node
                treeNode.Nodes.Add(symEntry.ToString());

                //Collect data

                //Inc var count
                symtblVars++;

                //Check for int type
                if (symEntry.DataType == DataType.DT_INT)
                {
                    //Inc int type count
                    symtblInts++;

                    //Inc memory 1 byte
                    symtblMemory++;
                }
                //Check for string type
                else if (symEntry.DataType == DataType.DT_STRING)
                {
                    //Inc total strings type counter
                    symtblStrings++;

                    //Incrmeent memory 1 byte for each char
                    symtblMemory += (symEntry.StringValue.Length);
                }
                //Check for boolean type
                else if (symEntry.DataType == DataType.DT_BOOLEAN)
                {
                    //Inc bool type counter
                    symtblBools++;

                    //Increment max memory 1 byte
                    symtblMemory++;
                }
            }

            //Cycle through children nodes, and call recursive function
            for (int i = 0; i < entry.NodeCount; i++)
            {
                //Increment block
                block++;

                //Create a new block node title
                newNode = treeNode.Nodes.Add(String.Format("Block {0}", block));

                //Call recursive function on child
                OutputSymbolTableEntry(entry.GetChild(i), newNode);
            }
        }
Esempio n. 4
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);
 }
Esempio n. 5
0
        //Output CST to treeViewCST
        private void OutputCST()
        {
            //Inits
            DynamicBranchTreeNode <CSTValue> curNode = null;

            //Get root node
            curNode = compiler.Parser.CSTRootNode;

            //Begin tree view update
            treeViewCST.BeginUpdate();

            //Reset tree control
            treeViewCST.Nodes.Clear();

            //Start recursive output cst node
            treeViewCST.Nodes.Add(curNode.Data.ToString());
            OutputCSTNode(curNode, treeViewCST.Nodes[0]);

            //Expand all
            treeViewCST.ExpandAll();

            //End update(suppress paint)
            treeViewCST.EndUpdate();
        }
Esempio n. 6
0
        // Gens op codes for all statments in block.
        // Manges which symbol table is current.
        // Keeps table of start and end of blocks.
        // All vars allocated on stack in this block
        // are set to not in use.
        //
        // Returns: Number of bytes generated.
        public override int GenOpCodes(OpCodeGenParam param)
        {
            // Inits
            int                 nextSymbIndex = 0;
            int                 bytesAdded    = 0;
            int                 block         = 0;
            ASTNode             curNode       = null;
            BlockSizeTableEntry entry         = null;
            VarTableEntry       varEntry      = null;
            SymbolHashTable     curSymbTable  = null;

            SymbolTableEntry[] symbEntries = null;
            DynamicBranchTreeNode <SymbolHashTable> startSymbolTable = null;

            // Get start symbol table
            startSymbolTable = param.curSymbTable;

            // Check if not root symbol table
            if (!param.firstBlock)
            {
                // Move into next symbol table child
                param.curSymbTable = param.curSymbTable.GetChild(param.curSymbTableIndex);


                // Record last symb index
                nextSymbIndex = param.curSymbTableIndex + 1;

                // Set current symbol table index to 0
                param.curSymbTableIndex = 0;
            }
            // Else root table
            else
            {
                // Set first block flag false
                param.firstBlock = false;
            }

            // Record current symbol table
            curSymbTable = param.curSymbTable.Data;

            // Add new entry to block size table
            entry = new BlockSizeTableEntry(param.curBlockID, param.curByte);
            param.tables.AddBlockSizeTableEntry(entry);
            param.curBlockID++;
            block = param.curBlockID - 1;

            // Cycle through stmts calling their gen op code
            curNode = this.leftMostChild;
            while (curNode != null)
            {
                // Try for over 256 bytes
                try
                {
                    // Call child statment gen op code method
                    bytesAdded += curNode.GenOpCodes(param);
                }

                // Catch over 256 bytes
                catch (IndexOutOfRangeException ex)
                {
                    throw ex;
                }

                // Get next symbling
                curNode = curNode.RightSibling;
            }

            // Reset cur symbol table index
            param.curSymbTableIndex = nextSymbIndex;

            // Reset cur symbol table
            param.curSymbTable = startSymbolTable;

            // Set end byte of block
            entry.EndByte = param.curByte - 1;

            // Remove out of scope vars from init var table
            param.tables.RemoveBlockVarEntries(block);

            // Cycle through symbol table
            symbEntries = curSymbTable.GetAllItems();
            for (int i = 0; i < symbEntries.Length; i++)
            {
                // Get item from temp var table
                varEntry = param.tables.GetVarTableEntry(symbEntries[i].EntryID);

                // Check if exists
                if (varEntry != null)
                {
                    // Set not in use flag
                    varEntry.InUse = false;

                    // Decrement var in use count
                    param.tables.DecVarInUseCount();
                }
            }

            // Return number of bytes
            return(bytesAdded);
        }
        // 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);
        }