From e430ee6cbacc9574cab5a507013fe3ea11783cba Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Sat, 4 Sep 2010 10:40:00 +1000 Subject: [PATCH] test/turing: refactor R=gri CC=golang-dev https://golang.org/cl/2116043 --- test/turing.go | 51 ++++++++++++++++++++++++-------------------------- 1 file 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++ } } -- 2.48.1