pub mod ast;
pub mod builtins;
pub mod collate;
pub mod compiler;
pub mod deparse;
pub mod docs;
pub mod ffi;
pub mod host;
pub mod intercepts;
pub mod lexer;
pub mod linalg;
pub mod nmath;
pub mod parser;
pub mod primargs;
pub mod re;
pub mod rng;
pub mod strwidth;
#[cfg(not(target_arch = "wasm32"))]
pub mod aot;
#[cfg(not(target_arch = "wasm32"))]
pub mod aot_runtime;
#[cfg(not(target_arch = "wasm32"))]
pub mod banner;
#[cfg(not(target_arch = "wasm32"))]
pub mod cache;
#[cfg(not(target_arch = "wasm32"))]
pub mod cli;
#[cfg(not(target_arch = "wasm32"))]
pub mod dap;
#[cfg(not(target_arch = "wasm32"))]
pub mod embed;
#[cfg(not(target_arch = "wasm32"))]
pub mod lsp;
#[cfg(not(target_arch = "wasm32"))]
pub mod rembed;
#[cfg(not(target_arch = "wasm32"))]
pub mod repl;
pub mod tiers;
#[cfg(target_arch = "wasm32")]
pub mod wasm;
#[cfg(not(target_arch = "wasm32"))]
pub use embed::run_program;
pub use fusevm::Value;
pub fn compile(src: &str) -> Result<compiler::Program, String> {
compiler::compile(&parser::parse(src)?)
}
pub fn compile_no_slots(src: &str) -> Result<compiler::Program, String> {
compiler::compile_no_slots(&parser::parse(src)?)
}
pub fn eval_str(src: &str) -> Result<Value, String> {
host::reset_host();
run_compiled(compile(src)?)
}
pub fn eval_captured(src: &str, vars: &[(&str, &str)]) -> (Result<Value, String>, String) {
host::reset_host();
host::with_host(|h| {
for (name, text) in vars {
let value = h.str_vec(vec![Some(text.to_string())]);
h.set_var(name, value);
}
});
host::start_capture();
let result = compile(src).and_then(run_compiled);
(result, host::take_capture())
}
pub fn eval_quiet(src: &str) -> Result<Value, String> {
host::reset_host();
host::with_host(|h| h.echo = false);
run_compiled(compile(src)?)
}
pub fn run_compiled(prog: compiler::Program) -> Result<Value, String> {
host::with_host(|h| h.load_closures(prog.closures));
host::run_main(prog.main)
}
pub fn eval_file(path: &str) -> Result<Value, String> {
let src = std::fs::read_to_string(path).map_err(|e| format!("cannot read {path}: {e}"))?;
eval_str(&src)
}
pub fn disasm(prog: &compiler::Program) -> String {
use std::fmt::Write as _;
let mut out = String::new();
let mut chunk = |title: String, c: &fusevm::Chunk| {
let _ = writeln!(out, "== {title} ==");
for (i, op) in c.ops.iter().enumerate() {
let _ = writeln!(out, "{i:>5} {op:?}");
}
if !c.constants.is_empty() {
let _ = writeln!(out, " constants: {:?}", c.constants);
}
};
chunk("main".to_string(), &prog.main);
for (i, c) in prog.closures.iter().enumerate() {
chunk(format!("closure #{i} ({})", c.params.join(", ")), &c.chunk);
}
out
}
pub fn eval_to_string(src: &str) -> Result<String, String> {
let v = eval_quiet(src)?;
Ok(builtins::format_value(&v).join("\n").trim_end().to_string())
}
pub fn eval_capture(src: &str) -> String {
host::reset_host();
host::start_capture();
let result = compile(src).and_then(run_compiled);
let mut out = host::take_capture();
if let Err(e) = result {
out.push_str(&format!("Error: {e}\n"));
}
out
}