Virtual machine for cas-rs.
This crate provides [Vm], a virtual machine that executes bytecode
instructions generated by the CalcScript [Compiler]. It maintains its own
state and allows you to inspect and manipulate its variables over the course of
program execution.
Usage
To run a program, you can easily create a [Vm] by compiling a program with
[Vm::compile_program], then calling [Vm::run] to execute it. It returns a
[Value] that contains the return value of the program.
use Value;
use Parser;
use Vm;
let source = "5sin(pi/2) * 6!";
let mut parser = new;
let stmts = parser.try_parse_full_many.unwrap;
let mut vm = compile_program.unwrap;
use float;
// 5sin(pi/2) * 6!
// = 5 * 1 * 720 = 3600
assert_eq!;
It is also possible to programatically manipulate the [Vm]'s variables,
although this will take some work. You'll have to manually declare the variable
in the compilation phase to obtain an index for it, and then use that index
to set the variable in the [Vm]. Here's an example of how to do that:
use Compiler;
use compile_stmts;
use LitSym;
use Parser;
use Vm;
let source = "x^2 + 5x + 6";
let mut parser = new;
let stmts = parser.try_parse_full_many.unwrap;
let mut compiler = new;
// declare existence of `x` in the compiler (or a compilation error will occur)
// while also obtaining an index for `x` in the compiler
let x_id = compiler.add_symbol.unwrap;
// `x` must be initialized before we execute the vm (or a runtime error will occur)!
// helper function to compile the statements in place of `Compiler::compile_program`
compile_stmts.unwrap;
// create the virtual machine from the compiler and run test cases
let mut vm = from;
let cases = ;
for in cases.into_iter
This process will likely be simplified in the future.