Created
December 9, 2015 16:50
-
-
Save mtmoses/bc6822c42fa3d4216427 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// turing.go | |
// by Todd Moses | |
package main | |
import ( | |
"fmt" | |
) | |
type Tape struct { | |
Left []byte | |
Right []byte | |
} | |
type Head struct { | |
Position int | |
Value byte | |
} | |
type Machine struct { | |
T Tape | |
H Head | |
Instructions func(int, byte) (byte, int, int, bool) | |
State int | |
} | |
func NewMachine(size int, instructions func(int, byte) (byte, int, int, bool)) (m *Machine) { | |
if size <= 0 { | |
size = 1000 | |
} | |
m = new(Machine) | |
m.T.Left = make([]byte, size) | |
m.T.Right = make([]byte, size) | |
m.H.Position = 0 | |
m.Instructions = instructions | |
m.State = 0 | |
return | |
} | |
func (m *Machine) Run(show_work bool) { | |
write_val, moves, out_state, halt := m.Instructions(m.State, m.read()) | |
if show_work == true { | |
fmt.Println("P", m.H.Position, "S", m.State, "R", m.read(), "W", write_val) | |
} | |
if halt == true { | |
fmt.Println("Left", m.T.Left) | |
fmt.Println("Right", m.T.Right) | |
return | |
} | |
m.write(write_val) | |
m.move(moves) | |
m.State = out_state | |
m.Run(show_work) | |
} | |
func (m *Machine) move(spaces int) { | |
m.H.Position = m.H.Position + spaces | |
} | |
func (m *Machine) read() byte { | |
var pos int | |
var value byte | |
if m.H.Position < 0 { | |
pos = (m.H.Position * -1) | |
if (len(m.T.Left)-1) < pos { | |
m.T.Left = append(m.T.Left, 0) | |
} | |
value = m.T.Left[pos] | |
}else { | |
pos = m.H.Position | |
if (len(m.T.Right)-1) < pos { | |
m.T.Right = append(m.T.Right, 0) | |
} | |
value = m.T.Right[pos] | |
} | |
return value | |
} | |
func (m *Machine) write(value byte) { | |
var pos int | |
if m.H.Position < 0 { | |
pos = (m.H.Position * -1) | |
if (len(m.T.Left)-1) < pos { | |
m.T.Left = append(m.T.Left, 0) | |
} | |
m.T.Left[pos] = value | |
}else { | |
pos = m.H.Position | |
if (len(m.T.Right)-1) < pos { | |
m.T.Right = append(m.T.Right, 0) | |
} | |
m.T.Right[pos] = value | |
} | |
m.H.Value = value | |
} | |
func main() { | |
fmt.Println("Turing Machine") | |
var three_state = func(in_state int, value byte) (write byte, move int, out_state int, halt bool) { | |
switch(in_state) { | |
case 0: | |
write = 1 | |
move = 1 | |
out_state = 2 | |
case 1: | |
write = 1 | |
move = -1 | |
out_state = 0 | |
case 2: | |
write = 1 | |
move = 1 | |
out_state = 3 | |
case 3: | |
write = 1 | |
move = -1 | |
out_state = 4 | |
default: halt = true | |
} | |
return | |
} | |
machine := NewMachine(1000, three_state) | |
machine.Run(true) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is my first attempt at a turing machine. A Turing machine is a theoretical computing machine invented by Alan Turing (1937) to serve as an idealized model for mathematical calculation. This one can take a function as instruction and read and write values in 2 directions.