Skip to content

Instantly share code, notes, and snippets.

@durandj
Last active August 29, 2015 13:57
Show Gist options
  • Save durandj/9799596 to your computer and use it in GitHub Desktop.
Save durandj/9799596 to your computer and use it in GitHub Desktop.
SNUSP Interpreter Plan

SNUSP Interpreter

What is SNUSP

SNUSP is an esoteric language that was created by Daniel Brockman in 2003. It's a fun language that is meant to be very simple and provide a challenge to code in. The language is a two dimensional language and its easiest to think of program flow being like a laser traveling through a set of mirrors and lenses. Along the path the laser will increase or decrease stored values and in some versions of the language can be split into multiple threads of execution.

Goal

  1. Create a SNUSP interpreter in the following languages:
  • C
  • C++
  • C#
  • Java
  • JavaScript
    • For added fun and as a bonus, implement a website that can show a SNUSP program and users can write SNUSP code.
  • Perl
  • Python
  1. The Core, Modular, and Bloated SNUSP should be supported in all languages.
  2. The interpreter should accept a single file as input. The file should container a SNUSP program and any output from the program should go directly to sysout. All errors returned by the interpreter should be output on syserr.
  3. A flag can be passed in on the command line specifying interactive mode. In interactive mode the program is shown on screen and animated to show the progress of execution. The user can step through the program and continue execution. Optionally look at implementing breakpoints (could be given an X, Y coordinate of a character)

Development Plan

Core SNUSP

Notes

While Core SNUSP is intended to be minimal it should still lay the foundations for the extensions and not require core modifications.

All aspects of Core SNUSP should still be relevant and present in all extensions of SNUSP.

Objects

Interpreter

The core part of the program. The interpreter reads the given file and executes the statements. The interpreter is responsible for tracking all threads of execution. The interpreter is also responsible for setting up the data space that the program will work in. The interpreter should maintain a registry of operators that are supported and be able to call them by their code symbol.

The interpreter will look for the first occurrence of the $ symbol. If no $ is found then execution begins at the first character in the file.

The interpreter will stop when no threads are running.

The interpreter is allowed to execute the threads in an arbitrary order but for simplicity implement the interpreter so that threads are executed tick by tick in order that they were created. Each thread will execute the current operator that it is on and then the next thread is run.

ControlOrientation

An enum type object that stores the direction of the control flow. Possible values are:

  • UP - Move instruction pointer up
  • DOWN - Move instruction pointer down
  • LEFT - Move instruction pointer left
  • RIGHT - Move instruction pointer right
Thread

Stores the position in the file of the instruction pointer. The thread will also need to contain relevant context about execution such as the direction of the current execution flow and a data pointer. Threads should also maintain their own call stack to stay compliant with Modular SNUSP. Execution of a thread continues until it leaves the code space at which point it is terminated.

Operator

Operators should be written as some kind of object so as to allow for easy extension. New operators can be written and registered.

All operators should have a symbol that represents them in a SNUSP program.

When an operator is invoked it will be given the thread of execution and the interpreter object.

Operators are allowed to modify the instruction pointer and data pointer of the thread.

Error Handling

In the event that something happens in the program the interpreter is allowed to terminate and when it does terminate it should return a useful error message for debugging the problem. Likely error conditions are:

  • General IO problem when reading in program file
  • Problem parsing/reading the program
  • Unknown operator found
  • Attempting to access data from a data value that has not been set yet (incrementing the data pointer too far)

These conditions should all be handled and the interpreter being properly terminated. They should not cause an exception in the interpreter that is returned to the user.

Implemented Operators

The following operators will need to be implemented:

  • < LEFT - Move the data pointer left
  • RIGHT - Move the data pointer right

    • INCR - Increment the current data value by 1
    • DECR - Decrements the current data value by 1
  • , READ - Read a byte into the current data cell from sysin
  • . WRITE - Write a byte from the current data cell to sysout
  • \ LURD - Move instruction pointer orientation according to the following rules:
    • Up if going left
    • Right if going down
    • Down if going right
    • Left if going up
  • / RULD - Move instruction pointer orientation according to the following rules:
    • Up if going right
    • Left if going down
    • Down if going left
    • Right if going up
  • ! SKIP - Move the instruction pointer forward one step
  • ? SKIPZ _ Move the instruction pointer forward one step if the current memory cell is zero

Modular SNUSP

Notes

Modular SNUSP adds a call stack implementation to allow for function type syntax.

Implemented Operators

The following operators will need to be implemented:

  • @ ENTER - Push the current instruction pointer and orientation onto the stack
  • LEAVE - Pop the call stack and move the instruction pointer

Bloated SNUSP

Notes

Changes the data buffer into a 2 dimensional space as well as allowing for threaded code. Child threads have an empty call stack but the instruction pointer and data pointer are copied from the parent.

Implemented Operators

  • : UP - Move the data pointer up
  • ; DOWN - Move the data pointer down
  • & SPLIT - Spawn a new thread and increment the parent thread's instruction pointer
  • % RAND - Set the current data cell to a random value between 0 and the current value of the cell inclusively

Testing

Testing will be done in phases to confirm proper functionality of different aspects of the system.

Operators

Test each operator individually to confirm that each can perform the desired action with the appropriate side effects.

Sample Programs

Create sample programs that will be run and validate the output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment