@@ -68,36 +68,55 @@ enum EvalSource<'str> {
6868#[ derive( Debug ) ]
6969pub struct Eval < ' str > {
7070 source : EvalSource < ' str > ,
71- table : Option < SymTable > ,
71+ table : SymTable ,
7272}
7373
7474impl < ' 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) ;
0 commit comments