]> Cypherpunks.ru repositories - gostls13.git/blob - test/turing.go
lots of new tests
[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 func main() {
12         var a [30000]byte;
13         prog := "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.!";
14         p := 0;
15         pc := 0;
16         for {
17                 switch prog[pc] {
18                         case '>':
19                                         p++;
20                         case '<':
21                                         p--;
22                         case '+':
23                                         a[p]++;
24                         case '-':
25                                         a[p]--;
26                         case '.':
27                                         print string(a[p]);
28                         case '[':
29                                 if a[p] == 0 {
30                                         for nest := 1; nest > 0; pc++ {
31                                                 switch prog[pc+1] {
32                                                         case ']':
33                                                                 nest--;
34                                                         case '[':
35                                                                 nest++;
36                                                 }
37                                         }
38                                 }
39                         case ']':
40                                 if a[p] != 0 {
41                                         for nest := -1; nest < 0; pc-- {
42                                                 switch prog[pc-1] {
43                                                         case ']':
44                                                                 nest--;
45                                                         case '[':
46                                                                 nest++;
47                                                 }
48                                         }
49                                 }
50                         default:
51                                         return;
52                 }
53                 pc++;
54         }
55 }