use super::tokenize;
use super::Token;
use super::Token::*;
fn generate(tokens: &[Token]) -> String {
let mut has_stdin_interraction = false;
let mut output = String::default();
let mut indent = 1;
for &token in tokens {
match token {
Add => {
for _ in 0..indent {
output.push('\t');
}
output.push_str("ptr[ndx] += 1\n")
}
Sub => {
for _ in 0..indent {
output.push('\t');
}
output.push_str("ptr[ndx] -= 1\n")
}
Right => {
for _ in 0..indent {
output.push('\t');
}
output.push_str("ndx += 1\n");
}
Left => {
for _ in 0..indent {
output.push('\t');
}
output.push_str("ndx -= 1\n");
}
Read => {
for _ in 0..indent {
output.push('\t');
}
output.push_str("ptr[ndx] = getchar(reader)\n");
has_stdin_interraction = true;
}
Write => {
for _ in 0..indent {
output.push('\t');
}
output.push_str("fmt.Printf(\"%c\", ptr[ndx])\n")
}
BeginLoop => {
indent += 1;
for _ in 0..(indent - 1) {
output.push('\t');
}
output.push_str("for (ptr[ndx] != 0) {\n");
}
EndLoop => {
indent -= 1;
for _ in 0..indent {
output.push('\t');
}
output.push_str("}\n");
}
}
}
output.push_str("}\n");
let mut header = if has_stdin_interraction {
String::from(include_str!("preface_stdin.go"))
} else {
String::from(include_str!("preface.go"))
};
header.push_str(&output);
header
}
pub fn brains(input: &str) -> String {
let tokens = tokenize(input);
generate(&tokens)
}