From 89c7c1a3004f1b869a2bcb7a9b055065765611df Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 25 Jan 2026 19:04:29 +0900 Subject: create constant table instead of embedding constants in instructions/ASTs --- src/jq/compile.zig | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) (limited to 'src/jq/compile.zig') diff --git a/src/jq/compile.zig b/src/jq/compile.zig index 78512b9..5ed1806 100644 --- a/src/jq/compile.zig +++ b/src/jq/compile.zig @@ -3,6 +3,8 @@ const jv = @import("../jv.zig"); const Ast = @import("./parse.zig").Ast; const BinaryOp = @import("./parse.zig").BinaryOp; +pub const ConstIndex = enum(u32) { _ }; + pub const Opcode = enum { nop, ret, @@ -23,7 +25,7 @@ pub const Opcode = enum { le, ge, object_key, - literal, + @"const", }; pub const Instr = union(Opcode) { @@ -48,7 +50,7 @@ pub const Instr = union(Opcode) { le, ge, object_key: []const u8, - literal: *jv.Value, + @"const": ConstIndex, pub fn op(self: Self) Opcode { return self; @@ -57,12 +59,6 @@ pub const Instr = union(Opcode) { pub fn deinit(self: Self, allocator: std.mem.Allocator) void { switch (self) { .object_key => |key| allocator.free(key), - .literal => |value| { - if (value.* == .string) { - allocator.free(value.string); - } - allocator.destroy(value); - }, else => {}, } } @@ -85,7 +81,7 @@ fn compileExpr(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocato try instrs.append(allocator, .array_index); }, .object_key => |key| try instrs.append(allocator, .{ .object_key = key }), - .literal => |value| try instrs.append(allocator, .{ .literal = value }), + .literal => |idx| try instrs.append(allocator, .{ .@"const" = idx }), .binary_expr => |binary_expr| { const rhs_instrs = try compileExpr(allocator, compile_allocator, binary_expr.rhs); defer allocator.free(rhs_instrs); -- cgit v1.3-1-g0d28