add number tokens

This commit is contained in:
Luna 2019-06-01 17:13:50 -03:00
parent 9d1d253c94
commit 566d8313f3

View file

@ -11,6 +11,20 @@ pub const TokenError = error{
Unterminated,
};
fn isDigit(char: u8) bool {
return char >= '0' and char <= '9';
}
fn isAlpha(c: u8) bool {
return (c >= 'a' and c <= 'z') or
(c >= 'A' and c <= 'Z') or
c == '_';
}
fn isAlphaNumeric(char: u8) bool {
return isAlpha(char) or isDigit(char);
}
pub const Scanner = struct {
source: []const u8,
@ -117,12 +131,32 @@ pub const Scanner = struct {
return self.makeToken(.STRING);
}
/// Consume a number
fn doNumber(self: *Scanner) Token {
while (isDigit(self.peek())) {
_ = self.advance();
}
// check if its a number like 12.34, where the '.' character
// exists and the one next to it is a digit.
if (self.peek() == '.' and isDigit(self.peekNext())) {
_ = self.advance();
while (isDigit(self.peek())) {
_ = self.advance();
}
}
return self.makeToken(.NUMBER);
}
pub fn scanToken(self: *Scanner) !?Token {
self.skipWhitespace();
self.start = self.current;
if (self.isAtEnd()) return self.makeToken(TokenType.EOF);
var c = self.advance();
if (isDigit(c)) return self.doNumber();
var token = switch (c) {
'(' => self.makeToken(.LEFT_PAREN),