Skip to content

Instantly share code, notes, and snippets.

@hmhmsh
Last active December 1, 2016 10:24
Show Gist options
  • Save hmhmsh/ee58e822199520ec03b8cc4ef1166a0f to your computer and use it in GitHub Desktop.
Save hmhmsh/ee58e822199520ec03b8cc4ef1166a0f to your computer and use it in GitHub Desktop.
Text to Brainf*ck
+++++++[>++++++++++<-]>++.<++[>++++++++++<-]>+++++++++.<[>++++++++++<-]>+++++++..<[>++++++++++<-]>+++.<++++++[>----------<-]>-------.<+[>----------<-]>--.<+++++[>++++++++++<-]>+++++.<++[>++++++++++<-]>++++.<[>++++++++++<-]>+++.<[>----------<-]>------.<[>----------<-]>--------.<++++++[>----------<-]>-------.
/*
## Usage
$ javac T2bf.java; java T2bf helloworld.tb hello.tt
*/
class T2bf extends Text2Brainfuck {
public static void main(String[] args) {
T2bf tb = new T2bf(args);
}
public T2bf(String[] args) {
super(args);
}
}
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
class Text2Brainfuck {
protected static enum Token {
NEXT,
PREVIOUS,
INC,
DEC,
GET,
PUT,
LOOP,
JUMP;
public String text;
}
protected void initTokenList() {
Token.NEXT.text = ">";
Token.PREVIOUS.text = "<";
Token.INC.text = "+";
Token.DEC.text = "-";
Token.GET.text = ",";
Token.PUT.text = ".";
Token.LOOP.text = "[";
Token.JUMP.text = "]";
}
protected Writer output;
protected int textcode = 0;
public Text2Brainfuck(String[] args) {
initTokenList();
try {
output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1]), "UTF-8"));
} catch (Exception e) {
System.out.println("output: " + e);
}
loadFile(new File(args[0]));
}
public void loadFile(File file) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String content = "";
String raw = "";
while (raw != null) {
content += raw;
try {
raw = reader.readLine();
} catch (Exception e) {
System.out.println("reader.readLine(): " + e);
}
}
splitString(content);
} catch (Exception e) {
System.out.println("new BufferedReader(new FileReader): " + e);
}
}
private void splitString(String str) {
for (int count = 0; count < str.length(); count++) {
String s = str.substring(count, count + 1);
byte[] b = s.getBytes();
if (s.length() == b.length) {
calcString(s);
}
}
try {
output.close();
} catch (Exception e) {
System.out.println("output close: " + e);
}
}
private void calcString(String str) {
int code = str.charAt(0);
int diff = calcDiff(code);
if (diff != 0) {
if (textcode != 0) {
outputBf(Token.PREVIOUS.text);
}
boolean isInc = diff > 0;
textcode = code;
int count = Math.abs(diff) / 10;
int remainder = Math.abs(diff) % 10;
outputTen(count, diff > 0);
outputRemainder(remainder, isInc);
}
outputBf(Token.PUT.text);
}
private int calcDiff(int code) {
return (code - textcode);
}
private void outputTen(int count, boolean isInc) {
for (int index = 0; index < count; index++) {
outputBf(Token.INC.text);
}
outputBf(Token.LOOP.text);
outputBf(Token.NEXT.text);
for (int index = 0; index < 10; index++) {
outputBf(isInc ? Token.INC.text : Token.DEC.text);
}
outputBf(Token.PREVIOUS.text);
outputBf(Token.DEC.text);
outputBf(Token.JUMP.text);
}
private void outputRemainder(int remainder, boolean isInc) {
outputBf(Token.NEXT.text);
for (int index = 0; index < remainder; index++) {
outputBf(isInc ? Token.INC.text : Token.DEC.text);
}
}
private void outputBf(String str) {
for (char c : str.toCharArray()) {
try {
output.write(c);
} catch (Exception e) {
System.out.println("output.write: " + e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment