golyb

package module
v0.0.0-...-ffa7d33 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2023 License: ISC Imports: 5 Imported by: 0

README

GoDoc Build

Go love your brain

Yet another optimizing BrainF*ck interpreter in Go

Installation

go get github.com/dim13/golyb/cmd/golyb

Usage

Usage of golyb:
  -dump
    	Dump AST and terminate
  -file string
    	Source file (required)
  -in string
    	Input file
  -noop
    	Disable optimization
  -out string
    	Output file or /dev/null
  -profile string
    	Write CPU profile to file
  -show
    	Dump tape cells
  -tape value
    	Tape type: static or dynamic (default static)

Tape storage type

  • static: 32k byte cells including 1k margin on the lower end (used by default)
  • dynamic: int cells allocated in 1k chunks as required on access

Code optimization

  • Contraction
  • Clear loops
  • Copy loops
  • Multiplication loops
  • Scan loops (kind of)
  • Operation offsets
  • Reduce NOPs

Reference: http://calmerthanyouare.org/2015/01/07/optimizing-brainfuck.html

Some rough results

Program w/o optimization with optimization speed gain
madelbrot.b 52.3 sec 11.8 sec 4.4x
long.b 48.5 sec 5.4 sec 9.0x
hanoi.b 47.0 sec 1.0 sec 47.0x
bench.b 6.6 sec 1.1 sec 6.0x

Executed on 1.3 GHz Intel Core i5

CPU profiles

mandelbrot.b
optimized

mandelbrot profile

not optimized

mandelbrot profile

long.b
optimized

long profile

not optimized

long profile

hanoi.b
optimized

hanoi profile

not optimized

hanoi profile

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Command

type Command struct {
	Op     Opcode
	Arg    int
	Dst    int
	Off    int
	Branch Program
}

func (Command) String

func (c Command) String() string

type Opcode

type Opcode int
const (
	Add Opcode = iota
	Move
	Print
	Scan
	Loop
	Clear
	Mult
	Search
)

func (Opcode) String

func (i Opcode) String() string

type Program

type Program []Command

func ParseFile

func ParseFile(fname string) (Program, error)

func ParseString

func ParseString(prog string) Program

func (Program) Execute

func (p Program) Execute(t Tape)

func (Program) String

func (p Program) String() string

type Tape

type Tape interface {
	Add(arg int, off int)
	Move(off int)
	Print(off int)
	Scan(off int)
	IsZero() bool
	Clear(off int)
	Mult(arg int, off int, dst int)
	Search(off int)
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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