Exemple #1
0
        public bool ExpectRegister(out Registers register)
        {
            register = Registers.RegA;
            if (this.IsAtEnd())
            {
                return(false);
            }
            else if (!this.ExpectLetter())
            {
                return(false);
            }

            string word = this.ScanWord();

            if (!RegisterToken.IsKeywordValid(word))
            {
                return(false);
            }

            register = RegisterToken.GetTypeFromKeyword(word);
            return(true);
        }
Exemple #2
0
        private void Tokenize()
        {
            this.tokenStartLine = this.line;
            this.tokenStartCol  = this.col - 1;

            while (!this.IsAtEnd())
            {
                char c = this.ReadNext();

                tokenStartLine = this.line;
                tokenStartCol  = this.col - 1; // because `current` points to the next char

                if (this.IsLetter(c))
                {
                    Token tok;

                    string word = this.ScanWord(c);

                    if (InstructionToken.IsKeywordValid(word))
                    {
                        Instructions instr = InstructionToken.GetTypeFromKeyword(word);
                        tok = new InstructionToken(this.tokenStartLine, this.tokenStartCol, instr);
                    }
                    else if (RegisterToken.IsKeywordValid(word))
                    {
                        Registers reg = RegisterToken.GetTypeFromKeyword(word);
                        tok = new RegisterToken(this.tokenStartLine, this.tokenStartCol, reg);
                    }
                    else
                    {
                        this.LogUnknownKeywordError(word);
                        continue;
                    }

                    this.tokens.Add(tok);
                }
                else if (c == '#')
                {
                    byte nb;
                    if (this.ExpectNumber(out nb))
                    {
                        var tok = new NumericalValueToken(this.tokenStartLine, this.tokenStartCol, nb);
                        this.tokens.Add(tok);
                    }
                    else
                    {
                        this.LogExpectedNumberError();
                    }
                }
                else if (c == '&')
                {
                    Registers reg;
                    byte      nb;
                    if (this.ExpectRegister(out reg))
                    {
                        var tok = new AddressRegisterToken(this.tokenStartLine, this.tokenStartCol, reg);
                        this.tokens.Add(tok);
                    }
                    else if (this.ExpectNumber(out nb))
                    {
                        var tok = new AddressValueToken(this.tokenStartLine, this.tokenStartCol, nb);
                        this.tokens.Add(tok);
                    }
                    else
                    {
                        this.LogUnexpectedTokenForAddressError();
                    }
                }
                else if (c != '\0')
                {
                    this.LogUnexpectedCharError(c);
                    //continue;
                }
            }

            if (this.errorsCount != 0)
            {
                Console.WriteLine(this.errorsCount + " errors found.");
                Console.WriteLine("---");

                throw new SyntaxErrorsException();
            }
        }