]> Cypherpunks.ru repositories - gostls13.git/blobdiff - test/turing.go
cmd/compile/internal/inline: score call sites exposed by inlines
[gostls13.git] / test / turing.go
index c622bfb74185c16bd57ca2bc76d1417dd5db24e2..acbe85b646ce7671dafc290afe39afb5aa5fe9e7 100644 (file)
@@ -1,55 +1,59 @@
-// $G $F.go && $L $F.$A && ./$A.out
+// run
 
 // Copyright 2009 The Go Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// Test simulating a Turing machine, sort of.
+
 package main
 
 // brainfuck
 
+var p, pc int
+var a [30000]byte
+
+const prog = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.!"
+
+func scan(dir int) {
+       for nest := dir; dir*nest > 0; pc += dir {
+               switch prog[pc+dir] {
+               case ']':
+                       nest--
+               case '[':
+                       nest++
+               }
+       }
+}
+
 func main() {
-       var a [30000]byte;
-       prog := "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.!";
-       p := 0;
-       pc := 0;
+       r := ""
        for {
                switch prog[pc] {
-                       case '>':
-                                       p++;
-                       case '<':
-                                       p--;
-                       case '+':
-                                       a[p]++;
-                       case '-':
-                                       a[p]--;
-                       case '.':
-                                       print string(a[p]);
-                       case '[':
-                               if a[p] == 0 {
-                                       for nest := 1; nest > 0; pc++ {
-                                               switch prog[pc+1] {
-                                                       case ']':
-                                                               nest--;
-                                                       case '[':
-                                                               nest++;
-                                               }
-                                       }
-                               }
-                       case ']':
-                               if a[p] != 0 {
-                                       for nest := -1; nest < 0; pc-- {
-                                               switch prog[pc-1] {
-                                                       case ']':
-                                                               nest--;
-                                                       case '[':
-                                                               nest++;
-                                               }
-                                       }
-                               }
-                       default:
-                                       return;
+               case '>':
+                       p++
+               case '<':
+                       p--
+               case '+':
+                       a[p]++
+               case '-':
+                       a[p]--
+               case '.':
+                       r += string(a[p])
+               case '[':
+                       if a[p] == 0 {
+                               scan(1)
+                       }
+               case ']':
+                       if a[p] != 0 {
+                               scan(-1)
+                       }
+               default:
+                       if r != "Hello World!\n" {
+                               panic(r)
+                       }
+                       return
                }
-               pc++;
+               pc++
        }
 }