Snek

From Esolang
Jump to navigation Jump to search

A stack-based language made almost entirely by the following characters: | _ / \. An instruction pointer moves along the "paths" made by these characters and commands are encoded as different numbers of each character in succession. Snek is so-called because of the tendencies for the program to look a bit like a snake with a long body and a @ as a head.

A single character is a noop, therefore you can move the instruction pointer without performing any operations.

List of Commands

 ______ = push number of _s - 1
 
|
| = 2: add, 3: sub, 4: mul, 5: push 0, 6: div
|
 
\
 \ = 2; dup, 3: swap, 4: get char input, 5: print as number, 6: print as ascii,
  \
 
  /
 / = 2: ==, 3: >, 4: <, 5: >= 6: <=
/
@ = ends program

Where the numbers represent the number of characters in a row.

Control Flow

When the instruction pointer comes across a fork in the path, it will choose the right path if the top value on the stack is zero and the left path if the value is one. Left and right are relative to the fork pointing upwards, like so:

left right
   \ /
    |

And likewise


___/ left
   \ right

Ambiguity in latyout

The ascii-art style of this language creates some ambiguity. Since the instruction pointer can only turn 45 degrees at once, examples such as this arise:

 _     _
/     /
|    |

Both of these are valid in Snek, however which one is picked if there are multiple options is not defined. Take this example:

  __
 /__
//
|

This is undefined behaviour, it is implementation specific which path the instruction pointer will take.

Examples

Truth machine

This program takes a single character from the user, if it is 1 then it prints 1 forever, else it exits. This is an example of how loops and control flow can be used.

_
 \
  \              ____   _
   \       _____/    \ / \        _@
    \_____/          | |  \     _/
                     | |   \   / \__
                     | |    |  | /  \
                     | |    |  | |   \
                     \_/    |  |  \   \
                            \__/   |   \
                                    \   \
                                     |  |
                                      \_/

Adder

This program takes 2 single digit numbers from the user, adds them and prints the result.

_
 \
  \
   \       _____
    \_____/     \
                |
    _           |
   / \_         |
   |   \        |
   \    |       \____
    \   |            \
     \  |            |
     |  \            |
     |   |           |
     |   |           |
     |   \            \
     \_   \            \
       \   \            \
       |    \            |
       |     \           | 
       |      @          |
       |                 \
       \____              \
            \              \
             |              \
             /               |
            |               /
            \_____       _  |
                  \_____/ \_/

Reference Interpreter

The reference interpreter is a work in progress. Knowing my track record with finishing projects, it may remain that way for a while. Pull requests are welcome.