adding basic trace enabler

This commit is contained in:
Patrick MARIE 2024-08-26 22:10:01 +02:00
parent 26d361fabd
commit 062d2e44de
3 changed files with 22 additions and 7 deletions

View File

@ -15,8 +15,6 @@ const VM = @import("./vm.zig").VM;
const ParsingError = @import("./errors.zig").ParsingError; const ParsingError = @import("./errors.zig").ParsingError;
const DEBUG_TRACE_EXECUTION = @import("./main.zig").DEBUG_TRACE_EXECUTION;
const Precedence = enum { const Precedence = enum {
None, None,
Assignement, Assignement,
@ -130,7 +128,7 @@ const Parser = struct {
} }
fn end_parser(self: *Parser) !void { fn end_parser(self: *Parser) !void {
if (!self.had_error and DEBUG_TRACE_EXECUTION) { if (!self.had_error and self.vm.has_tracing()) {
self.chunk.dissassemble("code"); self.chunk.dissassemble("code");
} }
try self.emit_return(); try self.emit_return();

View File

@ -10,8 +10,6 @@ const InterpretResult = @import("./vm.zig").InterpretResult;
// XXX imported to run tests. // XXX imported to run tests.
const Table = @import("./table.zig"); const Table = @import("./table.zig");
pub const DEBUG_TRACE_EXECUTION = true;
pub fn repl(allocator: Allocator, vm: *VM) !void { pub fn repl(allocator: Allocator, vm: *VM) !void {
var line: [1024]u8 = undefined; var line: [1024]u8 = undefined;
@ -61,6 +59,12 @@ pub fn main() !void {
var vm = VM.new(allocator); var vm = VM.new(allocator);
defer vm.free(); defer vm.free();
var env = try std.process.getEnvMap(allocator);
defer env.deinit();
if (env.get("TRACE") != null) {
vm.set_trace(true);
}
if (args.len == 1) { if (args.len == 1) {
try repl(allocator, &vm); try repl(allocator, &vm);
} else if (args.len == 2) { } else if (args.len == 2) {

View File

@ -32,6 +32,7 @@ pub const VM = struct {
// In the book, a linked list between objects is used to handle this. // In the book, a linked list between objects is used to handle this.
references: std.ArrayList(*Obj), references: std.ArrayList(*Obj),
strings: Table, strings: Table,
tracing: bool,
pub fn new(allocator: Allocator) VM { pub fn new(allocator: Allocator) VM {
return VM{ return VM{
@ -41,18 +42,30 @@ pub const VM = struct {
.stack = std.ArrayList(Value).init(allocator), .stack = std.ArrayList(Value).init(allocator),
.references = std.ArrayList(*Obj).init(allocator), .references = std.ArrayList(*Obj).init(allocator),
.strings = Table.new(allocator), .strings = Table.new(allocator),
.tracing = false,
}; };
} }
pub fn free(self: *VM) void { pub fn free(self: *VM) void {
self.stack.deinit(); self.stack.deinit();
self.strings.dump(); if (self.has_tracing()) {
self.strings.dump();
}
self.strings.deinit(); self.strings.deinit();
self.clean_references(); self.clean_references();
self.references.deinit(); self.references.deinit();
} }
pub fn set_trace(self: *VM, tracing: bool) void {
self.tracing = tracing;
}
pub fn has_tracing(self: *VM) bool {
return self.tracing;
}
pub fn interpret(self: *VM, allocator: Allocator, content: []const u8) !InterpretResult { pub fn interpret(self: *VM, allocator: Allocator, content: []const u8) !InterpretResult {
var chunk = Chunk.new(allocator); var chunk = Chunk.new(allocator);
defer chunk.deinit(); defer chunk.deinit();
@ -70,7 +83,7 @@ pub const VM = struct {
pub fn run(self: *VM) !InterpretResult { pub fn run(self: *VM) !InterpretResult {
while (true) { while (true) {
if (DEBUG_TRACE_EXECUTION) { if (self.has_tracing()) {
if (self.stack.items.len > 0) { if (self.stack.items.len > 0) {
debug.print("{s:32}", .{""}); debug.print("{s:32}", .{""});
for (self.stack.items) |item| { for (self.stack.items) |item| {