[][src]Struct lox_lang::VM

pub struct VM { /* fields omitted */ }

The Lox virtual machine.

Example

let mut vm = VM::default();
vm.interpret("3 + 3 == 6"); // true

Implementations

impl VM[src]

pub fn add_std_globals(&mut self)[src]

pub fn add_arguments_instance(&mut self)[src]

impl VM[src]

pub fn alloc<T: Any>(&mut self, obj: T) -> Gc<T>[src]

Allocate a garbage-collected value on the heap.

This method is how to obtain a Gc pointer (not exported from this crate and has no public constructor). Values allocated with this method will be owned (and eventually freed) by the VM. If the value lives until the VM goes out of scope, it will be freed in the VM's Drop implementation.

For a usage example, see NativeFun.

impl VM[src]

pub fn buffer_output(&mut self, should_buffer: bool)[src]

Print directly to stdout, or buffer output internally.

Pass true to this method to buffer print output internally. This output can then be accessed through this struct's Read implementation.

Pass false (the default mode) to sink all program output directly to Stdout.

Example

use std::io::Read;

let mut vm = lox_lang::VM::default();
vm.buffer_output(true);

vm.interpret("print nil == true;");

let mut buffer = String::new();
vm.read_to_string(&mut buffer).unwrap();
assert_eq!(buffer, "false\n");

vm.interpret("for (var a = 3; a < 12; a = a + 3) print a;");

buffer.clear();
vm.read_to_string(&mut buffer).unwrap();
assert_eq!(buffer, "3\n6\n9\n");

impl<'source> VM[src]

pub fn interpret<T: AsRef<str> + 'source>(
    &mut self,
    source: T
) -> Result<(), Vec<Error>>
[src]

Compile and run Lox code from a source string.

Errors

Errors are returned in a Vec. This is because compilation (not runtime) is able to continue after error(s) are encountered.

pub fn define_global<T: ?Sized + ToString>(&mut self, name: &T, value: Value)[src]

Define a global variable inside the runtime.

Since globals are late bound in Lox, functions that reference the provided name will see the provided value, even if they were declared in the runtime before calling this method.

Example

use std::io::Read;

let mut vm = lox_lang::VM::default();
vm.buffer_output(true);

vm.interpret("fun hello() { print world; }");
vm.define_global("world", "greetings, Earthling.".into());
vm.interpret("hello();");

let mut buffer = String::new();
vm.read_to_string(&mut buffer).unwrap();
assert_eq!(buffer, "greetings, Earthling.\n");

Trait Implementations

impl Default for VM[src]

fn default() -> Self[src]

The VM constructor.

Program output defaults to Stdout. To customize this behavior, see the buffer_output method.

impl Drop for VM[src]

impl Read for VM[src]

Auto Trait Implementations

impl !RefUnwindSafe for VM

impl !Send for VM

impl !Sync for VM

impl Unpin for VM

impl !UnwindSafe for VM

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.