]> Repositories - zlox.git/commitdiff
Fix style and refactor
authorAyo Reis <hey@ayoreis.com>
Fri, 13 Feb 2026 17:21:01 +0000 (17:21 +0000)
committerAyo Reis <hey@ayoreis.com>
Fri, 13 Feb 2026 17:21:01 +0000 (17:21 +0000)
README.md
Scanner.zig
Token.zig
lox.zig [moved from Lox.zig with 99% similarity]
token_type.zig [moved from token-type.zig with 100% similarity]

index c2b0cfa5ddf2ddfd98d9e58b9b0fd9e047d29fe1..1de96d00dfa87c9c77b4dc6a1009e2e72adcc7fd 100644 (file)
--- a/README.md
+++ b/README.md
@@ -3,5 +3,5 @@
 An implementation of the [Lox language](https://craftinginterpreters.com/the-lox-language.html) in Zig.
 
 ```shell
 An implementation of the [Lox language](https://craftinginterpreters.com/the-lox-language.html) in Zig.
 
 ```shell
-zig run Lox.zig
+zig run lox.zig
 ```
 ```
index d971ec5fac2178764941ff93470518f48714e790..622c98ed5b147002166adb656a3153daef1eb949 100644 (file)
@@ -1,10 +1,13 @@
 const std = @import("std");
 const Allocator = std.mem.Allocator;
 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 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,
 
 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 {
         } 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];
 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 {
 }
 
 fn number(self: *Scanner, allocator: Allocator) !void {
@@ -116,7 +118,7 @@ fn string(self: *Scanner, allocator: Allocator) !void {
     }
 
     if (self.isAtEnd()) {
     }
 
     if (self.isAtEnd()) {
-        try Lox.@"error"(self.line, "Unterminated string.");
+        try lox.@"error"(self.line, "Unterminated string.");
         return;
     }
 
         return;
     }
 
@@ -145,19 +147,13 @@ fn peekNext(self: *Scanner) u8 {
 }
 
 fn isAlpha(c: u8) bool {
 }
 
 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 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;
 }
 fn isAtEnd(self: *Scanner) bool {
     return self.current >= self.source.len;
 }
index bf988d89fbdef594afe4f256a2e52eaee0b68926..96f83784835e5abe25555d7215d48f76867ad3fb 100644 (file)
--- a/Token.zig
+++ b/Token.zig
@@ -1,5 +1,5 @@
 const std = @import("std");
 const std = @import("std");
-const TokenType = @import("token-type.zig").TokenType;
+const TokenType = @import("token_type.zig").TokenType;
 const Token = @This();
 
 type: TokenType,
 const Token = @This();
 
 type: TokenType,
diff --git a/Lox.zig b/lox.zig
similarity index 99%
rename from Lox.zig
rename to lox.zig
index 56a1572a8431f529aaafad5ce367fcd18c57f211..ec4478bce4aa8e614b020308006ef128d46f51ae 100644 (file)
--- a/Lox.zig
+++ b/lox.zig
@@ -29,8 +29,10 @@ fn runFile(allocator: Allocator, path: []const u8) !u8 {
     const bytes = try std.fs.cwd().readFileAlloc(allocator, path, std.math.maxInt(usize));
     defer allocator.free(bytes);
     try run(allocator, bytes);
     const bytes = try std.fs.cwd().readFileAlloc(allocator, path, std.math.maxInt(usize));
     defer allocator.free(bytes);
     try run(allocator, bytes);
+
     // Indicate an error in the exit code.
     if (hadError) return 65;
     // Indicate an error in the exit code.
     if (hadError) return 65;
+
     return 0;
 }
 
     return 0;
 }
 
similarity index 100%
rename from token-type.zig
rename to token_type.zig