rexim / ebf

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

Brainfuck language transpiler to Emacs Lisp

** Usage

The brainfuck language transpiler to Emacs Lisp consists of ~ebf~ macro which expands to the actual Emacs Lisp code. Here is the signature of this macro:

+BEGIN_SRC emacs-lisp

(ebf INPUT-CALLBACK OUTPUT-CALLBACK &rest INSTRUCTIONS)

+END_SRC

~INPUT-CALLBACK~ is called on comma instruction and should have zero arguments and return a number.

~OUTPUT-CALLBACK~ is called on dot instruction and should have one argument of an integer type.

~INSTRUCTIONS~ is a list of symbols and vectors of symbols. Vectors are accepted so we don't need to escape square brackets of our brainfuck program. Symbols' names should be sequences of valid brainfuck instructions excluding square brackets.

Evaluation of the macro expansion causes the brainfuck program execution.

So the code

+BEGIN_SRC emacs-lisp

(ebf input output \,+++[->+<].)

+END_SRC

will be expanded to

+BEGIN_SRC emacs-lisp

(let ((MEMORY68087 (make-vector 100 0)) (POINTER68088 0) (INPUT68085 input) (OUTPUT68086 output)) (aset MEMORY68087 POINTER68088 (funcall INPUT68085)) (cl-incf (aref MEMORY68087 POINTER68088) 3) (while (not (zerop (aref MEMORY68087 POINTER68088))) (cl-decf (aref MEMORY68087 POINTER68088) 1) (cl-incf POINTER68088 1) (while (<= (length MEMORY68087) POINTER68088) (let ((memory-length (length MEMORY68087))) (setq MEMORY68087 (vconcat MEMORY68087 (make-vector (max 1 (/ memory-length 2)) 0))))) (cl-incf (aref MEMORY68087 POINTER68088) 1) (cl-decf POINTER68088 1)) (funcall OUTPUT68086 (aref MEMORY68087 POINTER68088)))

+END_SRC

We collapse several instructions in a row and automatically expand the memory to the right. We plan to add more optimization in the future.

Here is the classical Hello World example with some output:

+BEGIN_SRC emacs-lisp

(require 'ebf)

(let ((result nil)) (ebf nil #'(lambda (x) (push x result)) ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++ .>+.+++++++..+++.>++.<<+++++++++++++++.>.+++. ------.--------.>+.>.) (apply #'string (reverse result)));<- put cursor here and press C-x C-e

+END_SRC

** Compiling ebf

~ebf~ macro produces the code that doesn't depend on ~ebf~ module itself. That means we can byte-compile our brainfuck programs so they will not require ~ebf~ at runtime.

Check [[http://www.gnu.org/software/emacs/manual/html_node/elisp/Compiling-Macros.html][Macros and Byte Compilation]] section of the official Emacs Lisp manual on how to do that.

Basically we will need to wrap our ~(require 'ebf)~ with ~eval-when-compile~ like

+BEGIN_SRC emacs-lisp

(eval-when-compile (require 'ebf))

+END_SRC

** License

Copyright (C) 2015 Alexey Kutepov a.k.a rexim

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.