kitt_score 0.1.0

Decision engine at the core of Project KITT — in-memory stateful matching with pluggable scoring backends.
Documentation
//! Bytecode for the predicate DSL.
//!
//! We use a stack machine over `f32`. Booleans are encoded as `0.0` / `1.0`.
//! The complete op set is small (~20 ops) so a single `match` in the VM is
//! branch-predictor-friendly.
//!
//! All slot references are resolved to absolute byte offsets at compile time
//! (see [`super::typecheck`]). The VM never touches the `Schema` — it only
//! reads from a `LocationView`'s buffer at known offsets.

/// One opcode. `Op` is `Copy` so `match *op` in the VM stays cheap.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Op {
    /// Push literal f32 onto the stack.
    PushF32(f32),
    /// Read i64 from slot at `offset`, convert to f32, push.
    LoadI64 {
        /// Byte offset into the per-location buffer.
        offset: u32,
    },
    /// Read f32 from slot at `offset`, push.
    LoadF32 {
        /// Byte offset into the per-location buffer.
        offset: u32,
    },
    /// Read f64 from slot at `offset`, narrow to f32, push.
    LoadF64 {
        /// Byte offset into the per-location buffer.
        offset: u32,
    },
    /// Read u32 (enum code) from slot at `offset`, convert to f32, push.
    LoadU32 {
        /// Byte offset into the per-location buffer.
        offset: u32,
    },
    /// Unary negation.
    Neg,
    /// Logical NOT (0 → 1, nonzero → 0).
    Not,
    /// Binary add.
    Add,
    /// Binary sub.
    Sub,
    /// Binary mul.
    Mul,
    /// Binary div; div-by-zero yields 0.0 (no panic).
    Div,
    /// Less-than.
    Lt,
    /// Less-or-equal.
    Le,
    /// Greater-than.
    Gt,
    /// Greater-or-equal.
    Ge,
    /// Equal.
    Eq,
    /// Not-equal.
    Ne,
    /// Logical AND.
    And,
    /// Logical OR.
    Or,
    /// Binary min of top two stack values.
    MinA,
    /// Binary max of top two stack values.
    MaxA,
    /// Unary abs.
    Abs,
}

/// A compiled predicate program.
#[derive(Clone, Debug)]
pub struct Program {
    /// Linear op sequence.
    pub ops: Vec<Op>,
    /// Max observed stack depth. The VM pre-sizes its stack from this.
    pub max_stack: usize,
}