X-Git-Url: https://git.ayoreis.com/zlox.git/blobdiff_plain/bebff07499e6ddad8a7b028e38db539372afc2f3..7b3023fd01ad1445eeccb48346217935d4e117f4:/Scanner.zig diff --git a/Scanner.zig b/Scanner.zig index d971ec5..622c98e 100644 --- a/Scanner.zig +++ b/Scanner.zig @@ -1,10 +1,13 @@ const std = @import("std"); const Allocator = std.mem.Allocator; +const ascii = std.ascii; +const isDigit = ascii.isDigit; +const isAlphabetic = ascii.isAlphabetic; const Token = @import("Token.zig"); const Literal = Token.Literal; const Scanner = @This(); -const TokenType = @import("token-type.zig").TokenType; -const Lox = @import("Lox.zig"); +const TokenType = @import("token_type.zig").TokenType; +const lox = @import("lox.zig"); source: []const u8, tokens: std.ArrayList(Token) = .empty, @@ -82,7 +85,7 @@ fn scanToken(self: *Scanner, allocator: Allocator) !void { } else if (isAlpha(c)) { try self.identifier(allocator); } else { - try Lox.@"error"(self.line, "Unexpected character."); + try lox.@"error"(self.line, "Unexpected character."); }, } } @@ -90,9 +93,8 @@ fn scanToken(self: *Scanner, allocator: Allocator) !void { fn identifier(self: *Scanner, allocator: Allocator) !void { while (isAlphanumeric(self.peek())) _ = self.advance(); const text = self.source[self.start..self.current]; - var @"type" = keyword.get(text); - if (@"type" == null) @"type" = .identifier; - try self.addToken(allocator, @"type".?, null); + const @"type" = keyword.get(text) orelse .identifier; + try self.addToken(allocator, @"type", null); } fn number(self: *Scanner, allocator: Allocator) !void { @@ -116,7 +118,7 @@ fn string(self: *Scanner, allocator: Allocator) !void { } if (self.isAtEnd()) { - try Lox.@"error"(self.line, "Unterminated string."); + try lox.@"error"(self.line, "Unterminated string."); return; } @@ -145,19 +147,13 @@ fn peekNext(self: *Scanner) u8 { } fn isAlpha(c: u8) bool { - return (c >= 'a' and c <= 'z') or - (c >= 'A' and c <= 'Z') or - c == '_'; + return isAlphabetic(c) or c == '_'; } fn isAlphanumeric(c: u8) bool { return isAlpha(c) or isDigit(c); } -fn isDigit(c: u8) bool { - return c >= '0' and c <= '9'; -} - fn isAtEnd(self: *Scanner) bool { return self.current >= self.source.len; }