Skip to content

Commit

Permalink
Refactor state serialization to json
Browse files Browse the repository at this point in the history
  • Loading branch information
bgk- committed Apr 1, 2024
1 parent 6df9035 commit 49d8833
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 234 deletions.
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.{
.name = "topiary",
.version = "0.10.2",
.version = "0.11.0",
.paths = .{""},
.dependencies = .{
},
Expand Down
13 changes: 2 additions & 11 deletions src/compiler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,6 @@ pub const Compiler = struct {
obj.* = .{
.data = .{
.@"enum" = .{
.allocator = self.allocator,
.name = try self.allocator.dupe(u8, e.name),
.values = try names.toOwnedSlice(),
},
Expand Down Expand Up @@ -507,11 +506,7 @@ pub const Compiler = struct {
},
.choice => |c| {
for (c.tags) |tag| {
const obj = try self.allocator.create(Value.Obj);
obj.* = .{ .data = .{ .string = try self.allocator.dupe(u8, tag) } };
const i = try self.addConstant(.{ .obj = obj });
try self.writeOp(.constant, token);
_ = try self.writeInt(OpCode.Size(.constant), i, token);
try self.getOrSetIdentifierConstant(tag, token);
}
const name = c.name orelse &c.id;
try self.visit_tree.list.append(name);
Expand Down Expand Up @@ -579,11 +574,7 @@ pub const Compiler = struct {
},
.dialogue => |d| {
for (d.tags) |tag| {
const obj = try self.allocator.create(Value.Obj);
obj.* = .{ .data = .{ .string = try self.allocator.dupe(u8, tag) } };
const i = try self.addConstant(.{ .obj = obj });
try self.writeOp(.constant, token);
_ = try self.writeInt(OpCode.Size(.constant), i, token);
try self.getOrSetIdentifierConstant(tag, token);
}

try self.compileExpression(d.content);
Expand Down
13 changes: 10 additions & 3 deletions src/enum.zig
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
const std = @import("std");
const Value = @import("values.zig").Value;

pub const Enum = struct {
allocator: std.mem.Allocator,
name: []const u8,
values: [][]const u8,

pub const Value = struct {
pub fn init(name: []const u8, values: [][]const u8) Enum {
return .{
.name = name,
.values = values,
};
}

pub const Val = struct {
index: u8,
base: *const Enum,
base: *const Value,
};
};
5 changes: 3 additions & 2 deletions src/export.zig
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,10 @@ test "Create and Destroy Vm" {

const vm_ptr = createVm(buf.ptr, buf.len, @intFromPtr(on_dialogue), @intFromPtr(on_choices));
const vm: *Vm = @ptrFromInt(vm_ptr);

defer destroyVm(vm_ptr);
vm.bytecode.print(std.debug);
defer vm.bytecode.free(alloc);
std.debug.print("\n=====\n", .{});
const val_name = "value";
subscribe(
Expand Down Expand Up @@ -612,6 +615,4 @@ test "Create and Destroy Vm" {
}
destroyValue(&map_value);
}
vm.bytecode.free(alloc);
destroyVm(vm_ptr);
}
1 change: 1 addition & 0 deletions src/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ pub const Parser = struct {
self.next();
var expr = try self.expression(.lowest);
if (expr.type == .function) {
// used for recursive calls
expr.type.function.name = name;
}
return .{
Expand Down
Loading

0 comments on commit 49d8833

Please sign in to comment.