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/compile.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/compile.zig')
| -rw-r--r-- | src/jq/compile.zig | 26 |
1 files changed, 20 insertions, 6 deletions
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); |
