Nix

From Esolang
Jump to navigation Jump to search

Nix is a one-dimensional esoteric language created by Quiqucode. It operates on a single top-access stack that can be manipulated via certain commands. Nix attempts to bridge the gap between linear and directional esoteric languages such as Befunge and ><> (fish). The official interpreter is written in Python, and can be found in this GitHub repository. Despite its relatively minimalistic set of commands, the author presumes that Nix could be used for a number of code golf challenges.

Basic properties

  • Memory system: Top access-only stack
  • Program format: Nested list of toroidal arrays
  • Usage: Through interpreter
  • Commands: Single character
  • Execution order: Directional

Overview

Nix programs are contained in UTF-8 encoded text files with the optional extension of .nx. Each character in a Nix file is either a command, a string capture, or a no-op. Nix programs are unable to reference other files, and in most senses, are entirely self contained.

  • Each command modifies the stack or the current direction/location of the instruction pointer (ip).
  • No-ops consist of any character that does not correspond to any commands and is not between quotes.
  • String captures are characters that are encountered in between two single quotes. Their ascii values are appended in the order they are encountered.

Nix code is parsed into a nested list of toroidal arrays. Whenever the instruction pointer encounters the leftmost or rightmost edge of the line, it wraps around to the other side and resumes execution. This is useful when attempting to capture program characters using string toggling.

Concept

Execution

Nix is a directional language, meaning that code is not always executed in the order the user would normally read it. One key piece of terminology needed when discussing directional languages is the instruction pointer, also referred to as the ip. The ip can be described as a tuple that contains both its location as well as the direction it is traveling in the current program.

In Nix the ip begins on the first character of the first line. Each time the interpreter reads a character and executes a command, the instruction pointer moves. If the current direction is right, then the ip moves right; if left, it moves left. The interpreter references the current character under the pointer in order to determine which command should be executed next.

Strings

Since each character in Nix acts as a single command, no tokenization is used to parse the language. Whenever the interpreter encounters a single quote, it will turn string mode on or off. If string mode is off, code execution continues as usual. However, if string mode is on, all normal operations cease until it is toggled to the off state. During this time, the interpreter appends the ascii value of the current character under the instruction pointer to the stack.

Methods

Methods are not a certain type of code, but more of a way to execute said code. For example, in a certain program it may be necessary to print all values from the stack. If printing only occurs once in the program, it makes sense to save space by adding the function to the program body. However, if I need to print repeatedly, it would be much simpler to put the function on its own line and call it using the call #|, where # is the line with the function.

Commands

Stack manipulation

  • : - Duplicates top of stack.
  • $ - Pops and discard top of stack.
  • [ - Shifts stack 1 to the left.
  • ] - Shifts stack 1 to the right.
  • r - Reverses entire stack.
  • f - Swaps the top two values of stack.
  • g - Pushes ascii value of character with index of popped value.

Movement commands

  • < - Sets pointer direction to the left.
  • > - Sets pointer direction to the right.
  • ~ - Reverses direction if popped value is not equal to zero.
  • @ - Skip the following command unconditionally.
  • # - Skip the following command if popped value equals zero.
  • ( - Jump to the matching ) in the rightwards direction.
  • ) - Jump to the matching ( in the leftwards direction.
  • | - Execute the line with index of popped value as a method, then continue.

Input and output

  • o - Output popped value as character.
  • n - Output popped value as integer.
  • i - Input integer and push to stack.
  • s - Input string and push value of each character to stack.

Literals and operators

  • 0-9 - Push corresponding number to stack.
  • + - * / % - Pop two values, perform operation and push result.

Example programs

"Hello, World!"

'Hello, World!'0r>:@o~x

Cat program

s0r>:@o~x

Standard Quine

'0r94*3+>:@o~x

Related languages

External resources

  • GitHub repository - includes reference implementation in Python and example programs.