From 6739144edaf34d10e0c0901231b196f377007934 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 17 Jan 2026 23:31:40 +0900 Subject: implement addition --- src/jq/execute.zig | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src/jq/execute.zig') diff --git a/src/jq/execute.zig b/src/jq/execute.zig index 8e9bbb0..29025b4 100644 --- a/src/jq/execute.zig +++ b/src/jq/execute.zig @@ -74,6 +74,18 @@ const ValueStack = struct { else => error.InvalidType, }; } + + pub fn dup(self: *Self) !void { + const top = self.stack.items[self.stack.items.len - 1]; + try self.push(top); + } + + pub fn swap(self: *Self) void { + const len = self.stack.items.len; + const tmp = self.stack.items[len - 1]; + self.stack.items[len - 1] = self.stack.items[len - 2]; + self.stack.items[len - 2] = tmp; + } }; pub fn execute(allocator: std.mem.Allocator, instrs: []const Instr, input: jv.Value) !jv.Value { @@ -89,17 +101,27 @@ pub fn execute(allocator: std.mem.Allocator, instrs: []const Instr, input: jv.Va switch (cur) { .nop => {}, .array_index => { - const index: usize = @intCast(try value_stack.popInteger()); const array = try value_stack.popArray(); + const index: usize = @intCast(try value_stack.popInteger()); const result = if (index < array.items.len) array.items[index] else .null; try value_stack.push(result); }, + .add => { + _ = try value_stack.pop(); + const lhs = try value_stack.popInteger(); + const rhs = try value_stack.popInteger(); + const result = lhs + rhs; + try value_stack.push(.{ .integer = result }); + }, + .subexp_begin => try value_stack.dup(), + .subexp_end => value_stack.swap(), .object_key => |key| { const obj = try value_stack.popObject(); const result = obj.get(key) orelse .null; try value_stack.push(result); }, .literal => |value| { + _ = try value_stack.pop(); try value_stack.push(value.*); }, } -- cgit v1.3-1-g0d28