use compiler::compiler::{Bytecode, Compiler};
use crate::vm::{GcClassifiedRuntimeError, GcRuntimeError, GcVM};
pub fn compile_source(source: &str) -> Result<Bytecode, String> {
let program = parser::parse(source).map_err(|errors| {
errors
.first()
.cloned()
.unwrap_or_else(|| "unknown parse error".to_string())
})?;
let mut compiler = Compiler::new();
compiler.compile(&program)
}
pub fn run_bytecode(
bytecode: Bytecode,
instruction_budget: usize,
) -> Result<String, GcRuntimeError> {
let mut vm = GcVM::new(bytecode);
vm.run_with_budget(instruction_budget)?;
Ok(vm.last_result_string())
}
pub fn run_bytecode_with_output(
bytecode: Bytecode,
instruction_budget: usize,
) -> (Result<String, GcClassifiedRuntimeError>, String) {
let mut vm = GcVM::new(bytecode);
vm.set_capture_output(true);
let result = vm
.run_with_budget_classified(instruction_budget)
.map(|()| vm.last_result_string());
let output = vm.take_output();
(result, output)
}