diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-01-17 23:31:40 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-01-18 00:37:54 +0900 |
| commit | 6739144edaf34d10e0c0901231b196f377007934 (patch) | |
| tree | 015d78a96fa66390fe332226a1f27e7a55bda52e /src/jq/compile.zig | |
| parent | 0295edea4c2d5e6f2e0a6761483cc9e2c64544f8 (diff) | |
| download | zgjq-6739144edaf34d10e0c0901231b196f377007934.tar.gz zgjq-6739144edaf34d10e0c0901231b196f377007934.tar.zst zgjq-6739144edaf34d10e0c0901231b196f377007934.zip | |
implement addition
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); |
