diff options
Diffstat (limited to 'src/root.zig')
| -rw-r--r-- | src/root.zig | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/src/root.zig b/src/root.zig index b89b54f..9c22deb 100644 --- a/src/root.zig +++ b/src/root.zig @@ -14,15 +14,36 @@ pub fn run(allocator: std.mem.Allocator, input: []const u8, query: []const u8) ! const parsed = try jv.parse(allocator, input); defer parsed.deinit(); const json = parsed.value; - const result = try jq.execute(allocator, instrs, json); + + var runtime = try jq.Runtime.init(allocator, instrs, json); + defer runtime.deinit(); + const result = try runtime.next() orelse return error.NoResult; const output = try jv.stringify(allocator, result); return output; } fn testRun(expected: []const u8, allocator: std.mem.Allocator, input: []const u8, query: []const u8) !void { - const result = try run(allocator, input, query); + var compile_allocator = std.heap.ArenaAllocator.init(allocator); + defer compile_allocator.deinit(); + var reader = std.Io.Reader.fixed(query); + const tokens = try jq.tokenize(compile_allocator.allocator(), &reader); + const ast = try jq.parse(compile_allocator.allocator(), tokens); + const instrs = try jq.compile(allocator, compile_allocator.allocator(), ast); + defer allocator.free(instrs); + + const parsed = try jv.parse(allocator, input); + defer parsed.deinit(); + const json = parsed.value; + + var runtime = try jq.Runtime.init(allocator, instrs, json); + defer runtime.deinit(); + + const result_value = try runtime.next() orelse return error.NoResult; + const result = try jv.stringify(allocator, result_value); defer allocator.free(result); try std.testing.expectEqualStrings(expected, result); + + try std.testing.expectEqual(null, try runtime.next()); } test "identity filter" { |
