From e958ca68997fb33cfcd0951d0f4a96aacc5d47c2 Mon Sep 17 00:00:00 2001 From: Jason Staten Date: Wed, 30 May 2018 16:21:44 -0600 Subject: [PATCH] It computes! --- .gitignore | 2 + Cargo.lock | 4 ++ Cargo.toml | 6 +++ src/main.rs | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..bdb4737 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "rsturing" +version = "0.1.0" + diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..42112cd --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "rsturing" +version = "0.1.0" +authors = ["Jason Staten "] + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d60f10b --- /dev/null +++ b/src/main.rs @@ -0,0 +1,118 @@ +type State = usize; + +#[derive(Debug, PartialEq, Clone)] +enum Cell { + Blank, + Zero, + One, +} + +#[derive(Debug)] +enum Move { + Stay, + Left, + Right, +} + +struct Transition { + start: State, + end: State, + read: Cell, + write: Cell, + operation: Move, +} + +struct Machine { + initial: State, + done: State, + transitions: Vec, +} + +type Tape = Vec; + +fn run(machine: Machine, code: Tape) -> Tape { + let mut position = 0; + let mut state = machine.initial; + let mut tape = code.to_vec(); + tape.resize(10, Cell::Blank); + + while state != machine.done { + let transition; + { + let cell = tape.get(position).unwrap_or(&Cell::Blank); + transition = machine + .transitions + .iter() + .find(|t| t.start == state && &t.read == cell) + .expect("Missing transition"); + } + state = transition.end; + tape[position] = transition.write.clone(); + position = match transition.operation { + Move::Left => position - 1, + Move::Right => position + 1, + Move::Stay => position, + }; + } + + return tape; +} + +#[test] +fn test_build() { + let machine = Machine { + initial: 0, + done: 2, + transitions: vec![ + Transition { + start: 0, + end: 0, + read: Cell::Zero, + write: Cell::Zero, + operation: Move::Right, + }, + Transition { + start: 0, + end: 0, + read: Cell::One, + write: Cell::One, + operation: Move::Right, + }, + Transition { + start: 0, + end: 1, + read: Cell::Blank, + write: Cell::Blank, + operation: Move::Left, + }, + Transition { + start: 1, + end: 1, + read: Cell::One, + write: Cell::Zero, + operation: Move::Left, + }, + Transition { + start: 1, + end: 2, + read: Cell::Zero, + write: Cell::One, + operation: Move::Stay, + }, + Transition { + start: 1, + end: 2, + read: Cell::Blank, + write: Cell::One, + operation: Move::Stay, + }, + ], + }; + + let result = run(machine, vec![Cell::One, Cell::One, Cell::Zero, Cell::One]); + println!("{:?}", result); + + let mut expected = vec![Cell::One, Cell::One, Cell::One, Cell::Zero]; + expected.resize(10, Cell::Blank); + assert_eq!(result, expected); +}