Struct moon_script::engine::Engine

source ·
pub struct Engine { /* private fields */ }
Expand description

Scripting engine, it allows to create runnable ASTs, and also to give functions and constant values for said scripts

Implementations§

source§

impl Engine

source

pub fn new() -> Self

Creates a new empty Engine containing just basic functions, like println or binary operators

source

pub fn add_constant<Name: ToString, Value: Into<Constant>>( &mut self, name: Name, value: Value, ) -> Option<Constant>

Adds a constant with a value

use moon_script::{ContextBuilder, Engine};
let mut engine = Engine::new();
engine.add_constant("MY_CONSTANT", 15);
let runnable_ast = engine.parse(r###"
    return MY_CONSTANT;
"###, ContextBuilder::default()).unwrap();
let result : i32 = runnable_ast.executor().execute().unwrap().try_into().unwrap();
assert_eq!(15, result);
source

pub fn add_function<Function: Into<FunctionDefinition>>( &mut self, function_definition: Function, )

Adds a function with a name

use moon_script::{ContextBuilder, Engine, FunctionDefinition};
let mut engine = Engine::new();
engine.add_function(FunctionDefinition::new("say_hi_and_return_number", ||{
    println!("Hi!");
    return 5;
}));
let runnable_ast = engine.parse(r###"
    return say_hi_and_return_number();
"###, ContextBuilder::default()).unwrap();
let result : i32 = runnable_ast.executor().execute().unwrap().try_into().unwrap();
assert_eq!(5, result);
source

pub fn parse<'input>( &self, input: &'input str, context_builder: ContextBuilder, ) -> Result<AST, ParsingError<'input>>

Parses a script into an AST using a specific context

Adds a function with a name

use moon_script::{InputVariable, ContextBuilder, Engine, FunctionDefinition};

let mut engine = Engine::new();
engine.add_function(FunctionDefinition::new("add_five", |num:u8| {
    return num + 5;
}));

let context = ContextBuilder::new()
    .with_variable(InputVariable::new("ten").value(10));


let runnable_ast = engine.parse(r###"
    return add_five(ten);
"###, context).unwrap();
let result : i32 = runnable_ast.executor().execute().unwrap().try_into().unwrap();
assert_eq!(15, result);

Trait Implementations§

source§

impl Clone for Engine

source§

fn clone(&self) -> Engine

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Default for Engine

source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Freeze for Engine

§

impl !RefUnwindSafe for Engine

§

impl Send for Engine

§

impl Sync for Engine

§

impl Unpin for Engine

§

impl !UnwindSafe for Engine

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<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> 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.