Created
January 3, 2013 18:20
-
-
Save jhbruhn/4445585 to your computer and use it in GitHub Desktop.
This file contains 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
package org.alternadev.brainfuck; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.nio.MappedByteBuffer; | |
import java.nio.channels.FileChannel; | |
import java.nio.charset.Charset; | |
public class BrainfuckInterpreter { | |
private int pointer; | |
private byte[] cells; | |
private OutputStream outStream; | |
private InputStream inStream; | |
private char[] chars; | |
private int currChar; | |
public BrainfuckInterpreter(int cellAmount, OutputStream outStream, | |
InputStream inStream) { | |
cells = new byte[cellAmount]; | |
this.outStream = outStream; | |
this.inStream = inStream; | |
} | |
public void interpret(String s) throws Exception { | |
chars = s.toCharArray(); | |
for (; currChar < chars.length; currChar++) | |
interpretChar(chars[currChar]); | |
} | |
private void interpretChar(char c) throws Exception { | |
switch (c) { | |
case Token.INC_PTR: | |
pointer++; | |
break; | |
case Token.DEC_PTR: | |
pointer--; | |
break; | |
case Token.INC_CELL: | |
cells[pointer]++; | |
break; | |
case Token.DEC_CELL: | |
cells[pointer]--; | |
break; | |
case Token.PUT: | |
outStream.write((char) cells[pointer]); | |
outStream.flush(); | |
break; | |
case Token.GET: | |
cells[pointer] = (byte) inStream.read(); | |
break; | |
case Token.BRACKET_LEFT: | |
if (cells[pointer] == 0) { | |
int i = 1; | |
while (i > 0) { | |
char c2 = chars[++currChar]; | |
if (c2 == Token.BRACKET_LEFT) | |
i++; | |
else if (c2 == Token.BRACKET_RIGHT) | |
i--; | |
} | |
} | |
break; | |
case Token.BRACKET_RIGHT: | |
int i = 1; | |
while (i > 0) { | |
char c2 = chars[--currChar]; | |
if (c2 == Token.BRACKET_LEFT) | |
i--; | |
else if (c2 == Token.BRACKET_RIGHT) | |
i++; | |
} | |
currChar--; | |
break; | |
} | |
} | |
class Token { | |
public static final char INC_PTR = '>'; | |
public static final char DEC_PTR = '<'; | |
public static final char INC_CELL = '+'; | |
public static final char DEC_CELL = '-'; | |
public static final char PUT = '.'; | |
public static final char GET = ','; | |
public static final char BRACKET_LEFT = '['; | |
public static final char BRACKET_RIGHT = ']'; | |
} | |
public static void main(String[] rolfi) throws Exception { | |
BrainfuckInterpreter inter = new BrainfuckInterpreter(100, System.out, | |
System.in); | |
if (rolfi.length == 0) | |
System.err.println("Please give me a File as Parameter!"); | |
else | |
inter.interpret(readFile(rolfi[0])); | |
} | |
private static String readFile(String path) throws IOException { | |
FileInputStream stream = new FileInputStream(new File(path)); | |
try { | |
FileChannel fc = stream.getChannel(); | |
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, | |
fc.size()); | |
return Charset.defaultCharset().decode(bb).toString(); | |
} finally { | |
stream.close(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment