From 3e78bb75c4b431b0a4d07892aba6a64f11da2210 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 25 Jan 2026 16:14:57 +0900 Subject: implement arithmetic operations --- src/jq/compile.zig | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/jq/compile.zig') diff --git a/src/jq/compile.zig b/src/jq/compile.zig index 40459c0..bb3a832 100644 --- a/src/jq/compile.zig +++ b/src/jq/compile.zig @@ -1,6 +1,7 @@ const std = @import("std"); const jv = @import("../jv.zig"); const Ast = @import("./parse.zig").Ast; +const BinaryOp = @import("./parse.zig").BinaryOp; pub const Opcode = enum { nop, @@ -11,6 +12,10 @@ pub const Opcode = enum { subexp_end, array_index, add, + sub, + mul, + div, + mod, object_key, literal, }; @@ -26,6 +31,10 @@ pub const Instr = union(Opcode) { subexp_end, array_index, add, + sub, + mul, + div, + mod, object_key: []const u8, literal: *jv.Value, @@ -68,7 +77,15 @@ fn compileExpr(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocato try instrs.append(allocator, .subexp_begin); try instrs.appendSlice(allocator, lhs_instrs); try instrs.append(allocator, .subexp_end); - try instrs.append(allocator, .add); + const op_instr: Instr = switch (binary_expr.op) { + .add => .add, + .sub => .sub, + .mul => .mul, + .div => .div, + .mod => .mod, + else => return error.Unimplemented, + }; + try instrs.append(allocator, op_instr); }, .pipe => |pipe_expr| { const lhs_instrs = try compileExpr(allocator, compile_allocator, pipe_expr.lhs); -- cgit v1.3-1-g0d28