This commit is contained in:
nora 2021-04-20 15:18:54 +02:00
parent 0899a48119
commit 84f19c208c
3 changed files with 106 additions and 4 deletions

View file

@ -1 +1,11 @@
++++++++++[>+++++++++<-].
++++++++++[>++++++++++<-]>>++++++++++>->>>>>>>>>>>>>>>>-->+++++++[->++
++++++++<]>[->+>+>+>+<<<<]+++>>+++>>>++++++++[-<++++<++++<++++>>>]++++
+[-<++++<++++>>]>>-->++++++[->+++++++++++<]>[->+>+>+>+<<<<]+++++>>+>++
++++>++++++>++++++++[-<++++<++++<++++>>>]++++++[-<+++<+++<+++>>>]>>-->
---+[-<+]-<[+[->+]-<<->>>+>[-]++[-->++]-->+++[---++[--<++]---->>-<+>[+
+++[----<++++]--[>]++[-->++]--<]>++[--+[-<+]->>[-]+++++[---->++++]-->[
->+<]>>[.>]++[-->++]]-->+++]---+[-<+]->>-[+>>>+[-<+]->>>++++++++++<<[-
>+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-<+>]>
+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->+++
+++++<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>]+[-<+]->>]+[-]<<<.>>>+[
-<+]-<<]

View file

@ -1,5 +1,5 @@
use std::io::{stdin, Read};
use std::{env, fs};
use std::io::{Read, stdin};
use std::time::SystemTime;
fn main() {
@ -92,3 +92,94 @@ fn interpret(pgm: Vec<char>) -> String {
out
}
///
/// # optimization time
///
/// first parse the bf so that it can be exectued faster
/// most importantly: loop jumps should be immediate
///
mod o1 {
const MEM_SIZE: usize = 0xFFFF;
type Memory = [u8; MEM_SIZE];
///
/// A single Statement, can be an instruction or a nestable loop
#[derive(Debug, PartialOrd, PartialEq)]
enum Statement {
Inc,
Dec,
R,
L,
Out,
In,
Loop(Vec<Statement>),
}
fn minify(code: String) -> String {
let allowed: Vec<char> = vec!['>', '<', '+', '-', '.', ',', '[', ']'];
code.chars().filter(|c| allowed.contains(c)).collect()
}
fn parse(chars: Vec<char>) -> Vec<Statement> {
let mut loop_stack = vec![vec![]];
for c in chars {
match c {
'+' => loop_stack.last_mut().unwrap().push(Statement::Inc),
'-' => loop_stack.last_mut().unwrap().push(Statement::Dec),
'>' => loop_stack.last_mut().unwrap().push(Statement::R),
'<' => loop_stack.last_mut().unwrap().push(Statement::L),
'.' => loop_stack.last_mut().unwrap().push(Statement::Out),
',' => loop_stack.last_mut().unwrap().push(Statement::In),
'[' => loop_stack.push(vec![]),
']' => {
let statement = Statement::Loop(loop_stack.pop().unwrap());
loop_stack.last_mut().unwrap().push(statement);
},
_ => ()
}
}
return loop_stack.pop().unwrap();
}
fn interpret_o1(pgm: Vec<Statement>) {
}
#[cfg(test)]
mod test {
use crate::o1::{parse, Statement::{self, Inc, Dec, R, L, In, Out, Loop}};
#[test]
fn parse_no_loop() {
let program = "+-<>,.";
let statements = vec![Inc, Dec, L, R, In, Out];
let result = parse(program.chars().collect());
assert_eq!(statements, result);
}
#[test]
fn parse_simple_loop() {
let program = "+[<<]-";
let statements = vec![Inc, Loop(vec![L, L]), Dec];
let result = parse(program.chars().collect());
assert_eq!(statements, result);
}
#[test]
fn parse_complex_loops() {
let program = ">[<[][<[<]>]>[>]]";
let statements = vec![R, Loop(vec![L, Loop(vec![]), Loop(vec![L, Loop(vec![L]), R]), R, Loop(vec![R])])];
let result = parse(program.chars().collect());
assert_eq!(statements, result);
}
}
}