Skip to content

Commit 6a6723c

Browse files
authored
Merge pull request #8 from rezigned/add-execution-mode
feat: add execution mode + refactor
2 parents 22936c0 + b23fc38 commit 6a6723c

File tree

20 files changed

+905
-602
lines changed

20 files changed

+905
-602
lines changed

examples/even-zeros-and-ones.tur

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# https://www.youtube.com/watch?v=PLVCscCY4xI
2+
name: Even Number of 0s and 1s
3+
tape: 0, 0, 1, 1
4+
rules:
5+
start:
6+
X, R, start
7+
0 -> X, R, B
8+
1 -> X, R, C
9+
_, R, accept
10+
B:
11+
X, R, B
12+
0, R, B
13+
1 -> X, L, D
14+
C:
15+
X, R, C
16+
1, R, C
17+
0 -> X, L, D
18+
D:
19+
X, L, D
20+
1, L, D
21+
0, L, D
22+
_, R, start
23+
accept:

examples/multi-tape-compare.tur

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ heads: [0, 0, 0]
33
tapes:
44
[a, b, c]
55
[a, b, c]
6-
[-]
6+
[_]
77
rules:
88
start:
9-
[a, a, -] -> [a, a, -], [R, R, S], start
10-
[b, b, -] -> [b, b, -], [R, R, S], start
11-
[c, c, -] -> [c, c, -], [R, R, S], start
12-
[-, -, -] -> [-, -, Y], [S, S, S], halt
13-
[a, b, -] -> [a, b, N], [S, S, S], halt
14-
[a, c, -] -> [a, c, N], [S, S, S], halt
15-
[b, a, -] -> [b, a, N], [S, S, S], halt
16-
[b, c, -] -> [b, c, N], [S, S, S], halt
17-
[c, a, -] -> [c, a, N], [S, S, S], halt
18-
[c, b, -] -> [c, b, N], [S, S, S], halt
19-
[a, -, -] -> [a, -, N], [S, S, S], halt
20-
[b, -, -] -> [b, -, N], [S, S, S], halt
21-
[c, -, -] -> [c, -, N], [S, S, S], halt
22-
[-, a, -] -> [-, a, N], [S, S, S], halt
23-
[-, b, -] -> [-, b, N], [S, S, S], halt
24-
[-, c, -] -> [-, c, N], [S, S, S], halt
9+
[a, a, _] -> [a, a, _], [R, R, S], start
10+
[b, b, _] -> [b, b, _], [R, R, S], start
11+
[c, c, _] -> [c, c, _], [R, R, S], start
12+
[_, _, _] -> [_, _, Y], [S, S, S], halt
13+
[a, b, _] -> [a, b, N], [S, S, S], halt
14+
[a, c, _] -> [a, c, N], [S, S, S], halt
15+
[b, a, _] -> [b, a, N], [S, S, S], halt
16+
[b, c, _] -> [b, c, N], [S, S, S], halt
17+
[c, a, _] -> [c, a, N], [S, S, S], halt
18+
[c, b, _] -> [c, b, N], [S, S, S], halt
19+
[a, _, _] -> [a, _, N], [S, S, S], halt
20+
[b, _, _] -> [b, _, N], [S, S, S], halt
21+
[c, _, _] -> [c, _, N], [S, S, S], halt
22+
[_, a, _] -> [_, a, N], [S, S, S], halt
23+
[_, b, _] -> [_, b, N], [S, S, S], halt
24+
[_, c, _] -> [_, c, N], [S, S, S], halt
2525
halt:

examples/multi-tape-copy.tur

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ name: Multi-Tape Copy
22
heads: [0, 0]
33
tapes:
44
[a, b, c]
5-
[-, -, -]
5+
[_, _, _]
66
rules:
77
start:
8-
[a, -] -> [a, a], [R, R], start
9-
[b, -] -> [b, b], [R, R], start
10-
[c, -] -> [c, c], [R, R], start
11-
[-, -] -> [-, -], [L, L], rewind
8+
[a, _] -> [a, a], [R, R], start
9+
[b, _] -> [b, b], [R, R], start
10+
[c, _] -> [c, c], [R, R], start
11+
[_, _] -> [_, _], [L, L], rewind
1212
rewind:
1313
[a, a] -> [a, a], [L, L], rewind
1414
[b, b] -> [b, b], [L, L], rewind
1515
[c, c] -> [c, c], [L, L], rewind
16-
[-, -] -> [-, -], [S, S], halt
16+
[_, _] -> [_, _], [S, S], halt
1717
halt:

examples/subtraction.tur

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,53 @@
11
name: Subtraction
22
tape: 1, 1, 1, -, 1, 1
3+
head: 0
4+
35
rules:
46
start:
5-
0 -> 0, R, start
67
1 -> 1, R, start
78
- -> -, R, s1
9+
_ -> _, L, stop
810
s1:
9-
0 -> 0, R, s1
1011
1 -> 1, R, s1
11-
- -> -, L, s2
12+
_ -> _, L, s2
1213
s2:
13-
0 -> 0, L, s2
14-
1 -> 0, L, s3
14+
1 -> _, L, s3
15+
_ -> _, L, s2
1516
- -> -, R, s5
1617
s3:
17-
0 -> 0, L, s3
1818
1 -> 1, L, s3
1919
- -> -, L, s8
20+
_ -> _, L, s3
2021
s4:
21-
0 -> 0, R, s4
2222
1 -> 1, R, s4
2323
- -> -, R, s5
24+
_ -> _, R, s4
2425
s5:
25-
0 -> 0, R, s5
2626
1 -> 1, R, s5
27-
- -> -, L, s6
27+
_ -> _, L, s6
2828
s6:
29-
0 -> -, L, s6
29+
_ -> _, L, s6
3030
1 -> 1, L, s6
3131
- -> -, L, s7
3232
s7:
33-
0 -> -, L, s7
33+
_ -> _, R, s9
3434
1 -> 1, L, s7
3535
- -> -, R, s9
3636
s8:
37-
0 -> 0, L, s8
38-
1 -> 0, R, start
39-
- -> B, R, s4
37+
1 -> _, R, start
38+
- -> _, R, s4
39+
_ -> _, L, s8
4040
s9:
41-
0 -> 0, L, stop
41+
_ -> _, R, s10
4242
1 -> 1, L, stop
43-
- -> 0, L, stop
43+
s10:
44+
_ -> _, R, s10
45+
1 -> 1, R, s10
46+
- -> _, L, s11
47+
s11:
48+
_ -> _, L, s11
49+
1 -> 1, L, s12
50+
s12:
51+
1 -> 1, L, s12
52+
_ -> _, R, stop
4453
stop:

platforms/cli/src/main.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::io::{self, BufRead};
33
use std::path::Path;
44
use tur::loader::ProgramLoader;
55
use tur::machine::TuringMachine;
6-
use tur::ExecutionResult;
6+
use tur::Step;
77

88
#[derive(Parser)]
99
#[clap(author, version, about, long_about = None, arg_required_else_help = true)]
@@ -52,48 +52,38 @@ fn main() {
5252
if cli.debug {
5353
run_with_debug(&mut machine);
5454
} else {
55-
machine.run_to_completion();
55+
machine.run();
5656
}
5757

58-
println!("{}", machine.get_tapes_as_strings().join("\n"));
58+
println!("{}", format_tapes(machine.tapes()).join("\n"));
5959
}
6060

6161
/// Runs the Turing machine with debug output, printing each step.
6262
fn run_with_debug(machine: &mut TuringMachine) {
6363
let print_state = |machine: &TuringMachine| {
64-
let tapes_str = machine
65-
.get_tapes_as_strings()
66-
.iter()
67-
.map(|s| s.to_string())
68-
.collect::<Vec<String>>()
69-
.join(", ");
70-
7164
println!(
7265
"Step: {}, State: {}, Tapes: [{}], Heads: {:?}",
7366
machine.step_count(),
7467
machine.state(),
75-
tapes_str,
76-
machine.head_positions()
68+
format_tapes(machine.tapes()).join(", "),
69+
machine.heads()
7770
);
7871
};
7972

8073
print_state(machine);
8174

8275
loop {
8376
match machine.step() {
84-
ExecutionResult::Continue => {
77+
Step::Continue => {
8578
print_state(machine);
8679
}
87-
ExecutionResult::Halt => {
80+
Step::Halt(_) => {
8881
println!("\nMachine halted.");
8982
break;
9083
}
91-
ExecutionResult::Error(e) => {
92-
println!("\nMachine error: {}", e);
93-
break;
94-
}
9584
}
9685
}
86+
9787
println!("\nFinal tapes:");
9888
}
9989

@@ -121,3 +111,8 @@ fn read_tape_inputs(inputs: &[String]) -> Result<Vec<String>, String> {
121111
Ok(Vec::new())
122112
}
123113
}
114+
115+
/// Returns the content of all tapes as a vector of `String`s.
116+
pub fn format_tapes(tapes: &[Vec<char>]) -> Vec<String> {
117+
tapes.iter().map(|tape| tape.iter().collect()).collect()
118+
}

0 commit comments

Comments
 (0)