A minuscule interpreter for the Brainfuck programming language, written in Ruby.
- Matches and caches brackets before executing code, speeding up the interpretation.
- Has an array of unsigned single-byte cells that can grow "infinitely" to the right.
- Wraps cell values if they exceed the minimum (0) or maximum (255).
- Ignores a
<
command if the array pointer is on the left-most cell. - Passes Daniel Cristofani's tests and follows his Epistle to the Implementors.
- Weighs only 328 bytes!
Apart from the eight standard Brainfuck commands, the first exclamation
mark (!
) is a delimiter used to separate the interpreted code from its
standard input (stdin).
Here's a usage example with Daniel Cristofani's numwarp.b:
$ ruby tinybf.b
>>>>+>+++>+++>>>>>+++[>,+>++++[>++++<-]>[<<[-[->]]>[<]>-]<<[>+>+>>+>+[<<<<]<+>>[
+<]<[>]>+[[>>>]>>+[<<<<]>-]+<+>>>-[<<+[>]>>+<<<+<+<--------[<<-<<+[>]>+<<-<<-[<<
<+<-[>>]<-<-<<<-<----[<<<->>>>+<-[<<<+[>]>+<<+<-<-[<<+<-<+[>>]<+<<<<+<-[<<-[>]>>
-<<<-<-<-[<<<+<-[>>]<+<<<+<+<-[<<<<+[>]<-<<-[<<+[>]>>-<<<<-<-[>>>>>+<-<<<+<-[>>+
<<-[<<-<-[>]>+<<-<-<-[<<+<+[>]<+<+<-[>>-<-<-[<<-[>]<+<++++[<-------->-]++<[<<+[>
]>>-<-<<<<-[<<-<<->>>>-[<<<<+[>]>+<<<<-[<<+<<-[>>]<+<<<<<-[>>>>-<<<-<-]]]]]]]]]]
]]]]]]]]]]]]>[>[[[<<<<]>+>>[>>>>>]<-]<]>>>+>>>>>>>+>]<]<[-]<<<<<<<++<+++<+++[[>]
>>>>>>++++++++[<<++++>++++++>-]<-<<[-[<+>>.<-]]<<<<[-[-[>+<-]>]>>>>>[.[>]]<<[<+>
-]>>>[<<++[<+>--]>>-]<<[->+<[<++>-]]<<<[<+>-]<<<<]>>+>>>--[<+>---]<.>>[[-]<<]<]
!1337
^D
/\
\
/\
/\
/\ /
/\
\ /
\
In the example above, 1337
is the input to the interpreted program and ^D
stands for pressing Ctrl-D.
- Very slow compared to a C implementation
- No error checking (e.g. unmatched brackets)
- Both the interpreted source code and its input are passed through the same stream (standard input). This makes I/O non-interactive.
- Unsurprisingly, the code is obfuscated and a bit hard to read. That said,
here's a list of what each variable is:
c
is aString
holding the code andb
its pointeri
is aString
holding the input andh
its pointera
is anArray
holding the memory andz
its pointerj
is aHash
used to cache the position of brackets ands
anArray
used (as a stack) to build it
Angelos Orfanakos, https://agorf.gr/