Please read this article
-
First of all please see the test files.
-
typical usage
// create new io.Reader from inputs code := strings.NewReader("++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.") //Hello World!\n // Standards interface to io input := new(bytes.Buffer) output := new(bytes.Buffer) // initialize the machine bfm := interpreter.NewInterpreter(input, output, code) // Store the result in output interface err := bfm.Run() if err != nil { //handle err } // print the result fmt.Println (output.String())
-
Add custom operators
// create new io.Reader from inputs code := strings.NewReader("+++***") // (1+1+1)*(2*2*2) = 24 // Standards interface to io input := new(bytes.Buffer) output := new(bytes.Buffer) // initialize the machine bfm := interpreter.NewInterpreter(input, output, code) // Add your own operator // C is complementary information about instructionlike position or counts of occurrence // Incase of opening loop, C is the index of the closing loop and vice versa err := bfm.AddOperator('*', func(c int, memory *interpreter.Memory) { memory.Cell[memory.Cursor] = (memory.Cell[memory.Cursor] * int(math.Pow(2, float64(c)))) % 255 }) // Store the result in output interface err := bfm.Run() if err != nil { //handle err } // print the result of arbitrary cell in the memory fmt.Println (bfm.GetValueInMemory(0))
In the root of the project run go test ./...