Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/even-zeros-and-ones.tur
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# https://www.youtube.com/watch?v=PLVCscCY4xI
name: Even Number of 0s and 1s
tape: 0, 0, 1, 1
rules:
start:
X, R, start
0 -> X, R, B
1 -> X, R, C
_, R, accept
B:
X, R, B
0, R, B
1 -> X, L, D
C:
X, R, C
1, R, C
0 -> X, L, D
D:
X, L, D
1, L, D
0, L, D
_, R, start
accept:
34 changes: 17 additions & 17 deletions examples/multi-tape-compare.tur
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ heads: [0, 0, 0]
tapes:
[a, b, c]
[a, b, c]
[-]
[_]
rules:
start:
[a, a, -] -> [a, a, -], [R, R, S], start
[b, b, -] -> [b, b, -], [R, R, S], start
[c, c, -] -> [c, c, -], [R, R, S], start
[-, -, -] -> [-, -, Y], [S, S, S], halt
[a, b, -] -> [a, b, N], [S, S, S], halt
[a, c, -] -> [a, c, N], [S, S, S], halt
[b, a, -] -> [b, a, N], [S, S, S], halt
[b, c, -] -> [b, c, N], [S, S, S], halt
[c, a, -] -> [c, a, N], [S, S, S], halt
[c, b, -] -> [c, b, N], [S, S, S], halt
[a, -, -] -> [a, -, N], [S, S, S], halt
[b, -, -] -> [b, -, N], [S, S, S], halt
[c, -, -] -> [c, -, N], [S, S, S], halt
[-, a, -] -> [-, a, N], [S, S, S], halt
[-, b, -] -> [-, b, N], [S, S, S], halt
[-, c, -] -> [-, c, N], [S, S, S], halt
[a, a, _] -> [a, a, _], [R, R, S], start
[b, b, _] -> [b, b, _], [R, R, S], start
[c, c, _] -> [c, c, _], [R, R, S], start
[_, _, _] -> [_, _, Y], [S, S, S], halt
[a, b, _] -> [a, b, N], [S, S, S], halt
[a, c, _] -> [a, c, N], [S, S, S], halt
[b, a, _] -> [b, a, N], [S, S, S], halt
[b, c, _] -> [b, c, N], [S, S, S], halt
[c, a, _] -> [c, a, N], [S, S, S], halt
[c, b, _] -> [c, b, N], [S, S, S], halt
[a, _, _] -> [a, _, N], [S, S, S], halt
[b, _, _] -> [b, _, N], [S, S, S], halt
[c, _, _] -> [c, _, N], [S, S, S], halt
[_, a, _] -> [_, a, N], [S, S, S], halt
[_, b, _] -> [_, b, N], [S, S, S], halt
[_, c, _] -> [_, c, N], [S, S, S], halt
halt:
12 changes: 6 additions & 6 deletions examples/multi-tape-copy.tur
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ name: Multi-Tape Copy
heads: [0, 0]
tapes:
[a, b, c]
[-, -, -]
[_, _, _]
rules:
start:
[a, -] -> [a, a], [R, R], start
[b, -] -> [b, b], [R, R], start
[c, -] -> [c, c], [R, R], start
[-, -] -> [-, -], [L, L], rewind
[a, _] -> [a, a], [R, R], start
[b, _] -> [b, b], [R, R], start
[c, _] -> [c, c], [R, R], start
[_, _] -> [_, _], [L, L], rewind
rewind:
[a, a] -> [a, a], [L, L], rewind
[b, b] -> [b, b], [L, L], rewind
[c, c] -> [c, c], [L, L], rewind
[-, -] -> [-, -], [S, S], halt
[_, _] -> [_, _], [S, S], halt
halt:
41 changes: 25 additions & 16 deletions examples/subtraction.tur
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
name: Subtraction
tape: 1, 1, 1, -, 1, 1
head: 0

rules:
start:
0 -> 0, R, start
1 -> 1, R, start
- -> -, R, s1
_ -> _, L, stop
s1:
0 -> 0, R, s1
1 -> 1, R, s1
- -> -, L, s2
_ -> _, L, s2
s2:
0 -> 0, L, s2
1 -> 0, L, s3
1 -> _, L, s3
_ -> _, L, s2
- -> -, R, s5
s3:
0 -> 0, L, s3
1 -> 1, L, s3
- -> -, L, s8
_ -> _, L, s3
s4:
0 -> 0, R, s4
1 -> 1, R, s4
- -> -, R, s5
_ -> _, R, s4
s5:
0 -> 0, R, s5
1 -> 1, R, s5
- -> -, L, s6
_ -> _, L, s6
s6:
0 -> -, L, s6
_ -> _, L, s6
1 -> 1, L, s6
- -> -, L, s7
s7:
0 -> -, L, s7
_ -> _, R, s9
1 -> 1, L, s7
- -> -, R, s9
s8:
0 -> 0, L, s8
1 -> 0, R, start
- -> B, R, s4
1 -> _, R, start
- -> _, R, s4
_ -> _, L, s8
s9:
0 -> 0, L, stop
_ -> _, R, s10
1 -> 1, L, stop
- -> 0, L, stop
s10:
_ -> _, R, s10
1 -> 1, R, s10
- -> _, L, s11
s11:
_ -> _, L, s11
1 -> 1, L, s12
s12:
1 -> 1, L, s12
_ -> _, R, stop
stop:
31 changes: 13 additions & 18 deletions platforms/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::io::{self, BufRead};
use std::path::Path;
use tur::loader::ProgramLoader;
use tur::machine::TuringMachine;
use tur::ExecutionResult;
use tur::Step;

#[derive(Parser)]
#[clap(author, version, about, long_about = None, arg_required_else_help = true)]
Expand Down Expand Up @@ -52,48 +52,38 @@ fn main() {
if cli.debug {
run_with_debug(&mut machine);
} else {
machine.run_to_completion();
machine.run();
}

println!("{}", machine.get_tapes_as_strings().join("\n"));
println!("{}", format_tapes(machine.tapes()).join("\n"));
}

/// Runs the Turing machine with debug output, printing each step.
fn run_with_debug(machine: &mut TuringMachine) {
let print_state = |machine: &TuringMachine| {
let tapes_str = machine
.get_tapes_as_strings()
.iter()
.map(|s| s.to_string())
.collect::<Vec<String>>()
.join(", ");

println!(
"Step: {}, State: {}, Tapes: [{}], Heads: {:?}",
machine.step_count(),
machine.state(),
tapes_str,
machine.head_positions()
format_tapes(machine.tapes()).join(", "),
machine.heads()
);
};

print_state(machine);

loop {
match machine.step() {
ExecutionResult::Continue => {
Step::Continue => {
print_state(machine);
}
ExecutionResult::Halt => {
Step::Halt(_) => {
println!("\nMachine halted.");
break;
}
ExecutionResult::Error(e) => {
println!("\nMachine error: {}", e);
break;
}
}
}

println!("\nFinal tapes:");
}

Expand Down Expand Up @@ -121,3 +111,8 @@ fn read_tape_inputs(inputs: &[String]) -> Result<Vec<String>, String> {
Ok(Vec::new())
}
}

/// Returns the content of all tapes as a vector of `String`s.
pub fn format_tapes(tapes: &[Vec<char>]) -> Vec<String> {
tapes.iter().map(|tape| tape.iter().collect()).collect()
}
Loading
Loading