pub mod aot_native;
pub mod ast;
pub mod banner;
pub mod cli;
pub mod compiler;
pub mod dap;
pub mod host;
pub mod lexer;
pub mod lsp;
pub mod parser;
pub mod pkg;
pub mod rust_ffi;
pub mod stdlib_vendor;
pub mod tiers;
pub use banner::version_banner;
use fusevm::{Op, Scheduler, VMResult, Value, VM};
pub fn parse(src: &str) -> Result<ast::Program, String> {
let src = rust_ffi::desugar(src);
parser::parse(&src)
}
pub fn compile(src: &str) -> Result<fusevm::Chunk, String> {
let prog = parse(src)?;
let linked = pkg::link(prog)?;
compiler::compile(&linked)
}
fn configure_vm(chunk: fusevm::Chunk) -> VM {
let mut vm = VM::new(chunk);
host::install(&mut vm);
vm.set_numeric_hook(std::sync::Arc::new(host::numeric_hook));
vm
}
fn run_chunk(chunk: fusevm::Chunk) -> Result<Value, String> {
let mut vm = configure_vm(chunk);
vm.enable_tracing_jit();
let result = vm.run();
if let Some(e) = host::take_ffi_error() {
return Err(e);
}
match result {
VMResult::Ok(v) => Ok(v),
VMResult::Halted => Ok(vm.stack.last().cloned().unwrap_or(Value::Undef)),
VMResult::Error(e) => Err(e),
}
}
fn run_scheduled(chunk: fusevm::Chunk) -> Result<Value, String> {
let main_vm = configure_vm(chunk.clone());
let make_vm = move || configure_vm(chunk.clone());
let sched = Scheduler::new(make_vm).with_recv_zero(host::chan_closed_sentinel());
match sched.run(main_vm) {
Ok(()) => match host::take_ffi_error() {
Some(e) => Err(e),
None => Ok(Value::Undef),
},
Err(e) => Err(format!("go-rs: {e}")),
}
}
fn uses_scheduler(chunk: &fusevm::Chunk) -> bool {
chunk.ops.iter().any(|op| {
matches!(
op,
Op::Go(..)
| Op::ChanMake
| Op::ChanSend
| Op::ChanRecv
| Op::ChanClose
| Op::Select(..)
)
})
}
pub fn run_str(src: &str) -> Result<Value, String> {
let chunk = compile(src)?;
host::heap_reset();
if uses_scheduler(&chunk) {
run_scheduled(chunk)
} else {
run_chunk(chunk)
}
}
pub fn run_file(path: &str) -> Result<Value, String> {
let src =
std::fs::read_to_string(path).map_err(|e| format!("go-rs: cannot read {path}: {e}"))?;
run_str(&src)
}
pub fn disassemble(src: &str) -> Result<String, String> {
Ok(compile(src)?.disassemble())
}
pub fn vet(src: &str) -> Result<(), String> {
compile(src).map(|_| ())
}
pub fn env_report() -> String {
let arch = match std::env::consts::ARCH {
"aarch64" => "arm64",
"x86_64" => "amd64",
other => other,
};
let os = match std::env::consts::OS {
"macos" => "darwin",
other => other,
};
[
("GOOS", os.to_string()),
("GOARCH", arch.to_string()),
("GOVERSION", format!("go{}", banner::GO_COMPAT_VERSION)),
("GOENGINE", format!("go-rs {}", env!("CARGO_PKG_VERSION"))),
("CGO_ENABLED", "0".to_string()),
("GOROOT", String::new()),
("GOPATH", String::new()),
]
.iter()
.map(|(k, v)| format!("{k}=\"{v}\""))
.collect::<Vec<_>>()
.join("\n")
+ "\n"
}