DzLox

:)
git clone https://git.sr.ht/~ashymad/DzLox
Log | Files | Refs | Submodules | LICENSE

commit d20c92698e4535a7ff4676401b9c6c6f379cdbb6
parent 21af2b44699d3f682c70983534e263ce8ebb4c83
Author: Szymon Mikulicz <szymon.mikulicz@posteo.net>
Date:   Mon, 17 Nov 2025 16:55:47 +0100

update to zig 0.15

Diffstat:
Mzlox/build.zig | 18++++++++++--------
Mzlox/build.zig.zon | 5+++--
Mzlox/src/comptime_utils.zig | 14+++++++++++++-
Mzlox/src/debug.zig | 4++--
Mzlox/src/gc.zig | 6+++---
Mzlox/src/main.zig | 9+++------
Mzlox/src/obj.zig | 6+++---
Mzlox/src/obj/closure.zig | 2+-
Mzlox/src/obj/function.zig | 2+-
Mzlox/src/obj/list.zig | 9++++-----
Mzlox/src/obj/native.zig | 2+-
Mzlox/src/obj/string.zig | 4++--
Mzlox/src/obj/table.zig | 11+++++------
Mzlox/src/obj/template.zig | 4+---
Mzlox/src/obj/upvalue.zig | 4++--
Mzlox/src/value.zig | 4++--
Mzlox/src/vm.zig | 6+++---
Mzlox/src/vm/callbacks.zig | 2+-
Mzlox/src/vm/native.zig | 2+-
19 files changed, 61 insertions(+), 53 deletions(-)

diff --git a/zlox/build.zig b/zlox/build.zig @@ -17,11 +17,11 @@ pub fn build(b: *std.Build) void { const exe = b.addExecutable(.{ .name = "zlox", - // In this case the main source file is merely a path, however, in more - // complicated build scripts, this could be a generated file. - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }), }); // This declares intent for the executable to be installed into the @@ -64,9 +64,11 @@ pub fn build(b: *std.Build) void { // Creates a step for unit testing. This only builds the test executable // but does not run it. const unit_tests = b.addTest(.{ - .root_source_file = b.path("src/test.zig"), - .target = target, - .optimize = optimize, + .root_module = b.createModule(.{ + .root_source_file = b.path("src/test.zig"), + .target = target, + .optimize = optimize, + }), }); const run_unit_tests = b.addRunArtifact(unit_tests); diff --git a/zlox/build.zig.zon b/zlox/build.zig.zon @@ -1,5 +1,5 @@ .{ - .name = "zlox", + .name = .zlox, // This is a [Semantic Version](https://semver.org/). // In a future version of Zig it will be used for package deduplication. .version = "0.0.0", @@ -17,7 +17,7 @@ .dependencies = .{ .linenoise = .{ .url = "https://github.com/antirez/linenoise/archive/refs/heads/master.tar.gz", - .hash = "1220f1e9b8ae57a1ed3c1110f6ccfbcbef26598a99ef225d68e14a863a35355f7abe", + .hash = "1220129271545fccd186dd1ce05c9ee7fb52c97cd86884e70597a0eef254a909e320", }, }, .paths = .{ @@ -33,4 +33,5 @@ //"LICENSE", //"README.md", }, + .fingerprint = 0xd8b023c61737c0b7, } diff --git a/zlox/src/comptime_utils.zig b/zlox/src/comptime_utils.zig @@ -38,7 +38,7 @@ pub fn if_not_null(comptime fun: anytype) fn (?param_type(fun, 0)) void { }.function; } -pub fn make_packed(s: type) type { +pub fn make_packed_t(s: type) type { const oldFields = @typeInfo(s).@"struct".fields; var newFields: [oldFields.len]std.builtin.Type.StructField = undefined; @@ -55,3 +55,15 @@ pub fn make_packed(s: type) type { .is_tuple = false }}); } + +pub fn make_packed(s: anytype) make_packed_t(@TypeOf(s)) { + const T = @TypeOf(s); + const fields = @typeInfo(T).@"struct".fields; + var packd: make_packed_t(T) = undefined; + + for (fields) |field| { + @field(packd, field.name) = @field(s, field.name); + } + + return packd; +} diff --git a/zlox/src/debug.zig b/zlox/src/debug.zig @@ -86,7 +86,7 @@ fn simpleInstruction(name: []const u8, offset: usize) usize { fn constantInstruction(name: []const u8, ch: *const chunk.Chunk, offset: usize, print_fn: bool) Error!usize { const constant = try ch.code.get(offset + 1); const constval = try ch.constants.get(constant); - print("{s:<32} {d:4} '{s}'\n", .{ name, constant, constval}); + print("{s:<32} {d:4} '{f}'\n", .{ name, constant, constval}); if (print_fn and constval.is(Obj.Type.Function)) { const function = constval.obj.cast(.Function) catch unreachable; if (function.name) |str| { @@ -118,7 +118,7 @@ fn closureInstruction(name: []const u8, ch: *const chunk.Chunk, offset: usize, p const constant = try ch.code.get(off); const val = try ch.constants.get(constant); const function = try val.obj.cast(.Function); - print("{s:<32} {d:4} '{s}'\n", .{ name, constant, function}); + print("{s:<32} {d:4} '{f}'\n", .{ name, constant, function}); for (0..function.upvalue_count) |_| { const isLocal = try ch.code.get(off + 1); const idx = try ch.code.get(off + 2); diff --git a/zlox/src/gc.zig b/zlox/src/gc.zig @@ -47,7 +47,7 @@ pub const GC = struct { if (DBG_STRESS) { self.collect(); } - dbg_print("Allocating {} at 0x{x}: {s}\n", .{ obj.obj.type, @intFromPtr(obj), obj }); + dbg_print("Allocating {any} at 0x{x}: {f}\n", .{ obj.obj.type, @intFromPtr(obj), obj }); try self.list.push(obj.cast()); } return obj; @@ -79,7 +79,7 @@ pub const GC = struct { else => {}, }, *Obj => { - dbg_print("Marking {} at 0x{x}: {s}\n", .{ arg.type, @intFromPtr(arg), arg }); + dbg_print("Marking {any} at 0x{x}: {f}\n", .{ arg.type, @intFromPtr(arg), arg }); arg.fields.mark = true; }, else => if (Obj.isChild(T)) { @@ -95,7 +95,7 @@ pub const GC = struct { pub fn deinit(self: *Self) void { while (true) { const el = self.list.pop() catch break; - dbg_print("Freeing {} at 0x{x}: {s}\n", .{ el.type, @intFromPtr(el), el }); + dbg_print("Freeing {any} at 0x{x}: {f}\n", .{ el.type, @intFromPtr(el), el }); el.free(self.allocator); } self.list.free(); diff --git a/zlox/src/main.zig b/zlox/src/main.zig @@ -31,14 +31,11 @@ pub fn main() anyerror!u8 { } pub fn runFile(allocator: std.mem.Allocator, path: []const u8) anyerror!void { - const file = try std.fs.cwd().openFile(path, .{}); - defer file.close(); + const text = try std.fs.cwd().readFileAlloc(allocator, path, 999999); + defer allocator.free(text); var VM = try vm.VM.init(allocator); defer VM.deinit(); - const text = try file.reader().readAllAlloc(allocator, 999999); - defer allocator.free(text); - try VM.interpret(text, false); } @@ -51,7 +48,7 @@ pub fn repl(allocator: std.mem.Allocator, dbg: bool) anyerror!void { while (Linenoise.linenoise("lox> ")) |line| { defer Linenoise.linenoiseFree(line); VM.interpret(std.mem.span(line), dbg) catch |err| { - std.debug.print("\nError: {}\n", .{err}); + std.debug.print("\nError: {any}\n", .{err}); }; _ = Linenoise.linenoiseHistoryAdd(line); } diff --git a/zlox/src/obj.zig b/zlox/src/obj.zig @@ -15,7 +15,7 @@ pub fn Obj(fields: anytype) type { const Self = @This(); type: Type, - fields: utils.make_packed(@TypeOf(fields)) = fields, + fields: utils.make_packed_t(@TypeOf(fields)) = utils.make_packed(fields), pub const List = @import("obj/list.zig").List(fields); pub const String = @import("obj/string.zig").String(fields); @@ -67,9 +67,9 @@ pub fn Obj(fields: anytype) type { return (try tp.get().init(arg, allocator)).cast(); } - pub fn format(self: *const Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + pub fn format(self: *const Self, writer: *std.Io.Writer) !void { switch (self.type) { - inline else => |tp| try self._cast(tp).format(fmt, options, writer), + inline else => |tp| try self._cast(tp).format(writer), } } pub fn eql(self: *const Self, other: *const Self) bool { diff --git a/zlox/src/obj/closure.zig b/zlox/src/obj/closure.zig @@ -32,7 +32,7 @@ pub fn Closure(fields: anytype) type { return @ptrCast(self); } - pub fn format(self: *const Self, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { + pub fn format(self: *const Self, writer: *std.Io.Writer) !void { _ = try writer.write("<C: "); if (self.function.name) |name| { _ = try writer.write(name.slice()); diff --git a/zlox/src/obj/function.zig b/zlox/src/obj/function.zig @@ -41,7 +41,7 @@ pub fn Function(fields: anytype) type { return @ptrCast(self); } - pub fn format(self: *const Self, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { + pub fn format(self: *const Self, writer: *std.Io.Writer) !void { switch(self.type) { .Function => _ = try writer.write("<F: "), .Script => _ = try writer.write("<S: "), diff --git a/zlox/src/obj/list.zig b/zlox/src/obj/list.zig @@ -31,16 +31,15 @@ pub fn List(fields: anytype) type { return @ptrCast(self); } - pub fn format(self: *const Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) utils.fn_error(@TypeOf(writer).write)!void { + pub fn format(self: *const Self, writer: *std.Io.Writer) !void { const Printer = struct { - options: std.fmt.FormatOptions, writer: @TypeOf(writer), count: usize, - pub fn print(this: *@This(), val: ?Value) utils.fn_error(@TypeOf(writer).write)!void { + pub fn print(this: *@This(), val: ?Value) std.Io.Writer.Error!void { this.count -= 1; if (val) |v| { - try v.format(fmt, this.options, this.writer); + try v.format(this.writer); } else { _ = try this.writer.write("-"); } @@ -49,7 +48,7 @@ pub fn List(fields: anytype) type { }; - var printer = Printer{.options = options, .writer = writer, .count = self.list.len}; + var printer = Printer{.writer = writer, .count = self.list.len}; _ = try writer.write("["); try self.list.for_each_try(&printer, Printer.print); diff --git a/zlox/src/obj/native.zig b/zlox/src/obj/native.zig @@ -40,7 +40,7 @@ pub fn Native(fields: anytype) type { return @ptrCast(self); } - pub fn format(self: *const Self, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { + pub fn format(self: *const Self, writer: *std.Io.Writer) !void { _ = try writer.write("<N: "); _ = try writer.write(self.name[0..self.name_len]); _ = try writer.writeAll(">"); diff --git a/zlox/src/obj/string.zig b/zlox/src/obj/string.zig @@ -22,7 +22,7 @@ pub fn String(fields: anytype) type { return p + @sizeOf(Self); } fn new(arg: Arg, params: ArgParams, allocator: std.mem.Allocator) Error!*Self { - const ret: *Self = @ptrCast(try allocator.alignedAlloc(u8, @alignOf(Self), @sizeOf(Self) + params.len)); + const ret: *Self = @ptrCast(try allocator.alignedAlloc(u8, std.mem.Alignment.of(Self), @sizeOf(Self) + params.len)); ret.* = Self{ .obj = Super.make(Self), .hash = params.hash, @@ -42,7 +42,7 @@ pub fn String(fields: anytype) type { return @ptrCast(self); } - pub fn format(self: *const Self, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { + pub fn format(self: *const Self, writer: *std.Io.Writer) !void { _ = try writer.writeAll(self.slice()); } pub fn eql(self: *const Self, other: *const Self) bool { diff --git a/zlox/src/obj/table.zig b/zlox/src/obj/table.zig @@ -45,24 +45,23 @@ pub fn Table(fields: anytype) type { if (self.table.delete(key)) self.len -= 1; } - pub fn format(self: *const Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + pub fn format(self: *const Self, writer: *std.Io.Writer) !void { const Printer = struct { - options: std.fmt.FormatOptions, writer: @TypeOf(writer), count: usize, - pub fn print(this: *@This(), key: Value, val: Value) utils.fn_error(@TypeOf(writer).write)!void { + pub fn print(this: *@This(), key: Value, val: Value) std.Io.Writer.Error!void { this.count -= 1; - try key.format(fmt, this.options, this.writer); + try key.format(this.writer); _ = try this.writer.write(":"); - try val.format(fmt, this.options, this.writer); + try val.format(this.writer); if (this.count > 0) _ = try this.writer.write(", "); } }; - var printer = Printer{.options = options, .writer = writer, .count = self.len}; + var printer = Printer{.writer = writer, .count = self.len}; _ = try writer.write("["); if (self.table.count > 0) { try self.table.for_each_try(&printer, Printer.print); diff --git a/zlox/src/obj/template.zig b/zlox/src/obj/template.zig @@ -22,10 +22,8 @@ pub fn Template(fields: anytype) type { return @ptrCast(self); } - pub fn format(self: *const Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + pub fn format(self: *const Self, writer: *std.Io.Writer) !void { _ = self; - _ = fmt; - _ = options; _ = writer; } diff --git a/zlox/src/obj/upvalue.zig b/zlox/src/obj/upvalue.zig @@ -42,8 +42,8 @@ pub fn Upvalue(fields: anytype) type { return @ptrCast(self); } - pub fn format(self: *const Self, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { - try writer.print("<Upvalue{{{} at 0x{x}, {}, {}}}>", .{self.location.*, @intFromPtr(self.location), self.closed, self.slot}); + pub fn format(self: *const Self, writer: *std.Io.Writer) !void { + try writer.print("<Upvalue{{{f} at 0x{x}, {any}, {d}}}>", .{self.location.*, @intFromPtr(self.location), self.closed, self.slot}); } pub fn eql(_: *const Self, _: *const Self) bool { diff --git a/zlox/src/value.zig b/zlox/src/value.zig @@ -15,13 +15,13 @@ pub const Value = union(enum) { pub const Array = array.Array(Value, u8, 8); - pub fn format(self: Self, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + pub fn format(self: Self, writer: *std.Io.Writer) !void { switch (self) { .number => |val| try writer.print("{d}", .{val}), .char => |val| try writer.writeAll(&[_]u8{val}), .bool => |val| try writer.writeAll(if (val) "true" else "false"), .nil => try writer.writeAll("nil"), - .obj => |o| try o.format(fmt, options, writer), + .obj => |o| try o.format(writer), } } diff --git a/zlox/src/vm.zig b/zlox/src/vm.zig @@ -258,7 +258,7 @@ pub const VM = struct { std.debug.print(" ", .{}); var stackPtr: [*]Value = &self.stack; while (stackPtr != self.stackTop) : (stackPtr += 1) { - std.debug.print("[{s}]", .{stackPtr[0]}); + std.debug.print("[{f}]", .{stackPtr[0]}); } std.debug.print("\n", .{}); _ = try debug.disassembleInstruction(self.frame().chunk, self.instruction_idx()); @@ -266,7 +266,7 @@ pub const VM = struct { const instruction: u8 = self.read_byte(); switch (instruction) { @intFromEnum(OP.PRINT) => { - std.debug.print("{s}\n", .{self.pop()}); + std.debug.print("{f}\n", .{self.pop()}); }, @intFromEnum(OP.RETURN) => { const result = self.pop(); @@ -433,7 +433,7 @@ pub const VM = struct { while (true) : (i -= 1) { const fram = self.frames[i]; const idx = @intFromPtr(fram.ip) - @intFromPtr(fram.chunk.code.data.ptr); - std.debug.print("[line {d}] in {s}\n", .{ fram.chunk.lines.get(idx) catch 1, fram.callee }); + std.debug.print("[line {d}] in {f}\n", .{ fram.chunk.lines.get(idx) catch 1, fram.callee }); if (i == 0) break; } std.debug.print(fmt ++ "\n", args); diff --git a/zlox/src/vm/callbacks.zig b/zlox/src/vm/callbacks.zig @@ -19,7 +19,7 @@ pub fn Type(comptime in_tag: anytype, comptime out_tag: anytype) type { }; } else { return struct { - call: fn (Value.tagType(in_tag), Value.tagType(in_tag)) callconv(.Inline) Error!Value.tagType(out_tag), + call: fn (Value.tagType(in_tag), Value.tagType(in_tag)) callconv(.@"inline") Error!Value.tagType(out_tag), }; } } diff --git a/zlox/src/vm/native.zig b/zlox/src/vm/native.zig @@ -19,7 +19,7 @@ pub const Clock = struct { }; pub fn put(_: *GC, args: []const Value) Error!Value { - std.debug.print("{s}", .{args[0]}); + std.debug.print("{f}", .{args[0]}); return Value.init({}); }