Skip to content

drellem2/BrainFawkes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BrainFawkes

Multitarget Optimizing Brainfuck Compiler

This compiler is implemented in Java, and currently targets Java. The code-emitting backend is pluggable, and can be extended by implementing com.drellem.bf.emit.Emitter to add support for different language targets.

Status
It can currently compile the standard "Hello world!" program.

How to Use
Command-line operation has not been developed yet, but you can run some of the files under com.drellem.bf.test for an example.

How can I help?
Write a class implementing com.drellem.bf.emit.Emitter to output code in your favorite language. It's very easy, and you can look at JavaEmitter as an example.

Stages

Hello world: +++++ +++++ initialize counter (cell #0) to 10[ use loop to set the next four cells to 70/100/30/10 > +++++ ++ add 7 to cell #1> +++++ +++++ add 10 to cell #2 > +++ > + add 1 to cell #4<<<< - decrement counter (cell #0)] > ++ . print 'H'> + . print 'e'+++++ ++ . print 'l'. print 'l'+++ . print 'o'> ++ . print ' '<< +++++ +++++ +++++ . print 'W'> . print 'o'+++ . print 'r'----- - . print 'l'----- --- . print 'd'> + . print '!'> . print ' '

  1. Compression
  2. The Brainfuck code is compressed so that ++---<<>>>>>> will become -2>>. Hello world: 10+[>7+>10+>3+>+4<-]>2+.>+.7+..3+.>2+.2<15+.>.3+.6-.8-.>+.>.
  3. Lexing
  4. Tokens are formed from this compressed code. Hello world: (PLUS,10)(LOOP,[)(INC,)(PLUS,7)(INC,)(PLUS,10)(INC,)(PLUS,2)(INC,)(PLUS,)(DEC,4)(MINUS,)(END,])(INC,)(PLUS,2)(PUT,.)(INC,)(PLUS,)(PUT,.)(PLUS,7)(PUT,.)(PUT,.)(PLUS,3)(PUT,.)(MINUS,6)(PUT,.)(MINUS,8)(PUT,.)(INC,)(PLUS,)(PUT,.)(INC,)(PUT,.) .
  5. Optimizing
  6. The code is parsed into a syntax tree and various passes are performed. Hello world: (PLUS)(LOOPPLUS PLUS PLUS PLUS MINUS )(PLUS)(PUT)(PLUS)(PUT)(PLUS)(PUT)(PUT)(PLUS)(PUT)(MINUS)(PUT)(MINUS)(PUT)(PLUS)(PUT)(PUT)(INC).
  7. Code generation
  8. All constants are propogated and code is generated. Hello world: public class Main { private static byte[] tape = new byte[30000]; private static int index = 0; public static void main(String[] args){ System.out.print("H"); System.out.print("e"); System.out.print("l"); System.out.print("l"); System.out.print("o"); System.out.print(" "); System.out.print("W"); System.out.print("o"); System.out.print("r"); System.out.print("l"); System.out.print("d"); System.out.print("!"); System.out.println(); } }

About

Multitarget Brainfuck Compiler

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages