38 lines
799 B
Rust
38 lines
799 B
Rust
|
|
use std::rc::Rc;
|
||
|
|
|
||
|
|
#[derive(Debug)]
|
||
|
|
pub enum Form {
|
||
|
|
Int(i32),
|
||
|
|
Symbol(String),
|
||
|
|
Pair(Rc<Form>,Rc<Form>),
|
||
|
|
PrimComb(String, fn(Rc<Form>, Rc<Form>) -> Rc<Form>),
|
||
|
|
Nil,
|
||
|
|
}
|
||
|
|
impl Form {
|
||
|
|
pub fn int(&self) -> Option<i32> {
|
||
|
|
match self {
|
||
|
|
Form::Int(i) => Some(*i),
|
||
|
|
_ => None,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
pub fn sym(&self) -> Option<&str> {
|
||
|
|
match self {
|
||
|
|
Form::Symbol(s) => Some(s),
|
||
|
|
_ => None,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
pub fn car(&self) -> Option<Rc<Form>> {
|
||
|
|
match self {
|
||
|
|
Form::Pair(car, cdr) => Some(Rc::clone(car)),
|
||
|
|
_ => None,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
pub fn cdr(&self) -> Option<Rc<Form>> {
|
||
|
|
match self {
|
||
|
|
Form::Pair(car, cdr) => Some(Rc::clone(cdr)),
|
||
|
|
_ => None,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|