Skip to main content

gc/
lib.rs

1#[cfg(test)]
2mod gc_test;
3#[cfg(test)]
4mod value_test;
5#[cfg(test)]
6mod vm_test;
7
8pub mod frame;
9pub mod header;
10pub mod heap;
11pub mod list;
12pub mod malloc;
13pub mod runtime;
14pub mod value;
15pub mod vm;
16
17pub use frame::Frame;
18pub use header::{GcId, GcObjectHeader, GcObjectType, GcPhase, RefCountHeader, RefCountId};
19pub use heap::{GcHeap, GcRef};
20pub use malloc::{MallocState, DEFAULT_GC_THRESHOLD, MALLOC_OVERHEAD};
21pub use runtime::{GcObject, GcRuntime, MarkFunc};
22pub use value::{export_object, import_object, GcClosure, Value};
23pub use vm::GcVM;
24
25use compiler::compiler::{Bytecode, Compiler};
26use object::Object;
27use parser::ast::Node;
28
29/// Compile Monkey source using the existing bytecode compiler.
30pub fn compile(program: &Node) -> Result<Bytecode, String> {
31    let mut compiler = Compiler::new();
32    compiler.compile(program)
33}
34
35/// Compile and execute on the GC-backed VM.
36pub fn eval(program: &Node) -> Result<Object, String> {
37    let bytecode = compile(program)?;
38    let mut vm = GcVM::new(bytecode);
39    vm.run();
40    vm.export_last_result()
41        .ok_or_else(|| "no result on stack".to_string())
42}
43
44/// Parse, compile, and execute Monkey source.
45pub fn eval_source(source: &str) -> Result<Object, String> {
46    let program = parser::parse(source).map_err(|errors| errors[0].clone())?;
47    eval(&program)
48}