diff --git a/crafting-interpreters/zlox/src/main.zig b/crafting-interpreters/zlox/src/main.zig index 24ec5bb..bce9bcf 100644 --- a/crafting-interpreters/zlox/src/main.zig +++ b/crafting-interpreters/zlox/src/main.zig @@ -31,7 +31,7 @@ fn runFile(io: Io, gpa: std.mem.Allocator, script_path: []const u8) !void { defer gpa.free(content); var lox: zlox.Lox = .{}; - try lox.run(content); + try lox.run(gpa, content); if (lox.had_error) std.process.exit(65); @@ -64,7 +64,7 @@ fn runPrompt(io: Io, gpa: std.mem.Allocator) !void { if (std.mem.eql(u8, source, "exit")) break; - lox.run(source) catch |err| { + lox.run(gpa, source) catch |err| { std.log.err("{}", .{err}); }; diff --git a/crafting-interpreters/zlox/src/root.zig b/crafting-interpreters/zlox/src/root.zig index 15e0633..4bba585 100644 --- a/crafting-interpreters/zlox/src/root.zig +++ b/crafting-interpreters/zlox/src/root.zig @@ -7,9 +7,15 @@ pub const tokenizer = @import("tokenizer.zig"); pub const Lox = struct { had_error: bool = false, - pub fn run(self: *Lox, source: []const u8) !void { + pub fn run(self: *Lox, gpa: std.mem.Allocator, source: []const u8) !void { _ = self; - std.debug.print("source: {s}\n", .{source}); + var scanner = try tokenizer.Scanner.init(gpa, source); + defer scanner.deinit(gpa); + + const tokens = try scanner.scanTokens(gpa); + for (tokens) |token| { + std.log.debug("{f}", .{token}); + } } fn err(line: usize, comptime msg: []const u8, args: anytype) void { diff --git a/crafting-interpreters/zlox/src/tokenizer.zig b/crafting-interpreters/zlox/src/tokenizer.zig index 92f2d3e..cfcb4cc 100644 --- a/crafting-interpreters/zlox/src/tokenizer.zig +++ b/crafting-interpreters/zlox/src/tokenizer.zig @@ -1,5 +1,7 @@ const std = @import("std"); +const log = std.log.scoped(.tokenizer); + pub const TokenType = enum { // Single-character tokens left_paren, @@ -57,7 +59,11 @@ pub const Token = struct { line: usize, pub fn format(self: *const Token, w: *std.Io.Writer) error{WriteFailed}!void { - try w.print("{} {s} {?}", .{ self.type, self.lexeme, null }); + if (self.type == .eof) { + try w.print("eof", .{}); + } else { + try w.print("{} {s} {}", .{ self.type, self.lexeme, null }); + } } }; @@ -69,11 +75,11 @@ pub const Scanner = struct { current: usize = 0, line: usize = 1, - pub fn init(source: []const u8, gpa: std.mem.Allocator) !Scanner { + pub fn init(gpa: std.mem.Allocator, source: []const u8) !Scanner { return .{ .source = source, // copied from zig's 8:1 ration of source bytes to token count (lib/std/zig/Ast.zig:152) - .tokens = .initCapacity(gpa, source.len / 8), + .tokens = try .initCapacity(gpa, source.len / 8), }; } @@ -84,10 +90,10 @@ pub const Scanner = struct { pub fn scanTokens(self: *Scanner, gpa: std.mem.Allocator) ![]const Token { while (!self.isAtEnd()) { self.start = self.current; - self.scanToken(gpa); + try self.scanToken(gpa); } - try self.tokens.append(.{ .type = .eof, .lexeme = "", .line = self.line }); + try self.tokens.append(gpa, .{ .type = .eof, .lexeme = "", .line = self.line }); return self.tokens.toOwnedSlice(gpa); } @@ -96,7 +102,33 @@ pub const Scanner = struct { } fn scanToken(self: *Scanner, gpa: std.mem.Allocator) !void { - _ = self; - _ = gpa; + const c = self.advance(); + switch (c) { + '(' => try self.addToken(gpa, .left_paren), + ')' => try self.addToken(gpa, .right_paren), + '{' => try self.addToken(gpa, .left_brace), + '}' => try self.addToken(gpa, .right_brace), + ',' => try self.addToken(gpa, .comma), + '.' => try self.addToken(gpa, .dot), + '-' => try self.addToken(gpa, .minus), + '+' => try self.addToken(gpa, .plus), + ';' => try self.addToken(gpa, .semicolon), + '*' => try self.addToken(gpa, .star), + else => log.err("unexpected character: {c}", .{c}), + } + } + + fn advance(self: *Scanner) u8 { + const c = self.source[self.current]; + self.current += 1; + return c; + } + + fn addToken(self: *Scanner, gpa: std.mem.Allocator, t: TokenType) !void { + return self.tokens.append(gpa, .{ + .type = t, + .line = self.line, + .lexeme = self.source[self.start..self.current], + }); } };