Execute SNUSP/Java: Difference between revisions

m
Fixed syntax highlighting.
(Added % command)
m (Fixed syntax highlighting.)
 
(13 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 charactervalue (code 0 through 255the 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;
}
}</syntaxhighlight>
 
The code pointer class:
}</java>
The<syntaxhighlight code pointer class:<javalang="java5">import java.awt.Point;
import java.io.BufferedReader;
import java.io.IOException;
Line 55 ⟶ 57:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Random;
 
public class CodePtr{
static final String instChars= "><.,/\\+%-#@$%!?";//valid chars
final int LEFT= 2, RIGHT= 0, UP= 3, DOWN= 1;//directions
static final Random rand = new Random();
final String instChars= "><.,/\\+%-#@$!?";//valid chars
ArrayList<String> code;//code array
intDirection dir;//current direction
ArrayList<Character> mem;//memory space
int memPtr;//memory pointer
Point place;//code pointer
LinkedList<Point> pStack;//code pointer stack
LinkedList<IntegerDirection> dStack;//direction stack
//input stream
BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
 
public CodePtr(final ArrayList<String> code, final Point place){
this.code= code;
dir= Direction.RIGHT;
this.place= place;
mem = new ArrayList<Character>();
mem.add('\0');//initial memory
memPtr= 0;
dStack= new LinkedList<IntegerDirection>();
pStack= new LinkedList<Point>();
}
 
public CodePtr(final Point place, final intDirection dir){
//This constructor is left over from attempts at '&'
this.dir= dir;
this.place= place;
Line 86 ⟶ 90:
 
public void run(){
for(;execute(code.get(place.y).charAt(place.x));place= moveCP());//move past $
while(execute(code.get(place.y).charAt(place.x))){
place= moveCP();
}
}
 
private boolean execute(final char inst){
//ignore char and keep going
if(!instChars.contains("" + inst)) return true;
Line 102 ⟶ 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 112 ⟶ 113:
break;
case '?'://conditional skip
if(mem.get(memPtr) == '\0') place= moveCP();
break;
case '>'://move pointer right
Line 130 ⟶ 131:
switch(dir){
case RIGHT:
dir= Direction.UP;
break;
case DOWN:
dir= Direction.LEFT;
break;
case LEFT:
dir= Direction.DOWN;
break;
case UP:
dir= Direction.RIGHT;
break;
default:
Line 147 ⟶ 148:
switch(dir){
case RIGHT:
dir= Direction.DOWN;
break;
case DOWN:
dir= Direction.RIGHT;
break;
case LEFT:
dir= Direction.UP;
break;
case UP:
dir= Direction.LEFT;
break;
default:
Line 169 ⟶ 170:
case '#'://pop
if(dStack.size() > 0){
final intDirection oldDir= dStack.pop();
final Point oldPlace= pStack.pop();
place= oldPlace;
dir= oldDir;
Line 183 ⟶ 184:
break;
case '%':
mem.set(memPtr, (char)rand.nextInt(Mathmem.randomget(memPtr) *+ 2561));
//use a random regular ASCII character
break;
mem.set(memPtr, (char)(Math.random() * 256));
default:
}
Line 208 ⟶ 209:
}
if(retVal.x<0 || retVal.y<0 ||
retVal.y >= code.size() || retVal.x >= code.get(0).length()){
retVal.x >= code.get(0).length()){
System.err.println("Code pointer has left the code space");
System.exit(-1);
Line 215:
return retVal;
}
private enum Direction{
}</java>
UP,DOWN,LEFT,RIGHT;
}
}</syntaxhighlight>
9,476

edits