├── README.md ├── cli.js ├── examples ├── fib.b ├── hello.b ├── jabh.b ├── rot13.b ├── self.b └── squares.b ├── index.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # bf2c 2 | 3 | > Compile Brainfuck to C. 4 | 5 | ## Install 6 | 7 | ```bash 8 | $ npm i bf2c 9 | ``` 10 | 11 | ## Examples 12 | 13 | ##### CLI 14 | 15 | ```bash 16 | $ bf2c examples/hello.b > hello.c && gcc hello.c && ./a.out 17 | Hello World! 18 | ``` 19 | 20 | ##### Module 21 | 22 | ```javascript 23 | var bf2c = require("bf2c"); 24 | var code = bf2c("+++++++++++++."); 25 | ``` 26 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | var fs = require("fs"); 3 | var bf2c = require("./"); 4 | 5 | var inputFile = process.argv[2]; 6 | var memory = process.argv[3]; 7 | var code = ""; 8 | 9 | if (inputFile) { 10 | code = bf2c(fs.readFileSync(inputFile, "utf8"), memory); 11 | console.log(code); 12 | } else { 13 | console.log("bf2c "); 14 | } 15 | -------------------------------------------------------------------------------- /examples/fib.b: -------------------------------------------------------------------------------- 1 | 2 | And the no comment neat little block: 3 | +++++++++++ 4 | >+>>>>++++++++++++++++++++++++++++++++++++++++++++ 5 | >++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+> 6 | +<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[- 7 | <-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<< 8 | -]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]] 9 | >[<<+>>[-]]<<<<<<<]>>>>>[+++++++++++++++++++++++++ 10 | +++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++ 11 | ++++++++++++++++++++++++++++++++++++++++++++.[-]<< 12 | <<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<< 13 | [-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]! 14 | -------------------------------------------------------------------------------- /examples/hello.b: -------------------------------------------------------------------------------- 1 | ++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.! 2 | -------------------------------------------------------------------------------- /examples/jabh.b: -------------------------------------------------------------------------------- 1 | +++[>+++++<-]>[>+>+++>+>++>+++++>++<[++<]>---]>->-.[>++>+<<--]>--.--.+.> 2 | >>++.<<.<------.+.+++++.>>-.<++++.<--.>>>.<<---.<.-->-.>+.[+++++.---<]>> 3 | [.--->]<<.<+.++.++>+++[.<][http://www.hevanet.com/cristofd/brainfuck/]<.! 4 | -------------------------------------------------------------------------------- /examples/rot13.b: -------------------------------------------------------------------------------- 1 | , 2 | [>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<- 3 | [>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<- 4 | [>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<- 5 | [>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<- 6 | [>++++++++++++++<- 7 | [>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<- 8 | [>>+++++[<----->-]<<- 9 | [>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<- 10 | [>++++++++++++++<- 11 | [>+<-[>+<-[>+<-[>+<-[>+<- 12 | [>++++++++++++++<- 13 | [>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<- 14 | [>>+++++[<----->-]<<- 15 | [>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<- 16 | [>++++++++++++++<- 17 | [>+<-]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] 18 | ]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]>.[-]<,] 19 | 20 | of course any function char f(char) can be made easily on the same principle 21 | 22 | [Daniel B Cristofani (cristofdathevanetdotcom) 23 | http://www.hevanet.com/cristofd/brainfuck/] 24 | -------------------------------------------------------------------------------- /examples/self.b: -------------------------------------------------------------------------------- 1 | >>>+[[-]>>[-]++>+>+++++++[<++++>>++<-]++>>+>+>+++++[>++>++++++<<-]+>>>,<++[[>[ 2 | ->>]<[>>]<<-]<[<]<+>>[>]>[<+>-[[<+>-]>]<[[[-]<]++<-[<+++++++++>[<->-]>>]>>]]<< 3 | ]<]<[[<]>[[>]>>[>>]+[<<]<[<]<+>>-]>[>]+[->>]<<<<[[<<]<[<]+<<[+>+<<-[>-->+<<-[> 4 | +<[>>+<<-]]]>[<+>-]<]++>>-->[>]>>[>>]]<<[>>+<[[<]<]>[[<<]<[<]+[-<+>>-[<<+>++>- 5 | [<->[<<+>>-]]]<[>+<-]>]>[>]>]>[>>]>>]<<[>>+>>+>>]<<[->>>>>>>>]<<[>.>>>>>>>]<<[ 6 | >->>>>>]<<[>,>>>]<<[>+>]<<[+<<]<] 7 | [input a brainfuck program and its input, separated by an exclamation point. 8 | Daniel B Cristofani (cristofdathevanetdotcom) 9 | http://www.hevanet.com/cristofd/brainfuck/] 10 | -------------------------------------------------------------------------------- /examples/squares.b: -------------------------------------------------------------------------------- 1 | ++++[>+++++<-]>[<+++++>-]+<+[ 2 | >[>+>+<<-]++>>[<<+>>-]>>>[-]++>[-]+ 3 | >>>+[[-]++++++>>>]<<<[[<++++++++<++>>-]+<.<[>----<-]<] 4 | <<[>>>>>[>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<]]<[>+<-]>]<<-]<<- 5 | ]! 6 | [Outputs square numbers from 0 to 10000. 7 | Daniel B Cristofani (cristofdathevanetdotcom) 8 | http://www.hevanet.com/cristofd/brainfuck/] 9 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var util = require("util"); 2 | 3 | var INS = { 4 | ">": "++p;", 5 | "<": "--p;", 6 | "+": "++*p;", 7 | "-": "--*p;", 8 | ".": "putchar(*p);", 9 | ",": "*p=getchar();", 10 | "[": "while (*p) {", 11 | "]": "}" 12 | }; 13 | 14 | var PROGRAM = "int main() { char a[%d] = {0}; char *p=a; %s; return 0; }"; 15 | 16 | var compile = module.exports = function (input, mem) { 17 | mem = mem || 30000; 18 | var ins = input.split("").map(function(c) { return INS[c] || ""; }).join(""); 19 | return util.format(PROGRAM, mem, ins); 20 | }; 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bf2c", 3 | "version": "1.0.0", 4 | "description": "Compile Brainfuck to C", 5 | "bin": "./cli.js", 6 | "main": "index.js", 7 | "repository": { 8 | "type": "git", 9 | "url": "git+https://github.com/olahol/bf2c.git" 10 | }, 11 | "keywords": [ 12 | "brainfuck", 13 | "compiler" 14 | ], 15 | "author": "Ola Holmström", 16 | "license": "ISC", 17 | "bugs": { 18 | "url": "https://github.com/olahol/bf2c/issues" 19 | }, 20 | "homepage": "https://github.com/olahol/bf2c#readme" 21 | } 22 | --------------------------------------------------------------------------------