]> Cypherpunks.ru repositories - gostls13.git/blob - test/turing.go
test/turing: refactor
[gostls13.git] / test / turing.go
1 // $G $F.go && $L $F.$A && ./$A.out
2
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 package main
8
9 // brainfuck
10
11 var p, pc int
12 var a [30000]byte
13 const prog = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.!"
14
15 func scan(dir int) {
16         for nest := dir; dir*nest > 0; pc += dir {
17                 switch prog[pc+dir] {
18                         case ']':
19                                 nest--
20                         case '[':
21                                 nest++
22                 }
23         }
24 }
25
26 func main() {
27         for {
28                 switch prog[pc] {
29                         case '>':
30                                         p++
31                         case '<':
32                                         p--
33                         case '+':
34                                         a[p]++
35                         case '-':
36                                         a[p]--
37                         case '.':
38                                         print(string(a[p]))
39                         case '[':
40                                 if a[p] == 0 {
41                                         scan(1)
42                                 }
43                         case ']':
44                                 if a[p] != 0 {
45                                         scan(-1)
46                                 }
47                         default:
48                                         return
49                 }
50                 pc++
51         }
52 }