Execute Brain****/Java: Difference between revisions
Content added Content deleted
m (Added a bit more description.) |
m (Cleaned up comments and description a bit.) |
||
Line 2: | Line 2: | ||
In this implementation of [[Brainf***]], the code is read in all at once and checked for uneven brackets (unequal amounts of [ and ] commands). If that error occurs, the code will obviously not be run. |
In this implementation of [[Brainf***]], the code is read in all at once and checked for uneven brackets (unequal amounts of [ and ] commands). If that error occurs, the code will obviously not be run. |
||
Under the hood, the program memory is an ArrayList of Integers which expands only to the right. So, if the pointer moves past zero to the left, the program will exit and a "Pointer out of range" error message will be displayed. Due to the BufferedReader input class, return characters ([http://www.asciitable.com ASCII] 10 and 13) are ignored on input (the , command) |
Under the hood, the program memory is an ArrayList of Integers which expands only to the right. So, if the pointer moves past zero to the left, the program will exit and a "Pointer out of range" error message will be displayed. Due to the BufferedReader input class, return characters ([http://www.asciitable.com ASCII] 10 and 13) are ignored on input (the , command), but are not ignored on output (the . command). More detailed information can be found in the comments scattered about the code. |
||
import java.io.BufferedReader; |
import java.io.BufferedReader; |
||
Line 25: | Line 25: | ||
* The main program. Holds the input loop and error-handling. |
* The main program. Holds the input loop and error-handling. |
||
* |
* |
||
* @param args 0: Source file others: ignored |
* @param args 0: Source file others: ignored |
||
*/ |
*/ |
||
public static void main(String[] args){ |
public static void main(String[] args){ |
||
Line 38: | Line 38: | ||
StringBuilder code= new StringBuilder(); |
StringBuilder code= new StringBuilder(); |
||
// |
//count for loop syntax errors |
||
int bCnt= 0; |
int bCnt= 0; |
||
Line 77: | Line 77: | ||
case '.': |
case '.': |
||
System.out.print((char)memory.get(pointer).intValue()); |
System.out.print((char)memory.get(pointer).intValue()); |
||
//no errors possible here |
|||
break; |
break; |
||
case ',': |
case ',': |
||
input(memory, input); |
input(memory, input); |
||
//no errors possible here |
|||
break; |
break; |
||
case '[': |
case '[': |
||
Line 97: | Line 95: | ||
case '>': |
case '>': |
||
pointer++; |
pointer++; |
||
//gets rid of NullPointerExceptions |
|||
while(pointer + 1 > memory.size()) memory.add(0); |
while(pointer + 1 > memory.size()) memory.add(0); |
||
break; |
break; |
||
case '<': |
case '<': |
||
if(pointer == 0){ |
if(pointer == 0){ |
||
//can't do it |
|||
System.err.println("Pointer out of range (negative)."); |
System.err.println("Pointer out of range (negative)."); |
||
return; |
return; |
||
Line 117: | Line 117: | ||
public static void jumpBack(StringBuilder code){ |
public static void jumpBack(StringBuilder code){ |
||
//initial count for the bracket we're on |
|||
//read back with pointer |
|||
⚫ | |||
int bracketCnt= 1; |
int bracketCnt= 1; |
||
⚫ | |||
while(codeIndex >= 0 && bracketCnt != 0){ |
while(codeIndex >= 0 && bracketCnt != 0){ |
||
codeIndex--; |
codeIndex--; |
||
Line 131: | Line 131: | ||
public static void jumpForward(StringBuilder code){ |
public static void jumpForward(StringBuilder code){ |
||
//initial count for the bracket we're on |
|||
//read back with code pointer |
|||
⚫ | |||
int bracketCnt= 1; |
int bracketCnt= 1; |
||
⚫ | |||
while(codeIndex < code.length() && bracketCnt != 0){ |
while(codeIndex < code.length() && bracketCnt != 0){ |
||
codeIndex++; |
codeIndex++; |