pub mod aot;
pub mod aot_native;
pub mod ast;
pub mod async_rt;
pub mod banner;
pub mod builtins;
pub mod cache;
pub mod casefold;
pub mod cli;
pub mod compiler;
pub mod dap;
pub mod embed;
pub mod excgroup;
pub mod excunicode;
pub mod extensions;
#[cfg(feature = "stdlib-ffi")]
pub mod ffi;
pub mod host;
pub mod intercepts;
pub mod lexer;
pub mod lsp;
pub mod lsp_nav;
pub mod mangle;
pub mod oracle;
pub mod parser;
pub mod pyhash;
pub mod regexpr;
pub mod repl;
pub mod rust_ffi;
pub mod sre_parse;
pub mod stack;
pub mod stdio;
pub mod stdlib;
pub mod suggest;
pub mod symtable;
pub mod tbcolor;
pub mod tiers;
pub mod unparse;
pub use embed::run_program;
pub use fusevm::Value;
pub fn compile(src: &str) -> Result<compiler::Program, String> {
let (stmts, warnings) = parse_with_warnings(src)?;
compiler::compile(&stmts, false)
.map(|p| with_parse_warnings(p, warnings))
.map_err(|e| parser::with_byte_columns(e, src))
}
type Warnings = Vec<(u32, String)>;
fn parse_with_warnings(src: &str) -> Result<(Vec<ast::Stmt>, Warnings), String> {
let _ = lexer::take_escape_warnings();
let stmts = parser::parse(src);
let warnings = lexer::take_escape_warnings();
Ok((stmts?, warnings))
}
fn with_parse_warnings(mut prog: compiler::Program, mut warnings: Warnings) -> compiler::Program {
warnings.append(&mut prog.warnings);
prog.warnings = warnings;
prog
}
pub fn compile_debug(src: &str) -> Result<compiler::Program, String> {
let (stmts, warnings) = parse_with_warnings(src)?;
compiler::compile(&stmts, true)
.map(|p| with_parse_warnings(p, warnings))
.map_err(|e| parser::with_byte_columns(e, src))
}
pub fn compile_interactive(src: &str) -> Result<compiler::Program, String> {
let stmts = parser::parse(src)?;
compiler::compile_interactive(&stmts).map_err(|e| parser::with_byte_columns(e, src))
}
pub fn load_merged(mut prog: compiler::Program) -> fusevm::Chunk {
let (func_off, try_off) = host::with_host(|h| h.program_offsets());
for (op_hash, table) in &prog.positions {
host::register_positions(*op_hash, table.clone());
}
compiler::rebase_program(&mut prog, func_off, try_off);
let compiler::Program {
main,
functions,
procs: _,
tries,
warnings: _,
positions: _,
} = prog;
let funcs: Vec<host::FuncDef> = functions
.into_iter()
.map(|(_, mut f)| {
if f.linked.is_some() {
f.unit = main.op_hash;
}
f
})
.collect();
host::with_host(|h| h.load_program(funcs, tries));
main
}
pub fn run_compiled(prog: compiler::Program) -> Result<Value, String> {
host::run_main(load_merged(prog))
}
pub fn compile_or_load(src: &str) -> Result<compiler::Program, String> {
compile_or_load_cacheable(src, true)
}
pub fn compile_or_load_labeled(src: &str, source: &str) -> Result<compiler::Program, String> {
let compile = |src: &str| compile(src).map_err(|e| parser::with_filename(e, source));
if !cache::cache_enabled() {
return compile(src);
}
if let Some(prog) = cache::load(src) {
return Ok(prog);
}
let prog = compile(src)?;
let _ = cache::store_labeled(src, &prog, source);
Ok(prog)
}
pub fn compile_or_load_cacheable(src: &str, cacheable: bool) -> Result<compiler::Program, String> {
if !cacheable || !cache::cache_enabled() {
return compile(src);
}
if let Some(prog) = cache::load(src) {
if std::env::var_os("PYTHONRS_TRACE").is_some() {
eprintln!(
"pythonrs: cache HIT ({} ops, {} functions) — skipped lex/parse/lower",
prog.main.ops.len(),
prog.functions.len()
);
}
return Ok(prog);
}
let prog = compile(src)?;
let _ = cache::store(src, &prog);
if std::env::var_os("PYTHONRS_TRACE").is_some() {
eprintln!(
"pythonrs: cache MISS — compiled + stored ({} ops, {} functions)",
prog.main.ops.len(),
prog.functions.len()
);
}
Ok(prog)
}
pub fn eval_str(src: &str) -> Result<Value, String> {
host::reset_host();
host::init_runtime(vec![String::new()], None, src, "<string>", true);
let result = compile_or_load_cacheable(src, false).and_then(run_compiled);
stdio::flush_std_files();
result.map_err(plain_error)
}
pub fn plain_error(e: String) -> String {
match parser::split_syntax_error(&e) {
(head, Some(_)) => head.to_string(),
(_, None) => e,
}
}
pub fn eval_str_captured(src: &str, globals: &[(&str, &str)]) -> (Result<Value, String>, String) {
eval_captured(src, globals, host::StdinSource::Process, true)
}
pub(crate) fn eval_captured(
src: &str,
globals: &[(&str, &str)],
stdin: host::StdinSource,
stderr: bool,
) -> (Result<Value, String>, String) {
host::reset_host();
host::init_runtime(vec![String::new()], None, src, "<string>", true);
host::with_host(|h| {
h.stdin = stdin;
for (name, text) in globals {
let value = h.new_str(*text);
h.set_global(name, value);
}
h.begin_capture(stderr);
});
let result = compile_or_load_cacheable(src, false)
.and_then(run_compiled)
.map_err(plain_error);
let output = host::with_host(|h| h.end_capture());
if !stderr {
stdio::flush(stdio::Stream::Stderr);
}
(result, output)
}
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}"))?;
host::reset_host();
host::init_runtime(vec![path.to_string()], None, &src, path, true);
let result = compile_or_load(&src).and_then(run_compiled);
stdio::flush_std_files();
result.map_err(plain_error)
}
#[cfg(feature = "stdlib-ffi")]
pub fn run_module(module: &str, args: &[String]) -> i32 {
ffi::run_module(module, args)
}
#[cfg(not(feature = "stdlib-ffi"))]
pub fn run_module(module: &str, _args: &[String]) -> i32 {
eprintln!("python: -m requires the stdlib-ffi bridge (not in this build): {module}");
1
}
pub fn render_compile_error(err: &str, src: &str, filename: &str) -> String {
let (head, pos) = parser::split_syntax_error(err);
let mut pos = match pos {
Some(p) if p.lineno.is_some() => p,
_ => {
let Some((text, line)) = head
.strip_suffix(')')
.and_then(|h| h.rsplit_once(" (line "))
.and_then(|(t, n)| n.parse::<i64>().ok().map(|n| (t, n)))
else {
return format!("{head}\n");
};
let pos = parser::SyntaxPos {
lineno: Some(line),
..Default::default()
};
return render_positioned(text, pos, src, filename);
}
};
if pos.filename.is_none() {
pos.filename = Some(filename.to_string());
}
render_positioned(head, pos, src, filename)
}
fn render_positioned(head: &str, mut pos: parser::SyntaxPos, src: &str, filename: &str) -> String {
if pos.text.is_none() && !filename.starts_with('<') {
pos.text = pos.lineno.and_then(|l| parser::source_line(src, l, true));
}
format!("{}\n", parser::render_syntax_head(&pos, head, filename))
}
pub struct RunReport {
pub exit_code: i32,
pub stderr: Option<String>,
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ReportOrder {
FlushThenReport,
ReportThenFlush,
}
pub fn run_program_report(
src: &str,
argv: Vec<String>,
main_file: Option<String>,
tb_filename: &str,
show_source: bool,
) -> RunReport {
let report = execute_program(src, argv, main_file, tb_filename, show_source);
shut_down();
report
}
pub fn run_main_program(
src: &str,
argv: Vec<String>,
main_file: Option<String>,
tb_filename: &str,
show_source: bool,
order: ReportOrder,
) -> i32 {
let report = execute_program(src, argv, main_file, tb_filename, show_source);
if order == ReportOrder::FlushThenReport {
stdio::flush_io();
}
if let Some(text) = &report.stderr {
stdio::write(stdio::Stream::Stderr, text);
}
shut_down();
report.exit_code
}
fn execute_program(
src: &str,
argv: Vec<String>,
main_file: Option<String>,
tb_filename: &str,
show_source: bool,
) -> RunReport {
host::reset_host();
let cacheable = main_file.is_some();
host::init_runtime(argv, main_file, src, tb_filename, show_source);
let prog = match compile_or_load_cacheable(src, cacheable) {
Ok(p) => p,
Err(e) => {
return RunReport {
exit_code: 1,
stderr: Some(crate::tbcolor::with(
crate::tbcolor::can_colorize_stderr(),
|| render_compile_error(&e, src, tb_filename),
)),
}
}
};
for (line, msg) in &prog.warnings {
eprintln!("{tb_filename}:{line}: SyntaxWarning: {msg}");
if !tb_filename.starts_with('<') {
if let Some(text) = src.lines().nth((*line as usize).saturating_sub(1)) {
eprintln!(" {}", text.trim_start());
}
}
}
match run_compiled(prog) {
Ok(_) => RunReport {
exit_code: 0,
stderr: None,
},
Err(e) => match crate::tbcolor::with(crate::tbcolor::can_colorize_stderr(), || {
host::classify_top_error(&e)
}) {
host::TopExit::SystemExit { code, message } => RunReport {
exit_code: code,
stderr: message,
},
host::TopExit::Uncaught { traceback } => RunReport {
exit_code: 1,
stderr: Some(traceback),
},
},
}
}
fn shut_down() {
host::run_atexit_callbacks();
#[cfg(feature = "stdlib-ffi")]
ffi::flush_open_files();
host::warn_unawaited_coroutines();
stdio::flush_std_files();
}
pub fn eval_file_debug(path: &str) -> Result<Value, String> {
let src = std::fs::read_to_string(path).map_err(|e| format!("cannot read {path}: {e}"))?;
let prog = compile_debug(&src).map_err(plain_error)?;
host::reset_host();
host::set_debug_mode(true);
let r = run_compiled(prog);
host::set_debug_mode(false);
stdio::flush_std_files();
r.map_err(plain_error)
}
pub fn eval_to_string(src: &str) -> Result<String, String> {
let v = eval_str(src)?;
Ok(host::with_host(|h| h.repr_of(&v)))
}