2008-05-08 18:12:15 -06:00
|
|
|
// $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
|
|
|
|
|
2010-09-03 18:40:00 -06:00
|
|
|
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++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-08 18:12:15 -06:00
|
|
|
func main() {
|
2008-05-15 16:20:59 -06:00
|
|
|
for {
|
|
|
|
switch prog[pc] {
|
|
|
|
case '>':
|
2010-09-03 18:40:00 -06:00
|
|
|
p++
|
2008-05-15 16:20:59 -06:00
|
|
|
case '<':
|
2010-09-03 18:40:00 -06:00
|
|
|
p--
|
2008-05-15 16:20:59 -06:00
|
|
|
case '+':
|
2010-09-03 18:40:00 -06:00
|
|
|
a[p]++
|
2008-05-15 16:20:59 -06:00
|
|
|
case '-':
|
2010-09-03 18:40:00 -06:00
|
|
|
a[p]--
|
2008-05-15 16:20:59 -06:00
|
|
|
case '.':
|
2010-09-03 18:40:00 -06:00
|
|
|
print(string(a[p]))
|
2008-05-15 16:20:59 -06:00
|
|
|
case '[':
|
|
|
|
if a[p] == 0 {
|
2010-09-03 18:40:00 -06:00
|
|
|
scan(1)
|
2008-05-15 16:20:59 -06:00
|
|
|
}
|
|
|
|
case ']':
|
|
|
|
if a[p] != 0 {
|
2010-09-03 18:40:00 -06:00
|
|
|
scan(-1)
|
2008-05-15 16:20:59 -06:00
|
|
|
}
|
|
|
|
default:
|
2010-09-03 18:40:00 -06:00
|
|
|
return
|
2008-05-15 16:20:59 -06:00
|
|
|
}
|
2010-09-03 18:40:00 -06:00
|
|
|
pc++
|
2008-05-15 16:20:59 -06:00
|
|
|
}
|
2008-05-08 18:12:15 -06:00
|
|
|
}
|