56 lines
1.5 KiB
Rust
56 lines
1.5 KiB
Rust
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>) -> Value;
|
|
|
|
#[derive(Clone)]
|
|
pub enum Value {
|
|
Number(f64),
|
|
String(String),
|
|
Bool(bool),
|
|
Nil,
|
|
Function(Rc<Function>),
|
|
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, "<function>"),
|
|
Value::NativeFunction(_) => write!(f, "<native function>"),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct Function {
|
|
pub params: Vec<String>,
|
|
pub body: Vec<crate::ast::Stmt>,
|
|
pub env: Rc<RefCell<Env>>, // 闭包捕获环境
|
|
pub name: Option<String>,
|
|
}
|