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}}
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 [
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:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
Line 47 ⟶ 49:
return code;
}
}</syntaxhighlight>
The code pointer class:
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 Random rand = new Random();
▲ final String instChars= "><.,/\\+%-#@$!?";//valid chars
ArrayList<String> code;//code array
ArrayList<Character> mem;//memory space
int memPtr;//memory pointer
Point place;//code pointer
LinkedList<Point> pStack;//code pointer stack
LinkedList<
//input stream
BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
public CodePtr(
this.code= code;
dir= Direction.RIGHT;
this.place= place;
mem = new ArrayList<Character>();
mem.add('\0');//initial memory
memPtr= 0;
dStack= new LinkedList<
pStack= new LinkedList<Point>();
}
public CodePtr(
//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());
}
private boolean execute(
//ignore char and keep going
if(!instChars.contains("" + inst)) return true;
Line 102 ⟶ 103:
try{
int in;
while((in= input.read()) ==
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){
place= oldPlace;
dir= oldDir;
Line 183 ⟶ 184:
break;
case '%':
break;
▲ mem.set(memPtr, (char)(Math.random() * 256));
default:
}
Line 208 ⟶ 209:
}
if(retVal.x<0 || retVal.y<0 ||
System.err.println("Code pointer has left the code space");
System.exit(-1);
Line 215:
return retVal;
}
private enum Direction{
UP,DOWN,LEFT,RIGHT;
}
}</syntaxhighlight>
|