Skip to content

Instantly share code, notes, and snippets.

@Property404
Last active March 18, 2016 14:14
Show Gist options
  • Save Property404/b53caf6cc7cf8fa0ff84 to your computer and use it in GitHub Desktop.
Save Property404/b53caf6cc7cf8fa0ff84 to your computer and use it in GitHub Desktop.
Self-hosting Brainfuck to C translator
// Make Room
>>
// Take code as input
----------[++++++++++>,----------]
// New Section
>
// Prepare to print
++++++++++[->+++>++++++++++>++++>++++++++++++>+++++++++>+++++<<<<<<]
// Print 'main(){'
>>+++++++++.------------.++++++++.+++++.>.+.>+++.
// Print 'char a(3000)={0};char* p=a;'
<<
-----------.+++++.-------.+++++++++++++++++.<++.'char '>-----------------.'a'
>>>
+.>+.---....<++.'(3000)'>+++++++++++.';'
<<<<
++.+++++.-------.+++++++++++++++++.>+.'char*'<<.' '>--.>>>>++.<<<<---------------.>>>>--.
[<]++++++++++.
//Go to start of Array
<<[<]>
// Here's where we start translating to C
[
// Check for 'plus'(ASCII 43)
----------
----------
----------
----------
---
[
<+ // Set flag cell
/* Transfer current cell to secondary cell*/
>
[-<<+>>]
//zero so exit
]
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++
/* Transfer secondary cell to current cell*/
<<[->>+<<]>>
< (Go to flag cell)
- (subtract 1 to do an inverse(as in 'if not*'))
[
// If in this loop means that 'plus' was found
[-] //Reset flag cell
// Print out C command in flag cell
<++++++++++[->++++<]>
+++..-.[-]
<++++++++++[->+++++++++++<]>
++.[-]
<++++++++++[->++++++<]>
-.[-]
++++++++++.
[-] //Reset flag cell
]
> // Go to current cell (now empty
/* REPEAT code*/
// Check for 'minus'(ASCII 45)
----------
----------
----------
----------
-----
[
<+ // Set flag cell
/* Transfer current cell to secondary cell*/
>
[-<<+>>]
//zero so exit
]
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++
/* Transfer secondary cell to current cell*/
<<[->>+<<]>>
< (Go to flag cell)
- (subtract 1 to do an inverse(as in 'if not*'))
[
// If in this loop means that 'minus' was found
[-] //Reset flag cell
// Print out C command in flag cell
<++++++++++[->++++<]>
+++++..---.[-]
<++++++++++[->+++++++++++<]>
++.[-]
<++++++++++[->++++++<]>
-.[-]
++++++++++.
[-] //Reset flag cell
]
> // Go to current cell (now empty
/* REPEAT code*/
// Check for 'Right Shift'(ASCII 62)
----------
----------
----------
----------
----------
----------
--
[
<+ // Set flag cell
/* Transfer current cell to secondary cell*/
>
[-<<+>>]
//zero so exit
]
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
++
/* Transfer secondary cell to current cell*/
<<[->>+<<]>>
< (Go to flag cell)
- (subtract 1 to do an inverse(as in 'if not*'))
[
// If in this loop means that 'right shift' was found
[-] //Reset flag cell
// Print out C command in flag cell
<++++++++++[->++++<]>
+++..[-]
<++++++++++[->+++++++++++<]>
++.[-]
<++++++++++[->++++++<]>
-.[-]
++++++++++.
[-] //Reset flag cell
]
> // Go to current cell (now empty
/* REPEAT code*/
// Check for 'Left Shift'(ASCII 60)
----------
----------
----------
----------
----------
----------
[
<+ // Set flag cell
/* Transfer current cell to secondary cell*/
>
[-<<+>>]
//zero so exit
]
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
/* Transfer secondary cell to current cell*/
<<[->>+<<]>>
< (Go to flag cell)
- (subtract 1 to do an inverse(as in 'if not*'))
[
// If in this loop means that 'left shift' was found
[-] //Reset flag cell
// Print out C command in flag cell
<++++++++++[->++++<]>
+++++..[-]
<++++++++++[->+++++++++++<]>
++.[-]
<++++++++++[->++++++<]>
-.[-]
++++++++++.
[-] //Reset flag cell
]
> // Go to current cell (now empty
/* REPEAT code*/
// Check for 'Left Bracket'(ASCII 91)
----------
----------
----------
----------
----------
----------
----------
----------
----------
-
[
<+ // Set flag cell
/* Transfer current cell to secondary cell*/
>
[-<<+>>]
//zero so exit
]
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+
/* Transfer secondary cell to current cell*/
<<[->>+<<]>>
< (Go to flag cell)
- (subtract 1 to do an inverse(as in 'if not*'))
[
// If in this loop means that 'left Bracket' was found
[-] //Reset flag cell
// Print out C command in flag cell
<++++++++++[->+++++++++++<]>
+++++++++.
---------------.
+.
+++.
-------.
[-]
<++++++++++[->++++<]>
.++.[-]
<++++++++++[->+++++++++++<]>
++.[-]
<++++++++++[->++++<]>
+.[-]
<++++++++++[->++++++++++++<]>
+++.[-]
++++++++++.
[-] //Reset flag cell
]
> // Go to current cell (now empty
/* REPEAT code*/
// Check for 'Right Bracket'(ASCII 93)
----------
----------
----------
----------
----------
----------
----------
----------
----------
---
[
<+ // Set flag cell
/* Transfer current cell to secondary cell*/
>
[-<<+>>]
//zero so exit
]
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++
/* Transfer secondary cell to current cell*/
<<[->>+<<]>>
< (Go to flag cell)
- (subtract 1 to do an inverse(as in 'if not*'))
[
// If in this loop means that 'right Bracket' was found
[-] //Reset flag cell
// Print out C command in flag cell
<++++++++++[->++++++++++++<]>
+++++.[-]
++++++++++.
[-] //Reset flag cell
]
> // Go to current cell (now empty
/* REPEAT code*/
/* REPEAT code*/
// Check for 'Period'(ASCII 46)
----------
----------
----------
----------
------
[
<+ // Set flag cell
/* Transfer current cell to secondary cell*/
>
[-<<+>>]
//zero so exit
]
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +
/* Transfer secondary cell to current cell*/
<<[->>+<<]>>
< (Go to flag cell)
- (subtract 1 to do an inverse(as in 'if not*'))
[
// If in this loop means that 'period' was found
[-] //Reset flag cell
// Print out C command in flag cell
<++++++++++[->+++++++++++<]>
++.
+++++.
-.
-----------------.
+++++.
-------.
+++++++++++++++++.
[-]
<++++++++++[->++++<]>
.++.[-]
<++++++++++[->+++++++++++<]>
++.[-]
<++++++++++[->++++<]>
+.[-]
<++++++++++[->+++ +++<]>
-.[-]
++++++++++.
[-] //Reset flag cell
]
> // Go to current cell (now empty
/* REPEAT code*/
// Check for 'comma'(ASCII 44)
----------
----------
----------
----------
----
[
<+ // Set flag cell
/* Transfer current cell to secondary cell*/
>
[-<<+>>]
//zero so exit
]
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
++++
/* Transfer secondary cell to current cell*/
<<[->>+<<]>>
< (Go to flag cell)
- (subtract 1 to do an inverse(as in 'if not*'))
[
// If in this loop means that 'comma' was found
[-] //Reset flag cell
// Print out C command in flag cell
//p*=
<++++++++++[->++++<]>
++.[-]
<++++++++++[->+++++++++++<]>
++.[-]
<++++++++++[->++++++<]>
+.[-]
// getchar
<++++++++++[->++++++++++<]>
+++.
--.
+++++++++++++++.
-----------------.
+++++.
-------.
+++++++++++++++++.
[-]
<++++++++++[->++++<]>
.+.[-]
<++++++++++[->+++ +++<]>
-.[-]
++++++++++.
[-] //Reset flag cell
]
> // Go to current cell (now empty
/* END
[-] // Reset current cell
> // Go to next cell
]
// Ending:('}')
[-]<++++++++++[->++++++++++++<]>
+++++.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment