[][src]Crate lovm2

lovm2 is a lightweight virtual machine with a focus on simplicity and extendability.

Features

  • dynamic typing
  • generate bytecode using highlevel intermediate representation
  • call into shared objects: lovm2_extend
  • python bindings: pylovm2
  • define own callbacks for interrupts

Example

use lovm2::prelude::*;
use lovm2::vm::Vm;

let mut module = ModuleBuilder::new();

// a module needs a code object called `main`
// if you want to make it runnable
let mut main_hir = module.entry();

// set the local variable n to 10
main_hir.step(Assign::local(&lv2_var!(n), 10));

// `print` is a builtin function. the `lv2_var!` macro
// ensures that the given identifier is not confused
// with a string.
main_hir.step(Call::new("print").arg(lv2_var!(n)).arg("Hello World"));
// ... this is equivalent to the developer-friendly version:
main_hir.step(lv2_call!(print, n, "Hello World"));

// consumes the `ModuleBuilder` and transforms
// it into a `Module`
let module = module.build().unwrap();
println!("{}", module);

// load the module and run it
let mut vm = Vm::with_std();
vm.add_main_module(module).expect("load error");
vm.run().expect("run error");

Modules

bytecode

definition of the bytecode

code

runnable bytecode objects

context

vm state

frame

stores local function values

gen
module

generic protocol for module like objects

prelude

important structs, enums and constants for using lovm2 as library

util
value

representation and operations for lovm2 values

var

wrapper type for identifiers

vm

runs modules and maintains program state

Macros

define_code

define CodeObject on a low-level basis

lv2_access

creates an Access expression

lv2_call

creates a Call expression

lv2_dict

creates a dict Initialize expression using Expr as items

lv2_list

creates a list Initialize expression using Expr as items

lv2_var

creates a Variable from a rust identifier

Attribute Macros

lovm2_builtin

used for generating wrappers of statically linked functions to be called from lovm2