Refactoring
This commit is contained in:
parent
60644ba3d7
commit
8493233b69
@ -71,14 +71,12 @@ impl EvaluatorState {
|
|||||||
fn eval(&mut self, expr: Sexp) -> Result<Sexp, String> {
|
fn eval(&mut self, expr: Sexp) -> Result<Sexp, String> {
|
||||||
use self::Sexp::*;
|
use self::Sexp::*;
|
||||||
Ok(match expr {
|
Ok(match expr {
|
||||||
SymbolAtom(ref sym) => {
|
SymbolAtom(ref sym) => match self.get_var(sym) {
|
||||||
match self.get_var(sym) {
|
|
||||||
Some(ref sexp) => {
|
Some(ref sexp) => {
|
||||||
let q: &Sexp = sexp; //WTF? if I delete this line, the copy doesn't work??
|
let q: &Sexp = sexp; //WTF? if I delete this line, the copy doesn't work??
|
||||||
q.clone() //TODO make this not involve a clone
|
q.clone() //TODO make this not involve a clone
|
||||||
},
|
},
|
||||||
None => return Err(format!("Variable {} not bound", sym)),
|
None => return Err(format!("Variable {} not bound", sym)),
|
||||||
}
|
|
||||||
},
|
},
|
||||||
expr @ StringAtom(_) => expr,
|
expr @ StringAtom(_) => expr,
|
||||||
expr @ NumberAtom(_) => expr,
|
expr @ NumberAtom(_) => expr,
|
||||||
@ -86,7 +84,23 @@ impl EvaluatorState {
|
|||||||
False => False,
|
False => False,
|
||||||
Cons(box operator, box operands) => {
|
Cons(box operator, box operands) => {
|
||||||
match operator {
|
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,
|
"quote" => operands,
|
||||||
"eq?" => match operands {
|
"eq?" => match operands {
|
||||||
Cons(box lhs, box Cons(box rhs, _)) => {
|
Cons(box lhs, box Cons(box rhs, _)) => {
|
||||||
@ -143,13 +157,12 @@ impl EvaluatorState {
|
|||||||
_ => return Err(format!("Bad if expression"))
|
_ => return Err(format!("Bad if expression"))
|
||||||
},
|
},
|
||||||
_ => unimplemented!(),
|
_ => 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> {
|
fn read(input: &str) -> Result<Vec<Sexp>, String> {
|
||||||
|
Loading…
Reference in New Issue
Block a user