Created
October 16, 2019 17:59
-
-
Save chelseatroy/be3751ef43b31966e51b5d3388a75793 to your computer and use it in GitHub Desktop.
Original Scanning Case Statement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
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