Last active
January 14, 2021 16:43
-
-
Save lukewilson2002/7ee78589cfb770104edeaec710aa61f9 to your computer and use it in GitHub Desktop.
Simple Brainfuck to C compiler written in Rust.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// "Hello, world!" | |
static PROGRAM: &'static str = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."; | |
fn main() { | |
let tokens = tokenize(PROGRAM); | |
//println!("{:?}", tokens); | |
let generated_code = generate(&tokens); | |
println!("{}", generated_code); | |
} | |
#[derive(Debug, PartialEq, Copy, Clone)] | |
enum Token { | |
Add, // + | |
Sub, // - | |
Right, // > | |
Left, // < | |
Read, // , | |
Write, // . | |
BeginLoop, // [ | |
EndLoop, // ] | |
} | |
use self::Token::*; | |
fn tokenize(input: &str) -> Vec<Token> { | |
let mut tokens = Vec::<Token>::new(); | |
let mut chars = input.chars(); | |
while let Some(c) = chars.next() { | |
match c { | |
'+' => tokens.push(Add), | |
'-' => tokens.push(Sub), | |
'>' => tokens.push(Right), | |
'<' => tokens.push(Left), | |
',' => tokens.push(Read), | |
'.' => tokens.push(Write), | |
'[' => tokens.push(BeginLoop), | |
']' => tokens.push(EndLoop), | |
_ => {} | |
} | |
} | |
tokens | |
} | |
fn generate(tokens: &[Token]) -> String { | |
let mut output = String::from(include_str!("preface.c")); | |
// Tabs are just for pretty printing indented text. | |
let mut indentation = 1u32; | |
fn indent(count: u32) -> String { | |
let mut indentation = String::new(); | |
for _ in 0..count { | |
indentation.push_str(" "); // 4-space indentions | |
} | |
indentation | |
} | |
for &token in tokens { | |
match token { | |
Add => { | |
// Increment the value at the selected cell | |
output.push_str(&indent(indentation)); | |
output.push_str("++*ptr;\n"); | |
} | |
Sub => { | |
// Decrement the value at the selected cell | |
output.push_str(&indent(indentation)); | |
output.push_str("--*ptr;\n"); | |
} | |
Right => { | |
// Change our selected cell to the next to the right | |
output.push_str(&indent(indentation)); | |
output.push_str("ptr++;\n"); | |
} | |
Left => { | |
// Change our selected cell to the next to the left | |
output.push_str(&indent(indentation)); | |
output.push_str("ptr--;\n"); | |
} | |
Read => { | |
// Read a single character into the selected cell | |
output.push_str(&indent(indentation)); | |
output.push_str("*ptr=getchar();\n"); | |
} | |
Write => { | |
// Print the character at the selected cell | |
output.push_str(&indent(indentation)); | |
output.push_str("putchar(*ptr);\n"); | |
} | |
BeginLoop => { | |
// Begin a loop at the current cell | |
output.push_str(&indent(indentation)); | |
output.push_str("while (*ptr) {\n"); | |
indentation += 1; | |
} | |
EndLoop => { | |
// Close a loop | |
indentation -= 1; | |
output.push_str(&indent(indentation)); | |
output.push_str("}\n"); | |
} | |
} | |
} | |
output.push_str("}\n"); | |
output | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdio.h" | |
int main() | |
{ | |
char tape[20000] = {0}; | |
char *ptr = tape; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment