Merge from trunk
[official-gcc.git] / gcc / testsuite / go.test / test / turing.go
blobacbe85b646ce7671dafc290afe39afb5aa5fe9e7
1 // run
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.
7 // Test simulating a Turing machine, sort of.
9 package main
11 // brainfuck
13 var p, pc int
14 var a [30000]byte
16 const prog = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.!"
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++
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)
47 case ']':
48 if a[p] != 0 {
49 scan(-1)
51 default:
52 if r != "Hello World!\n" {
53 panic(r)
55 return
57 pc++