Skip to content

Commit

Permalink
Version 1.0 of the SNUSP interpreter. It correctly interprets Modular…
Browse files Browse the repository at this point in the history
… SNUSP.
  • Loading branch information
akm committed Mar 16, 2007
0 parents commit 802b320
Show file tree
Hide file tree
Showing 12 changed files with 498 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3.8.1"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>SNUSP Interpreter</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
19 changes: 19 additions & 0 deletions resources/add.snusp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
ATOI ITOA
/!/@@@-@-----# /=!/=++++++++\
| | /--------/ | |/++++++++/
, , \--------\ | |\++++++++\
| | /--------/ | |/++++++++/
| | \--------\ | |\++++++++\
| > #--------/ | |#++++++++/
| | > <
| | | |
$=@/@/==!/==?\=<\/@/.@/.#
| - ||
\>+</ |\======================\
| |
| |
/====>+<=======!\==========================\
| | |
| /+=!/+=!/+=!/+=!/+=!/+=!/+=!/+=!/+==/ |
| | | | | | | | | | ! |
\==-\!?-\!?-\!?-\!?-\!?-\!?-\!?-\!?-\!?-\!?/
30 changes: 30 additions & 0 deletions src/org/malloys/akm/snusp/DeadThreadException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.malloys.akm.snusp;

public class DeadThreadException extends RuntimeException
{

public DeadThreadException()
{
super();
// TODO Auto-generated constructor stub
}

public DeadThreadException(String message, Throwable cause)
{
super(message, cause);
// TODO Auto-generated constructor stub
}

public DeadThreadException(String message)
{
super(message);
// TODO Auto-generated constructor stub
}

public DeadThreadException(Throwable cause)
{
super(cause);
// TODO Auto-generated constructor stub
}

}
9 changes: 9 additions & 0 deletions src/org/malloys/akm/snusp/Direction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.malloys.akm.snusp;

public enum Direction
{
UP,
DOWN,
LEFT,
RIGHT,
}
40 changes: 40 additions & 0 deletions src/org/malloys/akm/snusp/SnuspInstruction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.malloys.akm.snusp;

public enum SnuspInstruction
{
NOOP, LEFT, RIGHT, ADD, SUB, LURD, DLUR, SKIP, SKIP_ZERO, CALL, RETURN, BEGIN, READ, WRITE;

public static SnuspInstruction get(char c)
{
switch (c) {
case '<':
return LEFT;
case '>':
return RIGHT;
case '+':
return ADD;
case '-':
return SUB;
case '\\':
return LURD;
case '/':
return DLUR;
case '!':
return SKIP;
case '?':
return SKIP_ZERO;
case '@':
return CALL;
case '#':
return RETURN;
case '$':
return BEGIN;
case ',':
return READ;
case '.':
return WRITE;
default:
return NOOP;
}
}
}
83 changes: 83 additions & 0 deletions src/org/malloys/akm/snusp/SnuspInstructionPointer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.malloys.akm.snusp;

import org.malloys.akm.util.Pair;

public class SnuspInstructionPointer implements Cloneable
{
public SnuspInstructionPointer(Direction direction,
Pair<Integer, Integer> position)
{
this.direction = direction;
this.position = position;
}

public void doLurd()
{
direction =
direction == Direction.LEFT ? Direction.UP :
direction == Direction.UP ? Direction.LEFT :
direction == Direction.DOWN ? Direction.RIGHT :
Direction.DOWN;
}

public void doDlur()
{
direction =
direction == Direction.LEFT ? Direction.DOWN :
direction == Direction.DOWN ? Direction.LEFT :
direction == Direction.UP ? Direction.RIGHT :
Direction.UP;
}

public SnuspInstructionPointer clone()
{
try {
SnuspInstructionPointer result = (SnuspInstructionPointer)super.clone();
result.position = position.clone();
return result;
} catch (CloneNotSupportedException e) {
return null;
}
}

public Direction getDirection()
{
return direction;
}

public Pair<Integer, Integer> getPosition()
{
return position;
}

public void setDirection(Direction direction)
{
this.direction = direction;
}

public void setPosition(Pair<Integer, Integer> position)
{
this.position = position;
}

/**
* Move the instruction pointer one unit in the current direction, regardless
* of the content of the current instruction. Should only be called after the
* instruction has been handled.
*/
public void advance()
{
if (direction == Direction.UP)
position.y--;
else if (direction == Direction.LEFT)
position.x--;
else if (direction == Direction.RIGHT)
position.x++;
else if (direction == Direction.DOWN)
position.y++;
}

Direction direction;

Pair<Integer, Integer> position;
}
20 changes: 20 additions & 0 deletions src/org/malloys/akm/snusp/SnuspLauncher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.malloys.akm.snusp;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class SnuspLauncher
{
/**
* @param args
* @throws
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
SnuspProgram program = new SnuspProgram(new FileInputStream(args[0]));
program.run();
}

}
Loading

0 comments on commit 802b320

Please sign in to comment.