From 1054fe8ec2fe85504586eece9fe7c3143066916a Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Thu, 22 Apr 2021 19:32:29 +0200 Subject: [PATCH] o1 --- bfi-rust/src/main.rs | 67 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 12 deletions(-) diff --git a/bfi-rust/src/main.rs b/bfi-rust/src/main.rs index ebd5eb4..2ea7772 100644 --- a/bfi-rust/src/main.rs +++ b/bfi-rust/src/main.rs @@ -22,6 +22,11 @@ fn run(path: String) { println!("{}", program); let start = SystemTime::now(); + + //new + //let out = o1::run(&*program); + + //old let out = interpret(program.chars().collect()); println!("{}\nFinished execution in {}ms", out, start.elapsed().unwrap().as_millis()); @@ -96,7 +101,7 @@ fn interpret(pgm: Vec) -> String { /// /// # optimization time /// -/// first parse the bf so that it can be exectued faster +/// first parse the bf so that it can be executed faster /// most importantly: loop jumps should be immediate /// mod o1 { @@ -106,6 +111,14 @@ mod o1 { type Memory = [u8; MEM_SIZE]; + + pub fn run(pgm: &str) -> String { + let pgm = minify(pgm); + let pgm = parse(pgm.chars().collect()); + let out = interpret(&pgm); + out + } + /// /// A single Statement, can be an instruction or a nestable loop #[derive(Debug, PartialOrd, PartialEq)] @@ -119,14 +132,7 @@ mod o1 { Loop(Vec), } - fn run(pgm: String) -> String{ - let pgm = minify(pgm); - let pgm = parse(pgm.chars().collect()); - let out = interpret(pgm); - out - } - - fn minify(code: String) -> String { + fn minify(code: &str) -> String { let allowed: Vec = vec!['>', '<', '+', '-', '.', ',', '[', ']']; code.chars().filter(|c| allowed.contains(c)).collect() } @@ -154,13 +160,13 @@ mod o1 { return loop_stack.pop().unwrap(); } - fn interpret(pgm: Vec) -> String { + fn interpret(pgm: &Vec) -> String { let mut out = String::new(); let mut pointer: usize = 0; let mut mem: [u8; MEM_SIZE] = [0; MEM_SIZE]; for s in pgm { - execute(&s, &mut mem, &mut pointer, &mut out) + execute(s, &mut mem, &mut pointer, &mut out) } out @@ -191,7 +197,7 @@ mod o1 { #[cfg(test)] mod test { - use crate::o1::{parse, Statement::{self, Inc, Dec, R, L, In, Out, Loop}, execute}; + use crate::o1::{parse, Statement::{self, Inc, Dec, R, L, In, Out, Loop}, execute, run}; #[test] fn parse_no_loop() { @@ -236,5 +242,42 @@ mod o1 { execute(&Statement::Dec, &mut mem, &mut pointer, &mut out); assert_eq!(mem[pointer], 0); } + + #[test] + fn execute_false_loop() { + let statement = Statement::Loop(vec![Statement::Inc, Statement::Inc, Statement::R]); + let mut pointer: usize = 0; + let mut mem: [u8; 65535] = [0; 65535]; + + execute(&statement, &mut mem, &mut pointer, &mut String::new()); + assert_eq!(mem[0], 0); + assert_eq!(mem[1], 0); + } + + #[test] + fn execute_loop() { + let statement = Statement::Loop(vec![Statement::Inc, Statement::Inc, Statement::R]); + let mut pointer: usize = 0; + let mut mem: [u8; 65535] = [0; 65535]; + mem[0] = 1; + + execute(&statement, &mut mem, &mut pointer, &mut String::new()); + assert_eq!(mem[0], 3); + assert_eq!(mem[1], 0); + } + + #[test] + fn run_loop() { + let program = "++++++++++[>++++++++++<-]>."; + let out = run(program); + assert_eq!(out, String::from("d")); + } + + #[test] + fn hello_world() { + let program = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."; + let out = run(program); + assert_eq!(out, String::from("Hello World!\n")); + } } } \ No newline at end of file