1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#![warn(rust_2018_idioms)]

mod compiler;
mod instruction;
mod value;
mod vm;

pub use compiler::Compiler;
pub use instruction::Instruction;
use instruction::Program;
use rush_analyzer::ast::AnalyzedProgram;
pub use rush_analyzer::Diagnostic;
pub use value::Value;
pub use vm::{RuntimeError, Vm};

/// Compiles rush source code to VM instructions.
/// The `Ok(_)` variant is a [`CompilationResult`] which also includes any non-error diagnostics.
/// The `Err(_)` variant returns a `Vec<Diagnostic>` which contains at least one error.
pub fn compile(ast: AnalyzedProgram<'_>) -> Program {
    Compiler::new().compile(ast)
}

/// Executes the given program using the VM.
/// The `Ok(_)` variant also returns non-error diagnostics.
/// The `Err(_)` variant returns a `Vec<Diagnostic>` which contains at least one error.
pub fn run(ast: AnalyzedProgram<'_>) -> Result<i64, RuntimeError> {
    let program = Compiler::new().compile(ast);
    let mut vm = Vm::new();
    let exit_code = vm.run(program)?;
    Ok(exit_code)
}

/// Executes the given program using the VM on debug mode.
/// The `Ok(_)` variant also returns non-error diagnostics.
/// The `Err(_)` variant returns a `Vec<Diagnostic>` which contains at least one error.
pub fn debug_run(ast: AnalyzedProgram<'_>, clock_hz: u64) -> Result<i64, RuntimeError> {
    let program = Compiler::new().compile(ast);
    let mut vm = Vm::new();
    let exit_code = vm.debug_run(program, clock_hz)?;
    Ok(exit_code)
}