コード例 #1
0
ファイル: AsmParser.cs プロジェクト: MooseTheGoose/Zasm
        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);
        }
コード例 #2
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;
            }
        }