diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-01-19 03:38:29 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-01-19 04:14:28 +0900 |
| commit | fa8b75121b915e1ae1eb3311fa8051e241e7ddef (patch) | |
| tree | 122d3a1d4100d71b8d48b126f05d1d81765ba1b9 /src/jq/execute.zig | |
| parent | 1513ca1a19d4758b3c166ac14040d5d3f7193218 (diff) | |
| download | zgjq-fa8b75121b915e1ae1eb3311fa8051e241e7ddef.tar.gz zgjq-fa8b75121b915e1ae1eb3311fa8051e241e7ddef.tar.zst zgjq-fa8b75121b915e1ae1eb3311fa8051e241e7ddef.zip | |
refactor compilation phases
Diffstat (limited to 'src/jq/execute.zig')
| -rw-r--r-- | src/jq/execute.zig | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/src/jq/execute.zig b/src/jq/execute.zig index 3916d5b..82b6557 100644 --- a/src/jq/execute.zig +++ b/src/jq/execute.zig @@ -1,6 +1,9 @@ const std = @import("std"); const jv = @import("../jv.zig"); +const tokenize = @import("./tokenize.zig").tokenize; +const parse = @import("./parse.zig").parse; const Instr = @import("./compile.zig").Instr; +const compile = @import("./compile.zig").compile; pub const ExecuteError = error{ Unimplemented, @@ -105,27 +108,52 @@ const ValueStack = struct { pub const Runtime = struct { const Self = @This(); + allocator: std.mem.Allocator, values: ValueStack, instrs: []const Instr, - input: jv.Value, pc: usize, - pub fn init(allocator: std.mem.Allocator, instrs: []const Instr, input: jv.Value) !Self { - var self = Self{ + pub fn init(allocator: std.mem.Allocator) !Self { + return .{ + .allocator = allocator, .values = try ValueStack.init(allocator), - .instrs = instrs, - .input = input, + .instrs = &[_]Instr{}, .pc = 0, }; - try self.values.push(input); - return self; } pub fn deinit(self: *Self) void { + for (self.instrs) |instr| { + instr.deinit(self.allocator); + } + self.allocator.free(self.instrs); + self.values.deinit(); } + pub fn compileFromReader(self: *Self, reader: *std.Io.Reader) !void { + std.debug.assert(self.instrs.len == 0); + + var compile_allocator = std.heap.ArenaAllocator.init(self.allocator); + defer compile_allocator.deinit(); + const tokens = try tokenize(compile_allocator.allocator(), reader); + const ast = try parse(self.allocator, compile_allocator.allocator(), tokens); + const instrs = try compile(self.allocator, compile_allocator.allocator(), ast); + self.instrs = instrs; + } + + pub fn compileFromSlice(self: *Self, query: []const u8) !void { + var reader = std.Io.Reader.fixed(query); + return self.compileFromReader(&reader); + } + + pub fn start(self: *Self, input: jv.Value) !void { + try self.values.push(input); + } + pub fn next(self: *Self) !?jv.Value { + std.debug.assert(self.instrs.len > 0); + while (self.pc < self.instrs.len) : (self.pc += 1) { const cur = self.instrs[self.pc]; switch (cur) { |
