pandasoli / brainfuck

🧠 | Smallest Brainfuck interpreter and compiler

Home Page:https://brainfuck-org.vercel.app

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Interpreter 🐍

Inspired by: Brainfuck/brainfuck-visualizer-master

There is the smallest interpreter interpreters/smallest.js.

And the interpreters/complete.js with more features for web use cases.

  • Yields the result of each char before interpreting it
  • Receives a object with the last result to start from

const bf = bfinterpreter('++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.!', { pause: true })

while (!(a = bf.next()).done)
	console.log(a.value)

Depreter πŸ”€

depreter.js

It translates text to Brainfuck.


Depreter v2 ⛱️

depreterv2.js
Same configuration as #interpreter 🐍

A really smart depreter that writes code with the same techniques I myself use when I manually writing Brainf#ck.

It chooses the smallest result from many techniques to get to the number on the memory with less code as possible.

It uses:

  • Search for the cell with the closest number to the number I want, so we can have less instructions than we would need starting from zero.
  • ><[-]+++++++ and ><[+]-------
    This technique is used when the smallest way to get to the number is starting from zero.
  • Try creating a new cell to see if the code gets smaller.
  • Loops
    • In loops we lock the chosen cell (1st technique) so that the counter code doesn't choose it.
    • We choose the smallest factors of the number

Note

TODOS:

  • Support loops inside loops
  • Support loop counter to contain a loop

AS Transpiler 🦬

transpilers/gas

Its output can be redirected to a .s file, compiled with GNU Assembly, and then ran sending the output to xxd to show the result in hex.

node main.js > prog.s
./comp.sh prog && ./prog | xxd

❗ Different from #interpreter this works with ASCII (0-127), not UTF-16 (fromCharCode). Each memory cell is 1 byte!


JIT Compiler πŸ˜‚

compilers/jit
Inspired by: tsoding/bfjit

It compiles Brainfuck to a valid ELF64 format entirely in JavaScript, and runs it with a C shared library.

Just like #as-transpiler, it works with ASCII, so be careful.


Transpiler to GNU Cobol πŸ¦–

Soon.

About

🧠 | Smallest Brainfuck interpreter and compiler

https://brainfuck-org.vercel.app

License:MIT License


Languages

Language:JavaScript 93.9%Language:Assembly 3.3%Language:C 2.4%Language:Shell 0.5%