Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Original Scanning Case Statement
...
private void scanToken() {
char c = advance();
switch (c) {
case '(': addToken(LEFT_PAREN); break;
case ')': addToken(RIGHT_PAREN); break;
case '{': addToken(LEFT_BRACE); break;
case '}': addToken(RIGHT_BRACE); break;
case ',': addToken(COMMA); break;
case '.': addToken(DOT); break;
case '-': addToken(MINUS); break;
case '+': addToken(PLUS); break;
case ';': addToken(SEMICOLON); break;
case '*': addToken(STAR); break; // [slash]
//> two-char-tokens
case '!': addToken(match('=') ? BANG_EQUAL : BANG); break;
case '=': addToken(match('=') ? EQUAL_EQUAL : EQUAL); break;
case '<': addToken(match('=') ? LESS_EQUAL : LESS); break;
case '>': addToken(match('=') ? GREATER_EQUAL : GREATER); break;
//< two-char-tokens
//> slash
case '/':
if (match('/')) {
// A comment goes until the end of the line.
while (peek() != '\n' && !isAtEnd()) advance();
} else {
addToken(SLASH);
}
break;
//< slash
//> whitespace
case ' ':
case '\r':
case '\t':
// Ignore whitespace.
break;
case '\n':
line++;
break;
//< whitespace
//> string-start
case '"': string(); break;
//< string-start
//> char-error
default:
/* Scanning char-error < Scanning digit-start
Lox.error(line, "Unexpected character.");
*/
//> digit-start
if (isDigit(c)) {
number();
//> identifier-start
} else if (isAlpha(c)) {
identifier();
//< identifier-start
} else {
Lox.error(line, "Unexpected character.");
}
//< digit-start
break;
//< char-error
}
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment