Execute Brain****/Java: Difference between revisions
Content added Content deleted
m (Optimized a bit on loops) |
m (Added syntax highlighting and now it looks all cool) |
||
Line 10: | Line 10: | ||
More detailed information about the rest of the code can be found in the comments throughout it.<br clear=all> |
More detailed information about the rest of the code can be found in the comments throughout it.<br clear=all> |
||
<java>import java.io.BufferedReader; |
|||
import java.io.FileNotFoundException; |
|||
import java.io.FileReader; |
|||
import java.io.IOException; |
|||
import java.io.InputStreamReader; |
|||
import java.util.ArrayList; |
|||
⚫ | |||
/** |
|||
* Interprets Brainf**k source from std in or a file. |
|||
* |
|||
* @author Mike Neurohr |
|||
*/ |
|||
public class BF{ |
|||
private static int pointer;//memory pointer |
|||
private static int codeIndex;//instruction number |
|||
private static final int INITIAL_SIZE= 1000;//initial size of memory |
|||
private static final String instChars= "[]<>,.+-";//valid code characters |
|||
⚫ | |||
/** |
|||
* The main program. Holds the input loop and error-handling. |
|||
* |
|||
* @param args 0: Source file others: ignored |
|||
*/ |
|||
public static void main(String[] args){ |
|||
BufferedReader source; |
|||
try{ |
|||
if(args.length == 0){//if no file specified |
|||
source= new BufferedReader(new InputStreamReader(System.in)); |
|||
}else{ |
|||
source= new BufferedReader(new FileReader(args[0])); |
|||
} |
|||
char instruction; |
|||
⚫ | |||
//holds the cleaned up code |
|||
StringBuilder code= new StringBuilder(); |
|||
//count for loop syntax errors |
|||
int bCnt= 0; |
|||
//read whole source in |
|||
for(instruction= (char)source.read(); source.ready(); instruction= |
|||
(char)source.read()){ |
|||
//remove extra characters |
|||
if(instChars.contains(instruction + "")){ |
|||
//add the instruction to the clean code |
|||
code.append(instruction); |
|||
if(instruction == '[') bCnt++; |
|||
if(instruction == ']') bCnt--; |
|||
} |
|||
} |
|||
if(bCnt != 0){//if there is a bracket with no match |
|||
System.err.println("Error: Uneven brackets."); |
|||
return; |
|||
} |
|||
//set up input for , |
|||
BufferedReader input= |
|||
new BufferedReader(new InputStreamReader(System.in)); |
|||
//set up memory |
|||
ArrayList<Integer> memory= new ArrayList<Integer>(INITIAL_SIZE); |
|||
memory.add(new Integer(0)); |
|||
pointer= 0; //initialize pointer |
|||
//loop through the cleaned up code and execute |
|||
for(codeIndex= 0;codeIndex < code.length();codeIndex++){ |
|||
instruction= code.charAt(codeIndex); |
|||
switch(instruction){ |
|||
case '+': |
|||
memory.set(pointer, memory.get(pointer) + 1); |
|||
break; |
|||
case '-': |
|||
memory.set(pointer, memory.get(pointer) - 1); |
|||
break; |
|||
case '.': |
|||
System.out.print((char)memory.get(pointer).intValue()); |
|||
break; |
|||
case ',': |
|||
input(memory, input); |
|||
break; |
|||
case '[': |
|||
if(memory.get(pointer) == 0){ |
|||
//skip the code inside the loop |
|||
jumpForward(code); |
|||
} |
|||
//if the loop can continue... |
|||
//we don't need to check other things |
|||
break; |
|||
case ']': |
|||
if(memory.get(pointer) != 0){ |
|||
//go back to the corresponding [ to check |
|||
jumpBack(code); |
|||
} |
|||
//if the loop should stop... |
|||
//we don't need to check other things |
|||
break; |
|||
case '>': |
|||
pointer++; |
|||
//gets rid of NullPointerExceptions |
|||
while(pointer + 1 > memory.size()) memory.add(0); |
|||
break; |
|||
case '<': |
|||
if(--pointer < 0){ |
|||
//can't do it |
|||
System.err.println("Pointer out of range (negative)."); |
|||
return; |
|||
} |
|||
default: |
|||
} |
|||
} |
|||
}catch(FileNotFoundException e){ |
|||
System.err.println("Error opening file: " + args[0] + "."); |
|||
}catch(IOException e){ |
|||
System.err.println("Error on input."); |
|||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
}</java> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
|||
} |