Execute SNUSP/Java: Difference between revisions

m
Fixed syntax highlighting.
m (Forgot to change some things over to the enum)
m (Fixed syntax highlighting.)
 
(8 intermediate revisions by 5 users not shown)
Line 1:
{{implementation|SNUSP}}{{collection|RCSNUSP}}[[Category:Java]]
This [[Java]] implementation has all of the basic characters plus '<code>%</code>', '<code>@</code>', and '<code>#</code>' (characters from modular and bloated SNUSP found on [http[eso://esolangs.org/wiki/SNUSP |esolangs]]). The memory space grows to the right as needed, and the memory pointer cannot go negative. The program will exit if the memory pointer moves to a negative value or if the code pointer leaves the code space. The input ignores return characters because of the way <tt>BufferedReader</tt> is set up. The random command places a random ASCII value (code 0 through the value in the cell inclusive) in the current memory space.
 
The implementation comes in two classes: the main program and a code pointer class. The separation is for the easier addition of '<code>&</code>' (split) which could come in the future (the code pointer class would only need to extend <tt>Thread</tt> and code would need to be added to deal with the command). The main class decides on the input method (file or std in), reads the code, and starts the code pointer. It also figures out where the starting point in the code is ('<code>$</code>' or 0,0).<br clear=all>
 
{{works with|Java|1.5+}}
 
The main class:<java>import java.awt.Point;
The main class:
The<syntaxhighlight main class:<javalang="java5">import java.awt.Point;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
Line 47 ⟶ 49:
return code;
}
}</javasyntaxhighlight>
The code pointer class:<java>import java.awt.Point;
<syntaxhighlight lang="java5">import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
Line 54 ⟶ 57:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;
 
public class CodePtr{
static final String instChars= "><.,/\\+-#@$%!?";//valid chars
static final Random rand = new Random();
ArrayList<String> code;//code array
Direction dir;//current direction
Line 67 ⟶ 72:
BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
 
public CodePtr(final ArrayList<String> code, final Point place){
this.code= code;
dir= Direction.RIGHT;
Line 78 ⟶ 83:
}
 
public CodePtr(final Point place, final Direction dir){
//This constructor is left over from attempts at '&'
this.dir= dir;
Line 85 ⟶ 90:
 
public void run(){
whilefor(;execute(code.get(place.y).charAt(place.x));place= moveCP()){;
place= moveCP();
}
}
 
private boolean execute(final char inst){
//ignore char and keep going
if(!instChars.contains("" + inst)) return true;
Line 100 ⟶ 103:
try{
int in;
while((in= input.read()) == 10'\n' || in == 13'\r');//skip return chars
mem.set(memPtr, (char)in);
}catch(final IOException e){
Line 110 ⟶ 113:
break;
case '?'://conditional skip
if(mem.get(memPtr) == '\0') place= moveCP();
break;
case '>'://move pointer right
Line 127 ⟶ 130:
case '/'://mirror
switch(dir){
case Direction.RIGHT:
dir= Direction.UP;
break;
case Direction.DOWN:
dir= Direction.LEFT;
break;
case Direction.LEFT:
dir= Direction.DOWN;
break;
case Direction.UP:
dir= Direction.RIGHT;
break;
Line 144 ⟶ 147:
case '\\'://mirror
switch(dir){
case Direction.RIGHT:
dir= Direction.DOWN;
break;
case Direction.DOWN:
dir= Direction.RIGHT;
break;
case Direction.LEFT:
dir= Direction.UP;
break;
case Direction.UP:
dir= Direction.LEFT;
break;
Line 167 ⟶ 170:
case '#'://pop
if(dStack.size() > 0){
final Direction oldDir= dStack.pop();
final Point oldPlace= pStack.pop();
place= oldPlace;
dir= oldDir;
Line 181 ⟶ 184:
break;
case '%':
mem.set(memPtr, (char)(Mathrand.random() * nextInt(mem.get(memPtr) + 1)));
break;
default:
Line 191 ⟶ 194:
final Point retVal= new Point(place);
switch(dir){
case Direction.RIGHT:
retVal.x++;
break;
case Direction.DOWN:
retVal.y++;
break;
case Direction.LEFT:
retVal.x--;
break;
case Direction.UP:
retVal.y--;
break;
Line 212 ⟶ 215:
return retVal;
}
private enum Direction{
UP,DOWN,LEFT,RIGHT;
}
}</syntaxhighlight>
}
</java>
9,476

edits