diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-01-25 20:27:34 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-01-25 20:27:34 +0900 |
| commit | 797ceeaa2b01070cf31c8d24ce440b8ee5feff7f (patch) | |
| tree | 0c2bf8c3517f7512645f41acbca0dbf206b7c1b8 /src/jq/compile.zig | |
| parent | 8b8bc79d647285a170aa928ff31a0989c9ef6e33 (diff) | |
| download | zgjq-797ceeaa2b01070cf31c8d24ce440b8ee5feff7f.tar.gz zgjq-797ceeaa2b01070cf31c8d24ce440b8ee5feff7f.tar.zst zgjq-797ceeaa2b01070cf31c8d24ce440b8ee5feff7f.zip | |
implement optional index access
Diffstat (limited to 'src/jq/compile.zig')
| -rw-r--r-- | src/jq/compile.zig | 10 |
1 files changed, 6 insertions, 4 deletions
diff --git a/src/jq/compile.zig b/src/jq/compile.zig index 9a62bc0..32e309a 100644 --- a/src/jq/compile.zig +++ b/src/jq/compile.zig @@ -13,6 +13,7 @@ pub const Opcode = enum { subexp_begin, subexp_end, index, + index_opt, add, sub, mul, @@ -37,6 +38,7 @@ pub const Instr = union(Opcode) { subexp_begin, subexp_end, index, + index_opt, add, sub, mul, @@ -60,16 +62,16 @@ fn compileExpr(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocato switch (ast.*) { .identity => try instrs.append(allocator, .nop), - .index => |index| { - const base_instrs = try compileExpr(allocator, compile_allocator, index.base); + .index => |idx| { + const base_instrs = try compileExpr(allocator, compile_allocator, idx.base); defer allocator.free(base_instrs); - const index_instrs = try compileExpr(allocator, compile_allocator, index.index); + const index_instrs = try compileExpr(allocator, compile_allocator, idx.index); defer allocator.free(index_instrs); try instrs.appendSlice(allocator, base_instrs); try instrs.append(allocator, .subexp_begin); try instrs.appendSlice(allocator, index_instrs); try instrs.append(allocator, .subexp_end); - try instrs.append(allocator, .index); + try instrs.append(allocator, if (idx.is_optional) .index_opt else .index); }, .literal => |idx| try instrs.append(allocator, .{ .@"const" = idx }), .binary_expr => |binary_expr| { |
