Struct VM

Source
pub struct VM {
    ip: usize,
    stack: Vec<Value>,
    scopes: Vec<Scope>,
    frames: Vec<CallFrame>,
    current_frame: Option<usize>,
}
Expand description

Virtual Machine that executes bytecode

Fields§

§ip: usize

Instruction pointer

§stack: Vec<Value>

Stack for values

§scopes: Vec<Scope>

Stack of scopes, with global scope at index 0

§frames: Vec<CallFrame>

Call frames for function calls

§current_frame: Option<usize>

Index of the current call frame

Implementations§

Source§

impl VM

Source

pub fn new() -> Self

Creates a new virtual machine

Source

fn register_native_functions(&mut self)

Registers built-in functions

Source

fn define_native( &mut self, name: &str, arity: u8, function: fn(&[Value]) -> Result<Value, String>, )

Defines a native (built-in) function

§Arguments
  • name - Name of the native function
  • arity - Number of parameters
  • function - The Rust function implementing this native function
Source

pub fn interpret(&mut self, chunk: &Chunk) -> Result<(), String>

Interprets and executes a bytecode chunk

§Arguments
  • chunk - The bytecode chunk to execute
§Returns

Ok(()) on success, or an error message on failure

Source

fn execute_instruction(&mut self, chunk: &Chunk) -> Result<(), String>

Executes a single instruction

§Arguments
  • chunk - The bytecode chunk containing the instruction
§Returns

Ok(()) on success, or an error message on failure

Source

fn read_byte(&mut self, chunk: &Chunk) -> u8

Reads the next byte from the chunk and advances the instruction pointer

§Arguments
  • chunk - The bytecode chunk to read from
§Returns

The byte read from the chunk

Source

fn pop(&mut self) -> Result<Value, String>

Pops a value off the stack

§Returns

The popped value, or an error if the stack is empty

Source

fn peek(&self, distance: usize) -> Result<&Value, String>

Looks at a value on the stack without removing it

§Arguments
  • distance - How far from the top of the stack to look
§Returns

Reference to the value, or an error if the stack isn’t deep enough

Source

fn binary_op<F>(&mut self, op: F) -> Result<(), String>
where F: FnOnce(&Value, &Value) -> Result<Value, String>,

Performs a binary operation on the top two values of the stack

§Arguments
  • op - Function that implements the binary operation
§Returns

Ok(()) if successful, or an error message

Source

fn get_variable(&self, name: &str) -> Option<&Value>

Helper method to find a variable in any scope (from innermost to outermost)

Source

fn set_variable(&mut self, name: String, value: Value)

Helper method to set a variable (creates in current scope or updates existing)

Trait Implementations§

Source§

impl Default for VM

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Freeze for VM

§

impl RefUnwindSafe for VM

§

impl Send for VM

§

impl Sync for VM

§

impl Unpin for VM

§

impl UnwindSafe for VM

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.