use proc_macro2::Span;
use super::KernelType;
#[derive(Debug, Clone)]
#[allow(dead_code)] pub enum KernelExpr {
LitInt(i64, KernelType, Span),
LitFloat(f64, KernelType, Span),
LitBool(bool, Span),
Var(String, Span),
BinOp {
op: BinOpKind,
lhs: Box<KernelExpr>,
rhs: Box<KernelExpr>,
span: Span,
},
UnaryOp {
op: UnaryOpKind,
expr: Box<KernelExpr>,
span: Span,
},
Index {
array: String,
index: Box<KernelExpr>,
span: Span,
},
BuiltinCall {
name: String,
args: Vec<KernelExpr>,
span: Span,
},
Cast {
expr: Box<KernelExpr>,
target_ty: KernelType,
span: Span,
},
Paren(Box<KernelExpr>, Span),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)] pub enum BinOpKind {
Add,
Sub,
Mul,
Div,
Rem,
Lt,
Le,
Gt,
Ge,
Eq,
Ne,
BitAnd,
BitOr,
BitXor,
Shl,
Shr,
And,
Or,
}
#[allow(dead_code)] impl BinOpKind {
pub fn is_arithmetic(&self) -> bool {
matches!(
self,
BinOpKind::Add | BinOpKind::Sub | BinOpKind::Mul | BinOpKind::Div | BinOpKind::Rem
)
}
pub fn is_comparison(&self) -> bool {
matches!(
self,
BinOpKind::Lt
| BinOpKind::Le
| BinOpKind::Gt
| BinOpKind::Ge
| BinOpKind::Eq
| BinOpKind::Ne
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)] pub enum UnaryOpKind {
Neg,
Not,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn binop_is_arithmetic() {
assert!(BinOpKind::Add.is_arithmetic());
assert!(BinOpKind::Mul.is_arithmetic());
assert!(!BinOpKind::Lt.is_arithmetic());
assert!(!BinOpKind::And.is_arithmetic());
}
#[test]
fn binop_is_comparison() {
assert!(BinOpKind::Lt.is_comparison());
assert!(BinOpKind::Ge.is_comparison());
assert!(!BinOpKind::Add.is_comparison());
assert!(!BinOpKind::Or.is_comparison());
}
}