diff options
Diffstat (limited to 'test/turing.go')
-rw-r--r-- | test/turing.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/test/turing.go b/test/turing.go new file mode 100644 index 000000000..a7a8ea786 --- /dev/null +++ b/test/turing.go @@ -0,0 +1,55 @@ +// $G $F.go && $L $F.$A && ./$A.out + +// 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. + +package main + +// brainfuck + +func main() { + var a [30000]byte; + prog := "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>."; + p := 0; + pc := 0; + 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++ { + if prog[pc+1] == ']' { + nest--; + } + if prog[pc+1] == '[' { + nest++; + } + } + } + case ']': + if a[p] != 0 { + for nest := -1; nest < 0; pc-- { + if prog[pc-1] == ']' { + nest--; + } + if prog[pc-1] == '[' { + nest++; + } + } + } + default: + return; + } + pc++; + } +} |