mirror of
https://github.com/teo3300/rust-mal.git
synced 2026-01-12 01:05:32 +01:00
Went back to fixed iterative Int eval
Signed-off-by: teo3300 <matteo.rogora@live.it>
This commit is contained in:
30
src/types.rs
30
src/types.rs
@ -71,29 +71,25 @@ pub fn unescape_str(s: &str) -> String {
|
||||
|
||||
use MalType::Int;
|
||||
|
||||
fn if_number(val: MalType) -> Result<isize, String> {
|
||||
fn if_number(val: &MalType) -> Result<isize, String> {
|
||||
match val {
|
||||
Int(val) => Ok(val),
|
||||
Int(val) => Ok(*val),
|
||||
_ => Err(format!("{:?} is not a number", val)),
|
||||
}
|
||||
}
|
||||
|
||||
fn int_op_rec(f: fn(isize, isize) -> isize, args: &MalArgs) -> Result<isize, String> {
|
||||
if args.len() == 1 {
|
||||
return if_number(args[0].clone());
|
||||
}
|
||||
|
||||
// PLEASE fix this
|
||||
let left = int_op_rec(f, &args[..args.len() - 1].to_vec())?;
|
||||
let right = if_number(args[args.len() - 1].clone())?;
|
||||
|
||||
Ok(f(left, right))
|
||||
}
|
||||
|
||||
pub fn int_op(set: isize, f: fn(isize, isize) -> isize, args: MalArgs) -> MalRet {
|
||||
if args.is_empty() {
|
||||
Ok(Int(set))
|
||||
} else {
|
||||
Ok(Int(int_op_rec(f, &args)?))
|
||||
return Ok(Int(set));
|
||||
}
|
||||
|
||||
let mut left = if_number(&args[0])?;
|
||||
if args.len() > 1 {
|
||||
let right = &args[1..];
|
||||
for el in right {
|
||||
left = f(left, if_number(el)?);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Int(left))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user