Fix number parsing and add ++ operator

This commit is contained in:
Brandon Dyck 2023-06-08 08:00:26 -06:00
parent c991893f76
commit 0d36ba4a52

View File

@ -83,6 +83,7 @@ enum TokenType
Period, Period,
Equal, Equal,
Plus, Minus, Asterisk, Slash, Plus, Minus, Asterisk, Slash,
PlusPlus,
DoubleEqual, DoubleEqual,
Bang, BangEqual, Bang, BangEqual,
LessEqual, GreaterEqual, LessEqual, GreaterEqual,
@ -238,8 +239,11 @@ class Scanner
// Look for a fractional part. // Look for a fractional part.
if (peek() == '.' && isDigitOrSeparator(peekNext() ?? '\0')) if (peek() == '.' && isDigitOrSeparator(peekNext() ?? '\0'))
{ {
match('.');
while (match(isDigitOrSeparator)) ; while (match(isDigitOrSeparator)) ;
} }
double value = Double.Parse(source.Substring(start, current - start));
addToken(TokenType.Number, value);
} }
private bool isIdentifierStartChar(char c) private bool isIdentifierStartChar(char c)
@ -277,7 +281,6 @@ class Scanner
case '`': addToken(TokenType.Backtick); break; case '`': addToken(TokenType.Backtick); break;
case ',': addToken(TokenType.Comma); break; case ',': addToken(TokenType.Comma); break;
case '.': addToken(TokenType.Period); break; case '.': addToken(TokenType.Period); break;
case '+': addToken(TokenType.Plus); break;
case '*': addToken(TokenType.Asterisk); break; case '*': addToken(TokenType.Asterisk); break;
case '/': addToken(TokenType.Slash); break; case '/': addToken(TokenType.Slash); break;
case '-': case '-':
@ -286,6 +289,9 @@ class Scanner
case '!': case '!':
addToken(match('=') ? TokenType.BangEqual : TokenType.Bang); addToken(match('=') ? TokenType.BangEqual : TokenType.Bang);
break; break;
case '+':
addToken(match('+') ? TokenType.PlusPlus : TokenType.Plus);
break;
case '=': case '=':
addToken(match('=') ? TokenType.DoubleEqual : addToken(match('=') ? TokenType.DoubleEqual :
match('>') ? TokenType.DoubleArrow : match('>') ? TokenType.DoubleArrow :