tensorlogic_ir/pattern.rs
1//! Pattern type for TLExpr pattern matching.
2//!
3//! Provides the `MatchPattern` enum used in `TLExpr::Match` arms. Only concrete
4//! constant patterns and a wildcard are supported (Design A — no variable binding).
5
6use std::fmt;
7
8use serde::{Deserialize, Serialize};
9
10/// A pattern used in a `TLExpr::Match` arm.
11///
12/// Patterns are intentionally minimal (Design A):
13/// - `ConstSymbol(String)` — match a symbol literal by name.
14/// - `ConstNumber(f64)` — match a numeric literal by value.
15/// - `Wildcard` — match anything; must be the last arm in a `Match`.
16#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
17pub enum MatchPattern {
18 /// Match a symbol literal (lowered to `Eq(scrutinee, SymbolLiteral(s))`).
19 ConstSymbol(String),
20 /// Match a numeric literal (lowered to `Eq(scrutinee, Constant(n))`).
21 ConstNumber(f64),
22 /// Wildcard — matches anything. Must be the last arm.
23 Wildcard,
24}
25
26impl fmt::Display for MatchPattern {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 match self {
29 Self::ConstSymbol(s) => write!(f, ":{s}"),
30 Self::ConstNumber(n) => write!(f, "{n}"),
31 Self::Wildcard => write!(f, "_"),
32 }
33 }
34}
35
36#[cfg(test)]
37mod tests {
38 use super::*;
39
40 #[test]
41 fn display_patterns() {
42 assert_eq!(MatchPattern::ConstSymbol("ok".into()).to_string(), ":ok");
43 assert_eq!(MatchPattern::ConstNumber(2.71).to_string(), "2.71");
44 assert_eq!(MatchPattern::Wildcard.to_string(), "_");
45 }
46}