From: Ayo Reis Date: Fri, 13 Feb 2026 17:21:01 +0000 (+0000) Subject: Fix style and refactor X-Git-Url: https://git.ayoreis.com/zlox.git/commitdiff_plain/7b3023fd01ad1445eeccb48346217935d4e117f4?ds=sidebyside Fix style and refactor --- diff --git a/README.md b/README.md index c2b0cfa..1de96d0 100644 --- 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 -zig run Lox.zig +zig run lox.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; } diff --git a/Token.zig b/Token.zig index bf988d8..96f8378 100644 --- a/Token.zig +++ b/Token.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const TokenType = @import("token-type.zig").TokenType; +const TokenType = @import("token_type.zig").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 56a1572..ec4478b 100644 --- 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); + // Indicate an error in the exit code. if (hadError) return 65; + return 0; } diff --git a/token-type.zig b/token_type.zig similarity index 100% rename from token-type.zig rename to token_type.zig