nanoexpr 0.2.1

Fast no_std math expression parser and evaluator with zero heap allocation: formulas with variables and functions for microcontrollers, WebAssembly and C, in f32, f64, integers or calculator-exact decimals
Documentation
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Compiles an expression once into a [`Program`] of postfix instructions, to evaluate it many times.
//!
//! The instructions live in a slice the caller provides and the values in a fixed-size array, so neither compiling
//! nor running allocates.

use crate::parser::{Backend, Binary, Parser, Unary};
use crate::{Context, Error, ErrorKind, Evaluator, Number, Span};

/// One instruction of a [`Program`], borrowing the names of variables and functions from the expression.
///
/// Provide a slice of them to [`Evaluator::compile`], such as `[Instruction::default(); 64]`: an expression needs
/// about one instruction per number, name, operator and call, and one more per `&&`, `||` and `? :`.
#[derive(Debug, Clone, Copy)]
pub struct Instruction<'i, N>(Code<'i, N>);

impl<N: Default> Default for Instruction<'_, N> {
    fn default() -> Self {
        Self(Code::Number(N::default()))
    }
}

/// What an [`Instruction`] does.
///
/// The spans are those of the errors the instruction reports, the same as the evaluator reports.
#[derive(Debug, Clone, Copy)]
enum Code<'i, N> {
    /// Pushes a number.
    Number(N),
    /// Pushes the value of a variable.
    Variable(&'i str, Span),
    /// Replaces the arguments on top of the stack with the result of a call.
    Call {
        /// The name of the function, also the start of `span`.
        name: &'i str,
        /// How many arguments are on the stack.
        count: usize,
        /// The span of the whole call.
        span: Span,
    },
    /// Replaces the value on top of the stack with the result of an operation.
    Unary(Unary, Span),
    /// Replaces the two values on top of the stack with the result of an operation.
    Binary(Binary, Span),
    /// Pops the left operand of `||` if `or`, or of `&&` otherwise. If it decides the result, pushes it and jumps
    /// to `target`, past the right operand.
    Logic {
        /// Whether this is `||`.
        or: bool,
        /// Where the instructions after the operation start.
        target: usize,
        /// The span of an error reading the left operand.
        condition: Span,
        /// The span of the whole operation.
        span: Span,
    },
    /// Replaces the value on top of the stack with `1` if it is true, `0` otherwise.
    Truth(Span),
    /// Pops a condition and jumps to the given instruction if it is false.
    JumpUnless(usize, Span),
    /// Jumps to the given instruction.
    Jump(usize),
}

/// An expression compiled by [`Evaluator::compile`], to evaluate many times with different variables.
///
/// The variables and functions are resolved by the context at each evaluation, which gives the same results and
/// errors, with the same spans, as [`Evaluator::evaluate`] on the same expression and context. Errors that depend
/// only on the text, such as syntax errors, are reported by [`Evaluator::compile`] instead. Operations on numbers
/// alone, such as `2 * 3.5`, are computed once by the compilation, unless they fail.
///
/// `STACK` is the most values pending at once during an evaluation, such as the left operands of operators
/// waiting for their right one, or the arguments of calls. They are kept in an array on the call stack of
/// [`Program::evaluate`]. An expression that needs more fails to compile with [`ErrorKind::TooLong`].
///
/// ```
/// use nanoexpr::{Instruction, Program};
///
/// let mut code = [Instruction::default(); 16];
/// let program: Program<i64> = nanoexpr::compile("raw * 125 / 10000 - 40", &mut code).unwrap();
/// for (raw, celsius) in [(4000, 10), (4800, 20)] {
///     assert_eq!(program.evaluate(&[("raw", raw)]), Ok(celsius));
/// }
/// ```
#[derive(Debug, Clone, Copy)]
pub struct Program<'b, 'i, N, const STACK: usize = 32> {
    /// The instructions, in the order they run without jumps.
    code: &'b [Instruction<'i, N>],
    /// The most values pending at once.
    stack: usize,
}

impl<N: Number, const STACK: usize> Program<'_, '_, N, STACK> {
    /// Returns how many instructions the program takes in its buffer.
    pub const fn len(&self) -> usize {
        self.code.len()
    }

    /// Returns `false`, as a program has at least one instruction.
    pub const fn is_empty(&self) -> bool {
        self.code.is_empty()
    }

    /// Returns the most values pending at once during an evaluation, which is at most `STACK`.
    pub const fn stack(&self) -> usize {
        self.stack
    }

    /// Evaluates the program, resolving names with `context`.
    pub fn evaluate<C: Context<N> + ?Sized>(&self, context: &C) -> Result<N, Error> {
        let mut stack = Stack::<N, STACK>::new();
        let mut position = 0;
        while let Some(Instruction(code)) = self.code.get(position) {
            position += 1;
            match *code {
                Code::Number(value) => stack.push(value),
                Code::Variable(name, span) => match context.variable(name) {
                    Some(value) => stack.push(value),
                    None => return Err(Error::new(ErrorKind::UnknownVariable, span)),
                },
                Code::Call { name, count, span } => {
                    let result = match context.call(name, stack.top(count)) {
                        Some(result) => result.map_err(|kind| Error::new(kind, span))?,
                        None => {
                            let name = Span::new(span.start, span.start + name.len());
                            return Err(Error::new(ErrorKind::UnknownFunction, name));
                        }
                    };
                    stack.drop(count);
                    stack.push(result);
                }
                Code::Unary(operator, span) => {
                    let operand = stack.pop();
                    stack.push(at(operator.apply(operand), span)?);
                }
                Code::Binary(operator, span) => {
                    let right = stack.pop();
                    let left = stack.pop();
                    stack.push(at(operator.apply(left, right), span)?);
                }
                Code::Logic {
                    or,
                    target,
                    condition,
                    span,
                } => {
                    if !at(stack.pop().is_zero(), condition)? == or {
                        stack.push(at(N::from_bool(or), span)?);
                        position = target;
                    }
                }
                Code::Truth(span) => {
                    let truth = !at(stack.pop().is_zero(), span)?;
                    stack.push(at(N::from_bool(truth), span)?);
                }
                Code::JumpUnless(target, span) => {
                    if at(stack.pop().is_zero(), span)? {
                        position = target;
                    }
                }
                Code::Jump(target) => position = target,
            }
        }
        Ok(stack.pop())
    }
}

/// Turns the error of `result` into an [`Error`] at `span`.
fn at<T>(result: Result<T, ErrorKind>, span: Span) -> Result<T, Error> {
    result.map_err(|kind| Error::new(kind, span))
}

/// The values pending during an evaluation.
///
/// Compilation checks that an expression never needs more than `STACK` values nor pops a value it did not push,
/// so the operations never fail. They still check their bounds, returning zeros rather than panicking.
struct Stack<N, const STACK: usize> {
    /// The values, the pending ones first.
    values: [N; STACK],
    /// How many values are pending.
    length: usize,
}

impl<N: Number, const STACK: usize> Stack<N, STACK> {
    /// Creates an empty stack.
    fn new() -> Self {
        Self {
            values: [N::default(); STACK],
            length: 0,
        }
    }

    /// Pushes `value`.
    fn push(&mut self, value: N) {
        if let Some(slot) = self.values.get_mut(self.length) {
            *slot = value;
            self.length += 1;
        }
    }

    /// Pops the value on top.
    fn pop(&mut self) -> N {
        self.length = self.length.saturating_sub(1);
        self.values.get(self.length).copied().unwrap_or_default()
    }

    /// Returns the `count` values on top, the deepest first.
    fn top(&self, count: usize) -> &[N] {
        self.values
            .get(self.length.saturating_sub(count)..self.length)
            .unwrap_or_default()
    }

    /// Drops the `count` values on top.
    fn drop(&mut self, count: usize) {
        self.length = self.length.saturating_sub(count);
    }
}

/// The [`Backend`] that appends each operation to a program.
struct Compilation<'b, 'i, N> {
    /// Where the instructions go.
    code: &'b mut [Instruction<'i, N>],
    /// How many instructions are written.
    length: usize,
    /// How many values are pending after the instructions written, when they run without jumping.
    depth: usize,
    /// The most values pending at once so far.
    stack: usize,
    /// The most values the program may have pending.
    capacity: usize,
    /// The position of the last jump target: constants before it cannot be folded into later operations.
    barrier: usize,
}

impl<'i, N> Compilation<'_, 'i, N> {
    /// Appends an instruction that pops `pops` values and pushes `pushes`, and returns its position.
    ///
    /// Fails with [`ErrorKind::TooLong`] at `span` if the buffer is full or the values pending would not fit.
    fn emit(
        &mut self,
        code: Code<'i, N>,
        pops: usize,
        pushes: usize,
        span: Span,
    ) -> Result<usize, Error> {
        let slot = self
            .code
            .get_mut(self.length)
            .ok_or(Error::new(ErrorKind::TooLong, span))?;
        *slot = Instruction(code);
        self.depth = self.depth.saturating_sub(pops) + pushes;
        if self.depth > self.capacity {
            return Err(Error::new(ErrorKind::TooLong, span));
        }
        self.stack = self.stack.max(self.depth);
        self.length += 1;
        Ok(self.length - 1)
    }

    /// Returns the last `COUNT` instructions if they are all numbers that no jump lands between.
    fn constants<const COUNT: usize>(&self) -> Option<[N; COUNT]>
    where
        N: Copy + Default,
    {
        let start = self.length.checked_sub(COUNT)?;
        if start < self.barrier {
            return None;
        }
        let mut values = [N::default(); COUNT];
        for (value, Instruction(code)) in values.iter_mut().zip(self.code.get(start..self.length)?)
        {
            match code {
                Code::Number(number) => *value = *number,
                _ => return None,
            }
        }
        Some(values)
    }

    /// Replaces the last `count` instructions, numbers, with the number `value`.
    fn fold(&mut self, count: usize, value: N, span: Span) -> Result<(), Error> {
        self.length -= count;
        self.depth = self.depth.saturating_sub(count);
        self.emit(Code::Number(value), 0, 1, span).map(drop)
    }

    /// Points the jump at `position` to the next instruction, and sets the span of a [`Code::Logic`] to `span`.
    fn land(&mut self, position: usize, span: Span) {
        let next = self.length;
        self.barrier = next;
        if let Some(Instruction(code)) = self.code.get_mut(position) {
            match code {
                Code::Logic {
                    target, span: end, ..
                } => {
                    *target = next;
                    *end = span;
                }
                Code::JumpUnless(target, _) | Code::Jump(target) => *target = next,
                _ => {}
            }
        }
    }
}

impl<'i, N: Number> Backend<'i> for Compilation<'_, 'i, N> {
    type Value = ();
    type Branch = usize;

    fn number(&mut self, literal: &'i str, span: Span) -> Result<(), Error> {
        let value = N::parse(literal).ok_or(Error::new(ErrorKind::InvalidNumber, span))?;
        self.emit(Code::Number(value), 0, 1, span).map(drop)
    }

    fn variable(&mut self, name: &'i str, span: Span) -> Result<(), Error> {
        self.emit(Code::Variable(name, span), 0, 1, span).map(drop)
    }

    fn call(&mut self, name: &'i str, _: Span, arguments: &[()], span: Span) -> Result<(), Error> {
        let count = arguments.len();
        self.emit(Code::Call { name, count, span }, count, 1, span)
            .map(drop)
    }

    fn unary(&mut self, operator: Unary, _: (), span: Span) -> Result<(), Error> {
        if let Some(Ok(value)) = self.constants().map(|[operand]| operator.apply(operand)) {
            return self.fold(1, value, span);
        }
        self.emit(Code::Unary(operator, span), 1, 1, span).map(drop)
    }

    fn binary(&mut self, operator: Binary, _: (), _: (), span: Span) -> Result<(), Error> {
        if let Some(Ok(value)) = self
            .constants()
            .map(|[left, right]| operator.apply(left, right))
        {
            return self.fold(2, value, span);
        }
        self.emit(Code::Binary(operator, span), 2, 1, span)
            .map(drop)
    }

    fn logic(&mut self, or: bool, _: (), span: Span) -> Result<usize, Error> {
        let code = Code::Logic {
            or,
            target: 0,
            condition: span,
            span,
        };
        self.emit(code, 1, 0, span)
    }

    fn logic_end(&mut self, _: bool, branch: usize, _: (), span: Span) -> Result<(), Error> {
        self.emit(Code::Truth(span), 1, 1, span)?;
        self.land(branch, span);
        Ok(())
    }

    fn condition(&mut self, _: (), span: Span) -> Result<usize, Error> {
        self.emit(Code::JumpUnless(0, span), 1, 0, span)
    }

    fn otherwise(&mut self, branch: usize, span: Span) -> Result<usize, Error> {
        let jump = self.emit(Code::Jump(0), 0, 0, span)?;
        self.land(branch, span);
        self.depth = self.depth.saturating_sub(1);
        Ok(jump)
    }

    fn conditional_end(&mut self, branch: usize, _: (), _: ()) {
        self.land(branch, Span::new(0, 0));
    }
}

impl<C: ?Sized, const ARGUMENTS: usize> Evaluator<'_, C, ARGUMENTS> {
    /// Compiles `input` into `code`, to evaluate it many times with [`Program::evaluate`].
    ///
    /// The program follows the syntax settings of this evaluator, but resolves names with the context given to
    /// [`Program::evaluate`] rather than with the context of the evaluator. Returns the errors that depend only on
    /// the text: syntax errors, [`ErrorKind::InvalidNumber`], [`ErrorKind::TooDeep`] and
    /// [`ErrorKind::TooManyArguments`], as [`Evaluator::evaluate`] would, and [`ErrorKind::TooLong`] when `code`
    /// or `STACK` is too small.
    ///
    /// ```
    /// # #[cfg(feature = "libm")] {
    /// use nanoexpr::{Evaluator, Instruction, Math, PercentSign, Program};
    ///
    /// let calculator = Evaluator::<_>::new(&Math).percent_sign(PercentSign::Percentage);
    /// let mut code = [Instruction::default(); 32];
    /// let program: Program<f64> = calculator.compile("price * (1 - discount%)", &mut code).unwrap();
    /// assert_eq!(program.evaluate(&[("price", 80.0), ("discount", 25.0)]), Ok(60.0));
    /// assert_eq!(program.evaluate(&[("price", 10.0), ("discount", 50.0)]), Ok(5.0));
    /// # }
    /// ```
    pub fn compile<'b, 'i, N: Number, const STACK: usize>(
        &self,
        input: &'i str,
        code: &'b mut [Instruction<'i, N>],
    ) -> Result<Program<'b, 'i, N, STACK>, Error> {
        let compilation = Compilation {
            code,
            length: 0,
            depth: 0,
            stack: 0,
            capacity: STACK,
            barrier: 0,
        };
        let ((), compilation) = Parser::<_, ARGUMENTS>::parse(input, self.syntax(), compilation)?;
        let code: &'b [Instruction<'i, N>] = compilation.code;
        Ok(Program {
            code: code.get(..compilation.length).unwrap_or_default(),
            stack: compilation.stack,
        })
    }
}

/// Compiles `input` into `code` with the default settings, to evaluate it many times with [`Program::evaluate`].
///
/// See [`Evaluator::compile`].
pub fn compile<'b, 'i, N: Number, const STACK: usize>(
    input: &'i str,
    code: &'b mut [Instruction<'i, N>],
) -> Result<Program<'b, 'i, N, STACK>, Error> {
    Evaluator::<()>::new(&()).compile(input, code)
}