From 566d8313f3c8ec83177e52a014be0eb1d49810bb Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 1 Jun 2019 17:13:50 -0300 Subject: [PATCH] add number tokens --- src/new_scanner.zig | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/new_scanner.zig b/src/new_scanner.zig index b2005f7..2b48f48 100644 --- a/src/new_scanner.zig +++ b/src/new_scanner.zig @@ -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),