diff --git a/build.zig b/build.zig index a8c4ef9..29e5967 100644 --- a/build.zig +++ b/build.zig @@ -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`). diff --git a/src/main.zig b/src/main.zig index 01d2576..8450116 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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()`) - std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); + if (config.tracing) { + std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); + } var mem = std.heap.GeneralPurposeAllocator(.{}){}; const alloc = mem.allocator();