diff options
Diffstat (limited to 'src/jq/compile.zig')
| -rw-r--r-- | src/jq/compile.zig | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/jq/compile.zig b/src/jq/compile.zig index 0c30a3d..e3937c6 100644 --- a/src/jq/compile.zig +++ b/src/jq/compile.zig @@ -4,14 +4,20 @@ const Ast = @import("./parse.zig").Ast; pub const Opcode = enum { nop, + subexp_begin, + subexp_end, array_index, + add, object_key, literal, }; pub const Instr = union(Opcode) { nop, + subexp_begin, + subexp_end, array_index, + add, object_key: []const u8, literal: *jv.Value, @@ -28,11 +34,26 @@ pub fn compile(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocato .array_index => |index| { const index_instrs = try compile(allocator, compile_allocator, index); defer allocator.free(index_instrs); + try instrs.append(allocator, .subexp_begin); try instrs.appendSlice(allocator, index_instrs); + try instrs.append(allocator, .subexp_end); try instrs.append(allocator, .array_index); }, .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); + defer allocator.free(rhs_instrs); + const lhs_instrs = try compile(allocator, compile_allocator, binary_expr.lhs); + defer allocator.free(lhs_instrs); + try instrs.append(allocator, .subexp_begin); + try instrs.appendSlice(allocator, rhs_instrs); + try instrs.append(allocator, .subexp_end); + try instrs.append(allocator, .subexp_begin); + try instrs.appendSlice(allocator, lhs_instrs); + try instrs.append(allocator, .subexp_end); + try instrs.append(allocator, .add); + }, } return instrs.toOwnedSlice(allocator); |
