diff options
Diffstat (limited to 'src/jq')
| -rw-r--r-- | src/jq/compile.zig | 22 | ||||
| -rw-r--r-- | src/jq/execute.zig | 22 | ||||
| -rw-r--r-- | src/jq/parse.zig | 33 | ||||
| -rw-r--r-- | src/jq/tokenize.zig | 33 |
4 files changed, 110 insertions, 0 deletions
diff --git a/src/jq/compile.zig b/src/jq/compile.zig new file mode 100644 index 0000000..d81f8ac --- /dev/null +++ b/src/jq/compile.zig @@ -0,0 +1,22 @@ +const std = @import("std"); +const Ast = @import("./parse.zig").Ast; + +pub const Opcode = enum { + nop, + identity, +}; + +pub const Instr = struct { + op: Opcode, +}; + +pub fn compile(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocator, ast: *const Ast) ![]Instr { + _ = compile_allocator; + var instrs = try std.array_list.Aligned(Instr, null).initCapacity(allocator, 16); + + switch (ast.kind) { + .identity => try instrs.append(allocator, .{ .op = .identity }), + } + + return instrs.toOwnedSlice(allocator); +} diff --git a/src/jq/execute.zig b/src/jq/execute.zig new file mode 100644 index 0000000..9857d46 --- /dev/null +++ b/src/jq/execute.zig @@ -0,0 +1,22 @@ +const std = @import("std"); +const jv = @import("../jv.zig"); +const Instr = @import("./compile.zig").Instr; + +pub const ExecuteError = error{ + Unimplemented, +}; + +pub fn execute(allocator: std.mem.Allocator, instrs: []const Instr, input: jv.Value) !jv.Value { + _ = allocator; + const len = instrs.len; + var pc: usize = 0; + while (pc < len) { + const cur = instrs[pc]; + _ = switch (cur.op) { + .nop => void, + .identity => return input, + }; + pc += 1; + } + return ExecuteError.Unimplemented; +} diff --git a/src/jq/parse.zig b/src/jq/parse.zig new file mode 100644 index 0000000..0554dca --- /dev/null +++ b/src/jq/parse.zig @@ -0,0 +1,33 @@ +const std = @import("std"); +const Token = @import("./tokenize.zig").Token; + +pub const ParseError = error{ + UnexpectedEnd, + InvalidQuery, +}; + +pub const AstKind = enum { + identity, +}; + +pub const Ast = struct { + kind: AstKind, +}; + +pub fn parse(allocator: std.mem.Allocator, tokens: []const Token) !*Ast { + if (tokens.len != 2) { + return ParseError.InvalidQuery; + } + const t1 = tokens[0]; + const t2 = tokens[1]; + if (t1.kind != .identity) { + return ParseError.InvalidQuery; + } + if (t2.kind != .end) { + return ParseError.UnexpectedEnd; + } + + const root = try allocator.create(Ast); + root.kind = .identity; + return root; +} diff --git a/src/jq/tokenize.zig b/src/jq/tokenize.zig new file mode 100644 index 0000000..0823ea1 --- /dev/null +++ b/src/jq/tokenize.zig @@ -0,0 +1,33 @@ +const std = @import("std"); + +pub const TokenizeError = error{ + UnexpectedEnd, +}; + +pub const TokenKind = enum { + end, + identity, +}; + +pub const Token = struct { + kind: TokenKind, +}; + +pub fn tokenize(allocator: std.mem.Allocator, query: []const u8) ![]Token { + var tokens = try std.array_list.Aligned(Token, null).initCapacity(allocator, 16); + + const len = query.len; + var i: usize = 0; + while (i < len) { + const c = query[i]; + if (c == '.') { + try tokens.append(allocator, .{ .kind = .identity }); + } else { + return TokenizeError.UnexpectedEnd; + } + i += 1; + } + + try tokens.append(allocator, .{ .kind = .end }); + return tokens.toOwnedSlice(allocator); +} |
