diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-01-17 14:26:08 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-01-17 14:26:08 +0900 |
| commit | 94877ef2f689617706a646cf0abda7dc25ceba88 (patch) | |
| tree | 8e9ae94cc09ddb25724b4cc33a900cbc17d91541 /src/jq/execute.zig | |
| parent | b2c37cbc1ce7f6638d7b57376719f2e0ba2d53ca (diff) | |
| download | zgjq-94877ef2f689617706a646cf0abda7dc25ceba88.tar.gz zgjq-94877ef2f689617706a646cf0abda7dc25ceba88.tar.zst zgjq-94877ef2f689617706a646cf0abda7dc25ceba88.zip | |
implement array index filter
Diffstat (limited to 'src/jq/execute.zig')
| -rw-r--r-- | src/jq/execute.zig | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/src/jq/execute.zig b/src/jq/execute.zig index 9857d46..dfad6c4 100644 --- a/src/jq/execute.zig +++ b/src/jq/execute.zig @@ -4,19 +4,44 @@ const Instr = @import("./compile.zig").Instr; pub const ExecuteError = error{ Unimplemented, + InvalidType, + InternalError, }; pub fn execute(allocator: std.mem.Allocator, instrs: []const Instr, input: jv.Value) !jv.Value { - _ = allocator; + var value_stack = try std.array_list.Aligned(jv.Value, null).initCapacity(allocator, 16); + defer value_stack.deinit(allocator); + + try value_stack.append(allocator, input); + const len = instrs.len; var pc: usize = 0; while (pc < len) { const cur = instrs[pc]; - _ = switch (cur.op) { - .nop => void, - .identity => return input, - }; + switch (cur) { + .nop => {}, + .array_index => { + const v1 = value_stack.pop() orelse return error.InternalError; + const v1_integer = switch (v1) { + .integer => |integer| integer, + else => return error.InvalidType, + }; + const v2 = value_stack.pop() orelse return error.InternalError; + const v2_array = switch (v2) { + .array => |array| array, + else => return error.InvalidType, + }; + const index: usize = @intCast(v1_integer); + const result = if (index < v2_array.items.len) v2_array.items[index] else .null; + try value_stack.append(allocator, result); + }, + .literal => |value| { + try value_stack.append(allocator, value.*); + }, + } pc += 1; } - return ExecuteError.Unimplemented; + + const result = value_stack.pop() orelse return error.InternalError; + return result; } |
