From 94877ef2f689617706a646cf0abda7dc25ceba88 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 17 Jan 2026 14:26:08 +0900 Subject: implement array index filter --- src/jq/compile.zig | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'src/jq/compile.zig') diff --git a/src/jq/compile.zig b/src/jq/compile.zig index d81f8ac..a28c10a 100644 --- a/src/jq/compile.zig +++ b/src/jq/compile.zig @@ -1,21 +1,35 @@ const std = @import("std"); +const jv = @import("../jv.zig"); const Ast = @import("./parse.zig").Ast; pub const Opcode = enum { nop, - identity, + array_index, + literal, }; -pub const Instr = struct { - op: Opcode, +pub const Instr = union(Opcode) { + nop, + array_index, + literal: *jv.Value, + + pub fn op(self: @This()) Opcode { + return self; + } }; pub fn compile(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocator, ast: *const Ast) ![]Instr { - _ = compile_allocator; var instrs = try std.array_list.Aligned(Instr, null).initCapacity(allocator, 16); - switch (ast.kind) { - .identity => try instrs.append(allocator, .{ .op = .identity }), + switch (ast.*) { + .identity => try instrs.append(allocator, .nop), + .array_index => |index| { + const index_instrs = try compile(allocator, compile_allocator, index); + defer allocator.free(index_instrs); + try instrs.appendSlice(allocator, index_instrs); + try instrs.append(allocator, .array_index); + }, + .literal => |value| try instrs.append(allocator, .{ .literal = value }), } return instrs.toOwnedSlice(allocator); -- cgit v1.3-1-g0d28