aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/jq/compile.zig
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-01-25 16:14:57 +0900
committernsfisis <nsfisis@gmail.com>2026-01-25 16:14:57 +0900
commit3e78bb75c4b431b0a4d07892aba6a64f11da2210 (patch)
treeb7a8d2a5412c75cd3ace1032db38e6535d3d0a1d /src/jq/compile.zig
parent448c28d596184f39783f8a7813ae73901fce240a (diff)
downloadzgjq-3e78bb75c4b431b0a4d07892aba6a64f11da2210.tar.gz
zgjq-3e78bb75c4b431b0a4d07892aba6a64f11da2210.tar.zst
zgjq-3e78bb75c4b431b0a4d07892aba6a64f11da2210.zip
implement arithmetic operations
Diffstat (limited to 'src/jq/compile.zig')
-rw-r--r--src/jq/compile.zig19
1 files changed, 18 insertions, 1 deletions
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);