commit c2c305acb5bbde4c09870c379a7c83c773f50d1a
parent 77d90681b627f3334f30564e91d304ccc786014f
Author: Szymon Mikulicz <szymon.mikulicz@aptiv.com>
Date: Mon, 22 Apr 2024 23:24:52 +0200
-d and not copying constants
Diffstat:
4 files changed, 22 insertions(+), 9 deletions(-)
diff --git a/zlox/src/array.zig b/zlox/src/array.zig
@@ -18,6 +18,10 @@ pub fn Array(comptime T: type, comptime S: type, comptime size: S) type {
self.len += 1;
}
+ pub fn slice(self: *const @This()) []const T {
+ return self.data[0..self.len];
+ }
+
pub fn get(self: *const @This(), idx: S) !T {
if (idx >= self.len)
return error.IndexOutOfBounds;
diff --git a/zlox/src/chunk.zig b/zlox/src/chunk.zig
@@ -47,6 +47,11 @@ pub const Chunk = struct {
}
pub fn addConstant(self: *@This(), val: value.Value) Error!u8 {
+ for (self.constants.slice(), 0..) |el, i| {
+ if (el.eql(val)) {
+ return @intCast(i);
+ }
+ }
try self.constants.add(val);
return self.constants.len - 1;
}
diff --git a/zlox/src/main.zig b/zlox/src/main.zig
@@ -15,9 +15,13 @@ pub fn main() anyerror!u8 {
defer std.process.argsFree(allocator, args);
if (args.len == 1) {
- try repl(allocator);
+ try repl(allocator, false);
} else if (args.len == 2) {
- try runFile(allocator, args[1]);
+ if (std.mem.eql(u8, args[1], "-d")) {
+ try repl(allocator, true);
+ } else {
+ try runFile(allocator, args[1]);
+ }
} else {
std.debug.print("Usage: {s} [path]\n", .{args[0]});
return 64;
@@ -34,7 +38,7 @@ pub fn runFile(allocator: std.mem.Allocator, path: []const u8) anyerror!void {
defer allocator.free(text);
}
-pub fn repl(allocator: std.mem.Allocator) anyerror!void {
+pub fn repl(allocator: std.mem.Allocator, dbg: bool) anyerror!void {
var VM = vm.VM.init(allocator);
defer VM.deinit();
@@ -42,7 +46,7 @@ pub fn repl(allocator: std.mem.Allocator) anyerror!void {
while (Linenoise.linenoise("lox> ")) |line| {
defer Linenoise.linenoiseFree(line);
- VM.interpret(std.mem.span(line)) catch |err| {
+ VM.interpret(std.mem.span(line), dbg) catch |err| {
std.debug.print("Error: {}\n", .{err});
};
_ = Linenoise.linenoiseHistoryAdd(line);
diff --git a/zlox/src/vm.zig b/zlox/src/vm.zig
@@ -22,11 +22,11 @@ pub const VM = struct {
return @This(){ .globals = Globals.init(allocator), .objects = Obj.List.init(allocator), .allocator = allocator };
}
- pub fn interpret(self: *@This(), source: []const u8) InterpreterError!void {
+ pub fn interpret(self: *@This(), source: []const u8, dbg: bool) InterpreterError!void {
var chunk = try compiler.Compiler.compile(source, &self.objects, self.allocator);
defer chunk.deinit();
- try Interpreter(256).run(self, &chunk);
+ try Interpreter(256).run(self, &chunk, dbg);
}
fn Interpreter(size: comptime_int) type {
@@ -37,10 +37,10 @@ pub const VM = struct {
stack: [size]Value,
vm: *VM,
- pub fn run(vm: *VM, chunk: *Chunk) InterpreterError!void {
+ pub fn run(vm: *VM, chunk: *Chunk, dbg: bool) InterpreterError!void {
var self = @This(){ .ip = chunk.code.data.ptr, .chunk = chunk, .stack = [_]Value{Value.init({})} ** size, .stackTop = undefined, .vm = vm };
self.stackTop = &self.stack;
- try self.execute(false);
+ try self.execute(dbg);
}
fn read_byte(self: *@This()) u8 {
@@ -86,7 +86,7 @@ pub const VM = struct {
return @intFromPtr(self.ip) - @intFromPtr(self.chunk.code.data.ptr);
}
- fn execute(self: *@This(), comptime dbg: bool) !void {
+ fn execute(self: *@This(), dbg: bool) !void {
while (true) {
if (dbg) {
std.debug.print(" ", .{});