pub mod aot;
pub mod aot_runtime;
pub mod backquote;
pub mod banner;
pub mod bidi;
pub mod bidi_data;
pub mod builtins;
pub mod bundled;
pub mod cache;
pub mod compiler;
pub mod dap;
pub mod embed;
pub mod fileio;
pub mod freevars;
pub mod host;
pub mod intercepts;
pub mod json;
pub mod lread_fn;
pub mod lsp;
pub mod modhooks;
pub mod prelude;
pub mod prelude_31;
pub mod reader;
pub mod regexp;
pub mod rust_ffi;
pub mod strftime;
pub mod textprop;
pub mod tiers;
pub mod timefns;
pub mod window;
pub use embed::run_program;
pub use fusevm::Value;
pub use host::{reset_host, run_chunk, with_host};
pub const INTERP_STACK_BYTES: usize = 1 << 29;
pub fn with_interpreter_stack<T, F>(f: F) -> T
where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
{
std::thread::Builder::new()
.name("elisprs-interp".to_string())
.stack_size(INTERP_STACK_BYTES)
.spawn(f)
.expect("spawn interpreter thread")
.join()
.unwrap_or_else(|e| std::panic::resume_unwind(e))
}
pub fn eval_str(src: &str) -> Result<Value, String> {
load_prelude();
eval_forms(src)
}
pub fn read_forms(src: &str) -> Result<Vec<Value>, String> {
host::with_host(|h| reader::read_all(h, src))
}
pub fn compile_str(src: &str) -> Result<fusevm::Chunk, String> {
load_prelude();
let forms = host::with_host(|h| reader::read_all(h, src).map(|fs| splice_top_forms(h, fs)))?;
let mut expanded = Vec::with_capacity(forms.len());
for form in &forms {
expanded.push(host::macroexpand_all(form)?);
}
host::with_host(|h| compiler::compile_program(h, &expanded))
}
fn splice_top_forms(h: &mut host::ElispHost, forms: Vec<Value>) -> Vec<Value> {
let mut out = Vec::new();
for f in forms {
let progn = match h.list_vec(&f) {
Some(v) if !v.is_empty() && h.sym_name(&v[0]).as_deref() == Some("progn") => Some(v),
_ => None,
};
match progn {
Some(v) => out.extend(splice_top_forms(h, v[1..].to_vec())),
None => out.push(f),
}
}
out
}
pub(crate) fn run_top_forms(
src: &str,
load_buffer: Option<Value>,
) -> Result<(Vec<fusevm::Chunk>, Value), String> {
let loading = load_buffer.is_some();
let src = rust_ffi::desugar(src);
let read_forms = |h: &mut host::ElispHost, stream: &mut Option<reader::FormStream>| {
let prev_read = h.set_compiling_form(true);
let forms = match stream {
Some(s) => s
.next_form(h)
.map(|f| f.map(|f| splice_top_forms(h, vec![f]))),
None => reader::read_all(h, &src).map(|fs| Some(splice_top_forms(h, fs))),
};
h.set_compiling_form(prev_read);
forms
};
let mut stream = loading.then(|| reader::FormStream::new(&src));
let mut chunks = Vec::new();
let mut last = Value::Undef;
loop {
let forms = match host::with_host(|h| read_forms(h, &mut stream)) {
Ok(Some(forms)) => forms,
Ok(None) => break,
Err(e) if e == "end-of-file" && loading => {
return Err(host::with_host(|h| {
let sym = h.intern("end-of-file");
let buf = load_buffer.clone().unwrap_or(Value::Undef);
let obj = h.list_from(vec![sym, buf]);
h.set_pending_error(&e, obj);
e
}));
}
Err(e) => return Err(e),
};
run_forms(&forms, loading, &mut chunks, &mut last)?;
if stream.is_none() {
break;
}
}
Ok((chunks, last))
}
fn run_forms(
forms: &[Value],
loading: bool,
chunks: &mut Vec<fusevm::Chunk>,
last: &mut Value,
) -> Result<(), String> {
for form in forms {
let prev = host::with_host(|h| h.set_compiling_form(true));
let lowered = (|| -> Result<fusevm::Chunk, String> {
let expanded = if loading {
host::macroexpand_for_load(form)?
} else {
host::macroexpand_all(form)?
};
host::with_host(|h| compiler::compile_top(h, &expanded))
})();
host::with_host(|h| h.set_compiling_form(prev));
let chunk = lowered?;
chunks.push(chunk.clone());
*last = host::run_chunk(chunk)?;
}
Ok(())
}
fn eval_forms(src: &str) -> Result<Value, String> {
run_top_forms(src, None).map(|(_, last)| last)
}
fn install_process_environment() {
host::with_host(|h| {
let entries: Vec<String> = std::env::vars_os()
.map(|(k, v)| format!("{}={}", k.to_string_lossy(), v.to_string_lossy()))
.collect();
let strings: Vec<Value> = entries
.iter()
.rev()
.map(|e| h.new_string(e.clone()))
.collect();
let copy: Vec<Value> = strings.clone();
let process_env = h.list_from(strings);
let initial_env = h.list_from(copy);
let pe = h.intern("process-environment");
let ie = h.intern("initial-environment");
let _ = h.set_value(&pe, process_env);
let _ = h.set_value(&ie, initial_env);
});
}
fn install_abbreviated_home_dir() {
let saved = host::with_host(|h| h.match_data.clone());
let f = host::with_host(|h| {
let var = h.intern("abbreviated-home-dir");
let _ = h.set_value(&var, Value::Undef);
h.intern("abbreviate-file-name")
});
let root = host::with_host(|h| h.new_string("/".to_string()));
let _ = host::call_function(&f, &[root]);
host::with_host(|h| h.match_data = saved);
}
fn load_prelude() {
if host::prelude_loaded() {
return;
}
if host::restore_prelude_snapshot() {
install_process_environment();
host::with_host(crate::builtins::install_exec_path);
install_abbreviated_home_dir();
return;
}
host::set_prelude_loaded(true);
install_process_environment();
host::set_prelude_compiling(true);
for src in [prelude::PRELUDE, prelude::NADVICE]
.into_iter()
.chain(prelude_31::SEGMENTS)
{
let Ok(forms) = host::with_host(|h| reader::read_all(h, src)) else {
continue;
};
for form in &forms {
let r = (|| -> Result<(), String> {
let expanded = host::macroexpand_all(form)?;
let chunk = host::with_host(|h| compiler::compile_top(h, &expanded))?;
host::run_chunk(chunk)?;
Ok(())
})();
if let Err(e) = r {
eprintln!("elisprs: prelude form failed: {e}");
}
}
}
host::with_host(|h| {
let counter = h.intern("gensym-counter");
let _ = h.set_raw_global(&counter, Value::Int(0));
});
unload_rx_after_prelude();
host::with_host(|h| {
let end = h.arena_len();
h.set_prelude_end(end);
});
host::set_prelude_compiling(false);
host::save_prelude_snapshot();
install_abbreviated_home_dir();
}
fn unload_rx_after_prelude() {
let Ok(forms) = host::with_host(|h| reader::read_all(h, prelude_31::AUTOLOADS)) else {
return;
};
let fmakunbound = host::with_host(|h| h.intern("fmakunbound"));
for form in &forms {
let target = host::with_host(|h| {
let v = h.list_vec(form)?;
let is_rx = h.sym_name(v.first()?).as_deref() == Some("autoload")
&& h.str_text(v.get(2)?) == Some("rx");
is_rx.then(|| h.list_vec(&v[1])?.get(1).cloned()).flatten()
});
let Some(sym) = target else { continue };
let r = (|| -> Result<(), String> {
host::call_function(&fmakunbound, &[sym])?;
let expanded = host::macroexpand_all(form)?;
let chunk = host::with_host(|h| compiler::compile_top(h, &expanded))?;
host::run_chunk(chunk)?;
Ok(())
})();
if let Err(e) = r {
eprintln!("elisprs: prelude form failed: {e}");
}
}
host::with_host(|h| {
let features = h.intern("features");
let rx = h.intern("rx");
let kept: Vec<Value> = h
.raw_global_value(&features)
.ok()
.and_then(|l| h.list_vec(&l))
.unwrap_or_default()
.into_iter()
.filter(|f| !h.values_eq(f, &rx))
.collect();
let list = h.list_from(kept);
let _ = h.set_raw_global(&features, list);
});
}
pub fn print(v: &Value, readable: bool) -> String {
host::with_host(|h| h.print(v, readable))
}
pub fn format_error(e: &str) -> String {
let obj = host::with_host(|h| h.make_error_object(e));
let func = host::with_host(|h| h.intern("error-message-string"));
match host::call_function(&func, &[obj]) {
Ok(v) => {
host::with_host(|h| h.str_text(&v).map(str::to_string)).unwrap_or_else(|| e.to_string())
}
_ => e.to_string(),
}
}
fn with_load_file_name<T>(
path: &str,
src: &str,
run: impl FnOnce() -> Result<T, String>,
) -> Result<T, String> {
let abs = host::with_host(|h| {
let text = host::load_abspath(path).to_string_lossy().into_owned();
h.new_string(text)
});
let depth = host::with_host(|h| {
let d = h.specdepth();
let lfn = h.intern("load-file-name");
let ltn = h.intern("load-true-file-name");
let lip = h.intern("load-in-progress");
let _ = h.specbind(&lfn, abs.clone());
let _ = h.specbind(<n, abs.clone());
let _ = h.specbind(&lip, Value::Bool(true));
let lb = h.intern("lexical-binding");
let _ = h.specbind(&lb, Value::Bool(host::file_lexical_cookie(src)));
d
});
let r = run();
host::with_host(|h| h.unbind_to(depth));
r
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum EntryPoint {
Load,
Script,
}
fn install_entry_point_state(path: &str, src: &str, entry: EntryPoint) {
install_process_environment();
host::with_host(crate::builtins::install_exec_path);
install_abbreviated_home_dir();
host::with_host(|h| {
let tbl_sym = h.intern("lisp-interaction-mode-syntax-table");
if let Ok(tbl) = h.get_value(&tbl_sym) {
let cur = h.intern("--current-syntax-table--");
let _ = h.set_value(&cur, tbl);
}
});
let load_buf = host::with_host(|h| h.open_load_buffer(src, true));
if entry == EntryPoint::Script {
host::with_host(|h| {
let obj = h.buffer_object(load_buf);
let _ = h.set_buffer(&obj);
});
}
let args: Vec<String> = std::env::args().collect();
let tail: Vec<String> = match args.iter().position(|a| a == path) {
Some(i) => args[i + 1..].to_vec(),
None => Vec::new(),
};
host::with_host(|h| {
let left: Vec<Value> = tail.into_iter().map(|s| h.new_string(s)).collect();
let all: Vec<Value> = args.iter().cloned().map(|s| h.new_string(s)).collect();
let all = h.list_from(all);
let left = h.list_from(left);
let cla = h.intern("command-line-args");
let left_sym = h.intern("command-line-args-left");
let _ = h.set_value(&cla, all);
let _ = h.set_value(&left_sym, left);
});
}
pub fn eval_file(path: &str) -> Result<Value, String> {
eval_file_as(path, EntryPoint::Load)
}
pub fn eval_script(path: &str) -> Result<Value, String> {
eval_file_as(path, EntryPoint::Script)
}
pub fn eval_file_as(path: &str, entry: EntryPoint) -> Result<Value, String> {
let mtime_ns = std::fs::metadata(path)
.ok()
.and_then(|m| m.modified().ok())
.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
.map(|d| d.as_nanos() as i64)
.unwrap_or(0);
let schema_key = {
host::reset_host();
let base = cache::schema_key(host::with_host(|h| h.builtin_fingerprint()));
match entry {
EntryPoint::Load => base,
EntryPoint::Script => format!("{base}-script"),
}
};
let src = std::fs::read_to_string(path).map_err(|e| e.to_string())?;
let debug = std::env::var_os("ELISPRS_CACHE_DEBUG").is_some();
if let Some(cached) = cache::get(path, mtime_ns, &schema_key) {
let cache::CachedScript {
chunks,
heap,
oclosure_meta,
introspection_cells,
builtin_cells,
buffer_state,
base_len,
} = cached;
if debug {
eprintln!("elisprs: cache HIT {path} ({} chunks)", chunks.len());
}
host::reset_host();
host::with_host(|h| {
let end = h.builtin_count() + base_len;
h.import_heap_image(heap);
h.set_prelude_end(end);
h.import_oclosure_meta(oclosure_meta);
h.import_intrinsic_macro_cells(introspection_cells);
h.import_builtin_cells(builtin_cells);
h.import_buffer_state(buffer_state);
});
install_entry_point_state(path, &src, entry);
return with_load_file_name(path, &src, || {
let mut last = Value::Undef;
for chunk in chunks {
last = host::run_chunk(chunk)?;
}
Ok(last)
});
}
if debug {
eprintln!("elisprs: cache MISS {path}");
}
host::reset_host();
load_prelude();
let builtin_count = host::with_host(|h| h.builtin_count());
let prelude_end = host::with_host(|h| h.arena_len());
let clean_prelude = host::with_host(|h| h.export_heap_range(builtin_count, prelude_end));
let clean_builtin_cells = host::with_host(|h| h.export_builtin_cells());
let clean_buffer_state = host::with_host(|h| h.export_buffer_state());
install_entry_point_state(path, &src, entry);
let load_buffer = host::with_host(|h| h.load_buffer_object());
let (chunks, last) = with_load_file_name(path, &src, || {
run_top_forms(&src, Some(load_buffer.clone()))
})?;
let heap_tail = host::with_host(|h| h.export_heap_tail_clean(prelude_end));
let oclosure_meta = host::with_host(|h| h.export_oclosure_meta());
let introspection_cells = host::with_host(|h| h.export_intrinsic_macro_cells());
cache::put(
path,
mtime_ns,
&schema_key,
cache::ScriptParts {
chunks: &chunks,
prelude_heap: &clean_prelude,
heap_tail: &heap_tail,
oclosure_meta: &oclosure_meta,
introspection_cells: &introspection_cells,
builtin_cells: &clean_builtin_cells,
buffer_state: &clean_buffer_state,
},
);
Ok(last)
}