Beispiel #1
0
        private void String()
        {
            while (Peek() != '"' && !IsAtEnd())
            {
                if (Peek() == '\n')
                {
                    line++;
                }
                Advance();
            }

            if (IsAtEnd())
            {
                DotLox.Error(line, "Unclosed string");
                return;
            }

            Advance(); // consume closing "

            var value = source.Substring(start + 1, current - start - 2);

            AddToken(TokenType.STRING, value);
        }
Beispiel #2
0
        private void ScanToken()
        {
            var ch = Advance();

            switch (ch)
            {
            case '(': AddToken(TokenType.LEFT_PAREN); break;

            case ')': AddToken(TokenType.RIGHT_PAREN); break;

            case '{': AddToken(TokenType.LEFT_BRACE); break;

            case '}': AddToken(TokenType.RIGHT_BRACE); break;

            case ',': AddToken(TokenType.COMMA); break;

            case '.': AddToken(TokenType.DOT); break;

            case '-': AddToken(TokenType.MINUS); break;

            case '+': AddToken(TokenType.PLUS); break;

            case ';': AddToken(TokenType.SEMICOLON); break;

            case '*': AddToken(TokenType.STAR); break;

            case '!': AddToken(Match('=') ? TokenType.BANG_EQUAL : TokenType.BANG); break;

            case '=': AddToken(Match('=') ? TokenType.EQUAL_EQUAL : TokenType.EQUAL); break;

            case '<': AddToken(Match('=') ? TokenType.LESS_EQUAL : TokenType.LESS); break;

            case '>': AddToken(Match('=') ? TokenType.GREATER_EQUAL : TokenType.GREATER); break;

            case '/':
                if (Match('/'))
                {
                    while (Peek() != '\n' && !IsAtEnd())
                    {
                        Advance();
                    }
                }
                else if (Match('*'))
                {
                    while (!IsAtEnd())
                    {
                        if (Peek() == '*' && PeekNext() == '/')
                        {
                            Advance();     // consume *
                            Advance();     // consume /
                            break;
                        }
                        if (Peek() == '\n')
                        {
                            line++;
                        }
                        Advance();
                    }
                }
                else
                {
                    AddToken(TokenType.SLASH);
                }
                break;

            case ' ':
            case '\t':
            case '\r':
                break;

            case '\n':
                line++;
                break;

            case '"': String(); break;

            default:
                if (IsDigit(ch))
                {
                    Number();
                }
                else if (IsAlpha(ch))
                {
                    Identifier();
                }
                else
                {
                    DotLox.Error(line, "Unexpected character.");
                }

                break;
            }
        }