xmachine

Struct Machine

Source
pub struct Machine {
    pub stack: Vec<Ref<Value>>,
    pub registers: BTreeMap<String, Ref<Value>>,
}

Fields§

§stack: Vec<Ref<Value>>

A dynamically allocated stack to push and pop values onto and off of

§registers: BTreeMap<String, Ref<Value>>

The place to store named values (variables)

Implementations§

Source§

impl Machine

Source

pub fn new() -> Self

Return a new instance of a Machine with empty stack and registers

Source

pub fn get_arg(&mut self) -> Value

#################################################### The following functions are meant to be used to interface and interact with the virtual machine #################################################### FOR FOREIGN FUNCTIONS This gets an argument from the call to this foreign function by popping a value off the stack, and removing the reference

Source

pub fn return_value(&mut self, value: Value)

FOR FOREIGN FUNCTIONS This pushes a return value onto the stack

Source

pub fn duplicate(self) -> Self

#################################################### The following functions represent instructions that are natively supported by the virtual machine. These are not meant to be used by foreign functions, but they CAN be used without worry. #################################################### This function duplicates the current machine. This is VERY IMPORTANT. It iterates through the stack and copies each item into a new machine.

This is ONLY used to create the context for functions. If we don’t do this, the context Machine never goes out of scope, and never lets the Refs die. This causes a memory leak.

The addition of this method fixes the memory leak.

Source

pub fn push(&mut self, value: Ref<Value>)

Push an item onto the stack

Source

pub fn pop(&mut self) -> Ref<Value>

Pop an item off of the stack, and return it If the stack is empty, return an Error

Source

pub fn copy(&mut self)

  1. Pop off a REFERENCE value from the stack
  2. Push a copy the object and remove the reference
Source

pub fn assign(&mut self)

  1. Pop off a REFERENCE value from the stack
  2. Pop off a VALUE value from the stack
  3. Assign the value of VALUE to the memory location of REFERENCE

This can be used to assign to an indexed value from a list or table

Source

pub fn index(&mut self)

  1. Pop off the INDEX value from the stack
  2. Pop off a TABLE value from the stack
  3. Push the TABLE[INDEX] reference onto the stack
Source

pub fn method_call(&mut self)

  1. Pop off the INDEX value from the stack
  2. Pop off a TABLE value from the stack
  3. Push the TABLE onto the stack
  4. Call the value at TABLE[INDEX] as a function
Source

pub fn call(&mut self)

  1. Pop off function from the stack
  2. Call it with this Machine instance
Source

pub fn for_loop(&mut self)

  1. Pop off a COUNTER identifier from the stack
  2. Pop off an ELEMENT identifier from the stack
  3. Pop off a LIST value from the stack
  4. Pop off a BODY function from the stack
  5. For COUNTER, ELEMENT in enumeration of LIST:
  6. Store LIST[COUNTER] in ELEMENT
  7. Call BODY with current instance
  8. Increment COUNTER
Source

pub fn while_loop(&mut self)

  1. Pop off a CONDITION function from the stack
  2. Pop off a BODY function from the stack
  3. Call the CONDITION function with the context of this instance
  4. If the return value is true, run the BODY function with the context of this instance
  5. Goto step 3
Source

pub fn if_then_else(&mut self)

  1. Pop off a CONDITION function from the stack
  2. Pop off a THEN function from the stack
  3. Pop off an ELSE function from the stack
  4. Call the CONDITION function with the context of this instance
  5. If the return value is true, run the THEN function with the context of this instance
  6. If the return value is false, run the ELSE function with the context of this instance
Source

pub fn store(&mut self)

  1. Pop off a KEY value from the stack
  2. Pop off a VALUE value from the stack
  3. Assign the value of VALUE to the register named KEY
Source

pub fn load(&mut self)

  1. Pop off a KEY value from the stack
  2. Push the value in the register named KEY to the stack

Trait Implementations§

Source§

impl Clone for Machine

Source§

fn clone(&self) -> Machine

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 Machine

Source§

fn default() -> Machine

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

impl Display for Machine

How to print Machine / convert Machine to string This is for debugging code and seeing the current instance of the machine

Source§

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

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

impl PartialEq for Machine

Source§

fn eq(&self, other: &Machine) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Machine

Source§

fn partial_cmp(&self, other: &Machine) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Machine

Auto Trait Implementations§

§

impl Freeze for Machine

§

impl !RefUnwindSafe for Machine

§

impl !Send for Machine

§

impl !Sync for Machine

§

impl Unpin for Machine

§

impl !UnwindSafe for Machine

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> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.