Refactoring

This commit is contained in:
greg 2017-12-29 05:03:30 -08:00
parent 48a35aa382
commit 7f546fa879

View File

@ -222,15 +222,7 @@ 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")),
}
}
fn apply_primitive(&mut self, op: PrimitiveFn, operands: Sexp) -> Result<Sexp, String> {
use self::Sexp::*;
use self::PrimitiveFn::*;
let mut evaled_operands = Vec::new(); let mut evaled_operands = Vec::new();
let mut cur_operand = operands; let mut cur_operand = operands;
loop { loop {
@ -244,25 +236,11 @@ impl EvaluatorState {
} }
} }
Ok(match op { prim.apply(evaled_operands)
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 type to apply")),
return Err(format!("Bad operand: {:?}", arg));
} }
} }
NumberAtom(result)
},
op => return Err(format!("Primitive op {:?} not implemented", op)),
})
}
} }
fn read(input: &str) -> Result<Vec<Sexp>, String> { fn read(input: &str) -> Result<Vec<Sexp>, String> {
@ -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::*;