]> Repositories - zlox.git/blobdiff - Scanner.zig
Fix style and refactor
[zlox.git] / Scanner.zig
index d971ec5fac2178764941ff93470518f48714e790..622c98ed5b147002166adb656a3153daef1eb949 100644 (file)
@@ -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;
 }