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
|
2012-01-18 14:20:55 -07:00
|
|
|
|
2010-09-03 18:40:00 -06:00
|
|
|
const prog = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.!"
|
|
|
|
|
|
|
|
func scan(dir int) {
|
|
|
|
for nest := dir; dir*nest > 0; pc += dir {
|
|
|
|
switch prog[pc+dir] {
|
2012-01-18 14:20:55 -07:00
|
|
|
case ']':
|
|
|
|
nest--
|
|
|
|
case '[':
|
|
|
|
nest++
|
2010-09-03 18:40:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-08 18:12:15 -06:00
|
|
|
func main() {
|
2012-01-18 15:31:31 -07:00
|
|
|
r := ""
|
2008-05-15 16:20:59 -06:00
|
|
|
for {
|
|
|
|
switch prog[pc] {
|
2012-01-18 14:20:55 -07:00
|
|
|
case '>':
|
|
|
|
p++
|
|
|
|
case '<':
|
|
|
|
p--
|
|
|
|
case '+':
|
|
|
|
a[p]++
|
|
|
|
case '-':
|
|
|
|
a[p]--
|
|
|
|
case '.':
|
2012-01-18 15:31:31 -07:00
|
|
|
r += string(a[p])
|
2012-01-18 14:20:55 -07:00
|
|
|
case '[':
|
|
|
|
if a[p] == 0 {
|
|
|
|
scan(1)
|
|
|
|
}
|
|
|
|
case ']':
|
|
|
|
if a[p] != 0 {
|
|
|
|
scan(-1)
|
|
|
|
}
|
|
|
|
default:
|
2012-01-18 15:31:31 -07:00
|
|
|
if r != "Hello World!\n" {
|
|
|
|
panic(r)
|
|
|
|
}
|
2012-01-18 14:20:55 -07: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
|
|
|
}
|