Refactoring
This commit is contained in:
parent
5ea83e2da6
commit
4598802999
@ -222,15 +222,7 @@ impl EvaluatorState {
|
||||
self.pop_env();
|
||||
result
|
||||
},
|
||||
Primitive(builtin) => self.apply_primitive(builtin, operands),
|
||||
_ => 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::*;
|
||||
|
||||
Primitive(prim) => {
|
||||
let mut evaled_operands = Vec::new();
|
||||
let mut cur_operand = operands;
|
||||
loop {
|
||||
@ -244,25 +236,11 @@ impl EvaluatorState {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
prim.apply(evaled_operands)
|
||||
}
|
||||
} else {
|
||||
return Err(format!("Bad operand: {:?}", arg));
|
||||
_ => return Err(format!("Bad type to apply")),
|
||||
}
|
||||
}
|
||||
NumberAtom(result)
|
||||
},
|
||||
op => return Err(format!("Primitive op {:?} not implemented", op)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn read(input: &str) -> Result<Vec<Sexp>, String> {
|
||||
@ -307,6 +285,33 @@ enum PrimitiveFn {
|
||||
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 {
|
||||
fn print(&self) -> String {
|
||||
use self::Sexp::*;
|
||||
|
Loading…
Reference in New Issue
Block a user