1
0
mirror of https://github.com/golang/go synced 2024-11-24 03:20:02 -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
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++
}
}