pub struct Expression { /* private fields */ }
Implementations§
Source§impl Expression
impl Expression
Sourcepub fn new(string: &str, symbols: SymbolTable) -> Result<Expression, ParseError>
pub fn new(string: &str, symbols: SymbolTable) -> Result<Expression, ParseError>
Compiles a new Expression
. Missing variables will lead to a
exprtk::ParseError
.
§Example:
The above example melts down to this:
use exprtk_rs::*;
let mut symbol_table = SymbolTable::new();
symbol_table.add_variable("a", 2.).unwrap();
let mut expr = Expression::new("a + 1", symbol_table).unwrap();
assert_eq!(expr.value(), 3.);
Sourcepub fn parse_vars(
string: &str,
symbols: SymbolTable,
) -> Result<(Expression, Vec<(String, usize)>), ParseError>
pub fn parse_vars( string: &str, symbols: SymbolTable, ) -> Result<(Expression, Vec<(String, usize)>), ParseError>
Compiles a new Expression
like Expression::new
. In addition, if
unknown variables are encountered, they are automatically added an internal SymbolTable
and initialized with 0.
. Their names and variable IDs are returned as tuples together
with the new Expression
instance.
Sourcepub fn handle_unknown<F>(
string: &str,
symbols: SymbolTable,
func: F,
) -> Result<Expression, ParseError>
pub fn handle_unknown<F>( string: &str, symbols: SymbolTable, func: F, ) -> Result<Expression, ParseError>
Handles unknown variables like Expression::parse_vars()
does, but instead of creating
a new SymbolTable
, an existing one can be supplied, which may already have some
variables defined. The variables are handled in a closure, which can register
the names as variables, constants, strings or vectors to the supplied symbol table.
§Example
use exprtk_rs::*;
let formula = "s_string[] + a + b";
let mut expr = Expression::handle_unknown(formula, SymbolTable::new(), |name, sym| {
if name.starts_with("s_") {
sym.add_stringvar(name, "string").unwrap();
} else {
sym.add_variable(name, 1.).unwrap();
}
Ok(())
}).unwrap();
assert_eq!(expr.value(), 8.);
Note: this function is very flexible, but can cause problems if not registering anything for a given name. In that case, the same variable name will be brought up again and again, infinite loop, ultimately resulting in a stack overflow.
Sourcepub fn value(&mut self) -> c_double
pub fn value(&mut self) -> c_double
Calculates the value of the expression. Returns NaN
if the expression was not yet
compiled.
Note: This method requires mutable access to the underlying expression object, since executing an expression can have side-effects. Variables in the symbol table of the expression can be changed or added.
Sourcepub fn symbols(&self) -> &SymbolTable
pub fn symbols(&self) -> &SymbolTable
Returns a reference to the symbol table owned by the Expression
Sourcepub fn symbols_mut(&mut self) -> &mut SymbolTable
pub fn symbols_mut(&mut self) -> &mut SymbolTable
Returns a mutable reference to the symbol table owned by the Expression
Trait Implementations§
Source§impl Clone for Expression
impl Clone for Expression
Source§fn clone(&self) -> Expression
fn clone(&self) -> Expression
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more