Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Created October 16, 2019 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chelseatroy/be3751ef43b31966e51b5d3388a75793 to your computer and use it in GitHub Desktop.
Save chelseatroy/be3751ef43b31966e51b5d3388a75793 to your computer and use it in GitHub Desktop.
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