1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//! This module provides facilities for rejecting dice expressions
//! not supported by your chosen backend.
//!
//! The old dice backends no longer support the full list of features
//! the parser is allowed to accept.
//! While they are on the road to deprecation, the MIR based pipeline isn't ready yet.

/// A description of which backends support a given dice expression,
/// or set of features.
#[derive(Debug)]
#[non_exhaustive]
pub struct Support {
    /// [`interp`](crate::interp)
    pub ast_interp: bool,
    /// [`stack`](crate::stack)
    pub stack: bool,
    /// [`mir`](crate::mir)
    pub mir: MirSupport,
}
/// A description of which *MIR-based* backends support a
/// given dice expression, or set of features.
#[derive(Debug)]
#[non_exhaustive]
pub struct MirSupport {
    /// [`mir::stack`](crate::mir::stack)
    pub stack: bool,
}

/// A backend specifier.
#[derive(Debug)]
#[non_exhaustive]
pub enum Target {
    AstInterp,
    Stack,
    Mir(MirTarget),
}
/// A *MIR-specific* backend specifier.
#[derive(Debug)]
#[non_exhaustive]
pub enum MirTarget {
    Stack,
}

impl Target {
    /// Get the list of features supported by a backend.
    pub fn features(&self) -> Features {
        match self {
            Target::AstInterp | Target::Stack => Features {
                basic: true,
                keep_high: true,
                keep_low: false,
                filters: false,
                explosion: false,
            },
            Target::Mir(MirTarget::Stack) => Features {
                basic: true,
                keep_high: true,
                keep_low: true,
                filters: false,
                explosion: true,
            },
        }
    }

    /// Check whether a list of features is supported by a backend.
    // `(supported && used) || !used` reduces to `!used || supported`
    pub fn supports(&self, features: Features) -> bool {
        let supported = self.features();
        (!features.basic || supported.basic)
            && (!features.keep_high || supported.keep_high)
            && (!features.keep_low || supported.keep_low)
            && (!features.filters || supported.filters)
            && (!features.explosion || supported.explosion)
    }
}

/// A description of features a dice expression uses.
#[derive(Default, Debug, Copy, Clone)]
#[non_exhaustive]
pub struct Features {
    /// Basic dice terms `NdN`, and basic arithmetic `+` and `-`.
    pub basic: bool,
    /// The "keep high" filter.
    pub keep_high: bool,
    /// The "keep low" filter.
    pub keep_low: bool,
    /// The other filters:
    /// - "drop high"
    /// - "drop low"
    pub filters: bool,
    /// Dice explosion.
    pub explosion: bool,
}

impl Features {
    /// Get the list of backends that support a list of features.
    pub fn supported_by(&self) -> Support {
        Support {
            ast_interp: Target::AstInterp.supports(*self),
            stack: Target::Stack.supports(*self),
            mir: MirSupport {
                stack: Target::Mir(MirTarget::Stack).supports(*self),
            },
        }
    }

    /// Get the list of features used by a dice expression.
    pub fn of(expression: &crate::parse::Program) -> Self {
        use crate::for_;
        use crate::parse::Term;
        let mut features = Features::default();
        for_! [ (term, _ancestors) in expression.postorder() => {
            match term {
                Term::Constant(_) | Term::DiceRoll(_, _) | Term::Add(_, _) |
                Term::Subtract(_, _) | Term::UnarySubtract(_) |
                Term::UnaryAdd(_) => features.basic = true,
                Term::KeepHigh(_, _) => features.keep_high = true,
                Term::KeepLow(_, _) => features.keep_low = true,
                Term::Explode(_) => features.explosion = true,
            }
        }];
        features
    }
}

/// Type level reification of stuff. Just [`Target`] for now.
/// We use this in place of full const generics.
pub mod type_level {
    /// A type level reification of [`Target`](crate::backend_support::Target).
    #[allow(non_snake_case)]
    pub mod Target {
        mod seal {
            /// A non-publically exported trait.
            pub trait Sealed {}
            /// A type level reification of [`Target`](crate::backend_support::Target).
            pub trait Target: Sealed + Into<crate::backend_support::Target> {
                fn reify() -> crate::backend_support::Target;
            }
        }
        pub use seal::Target;
        macro_rules! decl_backends {
            (@[$m:ident [$($b:ident => $c:ident),*]]) => {
                #[allow(non_snake_case)]
                pub mod $m {
                    use super::seal::Target;
                    $(use crate::backend_support:: $c;
                      pub struct $b;
                      impl super::seal::Sealed for $b {}
                      impl From<$b> for crate::backend_support::Target {
                          fn from($b: $b) -> Self {
                              crate::backend_support::Target :: $m ( $c :: $b )
                          }
                      }
                      impl Target for $b {
                          fn reify() -> crate::backend_support::Target {
                              $b.into()
                          }
                      })*
                }
            };
            (@[$t:ident]) => {
                pub struct $t;
                impl seal::Sealed for $t {}
                impl From<$t> for crate::backend_support::Target {
                    fn from($t: $t) -> Self {
                        crate::backend_support::Target:: $t
                    }
                }
                impl Target for $t {
                    fn reify() -> crate::backend_support::Target {
                        $t.into()
                    }
                }
            };
            ($( $a:tt $([$($b:ident => $c:ident),*])? ),*) => {
                $(decl_backends! {@[
                    $a $([$($b => $c),*])?
                ]})*
            };
        }
        decl_backends![AstInterp, Stack, Mir[Stack => MirTarget]];
    }
}
pub use type_level::Target as TyTarget;

/// Error for when a dice program uses unsupported features.
pub struct UnsupportedFeature;

/// A wrapper around [`Program`](crate::parse::Program) that enforces having
/// been checked for feature support on a particular backend.
#[derive(::derive_more::Deref)]
pub struct FeatureCheckedProgram<'a, Target> {
    #[deref]
    program: &'a crate::parse::Program,
    _target: ::core::marker::PhantomData<Target>,
}
impl<'a, T: TyTarget::Target> FeatureCheckedProgram<'a, T> {
    pub fn check(program: &'a crate::parse::Program) -> Result<Self, UnsupportedFeature> {
        if T::reify().supports(Features::of(program)) {
            Ok(Self {
                program,
                _target: ::core::marker::PhantomData,
            })
        } else {
            Err(UnsupportedFeature)
        }
    }
}