Add build option to enable debug execution tracing

master
Araozu 2024-05-27 21:27:05 -05:00
parent e921a0dda8
commit 252190f5e6
2 changed files with 13 additions and 1 deletions

View File

@ -36,6 +36,14 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
// Declare an option to build the program with debug statements
// (crafting interpreters: A virtual machine: execution tracing)
const executionTracing = b.option(bool, "tracing", "enable execution tracing") orelse false;
const options = b.addOptions();
options.addOption(bool, "tracing", executionTracing);
exe.root_module.addOptions("config", options);
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).

View File

@ -1,11 +1,15 @@
const std = @import("std");
const config = @import("config");
const chunk = @import("./chunk.zig");
const OpCode = chunk.OpCode;
const VM = @import("./vm.zig").VM;
pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
if (config.tracing) {
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
}
var mem = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = mem.allocator();