add quote special form

This commit is contained in:
mehbark 2023-06-14 23:28:29 -04:00
parent 058cf26e89
commit aa40bfedbb

View file

@ -610,6 +610,7 @@ pub fn rw(rule: &Rule, sexp: &Sexp) -> Sexp {
pub fn simp(rules: &[Rule], mut expr: Sexp, step_limit: usize, complexity_limit: usize) -> Sexp { pub fn simp(rules: &[Rule], mut expr: Sexp, step_limit: usize, complexity_limit: usize) -> Sexp {
for _ in 0..step_limit { for _ in 0..step_limit {
expr = expr expr = expr
.apply_special_form::<Quote>()
.apply_special_form::<Genslop>() .apply_special_form::<Genslop>()
.apply_special_form::<LambdaLike>() .apply_special_form::<LambdaLike>()
.apply_special_form::<App>() .apply_special_form::<App>()
@ -867,6 +868,42 @@ impl<'src> SpecialForm<'src> for LambdaLike<'src> {
} }
} }
static QUOTE: Lazy<Symbol> = Lazy::new(|| TABLE.lock().unwrap().intern("quote").unwrap());
static CONS: Lazy<Symbol> = Lazy::new(|| TABLE.lock().unwrap().intern("cons").unwrap());
// sbcl has quote take one arg....
#[derive(Debug, Clone)]
struct Quote<'src>(&'src Sexp);
impl<'src> SpecialForm<'src> for Quote<'src> {
fn from_sexp(sexp: &'src Sexp) -> Option<Self> {
match sexp {
Sexp::Atom(_) => None,
Sexp::List(xs) => match &xs[..] {
[Sexp::Atom(quote), expr] if *quote == *QUOTE => Some(Self(expr)),
_ => None,
},
}
}
fn eval(self) -> Sexp {
let Self(expr) = self;
match expr {
Sexp::Atom(at) => Sexp::Atom(*at),
Sexp::List(xs) => consify(xs),
}
}
}
fn consify(xs: &[Sexp]) -> Sexp {
if xs.is_empty() {
Sexp::nil()
} else {
Sexp::List(vec![Sexp::Atom(*CONS), consify(&xs[1..])])
}
}
pub fn make_rule(sexp: &Sexp) -> Option<Rule> { pub fn make_rule(sexp: &Sexp) -> Option<Rule> {
match sexp { match sexp {
Sexp::List(xs) => match &xs[..] { Sexp::List(xs) => match &xs[..] {