pub mod engine; pub mod interpreter; pub mod lambda; pub mod module; pub mod parser; pub use engine::Engine; pub use lambda::show_lambda; pub use module::ModuleResolver; pub use parser::Statement; use thiserror::Error; pub type C = num_complex::Complex; fn format_float(v: f64) -> String { let s = format!("{}", v); if s.contains('.') || s.contains('e') || s.contains('E') || s == "inf" || s == "-inf" || s == "NaN" { s } else { format!("{}.0", s) } } pub fn format_complex(c: C) -> String { if c.im == 0.0 { format_float(c.re) } else if c.re == 0.0 { if c.im == 1.0 { "j".to_string() } else if c.im == -1.0 { "-j".to_string() } else { format!("{}j", format_float(c.im)) } } else if c.im > 0.0 { format!("{}+{}j", format_float(c.re), format_float(c.im)) } else { format!("{}{}j", format_float(c.re), format_float(c.im)) } } pub const MAX_REDUCTIONS: usize = 100000; #[cfg(feature = "wasm")] mod wasm_export { use crate::Engine; use std::sync::Mutex; use wasm_bindgen::prelude::*; static ENGINE: Mutex> = Mutex::new(None); #[wasm_bindgen] pub fn parse(input: &str) -> String { let mut guard = ENGINE.lock().unwrap(); let engine = guard.get_or_insert_with(Engine::new); match engine.eval(input) { Ok(output) => output, Err(e) => format!("Error: {}", e), } } #[wasm_bindgen] pub fn parse_raw(input: &str) -> String { let mut guard = ENGINE.lock().unwrap(); let engine = guard.get_or_insert_with(Engine::new); match engine.eval_raw(input) { Ok(output) => output, Err(e) => format!("Error: {}", e), } } #[wasm_bindgen] pub fn reset_engine() { let mut guard = ENGINE.lock().unwrap(); *guard = None; } } #[derive(Error, Debug, Clone, PartialEq)] pub enum Error { #[error("parse error: {0}")] Parse(String), #[error("eval error: {0}")] Eval(String), #[error("undefined variable: {0}")] Undefined(String), #[error("recursive definition: {0}")] Recursive(String), #[error("too many reductions (possible infinite loop): {0}")] TooManyReductions(String), } #[derive(Debug, Clone, PartialEq)] pub enum Term { S, K, Iota, Num(C), Var(String), App(Box, Box), } impl Term { pub fn show(&self) -> String { match self { Term::S => "s".to_string(), Term::K => "k".to_string(), Term::Iota => "i".to_string(), Term::Num(c) => format_complex(*c), Term::Var(v) => v.clone(), Term::App(l, r) => { let left = match l.as_ref() { Term::App(_, _) => format!("({})", l.show()), _ => l.show(), }; let right = match r.as_ref() { Term::App(_, _) => format!("({})", r.show()), _ => r.show(), }; format!("{} {}", left, right) } } } fn all_vars(&self) -> std::collections::HashSet { let mut vars = std::collections::HashSet::new(); self.collect_vars(&mut vars); vars } fn collect_vars(&self, vars: &mut std::collections::HashSet) { match self { Term::S | Term::K | Term::Iota | Term::Num(_) => {} Term::Var(v) => { vars.insert(v.clone()); } Term::App(l, r) => { l.collect_vars(vars); r.collect_vars(vars); } } } } #[cfg(test)] mod tests { use super::*; #[test] fn format_complex_nan() { let c = crate::C::new(f64::NAN, 0.0); assert_eq!(format_complex(c), "NaN"); } #[test] fn format_complex_neg_nan() { let c = crate::C::new(-f64::NAN, 0.0); assert_eq!(format_complex(c), "NaN"); } #[test] fn format_complex_nan_imag() { let c = crate::C::new(f64::NAN, 1.0); assert_eq!(format_complex(c), "NaN+1.0j"); } }