Skip to content

Commit 45493e3

Browse files
committed
Refactor Eval
1 parent 1b643a4 commit 45493e3

4 files changed

Lines changed: 41 additions & 18 deletions

File tree

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,25 @@ expr-solver-bin = "1.0.2"
3535
### Basic Example
3636

3737
```rust
38-
use expr_solver::{Eval};
38+
use expr_solver::Eval;
3939

4040
fn main() {
41-
let mut eval = Eval::new("2+3*4");
41+
// Quick one-liner evaluation
42+
match Eval::evaluate("2+3*4") {
43+
Ok(result) => println!("Result: {}", result),
44+
Err(e) => eprintln!("Error: {}", e),
45+
}
46+
47+
// Or create an evaluator instance for more control
48+
let mut eval = Eval::new("sqrt(16) + pi");
4249
match eval.run() {
4350
Ok(result) => println!("Result: {}", result),
4451
Err(e) => eprintln!("Error: {}", e),
4552
}
4653
}
4754
```
4855

49-
This will evaluate the hardcoded expression `2 + 3 * 4` and print the result.
56+
This will evaluate mathematical expressions and print the results.
5057

5158
## Testing
5259

bin/src/main.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,13 @@ fn run() -> Result<(), String> {
6666

6767
// load either from string input or a file
6868
let mut eval = if let Some(expr) = args.expression.as_ref().or(args.expr.as_ref()) {
69-
Eval::new(expr)
69+
Eval::with_table(expr, table)
7070
} else if let Some(input) = &args.input {
71-
Eval::new_from_file(input.clone())
71+
Eval::from_file_with_table(input.clone(), table)
7272
} else {
7373
return Err("no input".to_string());
7474
};
7575

76-
eval.with_table(table);
77-
7876
if args.assembly {
7977
let program = eval.build_program()?;
8078
print!("{}", program.get_assembly());

lib/src/lib.rs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,36 +68,55 @@ enum EvalSource<'str> {
6868
#[derive(Debug)]
6969
pub struct Eval<'str> {
7070
source: EvalSource<'str>,
71-
table: Option<SymTable>,
71+
table: SymTable,
7272
}
7373

7474
impl<'str> Eval<'str> {
75+
/// Quick evaluation of an expression with default standard library
76+
pub fn evaluate(expression: &'str str) -> Result<Decimal, String> {
77+
Self::new(expression).run()
78+
}
79+
80+
/// Create a new evaluator with an expression string
7581
pub fn new(string: &'str str) -> Self {
82+
Self::with_table(string, SymTable::stdlib())
83+
}
84+
85+
/// Create a new evaluator with an expression string and custom symbol table
86+
pub fn with_table(string: &'str str, table: SymTable) -> Self {
7687
let source = Source::new(string);
7788
Self {
7889
source: EvalSource::Source(Cow::Owned(source)),
79-
table: None,
90+
table,
8091
}
8192
}
8293

94+
/// Create a new evaluator from a Source reference
8395
pub fn new_from_source(source: &'str Source<'str>) -> Self {
96+
Self::from_source_with_table(source, SymTable::stdlib())
97+
}
98+
99+
/// Create a new evaluator from a Source reference with custom symbol table
100+
pub fn from_source_with_table(source: &'str Source<'str>, table: SymTable) -> Self {
84101
Self {
85102
source: EvalSource::Source(Cow::Borrowed(source)),
86-
table: None,
103+
table,
87104
}
88105
}
89106

107+
/// Create a new evaluator from a compiled binary file
90108
pub fn new_from_file(path: PathBuf) -> Self {
109+
Self::from_file_with_table(path, SymTable::stdlib())
110+
}
111+
112+
/// Create a new evaluator from a compiled binary file with custom symbol table
113+
pub fn from_file_with_table(path: PathBuf, table: SymTable) -> Self {
91114
Self {
92115
source: EvalSource::File(path),
93-
table: None,
116+
table,
94117
}
95118
}
96119

97-
pub fn with_table(&mut self, table: SymTable) {
98-
self.table = Some(table);
99-
}
100-
101120
pub fn run(&mut self) -> Result<Decimal, String> {
102121
let program = self.build_program()?;
103122
Vm::default().run(&program).map_err(|err| err.to_string())
@@ -110,7 +129,7 @@ impl<'str> Eval<'str> {
110129
}
111130

112131
pub fn build_program(&mut self) -> Result<Program<'_>, String> {
113-
let table = self.table.get_or_insert_with(SymTable::stdlib);
132+
let table = &self.table;
114133
match &self.source {
115134
EvalSource::Source(source) => {
116135
let mut parser = Parser::new(source);

lib/tests/integration_tests.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ fn eval_err(expr: &str) -> String {
1919

2020
// Helper function to evaluate an expression with a custom symbol table and expect an Ok result.
2121
fn eval_with_custom_table_ok(expr: &str, table: SymTable) -> Decimal {
22-
let mut eval = Eval::new(expr);
23-
eval.with_table(table);
22+
let mut eval = Eval::with_table(expr, table);
2423
eval.run().expect("Evaluation should be successful")
2524
}
2625

0 commit comments

Comments
 (0)