Пример #1
0
        // Add a new record to the table

        public void AddRecord(TableRecord in_record)
        {
            lookUp.Add(in_record.Lexeme(), size);
            in_record.SetOffset(size);
            size = in_record.Size() + size;
            records.Add(in_record);
            in_record.printRecord();
        }
Пример #2
0
        // Build code to push an identifier on the stack

        public void GenPushID(string lex)
        {
            SymbolTable curr          = tables;
            int         tableActual   = tableNum;
            int         cIndex        = -1;
            bool        keepSearching = true;

            // Search the symbol tables for the id
            while (keepSearching)
            {
                cIndex = curr.GetOffset(lex);
                if (cIndex != -1)
                {
                    keepSearching = false;
                }
                else if (cIndex == -1 && curr.GetParent() != null)
                {
                    curr        = curr.GetParent();
                    tableActual = tableActual - 1;
                }
                else
                {
                    keepSearching = false;
                    semanticError = true;
                }
            }

            // Couldn't find it
            if (cIndex == -1)
            {
                ErrorMessage("Identifier " + parse.currentLexeme +
                             "doesn't exist in this scope.");
                semanticError = true;
            }

            // Did find it, push it
            else
            {
                TableRecord cRec  = curr.GetRecord(cIndex);
                string      cType = cRec.Type();
                operandType.Push(cType);

                prog.Append("PUSH ");
                prog.Append(cIndex);
                prog.Append("(D");
                prog.Append(tableActual);
                prog.Append(")\n");


                if (!negFlag)
                {
                    //do nothing
                }
                else if (negFlag && cType == "int")
                {
                    prog.Append("NEGS\n");
                    negFlag = false;
                }
                else if (negFlag && cType == "float")
                {
                    prog.Append("NEGSF\n");
                    negFlag = false;
                }
                else
                {
                    semanticError = true;
                    ErrorMessage("Tried to negate an invalid type.");
                    negFlag = false;
                }
            }
        }