brainit

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2021 License: MIT Imports: 3 Imported by: 0

README

🄱🅁🄰🄸🄽🄸🅃

Go Report Card License

Extensible a rune interpreter library.

Give command sets, feed interpreter with a io.Reader and run it.

Command Sets
- Brainfuck

Usage

import (
    "github.com/rytsh/brainit"
    "github.com/rytsh/brainit/commandset"
)

    // ...
    resp, err := http.Get("https://raw.githubusercontent.com/erikdubbelboer/brainfuck-jit/master/mandelbrot.bf")
    // ...

    // get new interpreter
    myInterpreter := brainit.NewInterpreter()
    // add a command set
    myInterpreter.AddCommandSet(commandset.Brainfuck)

    // give an io.Reader
    myInterpreter.Interpret(resp.Body)

    // ....

License

The MIT License (MIT)

Tests

Test and get pprof results.

go test -cover -coverprofile cover.out -benchmem -cpuprofile cpu.out -memprofile mem.out -outputdir ./_out ./commandset/

go tool pprof commandset.test _out/cpu.out
go tool pprof commandset.test _out/mem.out

# Coverage
# Single test
# go test -cover -coverprofile cover.out -outputdir ./_out/ ./...
# Combine tests
go test -cover -coverprofile cover.out -coverpkg=github.com/rytsh/brainit,github.com/rytsh/brainit/commandset -outputdir ./_out/ ./...
go tool cover -html=./_out/cover.out

Documentation

Overview

Package brainit a customizable rune interpreter.

Source code and other details for the project are available at GitHub:

https://github.com/rytsh/brainit

Index

Constants

This section is empty.

Variables

View Source
var ErrExit = errors.New("brainit: exit loop")

ErrExit should throw for exiting loop.

Functions

This section is empty.

Types

type Cset

type Cset struct {
	LoopCommands map[LoopKey]Exec
	Commands     map[rune]Exec
}

Cset for commands sets.

type Element

type Element struct {
	Value interface{}
	// contains filtered or unexported fields
}

Element is an struct of duble-linked list.

func (*Element) NewElement

func (e *Element) NewElement() *Element

NewElement generate new element and set size of memory.

func (*Element) Next

func (e *Element) Next(v interface{}) *Element

Next generate new element or exist next element.

func (*Element) Prev

func (e *Element) Prev(v interface{}) *Element

Prev generate new element with argument or return exist previous element.

type Exec

type Exec func(*Interpreter) error

Exec is function signature, usable for add new ones.

type Interpreter

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

Interpreter is main struct hold all memory, code, executors and stacks.

func NewInterpreter

func NewInterpreter() *Interpreter

NewInterpreter is helps to initialize new interpreter and return it.

func (*Interpreter) AddCommand

func (i *Interpreter) AddCommand(key rune, fn Exec)

AddCommand for record new function to specific key.

func (*Interpreter) AddCommandSet

func (i *Interpreter) AddCommandSet(c Cset)

AddCommandSet for record new command sets.

func (*Interpreter) AddLoopCommand

func (i *Interpreter) AddLoopCommand(begin rune, end rune, fn Exec)

AddLoopCommand for record new function to specific loop keys.

func (*Interpreter) ClearMemory added in v0.1.1

func (i *Interpreter) ClearMemory()

ClearMemory erase all mem and set zero of current pointer.

func (*Interpreter) GetValue

func (i *Interpreter) GetValue() rune

GetValue is return current value in a current memory.

func (*Interpreter) Init

func (i *Interpreter) Init() *Interpreter

Init is initialize a Interpreter.

func (*Interpreter) Interpret

func (i *Interpreter) Interpret(r io.Reader) error

Interpret is a runner. if reader get error other than io.EOF, return it.

func (*Interpreter) Next

func (i *Interpreter) Next()

Next is move to next memory area.

func (*Interpreter) Prev

func (i *Interpreter) Prev()

Prev is move to previous memory area.

func (*Interpreter) SetValue

func (i *Interpreter) SetValue(v rune)

SetValue is setting a value in a current memory.

type LoopKey

type LoopKey struct {
	Begin rune
	End   rune
}

LoopKey to record new new loop runes.

type Memory

type Memory struct {
	Len     uint
	Front   *Element
	Back    *Element
	Current *Element
}

Memory is main struct of linked list.

func NewMemory

func NewMemory(v interface{}) *Memory

NewMemory is a helper function to get a functional memory.

func (*Memory) Init

func (m *Memory) Init(v interface{}) *Memory

Init help easily to get memory struct.

func (*Memory) RemoveUntil

func (m *Memory) RemoveUntil(e *Element)

RemoveUntil remove code from front of memory an element. if element not inside of memory, nothing change.

Directories

Path Synopsis
Package commandset is include predefined interpreter commands.
Package commandset is include predefined interpreter commands.

Jump to

Keyboard shortcuts

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