commit 807ba37e68c8ca4c4589ccf8a80b849725eb4501
parent 2349fe1c4555251571bf86f565a3188768a10c09
Author: walther chen <walther.chen@gmail.com>
Date: Thu, 13 Feb 2025 22:55:26 +0700
update to latest zig 0.14.0-dev.3066+1a1389c51
Diffstat:
7 files changed, 33 insertions(+), 26 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1 +1,2 @@
zig-*
+.zig-*
diff --git a/build.zig b/build.zig
@@ -1,6 +1,6 @@
const std = @import("std");
-pub fn build(b: *std.build.Builder) void {
+pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
@@ -9,7 +9,7 @@ pub fn build(b: *std.build.Builder) void {
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
- const mode = b.standardReleaseOptions();
+ const optimize = b.standardOptimizeOption(.{});
// Build options
const build_options_step = b.addOptions();
@@ -29,13 +29,19 @@ pub fn build(b: *std.build.Builder) void {
const test_step = b.step("test", "Run unit tests");
inline for (stages) |stage| {
- const exe = b.addExecutable(stage, "src/" ++ stage ++ ".zig");
- exe.setTarget(target);
- exe.setBuildMode(mode);
- exe.addOptions("build_options", build_options_step);
- exe.install();
-
- const run_cmd = exe.run();
+ var exe_mod = b.createModule(.{
+ .root_source_file = b.path("src/" ++ stage ++ ".zig"),
+ .target = target,
+ .optimize = optimize,
+ });
+ exe_mod.addOptions("build_options", build_options_step);
+ const exe = b.addExecutable(.{
+ .name = stage,
+ .root_module = exe_mod,
+ });
+ b.installArtifact(exe);
+
+ const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
@@ -44,10 +50,10 @@ pub fn build(b: *std.build.Builder) void {
const run_step = b.step(stage, "Run " ++ stage);
run_step.dependOn(&run_cmd.step);
- const exe_tests = b.addTest("src/" ++ stage ++ ".zig");
- exe_tests.setTarget(target);
- exe_tests.setBuildMode(mode);
- exe_tests.addOptions("build_options", build_options_step);
- test_step.dependOn(&exe_tests.step);
+ const exe_tests = b.addTest(.{
+ .root_module = exe_mod,
+ });
+ const run_exe_tests = b.addRunArtifact(exe_tests);
+ test_step.dependOn(&run_exe_tests.step);
}
}
diff --git a/justfile b/justfile
@@ -1,3 +1,3 @@
benchmark-factor bin:
- zig build -Drelease-fast && \
+ zig build -Doptimize=ReleaseFast && \
hyperfine 'echo 179424691 | zig-out/bin/og fixtures/factor.bf' 'echo 179424691 | zig-out/bin/{{bin}} fixtures/factor.bf'
diff --git a/src/common.zig b/src/common.zig
@@ -39,7 +39,7 @@ pub fn parseProgram(src: []const u8, alloc: std.mem.Allocator) !Program {
else => {},
}
}
- return .{ .instructions = instructions.toOwnedSlice(), .alloc = alloc };
+ return .{ .instructions = try instructions.toOwnedSlice(), .alloc = alloc };
}
pub const Program = struct {
@@ -63,8 +63,8 @@ pub fn testHelloWorld(parse: anytype, interpret: anytype) anyerror!void {
var empty_in = std.io.fixedBufferStream(&in_buf);
var memory = [_]u8{0} ** 30000;
- var rdr = empty_in.reader();
- var wtr = list.writer();
+ const rdr = empty_in.reader();
+ const wtr = list.writer();
const program = try parse(hello_world, std.testing.allocator);
defer program.deinit();
diff --git a/src/og.zig b/src/og.zig
@@ -34,7 +34,7 @@ fn interpret(program: Program, memory: []u8, rdr: anytype, wtr: anytype, alloc:
}
var bracket_nesting: usize = 1;
- var saved_pc = pc; // used for error message only
+ const saved_pc = pc; // used for error message only
while (bracket_nesting != 0 and pc < instructions.len - 1) {
pc += 1;
@@ -57,7 +57,7 @@ fn interpret(program: Program, memory: []u8, rdr: anytype, wtr: anytype, alloc:
}
var bracket_nesting: usize = 1;
- var saved_pc = pc; // used for error message only
+ const saved_pc = pc; // used for error message only
while (bracket_nesting != 0 and pc > 0) {
pc -= 1;
diff --git a/src/opt1.zig b/src/opt1.zig
@@ -40,7 +40,7 @@ fn interpret(program: Program, memory: []u8, rdr: anytype, wtr: anytype, alloc:
const instruction = instructions[pc];
if (TRACE) {
- var entry = try instruction_count.getOrPut(instruction);
+ const entry = try instruction_count.getOrPut(instruction);
if (entry.found_existing) {
entry.value_ptr.* += 1;
} else {
diff --git a/src/opt2.zig b/src/opt2.zig
@@ -75,7 +75,7 @@ const Op = struct {
fn opsDebug(ops: []const Op) void {
std.debug.print("\n# Ops\n", .{});
- for (ops) |item, idx| {
+ for (ops, 0..) |item, idx| {
std.debug.print("{d:0>2}: {any} {d}\n", .{ idx, item.tag, item.value });
}
}
@@ -171,7 +171,7 @@ fn parse(src: []const u8, alloc: Allocator) !Program {
opsDebug(ops.items);
}
- return .{ .ops = ops.toOwnedSlice(), .alloc = alloc };
+ return .{ .ops = try ops.toOwnedSlice(), .alloc = alloc };
}
test "opt2: parse basic 0" {
@@ -255,7 +255,7 @@ fn interpret(program: Program, memory: []u8, rdr: anytype, wtr: anytype, alloc:
const op = ops[pc];
if (TRACE) {
- var entry = try instruction_count.getOrPut(op.tag);
+ const entry = try instruction_count.getOrPut(op.tag);
if (entry.found_existing) {
entry.value_ptr.* += 1;
} else {
@@ -266,8 +266,8 @@ fn interpret(program: Program, memory: []u8, rdr: anytype, wtr: anytype, alloc:
switch (op.tag) {
.inc_ptr => dataptr += op.value,
.dec_ptr => dataptr -= op.value,
- .inc_data => memory[dataptr] += @intCast(u8, op.value),
- .dec_data => memory[dataptr] -= @intCast(u8, op.value),
+ .inc_data => memory[dataptr] += @intCast(op.value),
+ .dec_data => memory[dataptr] -= @intCast(op.value),
.read_stdin => {
var i: usize = 0;
while (i < op.value) : (i += 1) {