DzLox

:)
git clone https://git.sr.ht/~ashymad/DzLox
Log | Files | Refs | Submodules | LICENSE

commit 4d66bf9b40763f7ab7a28945c135076352f5616d
parent 543eaa45c9a95066b86c94fe159571bcd3532d7b
Author: Szymon Mikulicz <szymon.mikulicz@aptiv.com>
Date:   Tue,  6 Aug 2024 22:01:23 +0200

Some renaming

Diffstat:
Mzlox/src/compiler.zig | 30+++++++++++++++---------------
Mzlox/src/debug.zig | 68++++++++++++++++++++++++++++++++++++--------------------------------
Mzlox/src/hash.zig | 4++--
Mzlox/src/obj.zig | 50+++++++++++++++++++++++++-------------------------
Mzlox/src/vm.zig | 24++++++++++++------------
5 files changed, 90 insertions(+), 86 deletions(-)

diff --git a/zlox/src/compiler.zig b/zlox/src/compiler.zig @@ -81,7 +81,7 @@ pub fn Compiler(size: comptime_int) type { T.RIGHT_PAREN => R(null, null, P.NONE ), T.LEFT_BRACE => R(null, null, P.NONE ), T.RIGHT_BRACE => R(null, null, P.NONE ), - T.LEFT_BRACKET => R(S.map, S.index, P.CALL ), + T.LEFT_BRACKET => R(S.table, S.index, P.CALL ), T.RIGHT_BRACKET => R(null, null, P.NONE ), T.COMMA => R(null, null, P.NONE ), T.DOT => R(null, null, P.NONE ), @@ -286,7 +286,7 @@ pub fn Compiler(size: comptime_int) type { } else if (self.match(Token.NIL)) { return Value.init({}); } else if (self.match(Token.LEFT_BRACKET)) { - return self.parseLiteralMap(); + return self.parseLiteralTable(); } else { self.errorAtCurrent("Not a literal value"); return error.UnexpectedToken; @@ -305,30 +305,30 @@ pub fn Compiler(size: comptime_int) type { return Value.init(try self.objects.emplace(.String, &.{self.previous.lexeme[1 .. self.previous.lexeme.len - 1]})); } - fn parseLiteralMap(self: *Self) CompilerError!Value { - var ret = try self.objects.emplace(.Map, {}); - var retmap = ret.cast(.Map) catch unreachable; + fn parseLiteralTable(self: *Self) CompilerError!Value { + var ret = try self.objects.emplace(.Table, {}); + var tabl = ret.cast(.Table) catch unreachable; while (!self.match(Token.RIGHT_BRACKET)) { const key = try self.parseLiteralValue(); - self.consume(Token.COLON, "Expect ':' after key in map initalizer"); + self.consume(Token.COLON, "Expect ':' after key in table initalizer"); const val = try self.parseLiteralValue(); if (val.is(Value.nil)) { - self.errorAtPrevious("Nil cannot be stored in a map"); + self.errorAtPrevious("Nil cannot be stored in a table"); return error.UnexpectedToken; } - if (!try retmap.set(key, val)) { - self.errorAtPrevious("Duplicate key in map literal"); + if (!try tabl.set(key, val)) { + self.errorAtPrevious("Duplicate key in table literal"); return error.UnexpectedToken; } if (self.match(Token.RIGHT_BRACKET)) break; - self.consume(Token.COMMA, "Expect ',' after value in map initalizer"); + self.consume(Token.COMMA, "Expect ',' after value in table initalizer"); } return Value.init(ret); } - fn map(self: *Self, _: bool) void { - self.emitConstant(self.parseLiteralMap() catch |err| { + fn table(self: *Self, _: bool) void { + self.emitConstant(self.parseLiteralTable() catch |err| { self.lastError = err; return; }); @@ -613,11 +613,11 @@ pub fn Compiler(size: comptime_int) type { fn switchStatement(self: *Self) void { self.consume(Token.LEFT_PAREN, "Expect '(' after 'switch'."); - var ret = self.objects.emplace(.Map, {}) catch |err| { + var ret = self.objects.emplace(.Table, {}) catch |err| { self.lastError = err; return; }; - var retmap = ret.cast(.Map) catch unreachable; + var tabl = ret.cast(.Table) catch unreachable; self.emitConstant(Value.init(ret)); self.expression(); @@ -644,7 +644,7 @@ pub fn Compiler(size: comptime_int) type { self.errorAtCurrent("Switch body too large"); return; } - const isNew = retmap.set(case, Value.init(@as(Value.tagType(.number), @floatFromInt(distance)))) catch |err| { + const isNew = tabl.set(case, Value.init(@as(Value.tagType(.number), @floatFromInt(distance)))) catch |err| { self.lastError = err; return; }; diff --git a/zlox/src/debug.zig b/zlox/src/debug.zig @@ -22,36 +22,40 @@ pub fn disassembleInstruction(ch: chunk.Chunk, offset: usize) !usize { print("{d:4} ", .{try ch.lines.get(offset)}); } - return switch (try ch.code.get(offset)) { - @intFromEnum(OP.RETURN) => simpleInstruction("OP_RETURN", offset), - @intFromEnum(OP.NEGATE) => simpleInstruction("OP_NEGATE", offset), - @intFromEnum(OP.ADD) => simpleInstruction("OP_ADD", offset), - @intFromEnum(OP.SUBTRACT) => simpleInstruction("OP_SUBTRACT", offset), - @intFromEnum(OP.DIVIDE) => simpleInstruction("OP_DIVIDE", offset), - @intFromEnum(OP.MULTIPLY) => simpleInstruction("OP_MULTIPLY", offset), - @intFromEnum(OP.TRUE) => simpleInstruction("OP_TRUE", offset), - @intFromEnum(OP.FALSE) => simpleInstruction("OP_FALSE", offset), - @intFromEnum(OP.EQUAL) => simpleInstruction("OP_EQUAL", offset), - @intFromEnum(OP.LESS) => simpleInstruction("OP_LESS", offset), - @intFromEnum(OP.GREATER) => simpleInstruction("OP_GREATER", offset), - @intFromEnum(OP.NIL) => simpleInstruction("OP_NIL", offset), - @intFromEnum(OP.NOT) => simpleInstruction("OP_NOT", offset), - @intFromEnum(OP.CONSTANT) => try constantInstruction("OP_CONSTANT", ch, offset), - @intFromEnum(OP.DEFINE_GLOBAL) => try constantInstruction("OP_DEFINE_GLOBAL", ch, offset), - @intFromEnum(OP.GET_GLOBAL) => try constantInstruction("OP_GET_GLOBAL", ch, offset), - @intFromEnum(OP.SET_GLOBAL) => try constantInstruction("OP_SET_GLOBAL", ch, offset), - @intFromEnum(OP.PRINT) => simpleInstruction("OP_PRINT", offset), - @intFromEnum(OP.POP) => simpleInstruction("OP_POP", offset), - @intFromEnum(OP.GET_LOCAL) => try byteInstruction("OP_GET_LOCAL", ch, offset), - @intFromEnum(OP.SET_LOCAL) => try byteInstruction("OP_SET_LOCAL", ch, offset), - @intFromEnum(OP.JUMP_IF_FALSE) => try jumpInstruction("OP_JUMP_IF_FALSE", true, ch, offset), - @intFromEnum(OP.JUMP_POP) => simpleInstruction("OP_JUMP_POP", offset), - @intFromEnum(OP.JUMP) => try jumpInstruction("OP_JUMP", true, ch, offset), - @intFromEnum(OP.LOOP) => try jumpInstruction("OP_LOOP", false, ch, offset), - @intFromEnum(OP.SET_INDEX) => simpleInstruction("SET_INDEX", offset), - @intFromEnum(OP.GET_INDEX) => simpleInstruction("GET_INDEX", offset), + const op = try ch.code.get(offset); + const name = @tagName(@as(OP, @enumFromInt(op))); + + return switch (op) { + @intFromEnum(OP.RETURN) => simpleInstruction(name, offset), + @intFromEnum(OP.NEGATE) => simpleInstruction(name, offset), + @intFromEnum(OP.ADD) => simpleInstruction(name, offset), + @intFromEnum(OP.SUBTRACT) => simpleInstruction(name, offset), + @intFromEnum(OP.DIVIDE) => simpleInstruction(name, offset), + @intFromEnum(OP.MULTIPLY) => simpleInstruction(name, offset), + @intFromEnum(OP.TRUE) => simpleInstruction(name, offset), + @intFromEnum(OP.FALSE) => simpleInstruction(name, offset), + @intFromEnum(OP.EQUAL) => simpleInstruction(name, offset), + @intFromEnum(OP.LESS) => simpleInstruction(name, offset), + @intFromEnum(OP.GREATER) => simpleInstruction(name, offset), + @intFromEnum(OP.NIL) => simpleInstruction(name, offset), + @intFromEnum(OP.NOT) => simpleInstruction(name, offset), + @intFromEnum(OP.CONSTANT) => try constantInstruction(name, ch, offset), + @intFromEnum(OP.DEFINE_GLOBAL) => try constantInstruction(name, ch, offset), + @intFromEnum(OP.DEFINE_GLOBAL_CONSTANT) => try constantInstruction(name, ch, offset), + @intFromEnum(OP.GET_GLOBAL) => try constantInstruction(name, ch, offset), + @intFromEnum(OP.SET_GLOBAL) => try constantInstruction(name, ch, offset), + @intFromEnum(OP.PRINT) => simpleInstruction(name, offset), + @intFromEnum(OP.POP) => simpleInstruction(name, offset), + @intFromEnum(OP.GET_LOCAL) => try byteInstruction(name, ch, offset), + @intFromEnum(OP.SET_LOCAL) => try byteInstruction(name, ch, offset), + @intFromEnum(OP.JUMP_IF_FALSE) => try jumpInstruction(name, true, ch, offset), + @intFromEnum(OP.JUMP_POP) => simpleInstruction(name, offset), + @intFromEnum(OP.JUMP) => try jumpInstruction(name, true, ch, offset), + @intFromEnum(OP.LOOP) => try jumpInstruction(name, false, ch, offset), + @intFromEnum(OP.SET_INDEX) => simpleInstruction(name, offset), + @intFromEnum(OP.GET_INDEX) => simpleInstruction(name, offset), else => blk: { - print("Unknown opcode {}\n", .{try ch.code.get(offset)}); + print("Unknown opcode {d} {s}\n", .{op, name}); break :blk offset + 1; }, }; @@ -64,14 +68,14 @@ fn simpleInstruction(name: []const u8, offset: usize) usize { fn constantInstruction(name: []const u8, ch: chunk.Chunk, offset: usize) !usize { const constant = try ch.code.get(offset + 1); - print("{s:<16} {d:4} '", .{ name, constant }); + print("{s:<32} {d:4} '", .{ name, constant }); (try ch.constants.get(constant)).print(); print("'\n", .{}); return offset + 2; } fn byteInstruction(name: []const u8, ch: chunk.Chunk, offset: usize) !usize { - print("{s:<16} {d:4}\n", .{name, try ch.code.get(offset+1)}); + print("{s:<32} {d:4}\n", .{name, try ch.code.get(offset+1)}); return offset + 2; } @@ -80,6 +84,6 @@ fn jumpInstruction(name: []const u8, sign: bool, ch: chunk.Chunk, offset: usize) const lsb: u16 = try ch.code.get(offset + 2); const jump = (msb << 8) | lsb; - print("{s:<16} {d:4} -> {d}\n", .{name, offset, if (sign) offset + 3 + jump else offset + 3 - jump}); + print("{s:<32} {d:4} -> {d}\n", .{name, offset, if (sign) offset + 3 + jump else offset + 3 - jump}); return offset + 3; } diff --git a/zlox/src/hash.zig b/zlox/src/hash.zig @@ -35,7 +35,7 @@ pub fn hash_append(ret: u32, val: anytype) u32 { pub fn hash_t(T: type) fn (T) u32 { return switch (T) { - *Obj.Map, *const Obj.Map, *Obj.String, *const Obj.String => struct { + *Obj.Table, *const Obj.Table, *Obj.String, *const Obj.String => struct { pub fn fun(val: T) u32 { return val.hash; } @@ -44,7 +44,7 @@ pub fn hash_t(T: type) fn (T) u32 { pub fn fun(val: T) u32 { return switch (val.type) { .String => hash_append_t([]const u8)(hash(val.cast(.String) catch unreachable), "\x01"), - .Map => hash_append_t([]const u8)(hash(val.cast(.Map) catch unreachable), "\x02"), + .Table => hash_append_t([]const u8)(hash(val.cast(.Table) catch unreachable), "\x02"), }; } }.fun, diff --git a/zlox/src/obj.zig b/zlox/src/obj.zig @@ -19,10 +19,10 @@ pub const Obj = packed struct { tip: ?*Element, allocator: std.mem.Allocator, - map: String.Table, + table: String.Table, pub fn init(allocator: std.mem.Allocator) Self { - return Self{ .tip = null, .allocator = allocator, .map = String.Table.init(allocator) }; + return Self{ .tip = null, .allocator = allocator, .table = String.Table.init(allocator) }; } pub fn push(self: *Self, val: *Super) Error!void { @@ -35,7 +35,7 @@ pub const Obj = packed struct { pub fn emplace(self: *Self, comptime tp: Type, arg: tp.get().Arg) Error!*Super { var newObj = true; const obj = switch (tp) { - .String => (try String.intern(arg, &self.map, &newObj, self.allocator)).cast(), + .String => (try String.intern(arg, &self.table, &newObj, self.allocator)).cast(), else => try Super.init(tp, arg, self.allocator), }; if (newObj) try self.push(obj); @@ -55,7 +55,7 @@ pub const Obj = packed struct { while (self.pop()) |tip| { self.allocator.destroy(tip); } - self.map.deinit(); + self.table.deinit(); } }; @@ -108,7 +108,7 @@ pub const Obj = packed struct { const ArgParams = struct { len: usize, hash: u32 }; - fn map_check(m_arg: Arg, m_params: ArgParams) struct { + fn table_check(m_arg: Arg, m_params: ArgParams) struct { arg: Arg, params: ArgParams, pub fn check(self: *const @This(), k2: *const Self) bool { @@ -124,7 +124,7 @@ pub const Obj = packed struct { return false; } } { - return @TypeOf(map_check(m_arg, m_params)){ .arg = m_arg, .params = m_params }; + return @TypeOf(table_check(m_arg, m_params)){ .arg = m_arg, .params = m_params }; } fn arg_params(arg: Arg) ArgParams { @@ -137,14 +137,14 @@ pub const Obj = packed struct { return ret; } - pub fn intern(arg: Arg, map: *Table, isNewKey: *bool, allocator: std.mem.Allocator) Error!*Self { + pub fn intern(arg: Arg, tabl: *Self.Table, isNewKey: *bool, allocator: std.mem.Allocator) Error!*Self { const params = arg_params(arg); - try map.checkCapacity(); - const entry = Table.find_(map.entries, params.hash, map_check(arg, params)); - isNewKey.* = entry.* != Table.Entry.some; + try tabl.checkCapacity(); + const entry = Self.Table.find_(tabl.entries, params.hash, table_check(arg, params)); + isNewKey.* = entry.* != Self.Table.Entry.some; if (isNewKey.*) { - _ = map.set_(entry, try new(arg, params, allocator), {}); + _ = tabl.set_(entry, try new(arg, params, allocator), {}); } return entry.some.key; } @@ -159,25 +159,25 @@ pub const Obj = packed struct { } }; - pub const Map = packed struct { + pub const Table = packed struct { const Self = @This(); pub const Arg = void; const Table = table.Table(value.Value, value.Value, hash.hash_t(value.Value), value.Value.eql); obj: Super, - map: *Table, + table: *Self.Table, hash: u32, pub fn init(_: Arg, allocator: std.mem.Allocator) Error!*Self { const self: *Self = try allocator.create(Self); self.* = Self{ .obj = Super{ - .type = Super.Type.Map, + .type = Super.Type.Table, }, - .map = try allocator.create(Table), + .table = try allocator.create(Self.Table), .hash = 0, }; - self.map.* = Table.init(allocator); + self.table.* = Self.Table.init(allocator); return self; } pub fn cast(self: *Self) *Super { @@ -186,17 +186,17 @@ pub const Obj = packed struct { pub fn set(self: *Self, key: value.Value, val: value.Value) !bool { self.hash +%= hash.hash(key) +% hash.hash(val); - return self.map.set(key, val); + return self.table.set(key, val); } pub fn get(self: *Self, key: value.Value) !value.Value { - return self.map.get(key); + return self.table.get(key); } pub fn delete(self: *Self, key: value.Value) void { - const val = self.map.get(key) catch return; + const val = self.table.get(key) catch return; self.hash -%= hash.hash(key) -% hash.hash(val); - _ = self.map.delete(key); + _ = self.table.delete(key); } fn print_element(key: value.Value, val: value.Value) void { @@ -208,15 +208,15 @@ pub const Obj = packed struct { } pub fn print(self: *const Self) void { std.debug.print("[", .{}); - self.map.for_each(Self.print_element); + self.table.for_each(Self.print_element); std.debug.print("]", .{}); } pub fn eql(self: *const Self, other: *const Self) bool { - return self.map.eql(other.map, value.Value.eql); + return self.table.eql(other.table, value.Value.eql); } pub fn free(self: *const Self, allocator: std.mem.Allocator) void { - self.map.deinit(); - allocator.destroy(self.map); + self.table.deinit(); + allocator.destroy(self.table); allocator.destroy(self); } @@ -224,7 +224,7 @@ pub const Obj = packed struct { pub const Type = enum(u8) { String, - Map, + Table, pub fn get(comptime self: @This()) type { return @field(Super, @tagName(self)); diff --git a/zlox/src/vm.zig b/zlox/src/vm.zig @@ -206,11 +206,11 @@ pub const VM = struct { } }, @intFromEnum(OP.GET_INDEX) => { - const idx = self.pop(); - const map = self.pop(); - if (map.is(Obj.Type.Map) or map.is(Obj.Type.String)) { - switch(map.obj.type) { - inline else => |tp| self.push((map.obj.cast(tp) catch unreachable).get(idx) catch Value.init({})), + const key = self.pop(); + const obj = self.pop(); + if (obj.is(Obj.Type.Table) or obj.is(Obj.Type.String)) { + switch(obj.obj.type) { + inline else => |tp| self.push((obj.obj.cast(tp) catch unreachable).get(key) catch Value.init({})), } } else { self.runtimeError("Cannot index a non-map value", .{}); @@ -219,17 +219,17 @@ pub const VM = struct { }, @intFromEnum(OP.SET_INDEX) => { const val = self.pop(); - const idx = self.pop(); - const map = self.pop(); - if (!map.is(Obj.Type.Map)) { - self.runtimeError("Cannot index a non-map value", .{}); + const key = self.pop(); + const obj = self.pop(); + if (!obj.is(Obj.Type.Table)) { + self.runtimeError("Cannot index a non-table value", .{}); return InterpreterError.RuntimeError; } - var m = map.obj.cast(.Map) catch unreachable; + var m = obj.obj.cast(.Table) catch unreachable; if (val.is(Value.nil)) { - m.delete(idx); + m.delete(key); } else { - _ = try m.set(idx, val); + _ = try m.set(key, val); } self.push(val); },