bf

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2021 License: MIT Imports: 3 Imported by: 0

README

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

Documentation

Index

Constants

View Source
const (
	// Plus instruction constant
	Plus byte = '+'
	// Minus instruction constant
	Minus byte = '-'
	// Right instruction constant
	Right byte = '>'
	// Left instruction constant
	Left byte = '<'
	// PutChar instruction constant
	PutChar byte = '.'
	// ReadChar instruction constant
	ReadChar byte = ','
	// JumpIfZero instruction constant
	JumpIfZero byte = '['
	// JumpIfNotZero instruction constant
	JumpIfNotZero byte = ']'
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Interpreter

type Interpreter struct {
	// contains filtered or unexported fields
}

Interpreter brainfuck interpreter type

func NewInterpreter

func NewInterpreter(code io.Reader, in io.Reader, out io.Writer) *Interpreter

NewInterpreter creates an interpreter instance

func (*Interpreter) Execute

func (i *Interpreter) Execute() error

Execute executes the brainfuck code

func (*Interpreter) IsValid

func (i *Interpreter) IsValid() error

IsValid validates brainfuck code it ensures that code doesn't start with `]` and has a matching `[` and `]`

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL