Source file
test/turing.go
1
2
3
4
5
6
7
8
9 package main
10
11
12
13 var p, pc int
14 var a [30000]byte
15
16 const prog = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.!"
17
18 func scan(dir int) {
19 for nest := dir; dir*nest > 0; pc += dir {
20 switch prog[pc+dir] {
21 case ']':
22 nest--
23 case '[':
24 nest++
25 }
26 }
27 }
28
29 func main() {
30 r := ""
31 for {
32 switch prog[pc] {
33 case '>':
34 p++
35 case '<':
36 p--
37 case '+':
38 a[p]++
39 case '-':
40 a[p]--
41 case '.':
42 r += string(a[p])
43 case '[':
44 if a[p] == 0 {
45 scan(1)
46 }
47 case ']':
48 if a[p] != 0 {
49 scan(-1)
50 }
51 default:
52 if r != "Hello World!\n" {
53 panic(r)
54 }
55 return
56 }
57 pc++
58 }
59 }
60
View as plain text