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
//! A mathematical expression evaluator library with bytecode compilation.
//!
//! This library provides a complete compiler pipeline for mathematical expressions,
//! from parsing to bytecode execution on a stack-based virtual machine.
//!
//! # Features
//!
//! - **Flexible numeric types** - Choose between f64 (default) or 128-bit Decimal precision
//! - **Rich error messages** - Parse errors with syntax highlighting
//! - **Bytecode compilation** - Compile once, execute many times
//! - **Custom symbols** - Add your own constants and functions
//! - **Local constants** - `let` ... `then` syntax for declaring scoped constants
//! - **Control flow** - Conditional expressions with `if(condition, then, else)`
//! - **Serialization** - Save/load compiled programs (requires `serialization` feature)
//!
//! ## Numeric Type Selection
//!
//! The library supports two numeric backends via feature flags:
//!
//! - **`f64-floats`** (default) - Standard f64 floating-point arithmetic. Faster and simpler,
//! suitable for most use cases. Allows Inf and NaN results.
//! - **`decimal-precision`** - 128-bit Decimal arithmetic for high precision. No floating-point
//! errors, checked arithmetic with overflow detection. Use for financial calculations or when
//! exact decimal representation is required.
//!
//! **Note**: Only one numeric backend can be enabled at a time.
//!
//! # Quick Start
//!
//! ```
//! use expr_solver::{eval, num};
//!
//! let result = eval("2 + 3 * 4").unwrap();
//! assert_eq!(result, num!(14));
//! ```
//!
//! ## Working with Numbers
//!
//! Use the [`num!`] macro to create numeric values that work with both f64 and Decimal backends:
//!
//! ```
//! use expr_solver::num;
//!
//! let x = num!(42); // Works with any backend
//! let y = num!(3.14); // Supports decimals
//! let z = num!(-10); // And negative numbers
//! ```
//!
//! The macro ensures your code works regardless of which numeric backend is enabled.
//!
//! # Custom Symbols
//!
//! ```
//! use expr_solver::{num, eval_with_table, SymTable};
//!
//! let mut table = SymTable::stdlib();
//! table.add_const("x", num!(10), false).unwrap();
//!
//! let result = eval_with_table("x * 2", table).unwrap();
//! assert_eq!(result, num!(20));
//! ```
//!
//! # Advanced: Type-State Pattern
//!
//! The `Program` type uses the type-state pattern to enforce correct usage:
//!
//! ```
//! use expr_solver::{num, SymTable, Program};
//!
//! // Compile expression to bytecode
//! let program = Program::new_from_source("x + y").unwrap();
//!
//! // Link with symbol table (validated at link time)
//! let mut table = SymTable::new();
//! table.add_const("x", num!(10), false).unwrap();
//! table.add_const("y", num!(5), false).unwrap();
//!
//! let mut linked = program.link(table).unwrap();
//!
//! // Execute
//! let result = linked.execute().unwrap();
//! assert_eq!(result, num!(15));
//! ```
//!
//! # Local Constants with LET THEN
//!
//! Declare local constants using `let` ... `then` syntax:
//!
//! ```
//! use expr_solver::{eval, num};
//!
//! // Single declaration
//! let result = eval("let x = 10 then x * 2").unwrap();
//! assert_eq!(result, num!(20));
//!
//! // Multiple declarations
//! let result = eval("let x = 5, y = 3 then x + y").unwrap();
//! assert_eq!(result, num!(8));
//!
//! // Reference previous declarations
//! let result = eval("let x = 2, y = x * 3, z = y + 1 then z").unwrap();
//! assert_eq!(result, num!(7));
//!
//! // Use with globals and functions
//! let result = eval("let r = 5, area = pi * r ^ 2 then area").unwrap();
//! assert!((result - num!(78.53981633974483)).abs() < num!(0.0001));
//! ```
//!
//! **Notes:**
//! - Local constants are evaluated left-to-right
//! - Can reference previously declared locals and globals
//! - Cannot reference forward (e.g., `let x = y, y = 1 then x` is an error)
//! - Cannot shadow global constants (e.g., `let pi = 3 then pi` is an error)
//! - Duplicate names in the same `let` block are errors
//!
//! # Supported Operators
//!
//! - Arithmetic: `+`, `-`, `*`, `/`, `^` (power), `!` (factorial), unary `-`
//! - Comparison: `==`, `!=`, `<`, `<=`, `>`, `>=` (return 1 or 0)
//! - Grouping: `(` `)`
//! - Control flow: `if(condition, then_value, else_value)`
//!
//! # Built-in Functions
//!
//! See [`SymTable::stdlib()`] for the complete list of built-in functions and constants.
//!
//! # Architecture
//!
//! The library uses a multi-stage compilation pipeline:
//!
//! 1. **Parsing** ([`Parser`]) - Source code → Abstract Syntax Tree (AST)
//! 2. **IR Generation** ([`IrBuilder`]) - AST → Bytecode + Symbol metadata
//! 3. **Linking** ([`Linker`]) - Bytecode + SymTable → Linked bytecode
//! 4. **Execution** ([`Vm`]) - Linked bytecode → Result
//!
//! Each stage has its own error type ([`ParseError`], [`IrError`], [`LinkerError`], [`VmError`])
//! which automatically converts to [`ProgramError`] for convenience.
// Core types (shared)
// Expression solver implementation
// Public API
pub use ;
pub use ;
pub use ;
pub use ;
pub use Parser;
pub use ;
pub use ;
pub use SymTable;
pub use ;
// ============================================================================
// Helper functions for evaluating expressions
// ============================================================================
/// Evaluates an expression string with the standard library.
///
/// # Examples
///
/// ```
/// use expr_solver::{eval, num};
///
/// let result = eval("2 + 3 * 4").unwrap();
/// assert_eq!(result, num!(14));
/// ```
/// Evaluates an expression string with a custom symbol table.
///
/// # Examples
///
/// ```
/// use expr_solver::{num, eval_with_table, SymTable};
///
/// let mut table = SymTable::stdlib();
/// table.add_const("x", num!(42), false).unwrap();
///
/// let result = eval_with_table("x * 2", table).unwrap();
/// assert_eq!(result, num!(84));
/// ```
/// Evaluates an expression from a binary file with the standard library.
///
/// # Examples
///
/// ```no_run
/// use expr_solver::eval_file;
///
/// let result = eval_file("expr.bin").unwrap();
/// ```
/// Evaluates an expression from a binary file with a custom symbol table.
///
/// # Examples
///
/// ```no_run
/// use expr_solver::{eval_file_with_table, SymTable};
///
/// let result = eval_file_with_table("expr.bin", SymTable::stdlib()).unwrap();
/// ```