eval: started implementing binary operations
This commit is contained in:
21
src/main.rs
21
src/main.rs
@@ -80,6 +80,19 @@ fn parse(contents: String, opcodes: HashMap<&'static str, (i64, i32)>) -> Vec<i6
|
||||
return code
|
||||
}
|
||||
|
||||
fn binop<F>(stack: &mut Vec<i64>, fun: F) where F: Fn(i64, i64) -> i64 {
|
||||
let x = stack.pop();
|
||||
let y = stack.pop();
|
||||
match x {
|
||||
Some(x) =>
|
||||
match y {
|
||||
Some(y) => stack.push(fun(x, y)),
|
||||
_ => panic!("pop needs at least two elements on the stack")
|
||||
},
|
||||
_ => panic!("pop needs at least two elements on the stack")
|
||||
}
|
||||
}
|
||||
|
||||
fn exec(instructions: Vec<i64>) -> i64 {
|
||||
let mut stack: Vec<i64> = Vec::new();
|
||||
let ilen = instructions.len();
|
||||
@@ -88,10 +101,10 @@ fn exec(instructions: Vec<i64>) -> i64 {
|
||||
ip += 1; /* TMP */
|
||||
match instructions[ip] {
|
||||
/* ENT */ 0 => (),
|
||||
/* ADD */ 1 => (),
|
||||
/* SUB */ 2 => (),
|
||||
/* MUL */ 3 => (),
|
||||
/* DIV */ 4 => (),
|
||||
/* ADD */ 1 => binop(&mut stack, |x, y| x + y),
|
||||
/* SUB */ 2 => binop(&mut stack, |x, y| x - y),
|
||||
/* MUL */ 3 => binop(&mut stack, |x, y| x * y),
|
||||
/* DIV */ 4 => binop(&mut stack, |x, y| x / y),
|
||||
/* MOD */ 5 => (),
|
||||
/* LT */ 6 => (),
|
||||
/* EQ */ 7 => (),
|
||||
|
Reference in New Issue
Block a user