diff --git a/src/main.rs b/src/main.rs index ae49ac2..89d97ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,6 +80,19 @@ fn parse(contents: String, opcodes: HashMap<&'static str, (i64, i32)>) -> Vec(stack: &mut Vec, 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 { let mut stack: Vec = Vec::new(); let ilen = instructions.len(); @@ -88,10 +101,10 @@ fn exec(instructions: Vec) -> 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 => (),