Skip to content
Browse files

Merge branch 'master' of github.com:Tilka/JBrainfuck

  • Loading branch information...
2 parents 1e814d5 + e72690f commit 46320b1e4ba6e799b6c7a154edaec35dfc3a3915 @Tilka committed May 9, 2012
View
29 BrainfuckHeader.java
@@ -1,8 +1,33 @@
import java.io.IOException;
+class Number {
+ public static int limit = 512;
+ private int value;
+
+ public Number add(Number a) {
+ return add(a.get());
+ }
+
+ public Number add(int a) {
+ value = (value + a) %% limit;
+ if(value < 0) value += limit;
+ return this;
+ }
+
+ public int get() {
+ return value;
+ }
+ public void set(int i) {
+ value = i;
+ add(0); //make it between 0 and 255
+ }
+}
+
public class %s {
- private static short[] data = new short[256];
- private static short ptr = 0;
+ private static Number[] data = new Number[ Number.limit ];
+ private static Number ptr = new Number();
public static void main(String[] args) throws IOException {
+ for(int i = 0 ; i < Number.limit ; i++)
+ data[i] = new Number();
// end of header
View
3 DataModifier.java
@@ -9,6 +9,7 @@ public DataModifier(int value) {
public void compile(PrintStream out) {
indent(out);
- out.println("data[ptr] = (short)( (data[ptr] + " + value + ") % 256 ); if(data[ptr] < 0) data[ptr]+=256;");
+// out.println("data[ptr] = (short)( (data[ptr] + " + value + ") % 256 ); if(data[ptr] < 0) data[ptr]+=256;");
+ out.println("data[ptr.get()].add("+ value +");");
}
}
View
2 Input.java
@@ -5,7 +5,7 @@
public void compile(PrintStream out) {
indent(out);
- out.println("data[ptr] = (byte) System.in.read();");
+ out.println("data[ptr.get()].set( System.in.read() );");
}
}
View
2 Loop.java
@@ -9,7 +9,7 @@ public Loop(ExpressionList innerCode) {
public void compile(PrintStream out) {
indent(out);
- out.println("while (data[ptr] != 0) {");
+ out.println("while (data[ptr.get()].get() != 0) {");
innerCode.compile(out, getIndentationLevel() + 1);
indent(out);
out.println("}");
View
2 Output.java
@@ -5,7 +5,7 @@
public void compile(PrintStream out) {
indent(out);
- out.println("System.out.print((char) data[ptr]);");
+ out.println("System.out.print((char) data[ptr.get()].get());");
}
}
View
19 ParseBrainfuck.java
@@ -8,9 +8,18 @@
import java.nio.charset.Charset;
public class ParseBrainfuck {
- private static String getRawFileName(String inputFileName) {
+ private static String getOutFileName(String inputFileName) {
int fileNameEnd = inputFileName.lastIndexOf('.');
- return fileNameEnd == -1 ? inputFileName : inputFileName.substring(0, fileNameEnd);
+
+ return fileNameEnd == -1 ? inputFileName + ".java" : inputFileName.substring(0, fileNameEnd) + ".java";
+ }
+
+ private static String getClassName(String inputFileName) {
+ int fileNameEnd = inputFileName.lastIndexOf('.');
+ int fileNameBegin = inputFileName.lastIndexOf('/');
+System.out.println(inputFileName+fileNameEnd+fileNameBegin+inputFileName.substring(fileNameBegin+1, fileNameEnd));
+
+ return fileNameEnd == -1 ? inputFileName.substring(fileNameBegin+1) : inputFileName.substring(fileNameBegin+1, fileNameEnd);
}
private static String fileToString(String fileName) throws IOException {
@@ -40,12 +49,12 @@ public static void main(String[] args) throws Exception {
}
String inFileName = args[0];
- String rawFileName = getRawFileName(inFileName);
- String outFileName = rawFileName + ".java";
+ String outFileName = getOutFileName(inFileName);
+ String className = getClassName(inFileName);
PrintStream out = new PrintStream(outFileName);
try {
String header = fileToString("BrainfuckHeader.java");
- out.print(String.format(header, rawFileName));
+ out.print(String.format(header, className));
InputStream in = new FileInputStream(inFileName);
compile(in, out);
out.print(fileToString("BrainfuckFooter.java"));
View
3 PtrModifier.java
@@ -9,7 +9,8 @@ public PtrModifier(int value) {
public void compile(PrintStream out) {
indent(out);
- out.println("ptr = (short)( (ptr + " + value + ") % 256 ); if(ptr < 0) ptr+=256;");
+// out.println("ptr = (short)( (ptr + " + value + ") % 256 ); if(ptr < 0) ptr+=256;");
+ out.println("ptr.add("+ value +");");
}
}
View
9 exampels/fib.bf
@@ -0,0 +1,9 @@
+>++++++++++>+>+[
+ [+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[
+ [-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
+ [>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>>
+ ]<<<
+]
+This program doesn't terminate; you will have to kill it.
+Daniel B Cristofani (cristofdathevanetdotcom)
+http://www.hevanet.com/cristofd/brainfuck/
View
0 helloworld.bf → exampels/helloworld.bf
File renamed without changes.
View
60 exampels/neintyNineBotles.bf
@@ -0,0 +1,60 @@
+99 Bottles of Beer in Urban Mueller's BrainF*** (The actual
+name is impolite)
+
+by Ben Olmstead
+
+ANSI C interpreter available on the internet; due to
+constraints in comments the address below needs to have the
+stuff in parenthesis replaced with the appropriate symbol:
+
+http://www(dot)cats(dash)eye(dot)com/cet/soft/lang/bf/
+
+Believe it or not this language is indeed Turing complete!
+Combines the speed of BASIC with the ease of INTERCAL and
+the readability of an IOCCC entry!
+
+>+++++++++[<+++++++++++>-]<[>[-]>[-]<<[>+>+<<-]>>[<<+>>-]>>>
+[-]<<<+++++++++<[>>>+<<[>+>[-]<<-]>[<+>-]>[<<++++++++++>>>+<
+-]<<-<-]+++++++++>[<->-]>>+>[<[-]<<+>>>-]>[-]+<<[>+>-<<-]<<<
+[>>+>+<<<-]>>>[<<<+>>>-]>[<+>-]<<-[>[-]<[-]]>>+<[>[-]<-]<+++
++++++[<++++++<++++++>>-]>>>[>+>+<<-]>>[<<+>>-]<[<<<<<.>>>>>-
+]<<<<<<.>>[-]>[-]++++[<++++++++>-]<.>++++[<++++++++>-]<++.>+
+++++[<+++++++++>-]<.><+++++..--------.-------.>>[>>+>+<<<-]>
+>>[<<<+>>>-]<[<<<<++++++++++++++.>>>>-]<<<<[-]>++++[<+++++++
++>-]<.>+++++++++[<+++++++++>-]<--.---------.>+++++++[<------
+---->-]<.>++++++[<+++++++++++>-]<.+++..+++++++++++++.>++++++
+++[<---------->-]<--.>+++++++++[<+++++++++>-]<--.-.>++++++++
+[<---------->-]<++.>++++++++[<++++++++++>-]<++++.-----------
+-.---.>+++++++[<---------->-]<+.>++++++++[<+++++++++++>-]<-.
+>++[<----------->-]<.+++++++++++..>+++++++++[<---------->-]<
+-----.---.>>>[>+>+<<-]>>[<<+>>-]<[<<<<<.>>>>>-]<<<<<<.>>>+++
++[<++++++>-]<--.>++++[<++++++++>-]<++.>+++++[<+++++++++>-]<.
+><+++++..--------.-------.>>[>>+>+<<<-]>>>[<<<+>>>-]<[<<<<++
+++++++++++++.>>>>-]<<<<[-]>++++[<++++++++>-]<.>+++++++++[<++
++++++++>-]<--.---------.>+++++++[<---------->-]<.>++++++[<++
++++++++++>-]<.+++..+++++++++++++.>++++++++++[<---------->-]<
+-.---.>+++++++[<++++++++++>-]<++++.+++++++++++++.++++++++++.
+------.>+++++++[<---------->-]<+.>++++++++[<++++++++++>-]<-.
+-.---------.>+++++++[<---------->-]<+.>+++++++[<++++++++++>-
+]<--.+++++++++++.++++++++.---------.>++++++++[<---------->-]
+<++.>+++++[<+++++++++++++>-]<.+++++++++++++.----------.>++++
++++[<---------->-]<++.>++++++++[<++++++++++>-]<.>+++[<----->
+-]<.>+++[<++++++>-]<..>+++++++++[<--------->-]<--.>+++++++[<
+++++++++++>-]<+++.+++++++++++.>++++++++[<----------->-]<++++
+.>+++++[<+++++++++++++>-]<.>+++[<++++++>-]<-.---.++++++.----
+---.----------.>++++++++[<----------->-]<+.---.[-]<<<->[-]>[
+-]<<[>+>+<<-]>>[<<+>>-]>>>[-]<<<+++++++++<[>>>+<<[>+>[-]<<-]
+>[<+>-]>[<<++++++++++>>>+<-]<<-<-]+++++++++>[<->-]>>+>[<[-]<
+<+>>>-]>[-]+<<[>+>-<<-]<<<[>>+>+<<<-]>>>[<<<+>>>-]<>>[<+>-]<
+<-[>[-]<[-]]>>+<[>[-]<-]<++++++++[<++++++<++++++>>-]>>>[>+>+
+<<-]>>[<<+>>-]<[<<<<<.>>>>>-]<<<<<<.>>[-]>[-]++++[<++++++++>
+-]<.>++++[<++++++++>-]<++.>+++++[<+++++++++>-]<.><+++++..---
+-----.-------.>>[>>+>+<<<-]>>>[<<<+>>>-]<[<<<<++++++++++++++
+.>>>>-]<<<<[-]>++++[<++++++++>-]<.>+++++++++[<+++++++++>-]<-
+-.---------.>+++++++[<---------->-]<.>++++++[<+++++++++++>-]
+<.+++..+++++++++++++.>++++++++[<---------->-]<--.>+++++++++[
+<+++++++++>-]<--.-.>++++++++[<---------->-]<++.>++++++++[<++
+++++++++>-]<++++.------------.---.>+++++++[<---------->-]<+.
+>++++++++[<+++++++++++>-]<-.>++[<----------->-]<.+++++++++++
+..>+++++++++[<---------->-]<-----.---.+++.---.[-]<<<]
+
View
14 exampels/random.bf
@@ -0,0 +1,14 @@
+>>>++[
+ <++++++++[
+ <[<++>-]>>[>>]+>>+[
+ -[->>+<<<[<[<<]<+>]>[>[>>]]]
+ <[>>[-]]>[>[-<<]>[<+<]]+<<
+ ]<[>+<-]>>-
+ ]<.[-]>>
+]
+"Random" byte generator using the Rule 30 automaton.
+Doesn't terminate; you will have to kill it.
+To get x bytes you need 32x+4 cells.
+Turn off any newline translation!
+Daniel B Cristofani (cristofdathevanetdotcom)
+http://www.hevanet.com/cristofd/brainfuck/
View
122 helloworld.java
@@ -1,122 +0,0 @@
-import java.io.IOException;
-
-public class helloworld {
- private static byte[] data = new byte[256];
- private static byte ptr = 0;
-
- public static void main(String[] args) throws IOException {
- // end of header
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- do {
- ptr += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- ptr += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- ptr += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- ptr += 1;
- data[ptr] += 1;
- ptr += -1;
- ptr += -1;
- ptr += -1;
- ptr += -1;
- data[ptr] += -1;
- } while (data[ptr] != 0);
- ptr += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- System.out.print((char) data[ptr]);
- ptr += 1;
- data[ptr] += 1;
- System.out.print((char) data[ptr]);
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- System.out.print((char) data[ptr]);
- System.out.print((char) data[ptr]);
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- System.out.print((char) data[ptr]);
- ptr += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- System.out.print((char) data[ptr]);
- ptr += -1;
- ptr += -1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- System.out.print((char) data[ptr]);
- ptr += 1;
- System.out.print((char) data[ptr]);
- data[ptr] += 1;
- data[ptr] += 1;
- data[ptr] += 1;
- System.out.print((char) data[ptr]);
- data[ptr] += -1;
- data[ptr] += -1;
- data[ptr] += -1;
- data[ptr] += -1;
- data[ptr] += -1;
- data[ptr] += -1;
- System.out.print((char) data[ptr]);
- data[ptr] += -1;
- data[ptr] += -1;
- data[ptr] += -1;
- data[ptr] += -1;
- data[ptr] += -1;
- data[ptr] += -1;
- data[ptr] += -1;
- data[ptr] += -1;
- System.out.print((char) data[ptr]);
- ptr += 1;
- data[ptr] += 1;
- System.out.print((char) data[ptr]);
- ptr += 1;
- System.out.print((char) data[ptr]);
- // start of footer
- }
-}

0 comments on commit 46320b1

Please sign in to comment.
Something went wrong with that request. Please try again.