1
0
mirror of https://github.com/golang/go synced 2024-11-24 08:30:15 -07:00

test/turing: refactor

R=gri
CC=golang-dev
https://golang.org/cl/2116043
This commit is contained in:
Rob Pike 2010-09-04 10:40:00 +10:00
parent 4f61fc96b2
commit e430ee6cba

View File

@ -8,48 +8,45 @@ package main
// brainfuck // 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() { func main() {
var a [30000]byte;
prog := "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.!";
p := 0;
pc := 0;
for { for {
switch prog[pc] { switch prog[pc] {
case '>': case '>':
p++; p++
case '<': case '<':
p--; p--
case '+': case '+':
a[p]++; a[p]++
case '-': case '-':
a[p]--; a[p]--
case '.': case '.':
print(string(a[p])); print(string(a[p]))
case '[': case '[':
if a[p] == 0 { if a[p] == 0 {
for nest := 1; nest > 0; pc++ { scan(1)
switch prog[pc+1] {
case ']':
nest--;
case '[':
nest++;
}
}
} }
case ']': case ']':
if a[p] != 0 { if a[p] != 0 {
for nest := -1; nest < 0; pc-- { scan(-1)
switch prog[pc-1] {
case ']':
nest--;
case '[':
nest++;
}
}
} }
default: default:
return; return
} }
pc++; pc++
} }
} }