Using the mandlebrot visualization (Implemented by Erik ) as the bench mark:
The interpreter with no optimization: 71.31s
The compiler with instructions to reduce duplicate commands and searching for
corrosponding brackets: 13.17s
This can be run locally by cloning this repo and running the following command
to build, run and time the execution in either the interpreter and or the compiler
directory :
go build -o machine && time ./machine ../sample_bf/mandelbrot.b
An implementation of the Brain Fuck programming language. See wikipedia here.
Below is a check list I used while building the basic interpreter - in the future things may evolve beyond its current state.
To build and run:
- Navigate inside the interpreter dir
- Run command to build
go build -o machine
- Run command to see output in terminal
./machine ../sample_bf/hello_world.b
-
Create Machine:
- Handle input
- Handle output
- Accepts code as string
- Has instruction pointer for array (len 30000)
- Has context pointer (slot in memory to be updated)
-
Create Instantiate func:
- Pass in code, reader, writer, and set buf
-
Create Run func:
- Moves instruction pointer
- Switch statement to asses instruction at pointer
- Special attention/helper funcs for:
- ReadByte
- WriteByte
- Loops(not implemented as a seperate method at this time)
-
Create Main func that:
- Reads Brain Fuck code in from file
- Calls new instance of Machine
-
Handle bug =>
go build -o machine && ./machine ./hello_world.b
causes terminal to hang.
Solution:
The switch statement case associated with the ']' char has a mistake. This has been resolved.
-
Tests
- Define tests for each basic command
- Implement tests as defined
-
Create lexer with:
- Struct for instruction w/ data for optimizing repeated operations
- Similar struct w/ data for more efficient loops
Below is the working checklist to build a functioning brainfuck compiler.
The end goal is to benchmark the interpreter and compiler (perhaps even an
improved compiler) against the classic mandelbrot.b
(a mandelbrot fractal
viewer) written by Erik Bosman.
-
main.go
- Accept arg (brainfuck file location)
- Basic error handling
- Instantiate new instance of compiler
- Run/return output from compiler
- Instantiate new instance of bf machine
- Run/execute
-
machine.go
- Define Machine as struct
- Define
InstantiateMachine
func - Extend
Machine
w/Run
method- Iterate through new instruction list
- Switch/case w/ appropriate case for each instruction
- Read/Write helper functions
-
compiler.go
- Define
Compiler
struct- code, code length, position, instructions array
- Define
InstantiateCompiler
func - Extend
Compiler
w/Run
method- Iterate through code
- Switch/case
- Append instruction w/ arg for
[
/]
- Call helper to handle repetitive commands
- Append instruction w/ arg for
- Define
-
instructions.go
- Define
InstructionType
asbyte
- Define
Instruction
struct w/InstructionType
&Arg
- Map instruction names to
InstructionType
(all 8 chars)
- Define