Refactoring

This commit is contained in:
greg 2017-12-10 18:44:21 -08:00
parent 18a839bb91
commit 3386fcc505

View File

@ -71,14 +71,12 @@ impl EvaluatorState {
fn eval(&mut self, expr: Sexp) -> Result<Sexp, String> {
use self::Sexp::*;
Ok(match expr {
SymbolAtom(ref sym) => {
match self.get_var(sym) {
SymbolAtom(ref sym) => match self.get_var(sym) {
Some(ref sexp) => {
let q: &Sexp = sexp; //WTF? if I delete this line, the copy doesn't work??
q.clone() //TODO make this not involve a clone
},
None => return Err(format!("Variable {} not bound", sym)),
}
},
expr @ StringAtom(_) => expr,
expr @ NumberAtom(_) => expr,
@ -86,7 +84,23 @@ impl EvaluatorState {
False => False,
Cons(box operator, box operands) => {
match operator {
SymbolAtom(ref sym) => match &sym[..] {
SymbolAtom(sym) => match &sym[..] {
"quote" | "eq?" | "cons" | "car" | "cdr" | "atom?" |
"define" | "lambda" | "if" | "cond" => self.eval_special_form(&sym[..], operands)?,
_ => {
let evaled = self.eval(SymbolAtom(sym))?;
self.apply(evaled, operands)?
}
},
_ => unimplemented!()
}
},
Nil => Nil,
})
}
fn eval_special_form(&mut self, form: &str, operands: Sexp) -> Result<Sexp, String> {
use self::Sexp::*;
Ok(match form {
"quote" => operands,
"eq?" => match operands {
Cons(box lhs, box Cons(box rhs, _)) => {
@ -143,13 +157,12 @@ impl EvaluatorState {
_ => return Err(format!("Bad if expression"))
},
_ => unimplemented!(),
},
other => {println!("OTHER? {:?}", other); unimplemented!() }
}
},
Nil => Nil,
})
}
fn apply(&mut self, function: Sexp, operands: Sexp) -> Result<Sexp, String> {
Err(format!("Not implemented"))
}
}
fn read(input: &str) -> Result<Vec<Sexp>, String> {