Skip to content

Commit

Permalink
WIP parser
Browse files Browse the repository at this point in the history
  • Loading branch information
malcolmstill committed Mar 23, 2024
1 parent 10432f2 commit e7dc323
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 29 deletions.
4 changes: 4 additions & 0 deletions biscuit-builder/src/check.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ const std = @import("std");
const datalog = @import("biscuit-datalog");
const Predicate = @import("predicate.zig").Predicate;
const Term = @import("term.zig").Term;
const Rule = @import("rule.zig").Rule;

pub const Check = struct {
kind: datalog.Check.Kind,
queries: std.ArrayList(Rule),

pub fn deinit(_: Check) void {
//
}
Expand Down
4 changes: 2 additions & 2 deletions biscuit-builder/src/fact.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub const Fact = struct {
}

/// convert to datalog fact
pub fn convert(fact: Fact) datalog.Fact {
return .{ .predicate = fact.predicate.convert() };
pub fn convert(fact: Fact, allocator: std.mem.Allocator, symbols: *datalog.SymbolTable) !datalog.Fact {
return .{ .predicate = try fact.predicate.convert(allocator, symbols) };
}

pub fn format(fact: Fact, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
Expand Down
16 changes: 14 additions & 2 deletions biscuit-builder/src/predicate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@ pub const Predicate = struct {
terms: std.ArrayList(Term),

pub fn deinit(predicate: Predicate) void {
for (predicate.terms.items) |term| {
term.deinit();
}

predicate.terms.deinit();
}

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

var terms = std.ArrayList(datalog.Term).init(allocator);

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

return .{ .name = name, .terms = terms };
}

pub fn format(predicate: Predicate, comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void {
Expand Down
3 changes: 3 additions & 0 deletions biscuit-builder/src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ pub const Fact = @import("fact.zig").Fact;
pub const Predicate = @import("predicate.zig").Predicate;
pub const Term = @import("term.zig").Term;
pub const Check = @import("check.zig").Check;
pub const Rule = @import("rule.zig").Rule;
pub const Expression = @import("expression.zig").Expression;
pub const Scope = @import("scope.zig").Scope;
14 changes: 13 additions & 1 deletion biscuit-builder/src/rule.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,21 @@ pub const Rule = struct {
head: Predicate,
body: std.ArrayList(Predicate),
expressions: std.ArrayList(Expression),
variables: ?std.AutoHashMap([]const u8, ?Term),
variables: ?std.StringHashMap(?Term),
scopes: std.ArrayList(Scope),

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

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

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

/// convert to datalog predicate
pub fn convert(_: Rule) datalog.Rule {
unreachable;
Expand Down
12 changes: 12 additions & 0 deletions biscuit-builder/src/term.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const std = @import("std");
const datalog = @import("biscuit-datalog");

const TermTag = enum(u8) {
string,
bool,
Expand All @@ -6,4 +9,13 @@ const TermTag = enum(u8) {
pub const Term = union(TermTag) {
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) {
.string => |s| .{ .string = try symbols.insert(s) },
.bool => |b| .{ .bool = b },
};
}
};
2 changes: 1 addition & 1 deletion biscuit-datalog/src/check.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub const Check = struct {
queries: std.ArrayList(Rule),
kind: Kind,

const Kind = enum(u8) { one, all };
pub const Kind = enum(u8) { one, all };

pub fn fromSchema(allocator: std.mem.Allocator, schema_check: schema.CheckV2) !Check {
var rules = std.ArrayList(Rule).init(allocator);
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 @@ -5,6 +5,9 @@ pub const Predicate = @import("predicate.zig").Predicate;
pub const rule = @import("rule.zig");
pub const check = @import("check.zig");
pub const symbol_table = @import("symbol_table.zig");
pub const SymbolTable = @import("symbol_table.zig").SymbolTable;
pub const Term = @import("term.zig").Term;
pub const Check = @import("check.zig").Check;
pub const world = @import("world.zig");

test {
Expand Down
2 changes: 2 additions & 0 deletions biscuit-parser/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ 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 builder = b.dependency("biscuit-builder", .{ .target = target, .optimize = optimize });
const datalog = b.dependency("biscuit-datalog", .{ .target = target, .optimize = optimize });

_ = b.addModule("biscuit-parser", .{
.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 = "biscuit-builder", .module = builder.module("biscuit-builder") },
.{ .name = "biscuit-datalog", .module = datalog.module("biscuit-datalog") },
.{ .name = "ziglyph", .module = ziglyph.module("ziglyph") },
},
});
Expand Down
1 change: 1 addition & 0 deletions biscuit-parser/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
.@"biscuit-schema" = .{ .path = "../biscuit-schema" },
.@"biscuit-format" = .{ .path = "../biscuit-format" },
.@"biscuit-builder" = .{ .path = "../biscuit-builder" },
.@"biscuit-datalog" = .{ .path = "../biscuit-datalog" },
.ziglyph = .{
.url = "https://codeberg.org/dude_the_builder/ziglyph/archive/947ed39203bf90412e3d16cbcf936518b6f23af0.tar.gz",
.hash = "12208b23d1eb6dcb929e85346524db8f8b8aa1401bdf8a97dee1e0cfb55da8d5fb42",
Expand Down
Loading

0 comments on commit e7dc323

Please sign in to comment.