aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/jq/execute.zig
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-01-25 19:52:35 +0900
committernsfisis <nsfisis@gmail.com>2026-01-25 19:52:40 +0900
commit7fb93cc98fc7738e160bd6bc896cbafe7a1aadcc (patch)
tree9d57e69cbd84754ca623fc32f9c4f6b4be5bd076 /src/jq/execute.zig
parentf9c85486d8ecac3b5e151a4b89f142167e46356c (diff)
downloadzgjq-7fb93cc98fc7738e160bd6bc896cbafe7a1aadcc.tar.gz
zgjq-7fb93cc98fc7738e160bd6bc896cbafe7a1aadcc.tar.zst
zgjq-7fb93cc98fc7738e160bd6bc896cbafe7a1aadcc.zip
merge array_index and object_key into index
Diffstat (limited to 'src/jq/execute.zig')
-rw-r--r--src/jq/execute.zig38
1 files changed, 22 insertions, 16 deletions
diff --git a/src/jq/execute.zig b/src/jq/execute.zig
index ce8a2fc..2afb7b7 100644
--- a/src/jq/execute.zig
+++ b/src/jq/execute.zig
@@ -136,12 +136,7 @@ pub const Runtime = struct {
}
}
self.constants.deinit(self.allocator);
-
- for (self.instrs) |instr| {
- instr.deinit(self.allocator);
- }
self.allocator.free(self.instrs);
-
self.values.deinit();
self.forks.deinit(self.allocator);
}
@@ -193,12 +188,30 @@ pub const Runtime = struct {
},
.subexp_begin => try self.values.dup(),
.subexp_end => try self.values.swap(),
- .array_index => {
+ .index => {
std.debug.assert(self.values.ensureSize(2));
- const array = try self.values.popArray();
- const index: usize = @intCast(try self.values.popInteger());
- const result = if (index < array.items.len) array.items[index] else .null;
+ const base = self.values.pop();
+ const key = self.values.pop();
+
+ const result = switch (base) {
+ .array => |arr| blk: {
+ const idx: usize = @intCast(switch (key) {
+ .integer => |i| i,
+ else => return error.InvalidType,
+ });
+ break :blk if (idx < arr.items.len) arr.items[idx] else .null;
+ },
+ .object => |obj| blk: {
+ const k = switch (key) {
+ .string => |s| s,
+ else => return error.InvalidType,
+ };
+ break :blk obj.get(k) orelse .null;
+ },
+ .null => .null,
+ else => return error.InvalidType,
+ };
try self.values.push(result);
},
.add => {
@@ -300,13 +313,6 @@ pub const Runtime = struct {
const result = try compareValues(lhs, rhs, .ge);
try self.values.push(.{ .bool = result });
},
- .object_key => |key| {
- std.debug.assert(self.values.ensureSize(1));
-
- const obj = try self.values.popObject();
- const result = obj.get(key) orelse .null;
- try self.values.push(result);
- },
.@"const" => |idx| {
std.debug.assert(self.values.ensureSize(1));