exp-rs 0.2.0

no_std expression parser, compiler, and evaluation engine for math expressions designed for embedded, with qemu examples
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
//! Type definitions for the expression parser and evaluator.
//!
//! This module contains the core data structures used throughout the expression parser
//! and evaluator, including the Abstract Syntax Tree (AST) representation, token types,
//! function definitions, and other auxiliary types.

extern crate alloc;

// ============================================================================
// Heapless Migration - Type Aliases and Configuration
// ============================================================================

use heapless::{FnvIndexMap, String as HeaplessString};
use alloc::string::ToString;

// Configuration constants - can be adjusted based on target constraints
pub const EXP_RS_MAX_VARIABLES: usize = 16;
pub const EXP_RS_MAX_BATCH_PARAMS: usize = 64;
pub const EXP_RS_MAX_CONSTANTS: usize = 8;
pub const EXP_RS_MAX_ARRAYS: usize = 4;
pub const EXP_RS_MAX_ATTRIBUTES: usize = 4;
pub const EXP_RS_MAX_NESTED_ARRAYS: usize = 2;
pub const EXP_RS_MAX_AST_CACHE: usize = 16;
pub const EXP_RS_MAX_NATIVE_FUNCTIONS: usize = 64;
pub const EXP_RS_MAX_EXPRESSION_FUNCTIONS: usize = 8;
pub const EXP_RS_MAX_ATTR_KEYS: usize = 4;

// String length limits for embedded efficiency
pub const EXP_RS_MAX_KEY_LENGTH: usize = 32;
pub const EXP_RS_MAX_FUNCTION_NAME_LENGTH: usize = 32; // Changed from 24 to 32 for proper alignment

// Error message buffer size for ExprResult
pub const EXP_RS_ERROR_BUFFER_SIZE: usize = 256;

// Primary type aliases
pub type HString = HeaplessString<EXP_RS_MAX_KEY_LENGTH>;
pub type FunctionName = HeaplessString<EXP_RS_MAX_FUNCTION_NAME_LENGTH>;

// Container type aliases - using heapless FnvIndexMap
pub type VariableMap = FnvIndexMap<HString, crate::Real, EXP_RS_MAX_VARIABLES>;
pub type ConstantMap = FnvIndexMap<HString, crate::Real, EXP_RS_MAX_CONSTANTS>;
pub type BatchParamMap = FnvIndexMap<HString, crate::Real, EXP_RS_MAX_BATCH_PARAMS>;
pub type ArrayMap = FnvIndexMap<HString, alloc::vec::Vec<crate::Real>, EXP_RS_MAX_ARRAYS>;
pub type AttributeMap = FnvIndexMap<
    HString,
    FnvIndexMap<HString, crate::Real, EXP_RS_MAX_ATTR_KEYS>,
    EXP_RS_MAX_ATTRIBUTES,
>;
pub type NestedArrayMap = FnvIndexMap<
    HString,
    FnvIndexMap<usize, alloc::vec::Vec<crate::Real>, EXP_RS_MAX_NESTED_ARRAYS>,
    EXP_RS_MAX_NESTED_ARRAYS,
>;
pub type NativeFunctionMap = FnvIndexMap<FunctionName, NativeFunction, EXP_RS_MAX_NATIVE_FUNCTIONS>;
pub type ExpressionFunctionMap =
    FnvIndexMap<FunctionName, ExpressionFunction, EXP_RS_MAX_EXPRESSION_FUNCTIONS>;

// AST cache type - defined later after AstExpr is declared
// pub type AstCacheMap = FnvIndexMap<HString, alloc::rc::Rc<AstExpr>, MAX_AST_CACHE>;

#[cfg(test)]
use crate::Real;
#[cfg(not(test))]
use crate::{Real, String, Vec};
#[cfg(not(test))]
use alloc::rc::Rc;
#[cfg(test)]
use std::rc::Rc;
#[cfg(test)]
use std::string::String;
#[cfg(test)]
use std::vec::Vec;

/// Abstract Syntax Tree (AST) node representing an expression.
///
/// The AST is the core data structure used for representing parsed expressions.
/// Each variant of this enum represents a different type of expression node,
/// forming a tree structure that can be evaluated to produce a result.
///
/// This type uses arena allocation for all strings and recursive structures,
/// eliminating all dynamic allocations during evaluation.
///
/// Using repr(C) with explicit discriminant type and alignment to avoid ARM alignment issues
#[derive(Debug)]
#[repr(C, align(8))]
pub enum AstExpr<'arena> {
    /// A literal numerical value.
    ///
    /// Examples: `3.14`, `42`, `-1.5`
    Constant(Real),

    /// A named variable reference.
    ///
    /// Examples: `x`, `temperature`, `result`
    Variable(&'arena str),

    /// A function call with a name and list of argument expressions.
    ///
    /// Examples: `sin(x)`, `max(a, b)`, `sqrt(x*x + y*y)`
    Function {
        /// The name of the function being called
        name: &'arena str,
        /// The arguments passed to the function
        args: &'arena [AstExpr<'arena>],
    },

    /// An array element access.
    ///
    /// Examples: `array[0]`, `values[i+1]`
    Array {
        /// The name of the array
        name: &'arena str,
        /// The expression for the index
        index: &'arena AstExpr<'arena>,
    },

    /// An attribute access on an object.
    ///
    /// Examples: `point.x`, `settings.value`
    Attribute {
        /// The base object name
        base: &'arena str,
        /// The attribute name
        attr: &'arena str,
    },

    /// A logical operation with short-circuit evaluation.
    ///
    /// Represents logical AND (`&&`) and OR (`||`) operations with short-circuit behavior.
    /// Unlike function-based operators, these operators have special evaluation semantics
    /// where the right operand may not be evaluated based on the value of the left operand.
    ///
    /// # Examples
    ///
    /// - `a && b`: Evaluates `a`, then evaluates `b` only if `a` is non-zero (true)
    /// - `c || d`: Evaluates `c`, then evaluates `d` only if `c` is zero (false)
    /// - `x > 0 && y < 10`: Checks if both conditions are true, with short-circuit
    /// - `flag || calculate_value()`: Skips calculation if flag is true
    ///
    /// # Boolean Logic
    ///
    /// The engine represents boolean values as floating-point numbers:
    /// - `0.0` is considered `false`
    /// - Any non-zero value is considered `true`, typically `1.0` is used
    ///
    /// # Operator Precedence
    ///
    /// `&&` has higher precedence than `||`, consistent with most programming languages:
    /// - `a || b && c` is interpreted as `a || (b && c)`
    /// - Use parentheses to override default precedence: `(a || b) && c`
    LogicalOp {
        /// The logical operator (AND or OR)
        op: LogicalOperator,
        /// The left operand (always evaluated)
        left: &'arena AstExpr<'arena>,
        /// The right operand (conditionally evaluated based on left value)
        right: &'arena AstExpr<'arena>,
    },

    /// A ternary conditional operation (condition ? true_expr : false_expr).
    ///
    /// Represents a conditional expression with three parts: a condition to evaluate,
    /// an expression to return if the condition is true, and an expression to return
    /// if the condition is false. This uses short-circuit evaluation, meaning only
    /// the relevant branch is evaluated.
    ///
    /// # Examples
    ///
    /// - `x > 0 ? 1 : -1`: Returns 1 if x is positive, -1 otherwise
    /// - `flag ? value1 : value2`: Chooses between two values based on flag
    /// - `a > b ? a : b`: Returns the maximum of a and b
    ///
    /// # Boolean Logic
    ///
    /// Like logical operations, the ternary operator uses floating-point values for boolean logic:
    /// - `0.0` is considered `false`
    /// - Any non-zero value is considered `true`
    ///
    /// # Short-Circuit Evaluation
    ///
    /// Only one branch is evaluated based on the condition result:
    /// - If condition is non-zero (true), only the true_branch is evaluated
    /// - If condition is zero (false), only the false_branch is evaluated
    ///
    /// # Operator Precedence
    ///
    /// The ternary operator has low precedence:
    /// - `a + b ? c : d * e` is interpreted as `(a + b) ? c : (d * e)`
    /// - Use parentheses for clarity when nesting operations
    Conditional {
        /// The condition expression to evaluate
        condition: &'arena AstExpr<'arena>,
        /// Expression to evaluate if condition is true (non-zero)
        true_branch: &'arena AstExpr<'arena>,
        /// Expression to evaluate if condition is false (zero)
        false_branch: &'arena AstExpr<'arena>,
    },
}

// AST cache type - REMOVED: Incompatible with arena allocation
// pub type AstCacheMap = FnvIndexMap<HString, alloc::rc::Rc<AstExpr>, EXP_RS_MAX_AST_CACHE>;

// Helper trait for string conversion to heapless strings
pub trait TryIntoHeaplessString {
    fn try_into_heapless(self) -> Result<HString, crate::error::ExprError>;
}

impl TryIntoHeaplessString for &str {
    fn try_into_heapless(self) -> Result<HString, crate::error::ExprError> {
        HString::try_from(self).map_err(|_| {
            crate::error::ExprError::StringTooLong(self.to_string(), EXP_RS_MAX_KEY_LENGTH)
        })
    }
}

impl TryIntoHeaplessString for alloc::string::String {
    fn try_into_heapless(self) -> Result<HString, crate::error::ExprError> {
        HString::try_from(self.as_str())
            .map_err(|_| crate::error::ExprError::StringTooLong(self, EXP_RS_MAX_KEY_LENGTH))
    }
}

// Helper trait for function names
pub trait TryIntoFunctionName {
    fn try_into_function_name(self) -> Result<FunctionName, crate::error::ExprError>;
}

impl TryIntoFunctionName for &str {
    fn try_into_function_name(self) -> Result<FunctionName, crate::error::ExprError> {
        FunctionName::try_from(self).map_err(|_| {
            crate::error::ExprError::StringTooLong(
                self.to_string(),
                EXP_RS_MAX_FUNCTION_NAME_LENGTH,
            )
        })
    }
}

impl TryIntoFunctionName for alloc::string::String {
    fn try_into_function_name(self) -> Result<FunctionName, crate::error::ExprError> {
        FunctionName::try_from(self.as_str()).map_err(|_| {
            crate::error::ExprError::StringTooLong(self, EXP_RS_MAX_FUNCTION_NAME_LENGTH)
        })
    }
}

impl<'arena> AstExpr<'arena> {
    /// Helper method that raises a constant expression to a power.
    ///
    /// This is primarily used in testing to evaluate power operations on constants.
    /// For non-constant expressions, it returns 0.0 as a default value.
    ///
    /// # Parameters
    ///
    /// * `exp` - The exponent to raise the constant to
    ///
    /// # Returns
    ///
    /// The constant raised to the given power, or 0.0 for non-constant expressions
    pub fn pow(&self, exp: Real) -> Real {
        match self {
            AstExpr::Constant(val) => {
                #[cfg(all(feature = "libm", feature = "f32"))]
                {
                    libm::powf(*val, *exp)
                }
                #[cfg(all(feature = "libm", not(feature = "f32")))]
                {
                    libm::pow(*val, exp)
                }
                #[cfg(all(not(feature = "libm"), test))]
                {
                    val.powf(*exp)
                } // Use std::powf when in test mode
                #[cfg(all(not(feature = "libm"), not(test)))]
                {
                    // Without libm and not in tests, limited power implementation
                    if exp == 0.0 {
                        1.0
                    } else if exp == 1.0 {
                        *val
                    } else if exp == 2.0 {
                        *val * *val
                    } else {
                        0.0
                    } // This functionality requires explicit registration
                }
            }
            _ => 0.0, // Default for non-constant expressions
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::context::EvalContext;
    use crate::error::ExprError;
    use crate::eval::eval_ast;
    use bumpalo::Bump;

    use std::rc::Rc;

    #[test]
    fn test_eval_ast_array_and_attribute_errors() {
        let arena = Bump::new();

        // Array not found
        let index_ast = arena.alloc(AstExpr::Constant(0.0));
        let ast = AstExpr::Array {
            name: "arr",
            index: index_ast,
        };
        let err = eval_ast(&ast, None, &arena).unwrap_err();
        match err {
            ExprError::UnknownVariable { name } => assert_eq!(name, "arr"),
            _ => panic!("Expected UnknownVariable error"),
        }
        // Attribute not found
        let ast2 = AstExpr::Attribute {
            base: "foo",
            attr: "bar",
        };
        let err2 = eval_ast(&ast2, None, &arena).unwrap_err();
        match err2 {
            ExprError::AttributeNotFound { base, attr } => {
                assert_eq!(base, "foo");
                assert_eq!(attr, "bar");
            }
            _ => panic!("Expected AttributeNotFound error"),
        }
    }

    #[test]
    fn test_eval_ast_function_wrong_arity() {
        let arena = Bump::new();

        // Create a context that has 'sin' registered
        let mut ctx = EvalContext::new();

        // Register sin function that takes exactly 1 argument
        let _ = ctx.register_native_function("sin", 1, |args| args[0].sin());
        let ctx = Rc::new(ctx);

        // Create AST for sin with 2 args (should be 1)
        let args = arena.alloc([AstExpr::Constant(1.0), AstExpr::Constant(2.0)]);
        let ast = AstExpr::Function {
            name: "sin",
            args: args,
        };

        // Should give InvalidFunctionCall error because sin takes 1 arg but we gave 2
        let err = eval_ast(&ast, Some(ctx), &arena).unwrap_err();
        match err {
            ExprError::InvalidFunctionCall {
                name,
                expected,
                found,
            } => {
                assert_eq!(name, "sin");
                assert_eq!(expected, 1);
                assert_eq!(found, 2);
            }
            _ => panic!("Expected InvalidFunctionCall error"),
        }
    }

    #[test]
    fn test_eval_ast_unknown_function_and_variable() {
        let arena = Bump::new();

        // Unknown function
        let args = arena.alloc([AstExpr::Constant(1.0)]);
        let ast = AstExpr::Function {
            name: "notafunc",
            args: args,
        };
        let err = eval_ast(&ast, None, &arena).unwrap_err();
        match err {
            ExprError::UnknownFunction { name } => assert_eq!(name, "notafunc"),
            _ => panic!("Expected UnknownFunction error"),
        }
        // Unknown variable
        let ast2 = AstExpr::Variable("notavar");
        let err2 = eval_ast(&ast2, None, &arena).unwrap_err();
        match err2 {
            ExprError::UnknownVariable { name } => assert_eq!(name, "notavar"),
            _ => panic!("Expected UnknownVariable error"),
        }
    }
}

/// Classifies the kind of expression node in the AST.
///
/// This enum is used to categorize expression nodes at a higher level than the specific
/// AST node variants, making it easier to determine the general type of an expression
/// without matching on all variants.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ExprKind {
    /// A constant numerical value.
    Constant,

    /// A variable reference.
    Variable,

    /// A function call with a specific arity (number of arguments).
    Function {
        /// Number of arguments the function takes
        arity: usize,
    },

    /// An array element access.
    Array,

    /// An object attribute access.
    Attribute,

    /// A logical operation (AND/OR).
    LogicalOp,

    /// A conditional (ternary) operation.
    Conditional,
}

/// Classifies the kind of token produced during lexical analysis.
///
/// These token types are used by the lexer to categorize different elements
/// in the expression string during the parsing phase.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum TokenKind {
    /// A numerical literal.
    Number,

    /// A variable identifier.
    Variable,

    /// An operator such as +, -, *, /, ^, etc.
    Operator,

    /// An opening delimiter like '(' or '['.
    Open,

    /// A closing delimiter like ')' or ']'.
    Close,

    /// A separator between items, typically a comma.
    Separator,

    /// End of the expression.
    End,

    /// An error token representing invalid input.
    Error,

    /// A null or placeholder token.
    Null,
}

/*
    All legacy bitmasking, ExprType, and OperatorKind have been removed.
    All parser and evaluator logic now uses AstExpr and enums only.
    The old Expr struct and related types are no longer present.
    Next: Update and simplify the test suite to use the new AST parser and evaluator.
*/

/// Defines the type of logical operation.
///
/// Used by the `LogicalOp` variant of `AstExpr` to specify which logical operation
/// should be performed with short-circuit evaluation semantics.
///
/// # Short-Circuit Evaluation
///
/// Short-circuit evaluation is an optimization technique where the second operand
/// of a logical operation is evaluated only when necessary:
///
/// - For `&&` (AND): If the left operand is false, the result is false regardless
///   of the right operand, so the right operand is not evaluated.
///
/// - For `||` (OR): If the left operand is true, the result is true regardless
///   of the right operand, so the right operand is not evaluated.
///
/// This behavior is particularly useful for:
///
/// 1. Performance optimization - avoid unnecessary calculation
/// 2. Conditional execution - control evaluation of expressions
/// 3. Safe guards - prevent errors (e.g., division by zero)
///
/// # Boolean Representation
///
/// In this expression engine, boolean values are represented as floating-point numbers:
///
/// - `0.0` represents `false`
/// - Any non-zero value (typically `1.0`) represents `true`
#[derive(Clone, Debug, PartialEq)]
pub enum LogicalOperator {
    /// Logical AND (&&) - evaluates to true only if both operands are true.
    /// Short-circuits if the left operand is false.
    ///
    /// Examples:
    /// - `1 && 1` evaluates to `1.0` (true)
    /// - `1 && 0` evaluates to `0.0` (false)
    /// - `0 && expr` evaluates to `0.0` without evaluating `expr`
    And,

    /// Logical OR (||) - evaluates to true if either operand is true.
    /// Short-circuits if the left operand is true.
    ///
    /// Examples:
    /// - `1 || 0` evaluates to `1.0` (true)
    /// - `0 || 0` evaluates to `0.0` (false)
    /// - `1 || expr` evaluates to `1.0` without evaluating `expr`
    Or,
}

/// Implements Display for LogicalOperator to use in error messages.
impl core::fmt::Display for LogicalOperator {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            LogicalOperator::And => write!(f, "&&"),
            LogicalOperator::Or => write!(f, "||"),
        }
    }
}

/// Represents a native Rust function that can be registered with the evaluation context.
///
/// Native functions allow users to extend the expression evaluator with custom
/// functionality written in Rust. These functions can be called from within expressions
/// like any built-in function.
///
/// # Example
///
/// ```
/// # use exp_rs::{EvalContext, Real};
/// # use exp_rs::engine::interp;
/// # use std::rc::Rc;
/// let mut ctx = EvalContext::new();
///
/// // Register a custom function that calculates the hypotenuse
/// ctx.register_native_function(
///     "hypotenuse",     // Function name
///     2,                // Takes 2 arguments
///     |args: &[Real]| { // Implementation
///         (args[0] * args[0] + args[1] * args[1]).sqrt()
///     }
/// );
///
/// // Use the function in an expression
/// let result = interp("hypotenuse(3, 4)", Some(Rc::new(ctx))).unwrap();
/// assert_eq!(result, 5.0);
/// ```
#[derive(Clone)]
pub struct NativeFunction {
    /// Number of arguments the function takes.
    pub arity: usize,

    /// The actual implementation of the function as a Rust closure.
    pub implementation: Rc<dyn Fn(&[Real]) -> Real>,

    /// The name of the function as it will be used in expressions.
    pub name: FunctionName,

    /// Optional description of what the function does.
    pub description: Option<String>,
}

/* We can't derive Clone for NativeFunction because Box<dyn Fn> doesn't implement Clone.
Instead, we provide a shallow clone in context.rs for EvalContext, which is safe for read-only use.
Do NOT call .clone() on NativeFunction directly. */

use alloc::borrow::Cow;

/// Represents a function defined by an expression string rather than Rust code.
///
/// Expression functions allow users to define custom functions using the expression
/// language itself. These functions are compiled once when registered and can be called
/// from other expressions. They support parameters and can access variables from the
/// evaluation context.
///
/// # Example
///
/// ```
/// # use exp_rs::{EvalContext, Real};
/// # use exp_rs::engine::interp;
/// # use std::rc::Rc;
/// let mut ctx = EvalContext::new();
///
/// // Note: Expression functions require runtime parsing which is not supported
/// // in the current arena-based architecture. Use native functions instead:
/// ctx.register_native_function("circle_area", 1, |args| {
///     let radius = args[0];
///     std::f64::consts::PI * radius * radius
/// }).unwrap();
///
/// // Use the function in another expression
/// let result = interp("circle_area(2)", Some(Rc::new(ctx))).unwrap();
/// assert!(result > 12.56 && result < 12.57); // π * 4 ≈ 12.566
/// ```
pub struct ExpressionFunction {
    /// The name of the function as it will be used in expressions.
    pub name: FunctionName,

    /// The parameter names that the function accepts.
    pub params: Vec<String>,

    /// The original expression string defining the function body.
    pub expression: String,

    /// Optional description of what the function does.
    pub description: Option<String>,

    /// Pre-allocated parameter buffer for zero-allocation evaluation.
    /// When available, this points to an arena-allocated slice that can be reused
    /// for every function call instead of allocating new parameter storage.
    /// The slice size matches params.len() and gets filled with actual values during evaluation.
    pub param_buffer: Option<*mut [(crate::types::HString, crate::Real)]>,
}

impl Clone for ExpressionFunction {
    fn clone(&self) -> Self {
        Self {
            name: self.name.clone(),
            params: self.params.clone(),
            expression: self.expression.clone(),
            description: self.description.clone(),
            param_buffer: self.param_buffer, // Share the same buffer pointer
        }
    }
}

/// Internal representation of a variable in the evaluation system.
///
/// This is an implementation detail and should not be used directly by library users.
/// Variables are normally managed through the `EvalContext` interface.
#[doc(hidden)]
pub struct Variable<'a> {
    /// The name of the variable.
    pub name: Cow<'a, str>,

    /// Internal address/identifier for the variable.
    pub address: i8,

    /// Function associated with the variable (if any).
    pub function: fn(Real, Real) -> Real,

    /// Context or associated AST nodes.
    pub context: Vec<AstExpr<'a>>,
}

impl<'a> Variable<'a> {
    /// Creates a new variable with the given name and default values.
    pub fn new(name: &'a str) -> Variable<'a> {
        Variable {
            name: Cow::Borrowed(name),
            address: 0,
            function: crate::functions::dummy,
            context: Vec::<AstExpr<'a>>::new(),
        }
    }
}