pub mod env; pub mod interpreter; pub use env::Env; pub use interpreter::Interpreter; use std::rc::Rc; use std::cell::RefCell; use std::fmt; pub type NativeFn = fn(Vec) -> Value; #[derive(Clone)] pub enum Value { Number(f64), String(String), Bool(bool), Nil, Function(Rc), NativeFunction(NativeFn), } impl std::fmt::Debug for Value { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { Value::Number(n) => write!(f, "Number({:?})", n), Value::String(s) => write!(f, "String({:?})", s), Value::Bool(b) => write!(f, "Bool({:?})", b), Value::Nil => write!(f, "Nil"), Value::Function(_) => write!(f, "Function(...)"), Value::NativeFunction(_) => write!(f, "NativeFunction(...)"), } } } impl fmt::Display for Value { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Value::Number(n) => write!(f, "{}", n), Value::String(s) => write!(f, "{}", s), Value::Bool(b) => write!(f, "{}", b), Value::Nil => write!(f, "nil"), Value::Function(_) => write!(f, ""), Value::NativeFunction(_) => write!(f, ""), } } } #[derive(Debug, Clone)] pub struct Function { pub params: Vec, pub body: Vec, pub env: Rc>, // 闭包捕获环境 pub name: Option, }