Skip to documentation
SLOP

tiny.pluck.toplevel.prepared

Reference tiny.pluck toplevel prepared

Defined in toplevel.

API (4)

Actions

Public operations.

Types and contracts

Public types and contracts.

No direct callersNo direct callstoplevelprepared
Static calls · unresolved targets: unknown · external targets: unknown.

Source

Called byCallsNo direct callerspexprparseExprtoplevel.preparedprepareQuery
Static calls · unresolved targets: 0 · external targets: 6.

Source: lib/pluck/src/toplevel/prepared.zig

zig
const std = @import("std");const alloc_arena = @import("alloc_arena");const pluck = @import("../root.zig");const Allocator = std.mem.Allocator;const pexpr = pluck.pexpr;const PExpr = pexpr.PExpr;const context_owner = @import("context.zig");const ToplevelContext = context_owner.ToplevelContext;const top_types = @import("types.zig");const ToplevelError = top_types.ToplevelError;const result_owner = @import("result.zig");const QueryResult = result_owner.QueryResult;const source_owner = @import("source.zig");pub const PreparedQuery = struct {    allocator: Allocator,    arena: *alloc_arena.Arena,    expr: *PExpr,    pub fn deinit(self: *PreparedQuery) void {        self.arena.deinit();        self.allocator.destroy(self.arena);    }};pub fn prepareQuery(self: *ToplevelContext, allocator: Allocator, source: []const u8) ToplevelError!PreparedQuery {    self.last_error_span = null;    const arena = allocator.create(alloc_arena.Arena) catch return ToplevelError.OutOfMemory;    arena.* = alloc_arena.Arena.init(allocator);    errdefer {        arena.deinit();        allocator.destroy(arena);    }    const expr = pexpr.parseExpr(arena.allocator(), source, &self.types, &self.definitions) catch |err| {        self.last_error_span = source_owner.spanFromOffsets(source, 0, source.len);        return switch (err) {            error.OutOfMemory => ToplevelError.OutOfMemory,            else => ToplevelError.ParseError,        };    };    return .{        .allocator = allocator,        .arena = arena,        .expr = expr,    };}pub fn runPreparedQuery(self: *ToplevelContext, prepared: *const PreparedQuery) ToplevelError!QueryResult {    return self.runQueryExpr(prepared.expr);}test "prepared query runs through context" {    var ctx = try ToplevelContext.init(std.testing.allocator);    defer ctx.deinit();    var prepared = try ctx.prepareQuery(std.testing.allocator, "(Marginal (flip 0.5))");    defer prepared.deinit();    var result = try ctx.runPreparedQuery(&prepared);    defer result.deinit();    var found_true = false;    for (result.outcomes) |outcome| {        if (std.mem.eql(u8, outcome.value_str, "True")) {            found_true = true;            try std.testing.expectApproxEqAbs(@as(f64, 0.5), outcome.probability, 1e-9);        }    }    try std.testing.expect(found_true);}test "prepared query records parse span" {    var ctx = try ToplevelContext.init(std.testing.allocator);    defer ctx.deinit();    try std.testing.expectError(ToplevelError.ParseError, ctx.prepareQuery(std.testing.allocator, "(Marginal True"));    const span = ctx.lastErrorSpan().?;    try std.testing.expectEqual(@as(usize, 0), span.start.offset);    try std.testing.expectEqual(@as(usize, 14), span.end.offset);}

Source: lib/pluck/src/toplevel/root.zig:8

zig
pub const prepared = @import("prepared.zig");

Audit

Definitions5
Public names7
Members3
Version26.7.0
Revisiondaab053ee433