Compare commits
No commits in common. "bc99cbfc3b656fedf1cd77e5dc5d65dcdeea3918" and "e921a0dda85ec80bfdae61e2e203848f7e24a978" have entirely different histories.
bc99cbfc3b
...
e921a0dda8
@ -36,14 +36,6 @@ pub fn build(b: *std.Build) void {
|
|||||||
.optimize = optimize,
|
.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
|
// This declares intent for the executable to be installed into the
|
||||||
// standard location when the user invokes the "install" step (the default
|
// standard location when the user invokes the "install" step (the default
|
||||||
// step when running `zig build`).
|
// step when running `zig build`).
|
||||||
|
@ -54,7 +54,7 @@ pub const Chunk = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Prints the value of a single instruction
|
/// Prints the value of a single instruction
|
||||||
pub fn dissasemble_instruction(self: *Chunk, offset: usize) usize {
|
fn dissasemble_instruction(self: *Chunk, offset: usize) usize {
|
||||||
// Print the instruction offset
|
// Print the instruction offset
|
||||||
print("{d:0>4} ", .{offset});
|
print("{d:0>4} ", .{offset});
|
||||||
|
|
||||||
|
@ -1,15 +1,11 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const config = @import("config");
|
|
||||||
|
|
||||||
const chunk = @import("./chunk.zig");
|
const chunk = @import("./chunk.zig");
|
||||||
const OpCode = chunk.OpCode;
|
const OpCode = chunk.OpCode;
|
||||||
const VM = @import("./vm.zig").VM;
|
const VM = @import("./vm.zig").VM;
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
|
// 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"});
|
||||||
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
|
|
||||||
}
|
|
||||||
|
|
||||||
var mem = std.heap.GeneralPurposeAllocator(.{}){};
|
var mem = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
const alloc = mem.allocator();
|
const alloc = mem.allocator();
|
||||||
|
@ -4,8 +4,6 @@ const Chunk = chunk_mod.Chunk;
|
|||||||
const OpCode = chunk_mod.OpCode;
|
const OpCode = chunk_mod.OpCode;
|
||||||
const print = std.debug.print;
|
const print = std.debug.print;
|
||||||
|
|
||||||
const tracing = @import("config").tracing;
|
|
||||||
|
|
||||||
const InterpretResult = enum {
|
const InterpretResult = enum {
|
||||||
Ok,
|
Ok,
|
||||||
CompileError,
|
CompileError,
|
||||||
@ -30,12 +28,6 @@ pub const VM = struct {
|
|||||||
// Executes the instructions in the bytecode
|
// Executes the instructions in the bytecode
|
||||||
pub fn run(self: *VM) InterpretResult {
|
pub fn run(self: *VM) InterpretResult {
|
||||||
while (true) {
|
while (true) {
|
||||||
if (tracing) {
|
|
||||||
// dissasemble & print the current instruction
|
|
||||||
const offset: usize = @intFromPtr(self.ip) - @intFromPtr(self.chunk.code.ptr);
|
|
||||||
_ = self.chunk.dissasemble_instruction(offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
const next = self.ip[0];
|
const next = self.ip[0];
|
||||||
self.ip += 1;
|
self.ip += 1;
|
||||||
switch (next) {
|
switch (next) {
|
||||||
|
Loading…
Reference in New Issue
Block a user