MoreMathRPN/Brainfuck interpreter
Jump to navigation
Jump to search
← go back
Brainfuck can also be simulated; if you had any doubts of Turing completeness, this is even better
inputS "Enter brainfuck" hold iLen del 0 m $iLen 1 flip "remove invalid characters repeat $iLen >>> 0 1 - -> 1 ind ]1 0 del 2 >> 0 "+ 43 - >> 1 ", 44 - * >> 1 "- 45 - * >> 1 ". 46 - * >> 1 "< 60 - * >> 1 "> 62 - * >> 1 "[ 91 - * >> 1 "] 93 - * 1 -> 1 0 compare >> 0 * - 4 * jmp ]0 del 0 del 0 jmp 3 del 0 <- 1 next del 0 # hold iLen del 0 m $iLen 1 hold instructions del 0 "brackets -1 m 1 1 repeat $iLen >>> 0 1 - place instructions ind ]1 0 del 1 >> 0 91 - 0 compare >> 0 * 6 * jmp ]0 "[ del 0 del 0 m 1 1 aug jmp 33 del 0 93 - 0 compare >> 0 * 21 * jmp ]0 "] del 0 >>> 0 -> 2 rip ind 0 0 >> 0 del 2 -> 3 -> 1 - 2 + -> ]0 -> 4 <- ]2 -> 2 <- 3 del 0 del 0 jmp 5 del 0 del 0 0 <- 1 next del 0 m $iLen 1 hold jumps del 0 0 hold pointer -1 hold pc del 0 1 hold size del 0 "begin execution place pc 1 + hold pc place iLen - 1 + step 207 * jmp ]0 del 0 place size place pointer - place instructions ind $pc 0 del 1 >> 0 43 - 0 compare >> 0 * 11 * jmp ]0 "+ del 0 del 0 -> ]0 1 + 256 % <- ]1 del 0 jmp -38 del 0 >> 0 44 - 0 compare >> 0 * 27 * jmp ]0 ", del 0 del 0 del ]0 inputS "Input a character" hold len step 1 <- 1 - 9 * jmp ]0 del 0 m $len 1 flip ind 0 0 256 % del 1 jmp 3 del 0 0 <- ]1 del 0 lose len jmp -75 del 0 >> 0 45 - 0 compare >> 0 * 11 * jmp ]0 "- del 0 del 0 -> ]0 1 - 256 % <- ]1 del 0 jmp -96 del 0 >> 0 46 - 0 compare >> 0 * 8 * jmp ]0 ". del 0 del 0 -> ]0 outputC ]0 <- ]1 del 0 jmp -114 del 0 >> 0 60 - 0 compare >> 0 * 23 * jmp ]0 "< del 0 del 0 del 0 place pointer step 8 * jmp ]0 <- $size place size 1 + hold size del 0 jmp 7 del 0 place pointer 1 - hold pointer del 0 jmp -147 del 0 >> 0 62 - 0 compare >> 0 * 20 * jmp ]0 "> del 0 del 0 del 0 place size place pointer 1 + hold pointer - step 5 * jmp ]0 place size 1 + hold size del 0 jmp -177 del 0 91 - 0 compare >> 0 * 14 * jmp ]0 "[ del 0 >> ]0 step 5 * jmp ]0 place jumps ind $pc 0 del 1 hold pc del 0 del 0 jmp -200 "] del 0 >> ]0 step 1 <- 1 - 6 * jmp ]0 place jumps ind $pc 0 del 1 hold pc del 0 del 0 del 0 jmp -217 del 0
Using similar methods, one could write a self interpreter, but please don't waste your precious time on that.