Skip to main content

qraft_core/expression/
unary.rs

1//! Unary predicate expressions.
2
3use std::marker::PhantomData;
4
5use crate::{
6    Boolean, LowerCompatible,
7    expression::Expression,
8    lower::{Instructions, LowerCtx},
9};
10
11/// Postfix predicates lowered as unary instruction variants.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum PostfixOperator {
14    Null { negated: bool },
15    False,
16    True,
17}
18
19/// Prefix unary operators currently supported by the builder.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum UnaryOperator {
22    Not,
23}
24
25/// A typed unary expression.
26pub struct Unary<L, T> {
27    lhs: L,
28    op: UnaryOperator,
29    marker: PhantomData<T>,
30}
31
32/// Builds a `not ...` expression for boolean values.
33pub fn not<E, T>(e: E) -> Unary<E, T>
34where
35    T: Boolean,
36    E: LowerCompatible<T>,
37{
38    Unary {
39        lhs: e,
40        op: UnaryOperator::Not,
41        marker: PhantomData,
42    }
43}
44
45#[qraft_expression_macro::as_expression]
46impl<L, T> Expression for Unary<L, T>
47where
48    T: Boolean,
49    L: LowerCompatible<T>,
50{
51    type Type = T;
52
53    fn lower(&self, ctx: &mut LowerCtx) -> usize {
54        let rhs = self.lhs.lower_compatible(ctx);
55        ctx.instrs.push_unary(self.op, rhs);
56        rhs + 1
57    }
58}