got get github.com/Amirhossein2000/brainfuck
package main
import (
"fmt"
"github.com/Amirhossein2000/brainfuck"
"io"
"os"
)
func main() {
interpreter := brainfuck.NewInterpreter(128, os.Stdin, os.Stdout)
interpreter.AddOperation('*', func(codePointer, memPointer *int, mem, code []byte, reader io.Reader, writer io.Writer) {
mem[*memPointer] *= 2
})
err := interpreter.Run()
if err != nil {
fmt.Println("interpreter err:", err.Error())
}
}
// if you want to have and command-line execute for more than once you can put the for
for {
err := interpreter.Run()
if err != nil {
fmt.Println("interpreter err:", err.Error())
}
}
~ go run main.go
> ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
> Hello World!
In the classic distribution, the cells are of 8-bit size (cells are bytes), and this is still the most common size, therefore I used the 8-bit in this implementation. Also, it's much easier to use the byte array.
It's configurable in this implementation.
// in this example the memory size is 128
interpreter := brainfuck.NewInterpreter(128, os.Stdin, os.Stdout)
\n is the most common one, therefore I used it in this implementation. have any improvement in your mind please feel free to open an issue.