brainit

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2021 License: MIT Imports: 4 Imported by: 0

README

brainit

Extensible a rune interpreter library.

Feed interpreter with a io.Reader and run it.
Give own functions and add.

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")
    if err != nil {
        log.Fatalln("Upps:", err)
    }

    // 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)

Testing

Test and get pprof results explanations.

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

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")
	ErrSkip = errors.New("brainit: not possible case")
)

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 or exist previous element.

type Exec

type Exec func(*Interpreter) error

Exec 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) 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)

Interpret is a runner.

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.

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