1 USING: arrays assocs io kernel math namespaces
2 prettyprint sequences strings vectors words accessors ;
5 ! A turing machine simulator.
7 TUPLE: state sym dir next ;
9 ! Mapping from symbol/state pairs into new-state tuples
15 ! This is a simple program that outputs 5 1's
17 { { 1 0 } T{ state f 1 1 2 } }
18 { { 2 0 } T{ state f 1 1 3 } }
19 { { 3 0 } T{ state f 1 -1 1 } }
20 { { 1 1 } T{ state f 1 -1 2 } }
21 { { 2 1 } T{ state f 1 -1 3 } }
22 { { 3 1 } T{ state f 1 -1 halt } }
31 ! Position of head on tape
34 ! Initial tape position
37 ! The tape, a mutable sequence of some kind
41 20 0 <array> >vector tape set
44 #! Symbol at head position.
45 position get tape get nth ;
48 #! Set symbol at head position.
49 position get tape get set-nth ;
51 : next-state ( -- state )
52 #! Look up the next state/symbol/direction triplet.
53 state get sym 2array states get at ;
56 #! Do one step of the turing machine.
59 dup dir>> position [ + ] change
63 #! Print current turing machine state.
66 2 position get 2 * + CHAR: \s <string> write "^" print ;
69 #! Do one step and print new state.