rexim / ebf

Brainfuck language transpiler to Emacs Lisp
20 stars 3 forks source link

Procedural brainfuck #6

Open ForNeVeR opened 9 years ago

ForNeVeR commented 9 years ago

There is a brainfuck extension known as "procedural brainfuck". It adds two additional commands/concepts:

  1. ( and ) will define a piece of code as a procedure coded with the number in the current memory cell.
  2. : will call a procedure defined with a number in the current memory cell.

It helps to create more functional and interesting programs solving real-world problems with ease.

For example, this simple code defines a procedure with number 1 and calls it:

[+]+  ; now current = 1
(+++) ; define a procedure that will increment cell by 3
[+]+  ; clean up the cell and set it to 1 again
:     ; call the procedure
      ; now current = 4
rexim commented 9 years ago

Good proposal! Thanks!

I need to think how this will actually look after the transpilation...

rexim commented 8 years ago

@ForNeVeR [+]+ is rather strange way to clean a cell and set it to 1. Are you sure you didn't mean [-]+?

ForNeVeR commented 8 years ago

They are completely equivalent in all of the modern BF dialects, so yes, I am sure. Optimizing BF compiler (yes there is a such thing) should simply replace [+] and [-] with setting the cell value to zero.

Also, both of these instructions will execute in equal time (and very slowly) if the cell initially already was set to zero. Maybe even -[+] could be used as a common case optimization.

rexim commented 8 years ago

@ForNeVeR ok, thanks for the explanation