aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Pike <r@golang.org>2010-09-04 10:40:00 +1000
committerRob Pike <r@golang.org>2010-09-04 10:40:00 +1000
commite430ee6cbacc9574cab5a507013fe3ea11783cba (patch)
treedacda2cbe9094704998a34d2421830ed54a43c23
parent4f61fc96b2788be7cbfc6f7a72acef3d3feeeb6c (diff)
downloadgo-e430ee6cbacc9574cab5a507013fe3ea11783cba.tar.gz
go-e430ee6cbacc9574cab5a507013fe3ea11783cba.zip
test/turing: refactor
R=gri CC=golang-dev https://golang.org/cl/2116043
-rw-r--r--test/turing.go51
1 files changed, 24 insertions, 27 deletions
diff --git a/test/turing.go b/test/turing.go
index 462bb91684..0af39de8b2 100644
--- a/test/turing.go
+++ b/test/turing.go
@@ -8,48 +8,45 @@ 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;
for {
switch prog[pc] {
case '>':
- p++;
+ p++
case '<':
- p--;
+ p--
case '+':
- a[p]++;
+ a[p]++
case '-':
- a[p]--;
+ a[p]--
case '.':
- print(string(a[p]));
+ print(string(a[p]))
case '[':
if a[p] == 0 {
- for nest := 1; nest > 0; pc++ {
- switch prog[pc+1] {
- case ']':
- nest--;
- case '[':
- nest++;
- }
- }
+ scan(1)
}
case ']':
if a[p] != 0 {
- for nest := -1; nest < 0; pc-- {
- switch prog[pc-1] {
- case ']':
- nest--;
- case '[':
- nest++;
- }
- }
+ scan(-1)
}
default:
- return;
+ return
}
- pc++;
+ pc++
}
}