Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Depend on zig-regex + working test 014
Browse files Browse the repository at this point in the history
malcolmstill committed Mar 28, 2024
1 parent b029049 commit 5ca2107
Showing 3 changed files with 17 additions and 3 deletions.
3 changes: 3 additions & 0 deletions biscuit-datalog/build.zig
Original file line number Diff line number Diff line change
@@ -17,12 +17,14 @@ pub fn build(b: *std.Build) void {

const schema = b.dependency("biscuit_schema", .{ .target = target, .optimize = optimize });
const format = b.dependency("biscuit_format", .{ .target = target, .optimize = optimize });
const regex = b.dependency("regex", .{ .target = target, .optimize = optimize });

_ = b.addModule("biscuit-datalog", .{
.root_source_file = .{ .path = "src/main.zig" },
.imports = &.{
.{ .name = "biscuit-schema", .module = schema.module("biscuit-schema") },
.{ .name = "biscuit-format", .module = format.module("biscuit-format") },
.{ .name = "regex", .module = regex.module("regex") },
},
});

@@ -35,6 +37,7 @@ pub fn build(b: *std.Build) void {
});
lib_unit_tests.root_module.addImport("biscuit-schema", schema.module("biscuit-schema"));
lib_unit_tests.root_module.addImport("biscuit-format", format.module("biscuit-format"));
lib_unit_tests.root_module.addImport("regex", regex.module("regex"));

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

4 changes: 4 additions & 0 deletions biscuit-datalog/build.zig.zon
Original file line number Diff line number Diff line change
@@ -40,6 +40,10 @@
//},
.biscuit_schema = .{ .path = "../biscuit-schema" },
.biscuit_format = .{ .path = "../biscuit-format" },
.regex = .{
.url = "https://github.com/tiehuis/zig-regex/archive/825181369f30bc0d0554d9d330d34db4162b91e5.tar.gz",
.hash = "122090c3c6b4a8745fb22e721ae4e2e55b73347769696fd84170c660fe16c1f062e6",
},
},

// Specifies the set of files and directories that are included in this package.
13 changes: 10 additions & 3 deletions biscuit-datalog/src/expression.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const mem = std.mem;
const schema = @import("biscuit-schema");
const Regex = @import("regex").Regex;
const Term = @import("term.zig").Term;
const SymbolTable = @import("symbol_table.zig").SymbolTable;

@@ -86,7 +87,7 @@ pub const Expression = struct {
const right = stack.popOrNull() orelse return error.StackUnderflow;
const left = stack.popOrNull() orelse return error.StackUnderflow;

const result = try binary_op.evaluate(left, right, symbols);
const result = try binary_op.evaluate(allocator, left, right, symbols);

try stack.append(result);
},
@@ -206,7 +207,7 @@ const Binary = enum {
bitwise_xor,
not_equal,

pub fn evaluate(expr: Binary, left: Term, right: Term, symbols: SymbolTable) !Term {
pub fn evaluate(expr: Binary, allocator: std.mem.Allocator, left: Term, right: Term, symbols: SymbolTable) !Term {
// Integer operands
if (left == .integer and right == .integer) {
const i = left.integer;
@@ -235,7 +236,7 @@ const Binary = enum {
return switch (expr) {
.prefix => .{ .bool = mem.startsWith(u8, sl, sr) },
.suffix => .{ .bool = mem.endsWith(u8, sl, sr) },
.regex => return error.RegexUnimplemented,
.regex => .{ .bool = try match(allocator, sr, sl) },
.contains => .{ .bool = mem.containsAtLeast(u8, sl, 1, sr) },
.add => return error.StringConcatNotImplemented,
.equal => .{ .bool = mem.eql(u8, sl, sr) },
@@ -292,6 +293,12 @@ const Binary = enum {
}
};

fn match(allocator: std.mem.Allocator, regex: []const u8, string: []const u8) !bool {
var re = try Regex.compile(allocator, regex);

return re.match(string);
}

test {
const testing = std.testing;

0 comments on commit 5ca2107

Please sign in to comment.