pub struct Context<'ctx, T = DefaultTypeSet>where
T: TypeSet,{ /* private fields */ }Expand description
The expression context, which holds variables, functions, and other state needed for evaluation.
Implementations§
Source§impl<'ctx> Context<'ctx, DefaultTypeSet>
impl<'ctx> Context<'ctx, DefaultTypeSet>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new context with default types.
Sourcepub fn parse(source: &'ctx str) -> Result<Self, ExpressionError<'ctx>>
pub fn parse(source: &'ctx str) -> Result<Self, ExpressionError<'ctx>>
Loads the given program into a new context with default types.
Source§impl<'ctx, T> Context<'ctx, T>where
T: TypeSet,
impl<'ctx, T> Context<'ctx, T>where
T: TypeSet,
Sourcepub fn new_with_types() -> Self
pub fn new_with_types() -> Self
Creates a new context. The type set must be specified when using this function.
use somni_expr::{Context, TypeSet32};
let mut ctx = Context::<TypeSet32>::new_with_types();Sourcepub fn parse_with_types(
source: &'ctx str,
) -> Result<Self, ExpressionError<'ctx>>
pub fn parse_with_types( source: &'ctx str, ) -> Result<Self, ExpressionError<'ctx>>
Parses the given program into a new context. The type set must be specified when using this function.
use somni_expr::{Context, TypeSet32};
let mut ctx = Context::<TypeSet32>::parse_with_types("// program source comes here").unwrap();Sourcepub fn new_from_program(source: &'ctx str, program: Program<T::Parser>) -> Self
pub fn new_from_program(source: &'ctx str, program: Program<T::Parser>) -> Self
Loads the given program into a new context.
Sourcepub fn evaluate<'s, V>(
&'s mut self,
source: &'s str,
) -> Result<V::Output, ExpressionError<'s>>where
V: LoadOwned<T>,
pub fn evaluate<'s, V>(
&'s mut self,
source: &'s str,
) -> Result<V::Output, ExpressionError<'s>>where
V: LoadOwned<T>,
Parses and evaluates an expression and returns the result as a specific value type.
This function will attempt to convert the result of the expression to the specified type V.
If the conversion fails, it will return an ExpressionError.
use somni_expr::{Context, TypedValue};
let mut context = Context::new();
assert_eq!(context.evaluate::<u64>("1 + 2"), Ok(3));
assert_eq!(context.evaluate::<TypedValue>("1 + 2"), Ok(TypedValue::Int(3)));Sourcepub fn evaluate_parsed<'s, V>(
&'s mut self,
source: &'s str,
expression: &Expression<T::Parser>,
) -> Result<V::Output, ExpressionError<'s>>where
V: LoadOwned<T>,
pub fn evaluate_parsed<'s, V>(
&'s mut self,
source: &'s str,
expression: &Expression<T::Parser>,
) -> Result<V::Output, ExpressionError<'s>>where
V: LoadOwned<T>,
Evaluates a pre-parsed expression and returns the result as a specific value type.
This function will attempt to convert the result of the expression to the specified type V.
If the conversion fails, it will return an ExpressionError.
use somni_expr::{Context, TypedValue};
let mut context = Context::new();
let source = "1 + 2";
let expr = somni_parser::parser::parse_expression(source).unwrap();
assert_eq!(context.evaluate_parsed::<u64>(source, &expr), Ok(3));
assert_eq!(context.evaluate_parsed::<TypedValue>(source, &expr), Ok(TypedValue::Int(3)));Sourcepub fn add_variable<V>(&mut self, name: &'ctx str, value: V)where
V: LoadStore<T>,
pub fn add_variable<V>(&mut self, name: &'ctx str, value: V)where
V: LoadStore<T>,
Defines a new variable in the context.
The variable can be any type from the current TypeSet, even TypedValue.
The variable will act as a global variable in the context of the program. Its value can be changed by expressions.
use somni_expr::{Context, TypedValue};
let mut context = Context::new();
// Variable does not exist, it can't be assigned:
assert!(context.evaluate::<()>("counter = 0").is_err());
context.add_variable::<u64>("counter", 0);
// Variable exists now, so we can use it:
assert_eq!(context.evaluate::<()>("counter = counter + 1"), Ok(()));
assert_eq!(context.evaluate::<u64>("counter"), Ok(1));Sourcepub fn add_function<F, A>(&mut self, name: &'ctx str, func: F)where
F: DynFunction<A, T> + 'ctx,
pub fn add_function<F, A>(&mut self, name: &'ctx str, func: F)where
F: DynFunction<A, T> + 'ctx,
Adds a new function to the context.
use somni_expr::{Context, TypedValue};
let mut context = Context::new();
context.add_function("plus_one", |x: u64| x + 1);
assert_eq!(context.evaluate::<u64>("plus_one(2)"), Ok(3));Trait Implementations§
Source§impl<'ctx> Default for Context<'ctx, DefaultTypeSet>
impl<'ctx> Default for Context<'ctx, DefaultTypeSet>
Source§impl<T> ExprContext<T> for Context<'_, T>where
T: TypeSet,
impl<T> ExprContext<T> for Context<'_, T>where
T: TypeSet,
Source§fn declare(&mut self, variable: &str, value: TypedValue<T>)
fn declare(&mut self, variable: &str, value: TypedValue<T>)
Declares a variable in the context.
Source§fn assign_variable(
&mut self,
variable: &str,
value: &TypedValue<T>,
) -> Result<(), Box<str>>
fn assign_variable( &mut self, variable: &str, value: &TypedValue<T>, ) -> Result<(), Box<str>>
Assigns a new value to a variable in the context.
Source§fn open_scope(&mut self)
fn open_scope(&mut self)
Opens a new scope in the current stack frame.
Source§fn close_scope(&mut self)
fn close_scope(&mut self)
Closes the last scope in the current stack frame.
Source§fn type_context(&mut self) -> &mut T
fn type_context(&mut self) -> &mut T
TypeSet.