pub struct Engine<T: ModuleResolver = DefaultModuleResolver> { /* private fields */ }Expand description
The main execution engine for the mq.
The Engine manages parsing, optimization, and evaluation of mq code.
It provides methods for configuration, loading modules, and evaluating code.
§Examples
use mq_lang::DefaultEngine;
let mut engine = DefaultEngine::default();
engine.load_builtin_module();
let input = mq_lang::parse_text_input("hello").unwrap();
let result = engine.eval("add(\" world\")", input.into_iter());
assert_eq!(result.unwrap(), vec!["hello world".to_string().into()].into());Implementations§
Source§impl<T: ModuleResolver> Engine<T>
impl<T: ModuleResolver> Engine<T>
pub fn new(module_resolver: T) -> Self
Sourcepub fn set_optimization_level(&mut self, level: OptimizationLevel)
pub fn set_optimization_level(&mut self, level: OptimizationLevel)
Set the optimization level for AST transformations applied before evaluation.
Sourcepub fn set_max_call_stack_depth(&mut self, max_call_stack_depth: u32)
pub fn set_max_call_stack_depth(&mut self, max_call_stack_depth: u32)
Set the maximum call stack depth for function calls.
This prevents infinite recursion by limiting how deep function calls can be nested. Useful for controlling resource usage.
Sourcepub fn set_search_paths(&mut self, paths: Vec<PathBuf>)
pub fn set_search_paths(&mut self, paths: Vec<PathBuf>)
Set search paths for module loading.
These paths will be searched when loading external modules
via the include statement in mq code.
Sourcepub fn define_string_value(&self, name: &str, value: &str)
pub fn define_string_value(&self, name: &str, value: &str)
Define a string variable that can be used in mq code.
This allows you to inject values from the host environment into the mq execution context.
Sourcepub fn define_value(&self, name: &str, value: RuntimeValue)
pub fn define_value(&self, name: &str, value: RuntimeValue)
Defines an arbitrary runtime value in the current environment.
Sourcepub fn load_builtin_module(&mut self)
pub fn load_builtin_module(&mut self)
Load the built-in function modules.
This must be called to enable access to standard functions
like add, sub, map, filter, etc.
Sourcepub fn import_module(&mut self, module_name: &str) -> Result<(), Box<Error>>
pub fn import_module(&mut self, module_name: &str) -> Result<(), Box<Error>>
Import an external module by name.
The module will be searched for in the configured search paths and made available for use in mq code.
Sourcepub fn load_module(&mut self, module_name: &str) -> Result<(), Box<Error>>
pub fn load_module(&mut self, module_name: &str) -> Result<(), Box<Error>>
Load an external module by name.
The module will be searched for in the configured search paths and made available for use in mq code.
Sourcepub fn eval<I: Iterator<Item = RuntimeValue>>(
&mut self,
code: &str,
input: I,
) -> MqResult
pub fn eval<I: Iterator<Item = RuntimeValue>>( &mut self, code: &str, input: I, ) -> MqResult
The main engine for evaluating mq code.
The Engine manages parsing, optimization, and evaluation of mq.
It provides methods for configuration, loading modules, and evaluating code.
§Examples
let mut engine = mq_lang::DefaultEngine::default();
engine.load_builtin_module();
let input = mq_lang::parse_text_input("hello").unwrap();
let result = engine.eval("add(\" world\")", input.into_iter());
assert_eq!(result.unwrap(), vec!["hello world".to_string().into()].into());Sourcepub fn compile(&mut self, code: &str) -> Result<CompiledProgram, Box<Error>>
pub fn compile(&mut self, code: &str) -> Result<CompiledProgram, Box<Error>>
Compiles mq code into a CompiledProgram that can be evaluated multiple times.
Use this with eval_compiled to avoid re-parsing the same query for each input.
Sourcepub fn eval_compiled<I: Iterator<Item = RuntimeValue>>(
&mut self,
compiled: &CompiledProgram,
input: I,
) -> MqResult
pub fn eval_compiled<I: Iterator<Item = RuntimeValue>>( &mut self, compiled: &CompiledProgram, input: I, ) -> MqResult
Evaluates a pre-compiled program against the given input.
Use with compile to avoid re-parsing the same query for each input file,
or with a CompiledProgram constructed from a deserialized JSON AST (ast-json feature).
§Examples
let mut engine = mq_lang::DefaultEngine::default();
engine.load_builtin_module();
let compiled = engine.compile("add(\" world\")").unwrap();
let input = mq_lang::parse_text_input("hello").unwrap();
let result = engine.eval_compiled(&compiled, input.into_iter());
assert_eq!(result.unwrap(), vec!["hello world".to_string().into()].into());pub const fn version() -> &'static str
Trait Implementations§
Auto Trait Implementations§
impl<T = DefaultModuleResolver> !RefUnwindSafe for Engine<T>
impl<T = DefaultModuleResolver> !Send for Engine<T>
impl<T = DefaultModuleResolver> !Sync for Engine<T>
impl<T = DefaultModuleResolver> !UnwindSafe for Engine<T>
impl<T> Freeze for Engine<T>where
T: Freeze,
impl<T> Unpin for Engine<T>where
T: Unpin,
impl<T> UnsafeUnpin for Engine<T>where
T: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more