aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/jq/compile.zig
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-01-25 00:26:55 +0900
committernsfisis <nsfisis@gmail.com>2026-01-25 00:29:49 +0900
commit0b51c7019d55995ba53f361521163007941c844b (patch)
tree8beffbb50a7d1e85f53c5cdc532e62e5967c405a /src/jq/compile.zig
parentfa8b75121b915e1ae1eb3311fa8051e241e7ddef (diff)
downloadzgjq-0b51c7019d55995ba53f361521163007941c844b.tar.gz
zgjq-0b51c7019d55995ba53f361521163007941c844b.tar.zst
zgjq-0b51c7019d55995ba53f361521163007941c844b.zip
implement comma operator
Diffstat (limited to 'src/jq/compile.zig')
-rw-r--r--src/jq/compile.zig25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/jq/compile.zig b/src/jq/compile.zig
index 54563de..40459c0 100644
--- a/src/jq/compile.zig
+++ b/src/jq/compile.zig
@@ -5,6 +5,8 @@ const Ast = @import("./parse.zig").Ast;
pub const Opcode = enum {
nop,
ret,
+ jump,
+ fork,
subexp_begin,
subexp_end,
array_index,
@@ -18,6 +20,8 @@ pub const Instr = union(Opcode) {
nop,
ret,
+ jump: usize,
+ fork: usize,
subexp_begin,
subexp_end,
array_index,
@@ -74,6 +78,27 @@ fn compileExpr(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocato
try instrs.appendSlice(allocator, lhs_instrs);
try instrs.appendSlice(allocator, rhs_instrs);
},
+ .comma => |comma_expr| {
+ // FORK l1
+ // <lhs>
+ // JUMP l2
+ // l1: <rhs>
+ // l2:
+ const lhs_instrs = try compileExpr(allocator, compile_allocator, comma_expr.lhs);
+ defer allocator.free(lhs_instrs);
+ const rhs_instrs = try compileExpr(allocator, compile_allocator, comma_expr.rhs);
+ defer allocator.free(rhs_instrs);
+ const fork_index = instrs.items.len;
+ try instrs.append(allocator, .{ .fork = 0 });
+ try instrs.appendSlice(allocator, lhs_instrs);
+ const jump_index = instrs.items.len;
+ try instrs.append(allocator, .{ .jump = 0 });
+ const l1 = instrs.items.len;
+ try instrs.appendSlice(allocator, rhs_instrs);
+ const l2 = instrs.items.len;
+ instrs.items[fork_index] = .{ .fork = l1 - fork_index };
+ instrs.items[jump_index] = .{ .jump = l2 - jump_index };
+ },
}
return instrs.toOwnedSlice(allocator);