...
Run Format

Source file test/turing.go

Documentation: test

     1  // run
     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  // Test simulating a Turing machine, sort of.
     8  
     9  package main
    10  
    11  // brainfuck
    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