Exemplo n.º 1
0
        public bool FormAdrTree(Token[] adrtokens, int startindex)
        {
            kind = DTKind.ADR;

            Token curtoken;
            DTree currchild, tempchild;

            if (adrtokens.Length <= 0)
            {
                Console.Error.WriteLine("ERROR in AdrTree()");
                Console.Error.WriteLine("Tokens array is empty");
                Environment.Exit(-1);
            }

            curtoken = adrtokens[0];

            if (curtoken.id == TokenKind.IDENTIFIER)
            {
                string iden = ((IdentifierToken)curtoken).identifier.ToUpper();

                if (NormPairTree.IsNormPair(iden))
                {
                    this.children = new NormPairTree(iden);
                }
                else if (IPairTree.IsIPair(iden))
                {
                    this.children = new IPairTree(iden);
                }
                else if (RegTree.IsReg(iden))
                {
                    this.children = new RegTree(iden);
                }
                else
                {
                    return(SyntaxErr());
                }

                currchild = this.children;

                if (adrtokens.Length == 3 && currchild.kind == DTKind.IPAIR)
                {
                    curtoken = adrtokens[1];
                    Token numbertoken = adrtokens[2];

                    if (curtoken.id == TokenKind.OPERATOR &&
                        numbertoken.id == TokenKind.NUMBER)
                    {
                        OperatorKind op     = ((OperatorToken)curtoken).op;
                        int          number = ((NumberToken)numbertoken).number;

                        if (op != OperatorKind.PLUS && op != OperatorKind.MINUS)
                        {
                            return(SyntaxErr());
                        }

                        tempchild      = new OperatorTree(op);
                        currchild.next = tempchild;
                        currchild      = tempchild;
                        tempchild      = new ImmTree(number);
                        currchild.next = tempchild;
                        currchild      = tempchild;
                    }
                }
                else if (adrtokens.Length != 1)
                {
                    return(SyntaxErr());
                }

                return(true);
            }
            else if (curtoken.id == TokenKind.NUMBER)
            {
                this.children = new ImmTree(((NumberToken)curtoken).number);

                if (adrtokens.Length != 1)
                {
                    return(SyntaxErr());
                }
            }
            else
            {
                return(SyntaxErr());
            }

            return(true);
        }
Exemplo n.º 2
0
        /*
         * Method assumes preprocessor came
         * in and dealt with labels and directives.
         */
        public bool Derive(Token[] tokens)
        {
            DTree  curchild = null, tempchild = null;
            Token  curtoken;
            string nmemonic = null;
            string iden;
            int    i = 0;

            next     = null;
            children = null;

            if (tokens.Length == 0)
            {
                return(true);
            }

            curtoken = tokens[0];
            if (curtoken.id != TokenKind.IDENTIFIER)
            {
                return(SyntaxErr());
            }

            iden = ((IdentifierToken)curtoken).identifier.ToUpper();
            if (NmemonicTree.IsNmemonic(iden))
            {
                children = new NmemonicTree(iden);
                curchild = children;
                i        = 1;
                nmemonic = iden;
            }
            else
            {
                return(SyntaxErr());
            }


            while (i < tokens.Length)
            {
                curtoken = tokens[i];

                switch (curtoken.id)
                {
                case TokenKind.IDENTIFIER:
                    iden = ((IdentifierToken)curtoken).identifier.ToUpper();

                    if (RegTree.IsReg(iden))
                    {
                        /*
                         * C is a register and also a condition.
                         * To solve this ambiguity, if the nmemonic requires
                         * a condition, it doesn't require a register, so create
                         * a new register tree iff the nmemonic is not conditional.
                         */
                        if (NmemonicTree.IsConditionalNmemonic(nmemonic) &&
                            iden.Equals("C"))
                        {
                            tempchild = new ConditionTree("C");
                        }
                        else
                        {
                            tempchild = new RegTree(iden);
                        }
                    }

                    else if (NormPairTree.IsNormPair(iden))
                    {
                        tempchild = new NormPairTree(iden);
                    }

                    else if (SpecPairTree.IsSpecPair(iden))
                    {
                        tempchild = new SpecPairTree(iden);
                    }

                    else if (ConditionTree.IsCondition(iden))
                    {
                        tempchild = new ConditionTree(iden);
                    }

                    else if (SpecRegTree.IsSpecReg(iden))
                    {
                        tempchild = new SpecRegTree(iden);
                    }

                    else if (IRegTree.IsIReg(iden))
                    {
                        tempchild = new IRegTree(iden);
                    }

                    else if (IPairTree.IsIPair(iden))
                    {
                        tempchild = new IPairTree(iden);
                    }

                    else
                    {
                        return(SyntaxErr());
                    }

                    curchild.next = tempchild;
                    curchild      = tempchild;

                    i++;
                    break;

                case TokenKind.NUMBER:
                    tempchild     = new ImmTree(((NumberToken)curtoken).number);
                    curchild.next = tempchild;
                    curchild      = tempchild;
                    i++;
                    break;

                case TokenKind.OPERATOR:
                    OperatorKind op = ((OperatorToken)curtoken).op;
                    switch (op)
                    {
                    case OperatorKind.LPAREN:
                        int     start = i;
                        Token[] adrtokens;
                        i++;
                        while (i < tokens.Length)
                        {
                            curtoken = tokens[i];
                            if (curtoken.id == TokenKind.OPERATOR &&
                                ((OperatorToken)curtoken).op
                                == OperatorKind.RPAREN)
                            {
                                break;
                            }
                            i++;
                        }
                        if (i >= tokens.Length)
                        {
                            return(SyntaxErr());
                        }
                        adrtokens = new Token[i - start - 1];
                        Array.Copy(tokens, start + 1,
                                   adrtokens, 0, adrtokens.Length);
                        tempchild = new AdrTree();
                        bool success = ((AdrTree)tempchild).
                                       FormAdrTree(adrtokens, start + 1);
                        if (!success)
                        {
                            return(false);
                        }
                        curchild.next = tempchild;
                        curchild      = tempchild;
                        i++;
                        break;

                    case OperatorKind.PLUS:
                        if (i >= tokens.Length - 1)
                        {
                            return(SyntaxErr());
                        }
                        Token numtokenadd = tokens[i + 1];
                        if (numtokenadd.id != TokenKind.NUMBER)
                        {
                            return(SyntaxErr());
                        }
                        tempchild     = new ImmTree(((NumberToken)numtokenadd).number);
                        curchild.next = tempchild;
                        curchild      = tempchild;
                        i            += 2;
                        break;

                    case OperatorKind.MINUS:
                        if (i >= tokens.Length - 1)
                        {
                            return(SyntaxErr());
                        }
                        Token numtokensub = tokens[i + 1];
                        if (numtokensub.id != TokenKind.NUMBER)
                        {
                            return(SyntaxErr());
                        }
                        tempchild     = new ImmTree(-((NumberToken)numtokensub).number);
                        curchild.next = tempchild;
                        curchild      = tempchild;
                        i            += 2;
                        break;

                    default:
                        return(SyntaxErr());
                    }
                    break;

                default:
                    break;
                }

                if (i < tokens.Length)
                {
                    curtoken = tokens[i];
                    if (curtoken.id != TokenKind.OPERATOR ||
                        ((OperatorToken)curtoken).op != OperatorKind.COMMA)
                    {
                        return(SyntaxErr());
                    }
                    i++;
                    if (i >= tokens.Length)
                    {
                        return(SyntaxErr());
                    }
                }
            }

            return(true);
        }
Exemplo n.º 3
0
        public static void DebugTree(DTree t, int indent)
        {
            StringBuilder sb = new StringBuilder(indent);
            DTree         child;

            for (int i = 0; i < indent; i++)
            {
                sb.Append(' ');
            }

            switch (t.kind)
            {
            case DTKind.NMEMONIC:
                NmemonicTree nm = (NmemonicTree)t;
                Console.Write(sb);
                Console.WriteLine("(NMEMONIC) { " + nm.nmemonic + " }");
                break;

            case DTKind.OPERATOR:
                OperatorTree op = (OperatorTree)t;
                Console.Write(sb);
                Console.WriteLine("(OPERATOR) { " + op.op + " }");
                break;

            case DTKind.REG:
                RegTree reg = (RegTree)t;
                Console.Write(sb);
                Console.WriteLine("(REGISTER) { " + reg.reg + " }");
                break;

            case DTKind.NORMPAIR:
                NormPairTree npair = (NormPairTree)t;
                Console.Write(sb);
                Console.WriteLine("(NORMPAIR) { " + npair.normpair + " }");
                break;

            case DTKind.SPECPAIR:
                SpecPairTree spair = (SpecPairTree)t;
                Console.Write(sb);
                Console.WriteLine("(SPECPAIR) { " + spair.specpair + " }");
                break;

            case DTKind.IPAIR:
                IPairTree ipair = (IPairTree)t;
                Console.Write(sb);
                Console.WriteLine("(IPAIR) { " + ipair.ipair + " }");
                break;

            case DTKind.IMM:
                ImmTree imm = (ImmTree)t;
                Console.Write(sb);
                Console.WriteLine("(IMM) { " + imm.immediate + " }");
                break;

            case DTKind.ADR:
                AdrTree adr = (AdrTree)t;
                child = adr.children;
                Console.Write(sb);
                Console.WriteLine("(ADR) {");
                while (child != null)
                {
                    DebugTree(child, indent + 4);
                    child = child.next;
                }
                Console.Write(sb);
                Console.WriteLine("}");
                break;

            case DTKind.INSTRUCTION:
                InstructionTree inst = (InstructionTree)t;
                child = inst.children;
                Console.Write(sb);
                Console.WriteLine("(INSTRUCTION) {");
                while (child != null)
                {
                    DebugTree(child, indent + 4);
                    child = child.next;
                }
                Console.Write(sb);
                Console.WriteLine("}");
                break;

            case DTKind.CONDITION:
                ConditionTree cond = (ConditionTree)t;
                Console.Write(sb);
                Console.WriteLine("(CONDITION) { " + cond.cond + " }");
                break;

            case DTKind.SPECREG:
                SpecRegTree specreg = (SpecRegTree)t;
                Console.Write(sb);
                Console.WriteLine("(SPECREG) { " + specreg.reg + " }");
                break;

            default:
                break;
            }
        }