Skip to content

Commit

Permalink
Test 010 passes
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmstill committed Mar 23, 2024
1 parent e7dc323 commit 7e07bf5
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 33 deletions.
31 changes: 29 additions & 2 deletions biscuit-builder/src/check.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,34 @@ pub const Check = struct {
kind: datalog.Check.Kind,
queries: std.ArrayList(Rule),

pub fn deinit(_: Check) void {
//
pub fn deinit(check: Check) void {
for (check.queries.items) |query| {
query.deinit();
}

check.queries.deinit();
}

pub fn convert(check: Check, allocator: std.mem.Allocator, symbols: *datalog.SymbolTable) !datalog.Check {
var queries = std.ArrayList(datalog.Rule).init(allocator);

for (check.queries.items) |query| {
try queries.append(try query.convert(allocator, symbols));
}

return .{ .kind = check.kind, .queries = queries };
}

pub fn format(check: Check, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
try writer.print("check ", .{});

switch (check.kind) {
.one => try writer.print("if", .{}),
.all => try writer.print("all", .{}),
}

for (check.queries.items) |query| {
try writer.print(" {any}", .{query});
}
}
};
2 changes: 1 addition & 1 deletion biscuit-builder/src/expression.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Term = @import("term.zig").Term;

pub const Expression = struct {
/// convert to datalog fact
pub fn convert(_: Expression) datalog.Expression {
pub fn convert(_: Expression, _: std.mem.Allocator, _: *datalog.SymbolTable) !datalog.Expression {
unreachable;
}
};
35 changes: 33 additions & 2 deletions biscuit-builder/src/rule.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,38 @@ pub const Rule = struct {
}

/// convert to datalog predicate
pub fn convert(_: Rule) datalog.Rule {
unreachable;
pub fn convert(rule: Rule, allocator: std.mem.Allocator, symbols: *datalog.SymbolTable) !datalog.Rule {
const head = try rule.head.convert(allocator, symbols);

var body = std.ArrayList(datalog.Predicate).init(allocator);
var expressions = std.ArrayList(datalog.Expression).init(allocator);
var scopes = std.ArrayList(datalog.Scope).init(allocator);

for (rule.body.items) |predicate| {
try body.append(try predicate.convert(allocator, symbols));
}

for (rule.expressions.items) |expression| {
try expressions.append(try expression.convert(allocator, symbols));
}

for (rule.scopes.items) |scope| {
try scopes.append(try scope.convert(allocator, symbols));
}

return .{
.head = head,
.body = body,
.expressions = expressions,
.scopes = scopes,
};
}

pub fn format(rule: Rule, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
try writer.print("{any} <- ", .{rule.head});
for (rule.body.items, 0..) |*predicate, i| {
try writer.print("{any}", .{predicate.*});
if (i < rule.body.items.len - 1) try writer.print(", ", .{});
}
}
};
2 changes: 1 addition & 1 deletion biscuit-builder/src/scope.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub const Scope = struct {
public_key: Ed25519.PublicKey,

/// convert to datalog fact
pub fn convert(_: Scope) datalog.Scope {
pub fn convert(_: Scope, _: std.mem.Allocator, _: *datalog.SymbolTable) !datalog.Scope {
unreachable;
}
};
11 changes: 11 additions & 0 deletions biscuit-builder/src/term.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,31 @@ const std = @import("std");
const datalog = @import("biscuit-datalog");

const TermTag = enum(u8) {
variable,
string,
bool,
};

pub const Term = union(TermTag) {
variable: []const u8,
string: []const u8,
bool: bool,

pub fn deinit(_: Term) void {}

pub fn convert(term: Term, _: std.mem.Allocator, symbols: *datalog.SymbolTable) !datalog.Term {
return switch (term) {
.variable => |s| .{ .variable = @truncate(try symbols.insert(s)) }, // FIXME: assert symbol fits in u32
.string => |s| .{ .string = try symbols.insert(s) },
.bool => |b| .{ .bool = b },
};
}

pub fn format(term: Term, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
switch (term) {
.variable => |v| try writer.print("${s}", .{v}),
.string => |s| try writer.print("\"{s}\"", .{s}),
.bool => |b| if (b) try writer.print("true", .{}) else try writer.print("false", .{}),
}
}
};
1 change: 1 addition & 0 deletions biscuit-datalog/src/check.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub const Check = struct {
for (check.queries.items) |*query| {
query.deinit();
}

check.queries.deinit();
}

Expand Down
3 changes: 3 additions & 0 deletions biscuit-datalog/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ pub const fact = @import("fact.zig");
pub const Fact = @import("fact.zig").Fact;
pub const predicate = @import("predicate.zig");
pub const Predicate = @import("predicate.zig").Predicate;
pub const Expression = @import("expression.zig").Expression;
pub const Scope = @import("scope.zig").Scope;
pub const rule = @import("rule.zig");
pub const Rule = @import("rule.zig").Rule;
pub const check = @import("check.zig");
pub const symbol_table = @import("symbol_table.zig");
pub const SymbolTable = @import("symbol_table.zig").SymbolTable;
Expand Down
10 changes: 6 additions & 4 deletions biscuit-datalog/src/rule.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ pub const Rule = struct {
const head = try Predicate.fromSchema(allocator, schema_rule.head orelse return error.NoHeadInRuleSchema);

var body = std.ArrayList(Predicate).init(allocator);
var expressions = std.ArrayList(Expression).init(allocator);
var scopes = std.ArrayList(Scope).init(allocator);

for (schema_rule.body.items) |predicate| {
try body.append(try Predicate.fromSchema(allocator, predicate));
}

var expressions = std.ArrayList(Expression).init(allocator);
for (schema_rule.expressions.items) |expression| {
try expressions.append(try Expression.fromSchema(allocator, expression));
}

var scopes = std.ArrayList(Scope).init(allocator);
for (schema_rule.scope.items) |scope| {
try scopes.append(try Scope.fromSchema(scope));
}
Expand All @@ -41,16 +42,17 @@ pub const Rule = struct {

pub fn deinit(rule: *Rule) void {
rule.head.deinit();

for (rule.body.items) |*predicate| {
predicate.deinit();
}
rule.body.deinit();

for (rule.expressions.items) |*expression| {
expression.deinit();
}
rule.expressions.deinit();

rule.body.deinit();
rule.expressions.deinit();
rule.scopes.deinit();
}

Expand Down
1 change: 1 addition & 0 deletions biscuit-parser/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,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("biscuit-builder", builder.module("biscuit-builder"));
lib_unit_tests.root_module.addImport("biscuit-datalog", datalog.module("biscuit-datalog"));
lib_unit_tests.root_module.addImport("ziglyph", ziglyph.module("ziglyph"));

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
Expand Down
Loading

0 comments on commit 7e07bf5

Please sign in to comment.