diff --git a/bfi-rust/src/interpreter/mod.rs b/bfi-rust/src/interpreter/mod.rs index e636598..9d02624 100644 --- a/bfi-rust/src/interpreter/mod.rs +++ b/bfi-rust/src/interpreter/mod.rs @@ -50,7 +50,7 @@ fn parse(chars: Vec, direct_print: bool) -> Vec { } } - return loop_stack.pop().unwrap(); + loop_stack.pop().unwrap() } #[cfg(test)] diff --git a/bfi-rust/src/interpreter/optimized/mod.rs b/bfi-rust/src/interpreter/optimized/mod.rs index 7df4ab4..8260f61 100644 --- a/bfi-rust/src/interpreter/optimized/mod.rs +++ b/bfi-rust/src/interpreter/optimized/mod.rs @@ -37,7 +37,7 @@ impl From for ExStatement { Statement::In => ExStatement::In, Statement::Out => ExStatement::Out, Statement::Loop(v) => ExStatement::Loop( - v.into_iter().map(|s| ExStatement::from(s)).collect() + v.into_iter().map(ExStatement::from).collect() ), Statement::DOut => ExStatement::DOut } @@ -66,20 +66,19 @@ impl Error for BfErr {} pub fn run(pgm: &str, direct_print: bool) -> Result { let pgm = minify(pgm); - if pgm.len() < 1 { return Err(BfErr::new("no program found")); }; + if pgm.is_empty() { return Err(BfErr::new("no program found")); }; let pgm = parse(pgm.chars().collect(), direct_print); let pgm = optimize(&pgm); let out = interpret(&pgm); Ok(out) } -fn optimize(code: &Vec) -> Vec { +fn optimize(code: &[Statement]) -> Vec { let code = o_set_null(code); - let code = o_repeat(code); - code + o_repeat(code) } -fn o_set_null(code: &Vec) -> Vec { +fn o_set_null(code: &[Statement]) -> Vec { code.iter().map(|s| { match s { Statement::Loop(v) => { @@ -119,7 +118,7 @@ fn o_repeat(code: Vec) -> Vec { result } -fn interpret(pgm: &Vec) -> String { +fn interpret(pgm: &[ExStatement]) -> String { let mut out = String::new(); let mut pointer: usize = 0; let mut mem: [u8; MEM_SIZE] = [0; MEM_SIZE]; @@ -145,7 +144,7 @@ fn execute(statement: &ExStatement, mem: &mut Memory, pointer: &mut usize, out: } ExStatement::In => { let mut in_buffer = [0, 1]; - stdin().read(&mut in_buffer).unwrap(); + stdin().read_exact(&mut in_buffer).unwrap(); mem[*pointer] = in_buffer[0] as u8; } ExStatement::Loop(vec) => { diff --git a/bfi-rust/src/interpreter/parsed.rs b/bfi-rust/src/interpreter/parsed.rs index eb9f769..ce0caa5 100644 --- a/bfi-rust/src/interpreter/parsed.rs +++ b/bfi-rust/src/interpreter/parsed.rs @@ -12,11 +12,10 @@ use crate::interpreter::{MEM_SIZE, Memory, minify, parse, Statement}; pub fn run(pgm: &str) -> String { let pgm = minify(pgm); let pgm = parse(pgm.chars().collect(), false); - let out = interpret(&pgm); - out + interpret(&pgm) } -fn interpret(pgm: &Vec) -> String { +fn interpret(pgm: &[Statement]) -> String { let mut out = String::new(); let mut pointer: usize = 0; let mut mem: [u8; MEM_SIZE] = [0; MEM_SIZE]; @@ -37,7 +36,7 @@ fn execute(statement: &Statement, mem: &mut Memory, pointer: &mut usize, out: &m Statement::Out => out.push(mem[*pointer] as u8 as char), Statement::In => { let mut in_buffer = [0, 1]; - stdin().read(&mut in_buffer).unwrap(); + stdin().read_exact(&mut in_buffer).unwrap(); mem[*pointer] = in_buffer[0] as u8; } Statement::Loop(vec) => { diff --git a/bfi-rust/src/interpreter/simple.rs b/bfi-rust/src/interpreter/simple.rs index f9ce80a..4652577 100644 --- a/bfi-rust/src/interpreter/simple.rs +++ b/bfi-rust/src/interpreter/simple.rs @@ -27,7 +27,7 @@ fn interpret(pgm: Vec) -> String { '-' => mem[pointer] = mem[pointer].wrapping_sub(1), '.' => out.push(mem[pointer] as u8 as char), ',' => { - stdin().read(&mut in_buffer).unwrap(); + stdin().read_exact(&mut in_buffer).unwrap(); mem[pointer] = in_buffer[0] as u8; } '[' => { diff --git a/bfi-rust/src/main.rs b/bfi-rust/src/main.rs index 222a01c..0215baf 100644 --- a/bfi-rust/src/main.rs +++ b/bfi-rust/src/main.rs @@ -1,11 +1,12 @@ mod interpreter; +mod repl; use std::{env, fs}; use std::time::SystemTime; fn main() { - let path = env::args().skip(1).next(); + let path = env::args().nth(1); let path = match path { Some(p) => p, None => { diff --git a/bfi-rust/src/repl/mod.rs b/bfi-rust/src/repl/mod.rs new file mode 100644 index 0000000..e69de29