Skip to main content

Eval

Struct Eval 

Source
pub struct Eval {
    pub operand_stack: OperandStack,
    pub memory: Memory,
    /* private fields */
}
Expand description

§The ongoing evaluation of a script

This, alongside Script is one of the main entry points into this library’s API. To evaluate a script, first initialize an Eval instance using Eval::new, then use Eval::run or Eval::step to advance the evaluation.

§Example

use stack_assembly::{Eval, Script};

let script = Script::compile("1 2 +");

let mut eval = Eval::new();
eval.run(&script);

assert_eq!(eval.operand_stack.to_i32_slice(), &[3]);

Fields§

§operand_stack: OperandStack

§The operand stack

StackAssembly’s evaluation model is based on an implicit stack which stores all operands. An operator’s output is pushed to that stack, and any of its inputs are popped from there.

Alongside memory, this field is the primary channel for communication between script and host.

Most hosts should restrict modifications to this field to when the script triggers Effect::Yield, and then only do so in a well-reasoned and documented manner. Anything else might make reasoning about the script’s behavior very difficult.

None the less, the host has full access to this field, as to not restrict any experimental or non-standard use cases.

§memory: Memory

§The memory

StackAssembly provides a linear memory that is freely addressable per word.

Alongside operand_stack, this field is the primary channel for communication between script and host.

Most hosts should restrict modifications to this field to when the script triggers Effect::Yield, and then only do so in a well-reasoned and documented manner. Anything else might make reasoning about the script’s behavior very difficult.

None the less, the host has full access to this field, as to not restrict any experimental or non-standard use cases.

Implementations§

Source§

impl Eval

Source

pub fn new() -> Self

§Construct a new instance of Eval

To evaluate a script using the returned instance, you must call Eval::run or Eval::step.

Source

pub fn call_stack(&self) -> impl Iterator<Item = OperatorIndex>

§Access the current call stack

The returned iterator yields operator indices on the call stack, starting with the top-most one.

The yielded operator indices identify the operators where evaluation will continue after evaluating a return operator; not the operators that were the source of a call.

Source

pub fn run(&mut self, script: &Script) -> (Effect, OperatorIndex)

§Advance the evaluation until it triggers an effect

If an effect is currently active, do nothing and return immediately. Otherwise, keep evaluating operators until one triggers an effect.

The operator index returned alongside the effect identifies the operator that triggered the effect.

If you need more control over the evaluation, consider using Eval::step instead.

Source

pub fn step(&mut self, script: &Script) -> Option<(Effect, OperatorIndex)>

§Advance the evaluation by one step

If an effect is currently active, do nothing and return immediately. Otherwise, evaluate the next operator. If that triggers an effect, return that.

The operator index returned alongside the effect identifies the operator that triggered the effect.

This function may be used for advancing the evaluation of the script in a controlled manner. If you just want to keep evaluating until the next effect triggers, consider using Eval::run instead.

Source

pub fn clear_effect(&mut self) -> Option<(Effect, OperatorIndex)>

§Clear the active effect, if any

If no effect is active, this call does nothing. If an effect has been cleared, return that.

The operator index returned alongside the effect identifies the operator that triggered the effect.

Trait Implementations§

Source§

impl Debug for Eval

Source§

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

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

impl Default for Eval

Source§

fn default() -> Eval

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

Auto Trait Implementations§

§

impl Freeze for Eval

§

impl RefUnwindSafe for Eval

§

impl Send for Eval

§

impl Sync for Eval

§

impl Unpin for Eval

§

impl UnsafeUnpin for Eval

§

impl UnwindSafe for Eval

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> 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, 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.