aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/jq/compile.zig
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-01-19 03:17:52 +0900
committernsfisis <nsfisis@gmail.com>2026-01-19 03:17:52 +0900
commit1513ca1a19d4758b3c166ac14040d5d3f7193218 (patch)
tree58b6f32847cfa4bfbad72a59a5e6fb4283b02c66 /src/jq/compile.zig
parent13128e5e506c7b50716bd5d587135c63d7c8a278 (diff)
downloadzgjq-1513ca1a19d4758b3c166ac14040d5d3f7193218.tar.gz
zgjq-1513ca1a19d4758b3c166ac14040d5d3f7193218.tar.zst
zgjq-1513ca1a19d4758b3c166ac14040d5d3f7193218.zip
allow query to return multiple values
Diffstat (limited to 'src/jq/compile.zig')
-rw-r--r--src/jq/compile.zig23
1 files changed, 17 insertions, 6 deletions
diff --git a/src/jq/compile.zig b/src/jq/compile.zig
index f4044c9..5719f9c 100644
--- a/src/jq/compile.zig
+++ b/src/jq/compile.zig
@@ -4,6 +4,7 @@ const Ast = @import("./parse.zig").Ast;
pub const Opcode = enum {
nop,
+ ret,
subexp_begin,
subexp_end,
array_index,
@@ -14,6 +15,7 @@ pub const Opcode = enum {
pub const Instr = union(Opcode) {
nop,
+ ret,
subexp_begin,
subexp_end,
array_index,
@@ -26,13 +28,13 @@ pub const Instr = union(Opcode) {
}
};
-pub fn compile(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocator, ast: *const Ast) ![]Instr {
+fn compileExpr(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocator, ast: *const Ast) ![]Instr {
var instrs = try std.ArrayList(Instr).initCapacity(allocator, 16);
switch (ast.*) {
.identity => try instrs.append(allocator, .nop),
.array_index => |index| {
- const index_instrs = try compile(allocator, compile_allocator, index);
+ const index_instrs = try compileExpr(allocator, compile_allocator, index);
defer allocator.free(index_instrs);
try instrs.append(allocator, .subexp_begin);
try instrs.appendSlice(allocator, index_instrs);
@@ -42,9 +44,9 @@ pub fn compile(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocato
.object_key => |key| try instrs.append(allocator, .{ .object_key = key }),
.literal => |value| try instrs.append(allocator, .{ .literal = value }),
.binary_expr => |binary_expr| {
- const rhs_instrs = try compile(allocator, compile_allocator, binary_expr.rhs);
+ const rhs_instrs = try compileExpr(allocator, compile_allocator, binary_expr.rhs);
defer allocator.free(rhs_instrs);
- const lhs_instrs = try compile(allocator, compile_allocator, binary_expr.lhs);
+ const lhs_instrs = try compileExpr(allocator, compile_allocator, binary_expr.lhs);
defer allocator.free(lhs_instrs);
try instrs.append(allocator, .subexp_begin);
try instrs.appendSlice(allocator, rhs_instrs);
@@ -55,9 +57,9 @@ pub fn compile(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocato
try instrs.append(allocator, .add);
},
.pipe => |pipe_expr| {
- const lhs_instrs = try compile(allocator, compile_allocator, pipe_expr.lhs);
+ const lhs_instrs = try compileExpr(allocator, compile_allocator, pipe_expr.lhs);
defer allocator.free(lhs_instrs);
- const rhs_instrs = try compile(allocator, compile_allocator, pipe_expr.rhs);
+ const rhs_instrs = try compileExpr(allocator, compile_allocator, pipe_expr.rhs);
defer allocator.free(rhs_instrs);
try instrs.appendSlice(allocator, lhs_instrs);
try instrs.appendSlice(allocator, rhs_instrs);
@@ -66,3 +68,12 @@ pub fn compile(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocato
return instrs.toOwnedSlice(allocator);
}
+
+pub fn compile(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocator, ast: *const Ast) ![]Instr {
+ var instrs = try std.ArrayList(Instr).initCapacity(allocator, 16);
+ const expr_instrs = try compileExpr(allocator, compile_allocator, ast);
+ defer allocator.free(expr_instrs);
+ try instrs.appendSlice(allocator, expr_instrs);
+ try instrs.append(allocator, .ret);
+ return instrs.toOwnedSlice(allocator);
+}