~jpl8/piet_interpreter

fb174a566bd33cb18a3a192ef48374fa412a1549 — jpl 2 years ago eb56b64
fix whitespace
A images/alpha_filled_big.png => images/alpha_filled_big.png +0 -0
A images/euclid_clint_big.png => images/euclid_clint_big.png +0 -0
A images/piet_pi_big.png => images/piet_pi_big.png +0 -0
A images/power2_big.png => images/power2_big.png +0 -0
A images/test_whitespace.png => images/test_whitespace.png +0 -0
M src/interpreter/interpreter_tests.rs => src/interpreter/interpreter_tests.rs +10 -0
@@ 141,6 141,7 @@ fn move_next_works_right_left() {
        img: m,
        head,
        stack: vec!(),
        debug: false,
    };

    interpreter.move_next();


@@ 162,6 163,7 @@ fn move_next_works_right_right() {
        img: m,
        head,
        stack: vec!(),
        debug: false,
    };

    assert_eq!(interpreter.move_next(), Some((3,2)));


@@ 190,6 192,7 @@ fn move_next_works_trapped() {
        img: m,
        head,
        stack: vec!(),
        debug: false,
    };

    assert_eq!(interpreter.move_next(), None);


@@ 360,3 363,10 @@ fn num_out_works() {
    interpreter.exec().expect("Execution Failed!");
}

#[test]
fn whitespace_works() {
    let mut interpreter = Interpreter::new("images/test_whitespace.png").unwrap();
    interpreter.exec().expect("Execution Failed!");
    assert_eq!(interpreter.head().pos(), (13,9));
    assert_eq!(interpreter.stack().len(), 1);
}

M src/interpreter/mod.rs => src/interpreter/mod.rs +18 -15
@@ 20,7 20,7 @@ impl Interpreter {
    pub fn new<P>(path: P) -> io::Result<Self>
    where P: AsRef<Path>
    {
        let img = translator::png_to_piet_img(path)?;
        let img = translator::to_piet_img(path)?;
        Ok(Interpreter::from_img(img))
    }



@@ 47,6 47,7 @@ impl Interpreter {

       loop {
            let curr = self.head.pos();
            //eprintln!("{:?}", curr);
            if let Some(next) = self.move_next() {

                let curr_codel = self.img().get(curr.0, curr.1);


@@ 75,15 76,14 @@ impl Interpreter {
            let codel_on_edge = self.head.get_furthest_codel_on_edge(&edge);
            if tries == 8 { return None }

            if self.img.get(curr_codel.0, curr_codel.1).color() == Color::White {
                    return self.move_through_whitespace(curr_codel);
            }

            if let Some(next) = self.get_head_next(codel_on_edge.0, codel_on_edge.1) {

                if self.img.get(next.0, next.1).color() == Color::White {
                    return self.move_through_whitespace(next);
                }
                else {
                    self.head.pos = next;
                    return Some(next); 
                }
                self.head.pos = next;
                return Some(next); 

            }
            else {


@@ 103,15 103,17 @@ impl Interpreter {

    fn move_through_whitespace(&mut self, start: (u32, u32)) -> Option<(u32, u32)> {
        let (width, height) = self.img.dimensions();

        let on_top_edge    = start.1 == 0;
        let on_bottom_edge = start.1 == height - 1;
        let on_left_edge   = start.0 == 0;
        let on_right_edge  = start.0 == width - 1;

        let mut curr = start;
        let mut states = vec!((curr, self.head.direction_pointer));
        let mut states = vec!();
        loop {
            //eprintln!("in: {:?}", curr);

            let on_top_edge    = curr.1 == 0;
            let on_bottom_edge = curr.1 == height - 1;
            let on_left_edge   = curr.0 == 0;
            let on_right_edge  = curr.0 == width - 1;



            if states.contains(&(curr, self.head.direction_pointer)) {
                return None;


@@ 119,6 121,7 @@ impl Interpreter {

            let (x,y) = curr;
            if self.img.get(x, y).color() != Color::White {
                self.head_as_mut().pos = curr;
                return Some(curr);
            }


M src/interpreter/rules.rs => src/interpreter/rules.rs +5 -0
@@ 90,6 90,11 @@ pub fn do_op(interpreter: &mut Interpreter, curr: &Codel, next: &Codel) -> io::R
                    let first_pop  = interpreter.stack_as_mut().pop().unwrap();
                    let second_pop = interpreter.stack_as_mut().pop().unwrap();
                    interpreter.stack_as_mut().push(second_pop + first_pop);
                    
                    if interpreter.debug {
                        eprintln!("Executed: Add({}, {}) -> {}", second_pop, first_pop, second_pop+first_pop);
                    }

                }
                else {
                    eprintln!("Not enough values on the stack! Were {}, needed 2", interpreter.stack.len());

M src/translator.rs => src/translator.rs +2 -2
@@ 6,7 6,7 @@ use std::path::Path;

use crate::piet_img::{PietImage, Codel};

pub fn png_to_piet_img<P>(path: P) -> io::Result<PietImage>
pub fn to_piet_img<P>(path: P) -> io::Result<PietImage>
where P: AsRef<Path> 
{



@@ 80,7 80,7 @@ mod tests {
    use super::*;
    #[test]
    fn upscale_works() {
        let img = png_to_piet_img("images/test_upscale.png").expect("Error on new");
        let img = to_piet_img("images/test_upscale.png").expect("Error on new");
        assert_eq!(codel_size(img.codels()), 4);

    }