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
impl Machine
Sourcepub fn get_arg(&mut self) -> Value
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
Sourcepub fn return_value(&mut self, value: Value)
pub fn return_value(&mut self, value: Value)
FOR FOREIGN FUNCTIONS This pushes a return value onto the stack
Sourcepub fn duplicate(self) -> Self
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.
Sourcepub fn pop(&mut self) -> Ref<Value>
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
Sourcepub fn copy(&mut self)
pub fn copy(&mut self)
- Pop off a REFERENCE value from the stack
- Push a copy the object and remove the reference
Sourcepub fn assign(&mut self)
pub fn assign(&mut self)
- Pop off a REFERENCE value from the stack
- Pop off a VALUE value from the stack
- 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
Sourcepub fn index(&mut self)
pub fn index(&mut self)
- Pop off the INDEX value from the stack
- Pop off a TABLE value from the stack
- Push the TABLE[INDEX] reference onto the stack
Sourcepub fn method_call(&mut self)
pub fn method_call(&mut self)
- Pop off the INDEX value from the stack
- Pop off a TABLE value from the stack
- Push the TABLE onto the stack
- Call the value at TABLE[INDEX] as a function
Sourcepub fn for_loop(&mut self)
pub fn for_loop(&mut self)
- Pop off a COUNTER identifier from the stack
- Pop off an ELEMENT identifier from the stack
- Pop off a LIST value from the stack
- Pop off a BODY function from the stack
- For COUNTER, ELEMENT in enumeration of LIST:
- Store LIST[COUNTER] in ELEMENT
- Call BODY with current instance
- Increment COUNTER
Sourcepub fn while_loop(&mut self)
pub fn while_loop(&mut self)
- Pop off a CONDITION function from the stack
- Pop off a BODY function from the stack
- Call the CONDITION function with the context of this instance
- If the return value is true, run the BODY function with the context of this instance
- Goto step 3
Sourcepub fn if_then_else(&mut self)
pub fn if_then_else(&mut self)
- Pop off a CONDITION function from the stack
- Pop off a THEN function from the stack
- Pop off an ELSE function from the stack
- Call the CONDITION function with the context of this instance
- If the return value is true, run the THEN function with the context of this instance
- If the return value is false, run the ELSE function with the context of this instance
Trait Implementations§
Source§impl Display for Machine
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§impl PartialOrd for Machine
impl PartialOrd for Machine
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> 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)