~jpl8/piet_interpreter

057b1541360d156dd9544bfb790c29caec25dbca — jpl 2 years ago 360bad4
add in(char) and in(num)
A images/test_char_in.png => images/test_char_in.png +0 -0
A images/test_num_in.png => images/test_num_in.png +0 -0
M src/interpreter/interpreter_tests.rs => src/interpreter/interpreter_tests.rs +21 -0
@@ 327,3 327,24 @@ fn utf8_to_unicode() {
    assert_eq!(Interpreter::utf8_to_unicode(&bytes), 0x20AC);

}

#[test]
#[ignore]
fn char_in_input_works() {
    let mut interpreter = Interpreter::new("images/test_char_in.png").unwrap();
    interpreter.exec().expect("Execution failed!");
    let popped = interpreter.stack.pop().unwrap();
    // Enter an "A"
    assert_eq!(popped, 65);
}

#[test]
#[ignore]
fn num_in_input_works() {
    let mut interpreter = Interpreter::new("images/test_num_in.png").unwrap();
    interpreter.exec().expect("Execution failed!");
    let popped = interpreter.stack.pop().unwrap();
    // Enter an "2"
    assert_eq!(popped, 2);
}


M src/interpreter/mod.rs => src/interpreter/mod.rs +39 -8
@@ 233,9 233,35 @@ impl Interpreter {
 
                }

                // Op::InNum => {
                Op::InNum => {
                    let mut input = String::with_capacity(8);
                    // Ignore errors (it's what the author recommended)
                    if let Err(_) = io::stdin().read_line(&mut input) {
                        return Ok(());
                    }
                    input = input.trim().to_string();
                    if let Ok(val) = input.parse::<isize>() {
                        self.stack.push(val);
                    }

                    Ok(())
                }

                Op::InChar => {
                    let mut input = String::with_capacity(8);
                    // Ignore errors (it's what the author recommended)
                    if let Err(_) = io::stdin().read_line(&mut input) {
                        return Ok(());
                    }
                    input = input.trim().to_string();

                // }
                    let bytes = Self::get_utf8_char(&input);
                    let code_point = Self::utf8_to_unicode(&bytes);

                    self.stack.push(code_point);

                    Ok(())
                }

                _ => {Ok(())}



@@ 322,20 348,25 @@ impl Interpreter {
    fn get_utf8_char(buffer: &str) -> &[u8] { 

        let bytes = buffer.as_bytes();
        let num_bytes = bytes[0].reverse_bits().trailing_ones() as usize;


        return &bytes[..num_bytes];

        let num_bytes = bytes[0].leading_ones() as usize;

        if num_bytes == 0 { &bytes[..1] }
        else { &bytes[..num_bytes] }
    }

    fn utf8_to_unicode(bytes: &[u8]) -> isize { 
        let num_bytes = bytes[0].reverse_bits().trailing_ones() as usize;
        let num_bytes = bytes[0].leading_ones() as usize;

        let mut code_point: Vec<&str> = vec!();
        let byte_strs: Vec<String> = bytes.iter().map(|byte| format!("{:b}", byte)).collect();
        let header_str = &byte_strs[0];
       
        // Ascii
        if byte_strs.len() == 1 { return bytes[0] as isize }


        // Non-Ascii UTF-8

        code_point.push(&header_str[num_bytes+1..]);

        for i in 1..num_bytes {