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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use lalrpop_util::lalrpop_mod;
use num_runtime_fmt::NumFmt;

use crate::{
    types::{Calcable, CalcableError},
    Context,
};

// no point getting style warnings for generated code
lalrpop_mod!(#[allow(clippy::all)] pub parser);

/// Error encountered while parsing an expression
#[derive(Debug, thiserror::Error)]
pub enum ParseError {
    #[error("index must fit into usize")]
    Index(#[source] std::num::ParseIntError),
    #[error("failed to parse format string")]
    Format(#[from] num_runtime_fmt::parse::Error),
}

/// A prefix operator.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PrefixOperator {
    Negation,
    Not,
}

impl PrefixOperator {
    fn evaluate<N: Calcable>(&self, operand: N) -> Result<N, <N as Calcable>::Err> {
        match self {
            Self::Negation => operand.neg().ok_or_else(|| N::Err::unimplemented("-")),
            Self::Not => operand.not().ok_or_else(|| N::Err::unimplemented("!")),
        }
    }
}

/// An infix operator.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InfixOperator {
    Add,
    Sub,
    Mul,
    Div,
    TruncDiv,
    Pow,
    Rem,
    Lshift,
    Rshift,
    RotateL,
    RotateR,
    BitAnd,
    BitOr,
    BitXor,
}

impl InfixOperator {
    fn evaluate<N: Calcable>(&self, left: N, right: N) -> Result<N, <N as Calcable>::Err> {
        match self {
            Self::Add => <N as Calcable>::add(left, right),
            Self::Sub => <N as Calcable>::sub(left, right),
            Self::Mul => <N as Calcable>::mul(left, right),
            Self::Div => <N as Calcable>::div(left, right),
            Self::TruncDiv => left.trunc_div(right),
            Self::Pow => left.pow(right),
            Self::Rem => left.rem(right),
            Self::Lshift => left.shl(right),
            Self::Rshift => left.shr(right),
            Self::RotateL => left.rotate_left(right),
            Self::RotateR => left.rotate_right(right),
            Self::BitAnd => left
                .bit_and(right)
                .ok_or_else(|| N::Err::unimplemented("&")),
            Self::BitOr => left.bit_or(right).ok_or_else(|| N::Err::unimplemented("|")),
            Self::BitXor => left
                .bit_xor(right)
                .ok_or_else(|| N::Err::unimplemented("^")),
        }
    }
}

/// A function name.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Function {
    Abs,
    Ceil,
    Floor,
    Round,
    Sin,
    Cos,
    Tan,
    Sinh,
    Cosh,
    Tanh,
    Asin,
    Acos,
    Atan,
    Asinh,
    Acosh,
    Atanh,
    Rad,
    Deg,
    Sqrt,
    Cbrt,
    Log,
    Lg,
    Ln,
    Exp,
}

impl Function {
    fn evaluate<N: Calcable>(&self, operand: N) -> Result<N, <N as Calcable>::Err> {
        let (result, symbol) = match self {
            Self::Abs => (operand.abs(), "abs"),
            Self::Ceil => (operand.ceil(), "ceil"),
            Self::Floor => (operand.floor(), "floor"),
            Self::Round => (operand.round(), "round"),
            Self::Sin => (operand.sin(), "sin"),
            Self::Cos => (operand.cos(), "cos"),
            Self::Tan => (operand.tan(), "tan"),
            Self::Sinh => (operand.sinh(), "sinh"),
            Self::Cosh => (operand.cosh(), "cosh"),
            Self::Tanh => (operand.tanh(), "tanh"),
            Self::Asin => (operand.asin(), "asin"),
            Self::Acos => (operand.acos(), "acos"),
            Self::Atan => (operand.atan(), "atan"),
            Self::Asinh => (operand.asinh(), "asinh"),
            Self::Acosh => (operand.acosh(), "acosh"),
            Self::Atanh => (operand.atanh(), "atanh"),
            Self::Rad => (operand.rad(), "rad"),
            Self::Deg => (operand.deg(), "deg"),
            Self::Sqrt => (operand.sqrt(), "sqrt"),
            Self::Cbrt => (operand.cbrt(), "cbrt"),
            Self::Log => (operand.log(), "log"),
            Self::Lg => (operand.lg(), "lg"),
            Self::Ln => (operand.ln(), "ln"),
            Self::Exp => (operand.exp(), "exp"),
        };
        result.ok_or_else(|| N::Err::unimplemented(symbol))
    }
}

/// A constant.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Constant {
    E,
    Pi,
}

/// What kind of history lookup is desired.
///
/// Absolute history lookups begin at 0 and increment.
/// Relative history lookups count backwards from the current expression.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HistoryIndexKind {
    Relative,
    Absolute,
}

/// A term in the expression.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Term<'input> {
    Literal(&'input str),
    HexLiteral(&'input str),
    OctLiteral(&'input str),
    BinLiteral(&'input str),
    Constant(Constant),
    History(HistoryIndexKind, usize),
}

impl<'input> Term<'input> {
    fn evaluate<N: Calcable>(&self, ctx: &Context<N>) -> Result<N, <N as Calcable>::Err> {
        match self {
            Self::Literal(s) => N::parse_decimal(s),
            Self::HexLiteral(s) => N::parse_hex(s),
            Self::OctLiteral(s) => N::parse_octal(s),
            Self::BinLiteral(s) => N::parse_binary(s),
            Self::Constant(Constant::E) => N::E.ok_or_else(|| N::Err::unimplemented("e")),
            Self::Constant(Constant::Pi) => N::PI.ok_or_else(|| N::Err::unimplemented("pi")),
            Self::History(kind, idx) => {
                let real_idx = match kind {
                    HistoryIndexKind::Absolute => *idx,
                    HistoryIndexKind::Relative => {
                        ctx.history.len().checked_sub(*idx).ok_or_else(|| {
                            N::Err::history_out_of_bounds(*kind, *idx, ctx.history.len())
                        })?
                    }
                };
                match ctx.history.get(real_idx) {
                    Some(n) => Ok(n.clone()),
                    None => Err(N::Err::history_out_of_bounds(
                        *kind,
                        *idx,
                        ctx.history.len(),
                    )),
                }
            }
        }
    }
}

/// An expression or subexpression
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Expr<'input> {
    Term(Term<'input>),
    Prefix(PrefixOperator, Box<Expr<'input>>),
    Infix(Box<Expr<'input>>, InfixOperator, Box<Expr<'input>>),
    Func(Function, Box<Expr<'input>>),
    Group(Box<Expr<'input>>),
}

impl<'input> Expr<'input> {
    /// Evaluate this expression into its mathematical result.
    pub(crate) fn evaluate<N: Calcable>(
        &self,
        ctx: &Context<N>,
    ) -> Result<N, <N as Calcable>::Err> {
        match self {
            Self::Term(term) => term.evaluate(ctx),
            Self::Prefix(prefix, expr) => prefix.evaluate(expr.evaluate(ctx)?),
            Self::Infix(left, infix, right) => {
                infix.evaluate(left.evaluate(ctx)?, right.evaluate(ctx)?)
            }
            Self::Func(func, expr) => func.evaluate(expr.evaluate(ctx)?),
            Self::Group(expr) => expr.evaluate(ctx),
        }
    }
}

/// Error produced by [`AnnotatedExpr`].
#[derive(Debug, thiserror::Error)]
pub enum AnnotatedError<N>
where
    N: std::fmt::Debug + Calcable,
    <N as Calcable>::Err: 'static,
{
    #[error(transparent)]
    Calculation(<N as Calcable>::Err),
    #[error("failed to render calculation result in desired format")]
    Format(#[from] num_runtime_fmt::Error),
}

/// An expression annotated with some metadata.
pub struct AnnotatedExpr<'input> {
    pub expr: Expr<'input>,
    pub format: NumFmt,
}

impl<'input> AnnotatedExpr<'input> {
    /// Evaluate this expression into its mathematical result.
    ///
    /// Return the result as a bare type and also formatted according to the
    /// requested format string.
    pub fn evaluate<N>(&self, ctx: &Context<N>) -> Result<(N, String), AnnotatedError<N>>
    where
        N: std::fmt::Debug + Calcable + num_runtime_fmt::Numeric,
        <N as Calcable>::Err: 'static,
    {
        let value = self
            .expr
            .evaluate(ctx)
            .map_err(AnnotatedError::Calculation)?;
        let formatted = self.format.fmt(value.clone())?;
        Ok((value, formatted))
    }
}