Created
September 13, 2020 08:09
-
-
Save gaufung/ea635b6df44c4b4444b5e4a2a9e590af to your computer and use it in GitHub Desktop.
Calculator Lexer
This file contains hidden or 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
// Lexer.cs | |
public class Lexer | |
{ | |
//... | |
public Token NextToken() | |
{ | |
//... | |
char character = this.input[this.position]; | |
switch (character) | |
{ | |
case '+': | |
token = new Token { Type = TokenType.PLUS, Literal = "+" }; | |
break; | |
case '-': | |
token = new Token { Type = TokenType.MINUS, Literal = "-" }; | |
break; | |
case '*': | |
token = new Token { Type = TokenType.MULTIPLY, Literal = "*" }; | |
break; | |
// ... | |
default: | |
if(IsDigit(character)) | |
{ | |
token = new Token { Type = TokenType.INT }; | |
token.Literal = this.ReadNumber(); | |
} | |
else | |
{ | |
token = new Token { Type = TokenType.ILLEGAL, Literal=""}; | |
} | |
break; | |
} | |
this.position++; | |
return token; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment