A brainfuck interpreter (a jit compiler to bash) written in bash
- EOF = 0
- 2^64-1 cells to the right of the start position (can be easily changed to start in the middle but it's slower)
- 8-bit unsigned values in cells
- times itself if the environment variable TIMES is found
- prints the compiled code if the environment variable PRINT is found
- pretty prints the brainfuck code if the environment variable PRETTY is found
- removes as much dead code as possible if the environment variable DEADCODE is found
- Pointless code is removed
- Multiple increments/decrements of value and pointer are merged together
- Clear loops (
[-]
and[+]
), possibly followed by increments/decrements, are reduced - Shifted increments/decrements (
>>>>++<<<<
,<<----->>
...) are reduced to a single instruction - Multiplication loops (
[->+>++>+++<<<]
) are reduced to a single instruction (unless they clear a cell, in which case they're converted into an if + that instruction)
Optimizing scan loops ([>]
and [<]
) doesn't make sense in bash
- Nested loops
- Precompute tape
- moar
bf filename-of-your-bf-program
bf -c 'your-bf-code-here'
It's extremely slow compared to anything that compiles to C or to assembly.
That said, it's probably faster than many interpreters written in "faster" languages.
- elliott
- gniourf