A images/test_div.png => images/test_div.png +0 -0
A images/test_mod.png => images/test_mod.png +0 -0
A images/test_mult.png => images/test_mult.png +0 -0
A images/test_sub.png => images/test_sub.png +0 -0
M src/interpreter/interpreter_tests.rs => src/interpreter/interpreter_tests.rs +39 -1
@@ 254,7 254,6 @@ fn pop_works() {}
#[test]
fn add_works() {
let mut interpreter = Interpreter::new("images/test_add.png").unwrap();
- //println!("{:#?}", interpreter.img());
interpreter.exec().expect("Execution failed!");
let popped = interpreter.stack.pop().unwrap();
assert_eq!(popped, 5);
@@ 262,3 261,42 @@ fn add_works() {
}
+#[test]
+fn sub_works() {
+ let mut interpreter = Interpreter::new("images/test_sub.png").unwrap();
+ interpreter.exec().expect("Execution failed!");
+ let popped = interpreter.stack.pop().unwrap();
+ assert_eq!(popped, 1);
+
+}
+
+#[test]
+fn mult_works() {
+ let mut interpreter = Interpreter::new("images/test_mult.png").unwrap();
+ interpreter.exec().expect("Execution failed!");
+ let popped = interpreter.stack.pop().unwrap();
+ assert_eq!(popped, 6);
+
+}
+
+#[test]
+fn div_works() {
+ let mut interpreter = Interpreter::new("images/test_div.png").unwrap();
+ interpreter.exec().expect("Execution failed!");
+ let popped = interpreter.stack.pop().unwrap();
+ assert_eq!(popped, 1);
+
+}
+
+#[test]
+fn mod_works() {
+ let mut interpreter = Interpreter::new("images/test_mod.png").unwrap();
+ interpreter.exec().expect("Execution failed!");
+ let popped = interpreter.stack.pop().unwrap();
+ assert_eq!(popped, 3);
+
+}
+
+#[test]
+#[ignore]
+fn not_works() {}
M src/interpreter/mod.rs => src/interpreter/mod.rs +70 -1
@@ 80,9 80,78 @@ impl Interpreter {
self.stack.push(a + b);
Ok(())
}
- else { Err(Error::new(ErrorKind::InvalidInput, "Not enough values on stack to add!")) }
+ else {
+ Err(Error::new(ErrorKind::InvalidInput, "Not enough values on stack to add!"))
+ }
}
+ Op::Sub => {
+ let first_pop = self.stack.pop();
+ let second_pop = self.stack.pop();
+ if let (Some(a), Some(b)) = (first_pop, second_pop) {
+ self.stack.push(b - a);
+ Ok(())
+ }
+ else {
+ Err(Error::new(ErrorKind::InvalidInput, "Not enough values on stack to add!"))
+ }
+
+ }
+
+
+ Op::Mult => {
+ let first_pop = self.stack.pop();
+ let second_pop = self.stack.pop();
+ if let (Some(a), Some(b)) = (first_pop, second_pop) {
+ self.stack.push(b * a);
+ Ok(())
+ }
+ else {
+ Err(Error::new(ErrorKind::InvalidInput, "Not enough values on stack to add!"))
+ }
+
+ }
+
+
+ Op::Div => {
+ let first_pop = self.stack.pop();
+ let second_pop = self.stack.pop();
+ if let (Some(a), Some(b)) = (first_pop, second_pop) {
+ if a == 0 {
+ return Err(Error::new(ErrorKind::InvalidInput, "Division by zero!"))
+ }
+ self.stack.push(b / a);
+ Ok(())
+ }
+ else {
+ Err(Error::new(ErrorKind::InvalidInput, "Not enough values on stack to add!"))
+ }
+
+ }
+
+ Op::Mod => {
+ let first_pop = self.stack.pop();
+ let second_pop = self.stack.pop();
+ if let (Some(a), Some(b)) = (first_pop, second_pop) {
+ if a == 0 {
+ return Err(Error::new(ErrorKind::InvalidInput, "Division by zero!"))
+ }
+ self.stack.push(b % a + if b%a < 0 { a } else { 0 } );
+ Ok(())
+ }
+ else {
+ Err(Error::new(ErrorKind::InvalidInput, "Not enough values on stack to add!"))
+ }
+
+ }
+
+ Op::Not => {
+ if self.stack.pop() == Some(0) { self.stack.push(1) } else { self.stack.push(0) }
+ Ok(())
+ }
+
+
+
_ => {Ok(())}
M src/rules.rs => src/rules.rs +1 -0
@@ 24,6 24,7 @@ pub enum Op {
pub fn get_op(curr: &Codel, next: &Codel) -> Option<Op> {
if let (Some(hue_diff), Some(light_diff)) = (curr.color().hue_diff(&next.color()), curr.color().light_diff(&next.color())) {
+
match (hue_diff, light_diff) {
(0, 1) => Some(Op::Push),
(0, 2) => Some(Op::Pop),