Skip to main content

Engine

Struct Engine 

Source
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>

Source

pub fn new(module_resolver: T) -> Self

Source

pub fn set_optimization_level(&mut self, level: OptimizationLevel)

Set the optimization level for AST transformations applied before evaluation.

Source

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.

Source

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.

Source

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.

Source

pub fn define_value(&self, name: &str, value: RuntimeValue)

Defines an arbitrary runtime value in the current environment.

Source

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.

Source

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.

Source

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.

Source

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());
Source

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.

Source

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());
Source

pub const fn version() -> &'static str

Trait Implementations§

Source§

impl<T: Clone + ModuleResolver> Clone for Engine<T>

Source§

fn clone(&self) -> Engine<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + ModuleResolver> Debug for Engine<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: ModuleResolver> Default for Engine<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.