Skip to content
/ bf Public

A Brainfuck interpreter written in Go to Learn Interpreters and Compilers.

License

Notifications You must be signed in to change notification settings

Clivern/bf

Repository files navigation

Brainfuck Interpreter

A Brainfuck interpreter written in Go

Build Status Docs Go Report Card

Installation

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 of bf 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"

Quick start

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

About

A Brainfuck interpreter written in Go to Learn Interpreters and Compilers.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published