4040//! The `Program` type uses the type-state pattern to enforce correct usage:
4141//!
4242//! ```
43- //! use expr_solver::{load, SymTable };
43+ //! use expr_solver::{SymTable, Program };
4444//! use rust_decimal_macros::dec;
4545//!
4646//! // Compile expression to bytecode
47- //! let program = load ("x + y").unwrap();
47+ //! let program = Program::new_from_source ("x + y").unwrap();
4848//!
4949//! // Link with symbol table (validated at link time)
5050//! let mut table = SymTable::new();
@@ -109,8 +109,7 @@ pub use vm::{Vm, VmError};
109109/// assert_eq!(result.to_string(), "14");
110110/// ```
111111pub fn eval ( expression : & str ) -> Result < Decimal , String > {
112- let program = load_with_table ( expression, SymTable :: stdlib ( ) ) ?;
113- program. execute ( ) . map_err ( |err| err. to_string ( ) )
112+ eval_with_table ( expression, SymTable :: stdlib ( ) )
114113}
115114
116115/// Evaluates an expression string with a custom symbol table.
@@ -128,8 +127,12 @@ pub fn eval(expression: &str) -> Result<Decimal, String> {
128127/// assert_eq!(result, dec!(84));
129128/// ```
130129pub fn eval_with_table ( expression : & str , table : SymTable ) -> Result < Decimal , String > {
131- let program = load_with_table ( expression, table) ?;
132- program. execute ( ) . map_err ( |err| err. to_string ( ) )
130+ Program :: new_from_source ( expression)
131+ . map_err ( |err| err. to_string ( ) ) ?
132+ . link ( table)
133+ . map_err ( |err| err. to_string ( ) ) ?
134+ . execute ( )
135+ . map_err ( |err| err. to_string ( ) )
133136}
134137
135138/// Evaluates an expression from a binary file with the standard library.
@@ -159,34 +162,3 @@ pub fn eval_file_with_table(path: impl AsRef<str>, table: SymTable) -> Result<De
159162 let linked = program. link ( table) . map_err ( |err| err. to_string ( ) ) ?;
160163 linked. execute ( ) . map_err ( |err| err. to_string ( ) )
161164}
162-
163- /// Loads and compiles an expression, returning a compiled program.
164- ///
165- /// # Examples
166- ///
167- /// ```
168- /// use expr_solver::{load, SymTable};
169- ///
170- /// let program = load("2 + 3 * 4").unwrap();
171- /// let linked = program.link(SymTable::stdlib()).unwrap();
172- /// let result = linked.execute().unwrap();
173- /// assert_eq!(result.to_string(), "14");
174- /// ```
175- pub fn load ( expression : & str ) -> Result < Program < ' _ , Compiled > , String > {
176- Program :: new_from_source ( expression) . map_err ( |err| err. to_string ( ) )
177- }
178-
179- /// Loads, compiles, and links an expression, returning a ready-to-execute program.
180- ///
181- /// # Examples
182- ///
183- /// ```
184- /// use expr_solver::{load_with_table, SymTable};
185- ///
186- /// let program = load_with_table("sin(pi/2)", SymTable::stdlib()).unwrap();
187- /// let result = program.execute().unwrap();
188- /// ```
189- pub fn load_with_table ( expression : & str , table : SymTable ) -> Result < Program < ' _ , Linked > , String > {
190- let program = load ( expression) ?;
191- program. link ( table) . map_err ( |err| err. to_string ( ) )
192- }
0 commit comments