Esempio n. 1
0
        private void ProcessLine()
        {
            if (lexer.PeekAndDiscard(TokenType.EOL))
            {
                return;
            }
            if (lexer.PeekAndDiscard(TokenType._Page))
            {
                return;
            }
            if (lexer.Peek().Type == TokenType.Id)
            {
                var id = (string)lexer.Expect(TokenType.Id);
                if (lexer.PeekAndDiscard(TokenType.Eq))
                {
                    var exp = ParseExpression();
                    Assembler.Equate(id, exp);
                    lexer.PeekAndDiscard(TokenType.EOL);
                    return;
                }
                else if (lexer.PeekAndDiscard(TokenType.Colon))
                {
                    Assembler.Label(id);
                }
                else
                {
                    lexer.Unexpected(lexer.Get());
                }
            }
            switch (lexer.Peek().Type)
            {
            case TokenType.EOL:
                lexer.Get();
                return;

            case TokenType._Word:
                lexer.Get();
                ProcessWords(PrimitiveType.Word16);
                break;

            case TokenType._Dot:
                lexer.Get();
                lexer.Expect(TokenType.Eq);
                lexer.Expect(TokenType._Dot);
                lexer.Expect(TokenType.Plus);
                var delta = (int)ParseExpression();
                emitter.Reserve(delta);
                break;

            case TokenType.ASR: ProcessSingleOperand(Assembler.Asr); break;

            case TokenType.CLR: ProcessSingleOperand(Assembler.Clr); break;

            case TokenType.CLRB: ProcessSingleOperand(Assembler.Clrb); break;

            case TokenType.BEQ: ProcessBranch(Assembler.Beq); break;

            case TokenType.DEC: ProcessSingleOperand(Assembler.Dec); break;

            case TokenType.INC: ProcessSingleOperand(Assembler.Inc); break;

            case TokenType.JSR: lexer.Get(); Assembler.Jsr(ParseOperands()); break;

            case TokenType.MOV: ProcessDoubleOperand(Assembler.Mov); break;

            case TokenType.MOVB: ProcessDoubleOperand(Assembler.Movb); break;

            case TokenType.RESET: lexer.Get(); Assembler.Reset(); break;

            case TokenType.SUB: ProcessDoubleOperand(Assembler.Sub); break;

            default:
                lexer.Unexpected(lexer.Get());
                break;
            }
        }
Esempio n. 2
0
        public ParsedOperand ParseOperand()
        {
            var tok = lexer.Get();

            switch (tok.Type)
            {
            case TokenType.Register:
                return(new ParsedOperand
                {
                    Type = AddressMode.Register,
                    Register = arch.GetRegister((string)tok.Value)
                });

            case TokenType.Hash:
                tok = lexer.Get();
                switch (tok.Type)
                {
                case TokenType.Id:
                    return(new ParsedOperand
                    {
                        Type = AddressMode.Immediate,
                        Symbol = asm.Symtab.CreateSymbol((string)tok.Value)
                    });

                default:
                    lexer.Unexpected(tok);
                    return(null);
                }

            case TokenType.At:
                var op = ParseOperand();
                switch (op.Type)
                {
                case AddressMode.Register: op.Type = AddressMode.RegDef; break;

                case AddressMode.RegDef:
                case AddressMode.AutoIncrDef:
                case AddressMode.AutoDecrDef:
                case AddressMode.IndexedDef:
                    throw new FormatException("Can't re-defer.");

                case AddressMode.AutoIncr: op.Type = AddressMode.AutoIncrDef; break;

                case AddressMode.AutoDecr: op.Type = AddressMode.AutoDecrDef; break;

                case AddressMode.Indexed: op.Type = AddressMode.IndexedDef; break;
                }
                return(op);

            case TokenType.Minus:
                lexer.Expect(TokenType.LParen);
                var regStr = (string)lexer.Expect(TokenType.Register);
                lexer.Expect(TokenType.RParen);
                return(new ParsedOperand
                {
                    Type = AddressMode.AutoDecr,
                    Register = arch.GetRegister(regStr),
                });

            case TokenType.LParen:
                regStr = (string)lexer.Expect(TokenType.Register);
                lexer.Expect(TokenType.RParen);
                if (lexer.PeekAndDiscard(TokenType.Plus))
                {
                    return(new ParsedOperand
                    {
                        Type = AddressMode.AutoIncr,
                        Register = arch.GetRegister(regStr)
                    });
                }
                else
                {
                    return(new ParsedOperand
                    {
                        Type = AddressMode.RegDef,
                        Register = arch.GetRegister(regStr)
                    });
                }

            default:
                return(new ParsedOperand
                {
                    Type = AddressMode.Absolute,
                    Symbol = asm.Symtab.CreateSymbol((string)tok.Value)
                });
            }
        }