use crate::{
error::EvalexprResultValue,
token, tree,
value::{
numeric_types::{default_numeric_types::DefaultNumericTypes, EvalexprNumericTypes},
TupleType,
},
Context, ContextWithMutableVariables, EmptyType, EvalexprError, EvalexprResult, HashMapContext,
Node, Value, EMPTY_VALUE,
};
pub fn eval(string: &str) -> EvalexprResultValue {
eval_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
}
pub fn eval_with_context<C: Context>(
string: &str,
context: &C,
) -> EvalexprResultValue<C::NumericTypes> {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context(context)
}
pub fn eval_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResultValue<C::NumericTypes> {
tree::tokens_to_operator_tree(token::tokenize(string)?)?.eval_with_context_mut(context)
}
pub fn build_operator_tree<NumericTypes: EvalexprNumericTypes>(
string: &str,
) -> EvalexprResult<Node<NumericTypes>, NumericTypes> {
tree::tokens_to_operator_tree(token::tokenize(string)?)
}
pub fn eval_string(string: &str) -> EvalexprResult<String> {
eval_string_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
}
pub fn eval_int(
string: &str,
) -> EvalexprResult<<DefaultNumericTypes as EvalexprNumericTypes>::Int> {
eval_int_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
}
pub fn eval_float(
string: &str,
) -> EvalexprResult<<DefaultNumericTypes as EvalexprNumericTypes>::Float> {
eval_float_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
}
pub fn eval_number(
string: &str,
) -> EvalexprResult<<DefaultNumericTypes as EvalexprNumericTypes>::Float> {
eval_number_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
}
pub fn eval_boolean(string: &str) -> EvalexprResult<bool> {
eval_boolean_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
}
pub fn eval_tuple(string: &str) -> EvalexprResult<TupleType> {
eval_tuple_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
}
pub fn eval_empty(string: &str) -> EvalexprResult<EmptyType> {
eval_empty_with_context_mut(string, &mut HashMapContext::<DefaultNumericTypes>::new())
}
pub fn eval_string_with_context<C: Context>(
string: &str,
context: &C,
) -> EvalexprResult<String, C::NumericTypes> {
match eval_with_context(string, context) {
Ok(Value::String(string)) => Ok(string),
Ok(value) => Err(EvalexprError::expected_string(value)),
Err(error) => Err(error),
}
}
pub fn eval_int_with_context<C: Context>(
string: &str,
context: &C,
) -> EvalexprResult<<C::NumericTypes as EvalexprNumericTypes>::Int, C::NumericTypes> {
match eval_with_context(string, context) {
Ok(Value::Int(int)) => Ok(int),
Ok(value) => Err(EvalexprError::expected_int(value)),
Err(error) => Err(error),
}
}
pub fn eval_float_with_context<C: Context>(
string: &str,
context: &C,
) -> EvalexprResult<<C::NumericTypes as EvalexprNumericTypes>::Float, C::NumericTypes> {
match eval_with_context(string, context) {
Ok(Value::Float(float)) => Ok(float),
Ok(value) => Err(EvalexprError::expected_float(value)),
Err(error) => Err(error),
}
}
pub fn eval_number_with_context<C: Context>(
string: &str,
context: &C,
) -> EvalexprResult<<C::NumericTypes as EvalexprNumericTypes>::Float, C::NumericTypes> {
match eval_with_context(string, context) {
Ok(Value::Float(float)) => Ok(float),
Ok(Value::Int(int)) => Ok(<C::NumericTypes as EvalexprNumericTypes>::int_as_float(
&int,
)),
Ok(value) => Err(EvalexprError::expected_number(value)),
Err(error) => Err(error),
}
}
pub fn eval_boolean_with_context<C: Context>(
string: &str,
context: &C,
) -> EvalexprResult<bool, C::NumericTypes> {
match eval_with_context(string, context) {
Ok(Value::Boolean(boolean)) => Ok(boolean),
Ok(value) => Err(EvalexprError::expected_boolean(value)),
Err(error) => Err(error),
}
}
pub fn eval_tuple_with_context<C: Context>(
string: &str,
context: &C,
) -> EvalexprResult<TupleType<C::NumericTypes>, C::NumericTypes> {
match eval_with_context(string, context) {
Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(EvalexprError::expected_tuple(value)),
Err(error) => Err(error),
}
}
pub fn eval_empty_with_context<C: Context>(
string: &str,
context: &C,
) -> EvalexprResult<EmptyType, C::NumericTypes> {
match eval_with_context(string, context) {
Ok(Value::Empty) => Ok(EMPTY_VALUE),
Ok(value) => Err(EvalexprError::expected_empty(value)),
Err(error) => Err(error),
}
}
pub fn eval_string_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<String, C::NumericTypes> {
match eval_with_context_mut(string, context) {
Ok(Value::String(string)) => Ok(string),
Ok(value) => Err(EvalexprError::expected_string(value)),
Err(error) => Err(error),
}
}
pub fn eval_int_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<<C::NumericTypes as EvalexprNumericTypes>::Int, C::NumericTypes> {
match eval_with_context_mut(string, context) {
Ok(Value::Int(int)) => Ok(int),
Ok(value) => Err(EvalexprError::expected_int(value)),
Err(error) => Err(error),
}
}
pub fn eval_float_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<<C::NumericTypes as EvalexprNumericTypes>::Float, C::NumericTypes> {
match eval_with_context_mut(string, context) {
Ok(Value::Float(float)) => Ok(float),
Ok(value) => Err(EvalexprError::expected_float(value)),
Err(error) => Err(error),
}
}
pub fn eval_number_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<<C::NumericTypes as EvalexprNumericTypes>::Float, C::NumericTypes> {
match eval_with_context_mut(string, context) {
Ok(Value::Float(float)) => Ok(float),
Ok(Value::Int(int)) => Ok(<C::NumericTypes as EvalexprNumericTypes>::int_as_float(
&int,
)),
Ok(value) => Err(EvalexprError::expected_number(value)),
Err(error) => Err(error),
}
}
pub fn eval_boolean_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<bool, C::NumericTypes> {
match eval_with_context_mut(string, context) {
Ok(Value::Boolean(boolean)) => Ok(boolean),
Ok(value) => Err(EvalexprError::expected_boolean(value)),
Err(error) => Err(error),
}
}
pub fn eval_tuple_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<TupleType<C::NumericTypes>, C::NumericTypes> {
match eval_with_context_mut(string, context) {
Ok(Value::Tuple(tuple)) => Ok(tuple),
Ok(value) => Err(EvalexprError::expected_tuple(value)),
Err(error) => Err(error),
}
}
pub fn eval_empty_with_context_mut<C: ContextWithMutableVariables>(
string: &str,
context: &mut C,
) -> EvalexprResult<EmptyType, C::NumericTypes> {
match eval_with_context_mut(string, context) {
Ok(Value::Empty) => Ok(EMPTY_VALUE),
Ok(value) => Err(EvalexprError::expected_empty(value)),
Err(error) => Err(error),
}
}