A Brainfuck interpreter written in Go
To install bf
package, you need to install Go (version 1.13+
is required) and setup your Go project first.
$ mkdir example
$ cd example
$ go mod init example.com
- Then you can use the below
Go
command to install the latest version ofbf
package.
$ go get -u github.com/clivern/bf
Or the following for a specific version vx.x.x
go get -u github.com/clivern/bf@vx.x.x
- Import the package in your code.
import "github.com/clivern/bf"
Add the following code in example.go
file
$ cat example.go
package main
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/clivern/bf"
)
func main() {
buf := new(bytes.Buffer)
interpreter := bf.NewInterpreter(
strings.NewReader("-[------->+<]>-.-[->+++++<]>++.+++++++..+++.[--->+<]>-----.---[->+++<]>.-[--->+<]>---.+++.------.--------."),
os.Stdin,
buf,
)
err := interpreter.IsValid()
if err != nil {
panic(fmt.Sprintf("Invalid code: %s", err.Error()))
}
err = interpreter.Execute()
if err != nil {
panic(fmt.Sprintf("Invalid code: %s", err.Error()))
}
fmt.Println(buf.String()) // Hello World
}
Run example.go
$ go run example.go
It will return Hello World