diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-01-25 19:04:29 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-01-25 19:04:29 +0900 |
| commit | 89c7c1a3004f1b869a2bcb7a9b055065765611df (patch) | |
| tree | 301cfcd9cedd83c4bc23fe426893106903bbc64d /src/jq/execute.zig | |
| parent | afb238972816b560b082229ac20cdab64bca8aca (diff) | |
| download | zgjq-89c7c1a3004f1b869a2bcb7a9b055065765611df.tar.gz zgjq-89c7c1a3004f1b869a2bcb7a9b055065765611df.tar.zst zgjq-89c7c1a3004f1b869a2bcb7a9b055065765611df.zip | |
create constant table instead of embedding constants in instructions/ASTs
Diffstat (limited to 'src/jq/execute.zig')
| -rw-r--r-- | src/jq/execute.zig | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/jq/execute.zig b/src/jq/execute.zig index 380e5be..ce8a2fc 100644 --- a/src/jq/execute.zig +++ b/src/jq/execute.zig @@ -113,6 +113,7 @@ pub const Runtime = struct { forks: std.ArrayList(usize), instrs: []const Instr, pc: usize, + constants: std.ArrayList(jv.Value), pub fn init(allocator: std.mem.Allocator) !Self { return .{ @@ -121,10 +122,21 @@ pub const Runtime = struct { .forks = .{}, .instrs = &[_]Instr{}, .pc = 0, + .constants = .{}, }; } pub fn deinit(self: *Self) void { + for (self.constants.items) |*value| { + switch (value.*) { + .string => |s| self.allocator.free(s), + .array => |*a| a.deinit(), + .object => |*o| o.deinit(), + else => {}, + } + } + self.constants.deinit(self.allocator); + for (self.instrs) |instr| { instr.deinit(self.allocator); } @@ -140,7 +152,7 @@ pub const Runtime = struct { var compile_allocator = std.heap.ArenaAllocator.init(self.allocator); defer compile_allocator.deinit(); const tokens = try tokenize(compile_allocator.allocator(), reader); - const ast = try parse(self.allocator, compile_allocator.allocator(), tokens); + const ast = try parse(self.allocator, compile_allocator.allocator(), tokens, &self.constants); const instrs = try compile(self.allocator, compile_allocator.allocator(), ast); self.instrs = instrs; // std.debug.print("BEGIN\n", .{}); @@ -295,11 +307,11 @@ pub const Runtime = struct { const result = obj.get(key) orelse .null; try self.values.push(result); }, - .literal => |value| { + .@"const" => |idx| { std.debug.assert(self.values.ensureSize(1)); _ = self.values.pop(); - try self.values.push(value.*); + try self.values.push(self.constants.items[@intFromEnum(idx)]); }, } } |
