Skip to main content

semi_persistent_egraph/
surface_ast.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3//! Surface AST — uniform syntax before resolve-time dispatch.
4//!
5//! `(op child1 child2 ..rest)` looks the same regardless of operator kind.
6//! The lowering pass inspects `OpKind` to produce the strongly-typed `Pattern`.
7
8use crate::ast::{Action, Command, MultSpec, RhsTerm, Span};
9
10/// A child in a surface pattern.
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub enum SurfacePatChild {
13    /// Single element pattern.
14    Elem(SurfacePattern),
15    /// Element with multiplicity: `x:2`, `x:k`, `x:k>=2`.
16    ElemMult(SurfacePattern, MultSpec),
17}
18
19/// Uniform pattern — `(op children...)` with optional `..rest` and `:mult`.
20#[derive(Clone, Debug, PartialEq, Eq)]
21pub enum SurfacePattern {
22    /// Bare identifier = pattern variable.
23    Var(String, Span),
24    /// Literal constant.
25    Lit(String, Span),
26    /// `(op [..pre] child1 child2 ... [..suf])`.
27    App {
28        op: String,
29        prefix: Option<(String, Span)>,
30        children: Vec<SurfacePatChild>,
31        suffix: Option<(String, Span)>,
32        span: Span,
33    },
34}
35
36impl SurfacePattern {
37    pub fn span(&self) -> Span {
38        match self {
39            SurfacePattern::Var(_, s) | SurfacePattern::Lit(_, s) => *s,
40            SurfacePattern::App { span, .. } => *span,
41        }
42    }
43}
44
45/// Surface command — pattern-bearing commands use `SurfacePattern`,
46/// everything else passes through as `Command`.
47#[derive(Clone, Debug, PartialEq, Eq)]
48pub enum SurfaceCommand {
49    Rewrite {
50        lhs: SurfacePattern,
51        rhs: RhsTerm,
52        when: Vec<SurfacePattern>,
53        subsume: bool,
54        /// `:ruleset name`, or `None` for the default ruleset.
55        ruleset: Option<String>,
56    },
57    Rule {
58        body: Vec<SurfacePattern>,
59        head: Vec<Action>,
60        /// `:ruleset name`, or `None` for the default ruleset.
61        ruleset: Option<String>,
62    },
63    Pass(Command),
64}