NIB (from nibble, meaning 4 bits) is an esoteric programming language that uses instructions encoded as nibbles.
This is a cross-platform C11 interpreter for NIB.
NIB was created by UnexomWid.
The NIB language is licensed under the MIT license.
Note: versions with the suffix R are considered stable releases, while those with the suffix D are considered unstable.
None.
The NIB language is similar to BF (i.e. they share the same instructions).
However, while BF stores instructions as characters, NIB stores them as nibbles.
All nibbles that start with the bit 0
are considered instructions.
The table below contains all NIB instructions.
Instruction | BF Equivalent | Nibble |
---|---|---|
INCREMENT_POINTER | > | 0011 |
DECREMENT_POINTER | < | 0110 |
INCREMENT_VALUE | + | 0010 |
DECREMENT_VALUE | - | 0101 |
WRITE_VALUE | . | 0000 |
READ_VALUE | , | 0111 |
LOOP_START | [ | 0100 |
LOOP_END | ] | 0001 |
Nibbles that start with the bit 1
are ignored, and can used for padding.
One byte stores exactly 2 instructions.
If you have an odd number of instructions, the last nibble should be a padding nibble.
Note: The standard padding nibble is
1000
.
The interpreter is used as follows:
nib FILE [OPTIONS]
Where:
- FILE - The file to interpret
- OPTIONS - Optional interpreter options (see below)
Option | Description | Default |
---|---|---|
-m, --memory-size AMOUNT | The amount of extra memory to allocate for the data array and loops stack when they run out of it, in bytes | 32768 |
-s, --safe | Interprets safely and ignores some invalid instructions (e.g. moving to a negative index) | false |
Note: safe interpretation is slower than unsafe interpretation.
However, it is useful when you need to run a script that may have runtime errors.
By default, unsafe interpretation is used.
nib ./script.nib
# Interprets the script.nib file from the current directory.
nib ./script.nib -s
# Safely interprets the script.nib file from the current directory.
nib ./script.nib -m 16384
# Interprets the script.nib file from the current directory, and uses a memory step size of 16384 bytes.
nib ./script.nib -m 16384 -s
# Safely interprets the script.nib file from the current directory, and uses a memory step size of 16384 bytes.
This interpreter favors speed over memory.
Here are the steps that the interpreter takes:
- The interpreter checks if the input file exists, and reads all of the contents
- Every input byte is split into 2 bytes, the first containing only the left nibble and the second containing only the right nibble
- This results in 2 bytes, which both have the left nibble equal to
0000
and the right one equal to an instruction or padding nibble
- This results in 2 bytes, which both have the left nibble equal to
- The execution of the script starts, and all of the instructions are interpreted
Note: Each input byte is split into 2 bytes in order to save time.
However, this means that the interpreter uses two times more memory.
Not splitting the input bytes uses less memory but is slower, as two masks need to be used to extract the nibbles on the fly.
Each interpreter implementation can choose whether or not to favor time over memory.