Refactoring

This commit is contained in:
greg 2017-12-29 05:03:30 -08:00
parent 5ea83e2da6
commit 4598802999

View File

@ -222,46 +222,24 @@ impl EvaluatorState {
self.pop_env(); self.pop_env();
result result
}, },
Primitive(builtin) => self.apply_primitive(builtin, operands), Primitive(prim) => {
_ => return Err(format!("Bad type to apply")), let mut evaled_operands = Vec::new();
} let mut cur_operand = operands;
} loop {
match cur_operand {
fn apply_primitive(&mut self, op: PrimitiveFn, operands: Sexp) -> Result<Sexp, String> { Nil => break,
use self::Sexp::*; Cons(box l, box rest) => {
use self::PrimitiveFn::*; evaled_operands.push(self.eval(l)?);
cur_operand = rest;
let mut evaled_operands = Vec::new(); },
let mut cur_operand = operands; _ => return Err(format!("Bad operands list"))
loop {
match cur_operand {
Nil => break,
Cons(box l, box rest) => {
evaled_operands.push(self.eval(l)?);
cur_operand = rest;
},
_ => return Err(format!("Bad operands list"))
}
}
Ok(match op {
Plus | Mult => {
let mut result = match op { Plus => 0, Mult => 1, _ => unreachable!() };
for arg in evaled_operands {
if let NumberAtom(n) = arg {
if let Plus = op {
result += n;
} else if let Mult = op {
result *= n;
}
} else {
return Err(format!("Bad operand: {:?}", arg));
} }
} }
NumberAtom(result)
}, prim.apply(evaled_operands)
op => return Err(format!("Primitive op {:?} not implemented", op)), }
}) _ => return Err(format!("Bad type to apply")),
}
} }
} }
@ -307,6 +285,33 @@ enum PrimitiveFn {
Plus, Minus, Mult, Div, Mod, Greater, Less, GreaterThanOrEqual, LessThanOrEqual Plus, Minus, Mult, Div, Mod, Greater, Less, GreaterThanOrEqual, LessThanOrEqual
} }
impl PrimitiveFn {
fn apply(&self, evaled_operands: Vec<Sexp>) -> Result<Sexp, String> {
use self::Sexp::*;
use self::PrimitiveFn::*;
let op = self.clone();
Ok(match op {
Plus | Mult => {
let mut result = match op { Plus => 0, Mult => 1, _ => unreachable!() };
for arg in evaled_operands {
if let NumberAtom(n) = arg {
if let Plus = op {
result += n;
} else if let Mult = op {
result *= n;
}
} else {
return Err(format!("Bad operand: {:?}", arg));
}
}
NumberAtom(result)
},
op => return Err(format!("Primitive op {:?} not implemented", op)),
})
}
}
impl Sexp { impl Sexp {
fn print(&self) -> String { fn print(&self) -> String {
use self::Sexp::*; use self::Sexp::*;