Ly

Ly is a stack-based esoteric programming language inspired by ><> created by User:LyricLy in 2017.
==Concepts==
Ly is a stack-based language, so all operations are performed on a stack. However, the program operates on not just one stack, but on an infinite strip of stacks. It also has a backup cell that can store one value for later use. Execution is mostly linear, with the exception of the [ and ] instructions.
===Code execution===
In Ly, execution is mostly linear, with a single execution pointer that performs the action dictated by the character it is currently on, before moving to the right.
However, there are two instructions that can move the pointer: [ and ], which check if the top value on the stack is 0, and jump accordingly between each other. This allows for looping and conditional behavior.
===The stacks===
In Ly, a stack is a simple one-dimensional list of integers and floats. The language operates on an infinite list of stacks.
There are several operations for stacks such as arithmetic, reversal, popping and I/O, but values in a stack below the two highest ones cannot be accessed without reversing the stack (except with the I instruction). This can prove problematic in some cases where you need a place to store values. That's where other stacks come in. The < and > operators can be used to change stacks at will, allowing greater control over your programs. The < and > operators also complete the language's set of instructions to be able to emulate brainfuck, proving Turing completion of the language.
===The backup cell===
The backup cell is accessible all the time no matter what the current state of the stacks are. The l command will copy (not move) the current top value on the stack to the backup cell, and the s will copy it back. This can be incredibly useful for storing a value for later use.
===Input/output===
Ly has four I/O commands, two for input(i, n) and two for output. (o, u)
The string I/O commands (i and o) will input and output characters as ASCII codes.
The number I/O commands (n and u) will input and output numbers directly. n takes input as space-separated digits, to make the job of taking input as a list of numbers much easier.
===Functions===
Ly also has the ability to create functions, pieces of reusable code that can be called at will.
Functions are ran independently from the main program, allowing great flexibility with programs and even code that can be reused between programs.
Functions, like with many other languages, can also be called with functions, allowing them to be dynamic and powerful.
===Errors===
The official Ly interpreter will throw an error if any of the following things occurs:
* Invalid arithmetic operations, such as dividing by zero
* Trying to pop from the stack when it is empty (and implicit input fails)
* The program has unbalanced brackets
* Trying to load from the backup cell when it is empty
* Receiving a non-integer as input with n (or implicit input)
Error messages look like so:
 Error occurred at program index 6 (zero-indexed, includes comments)
 EmptyStackError: cannot pop from an empty stack
===Implicit I/O===
Implicit input and output is an optional feature of Ly mainly aimed towards code golfers.
Implicit input will occur whenever an instruction attempts to pop from the stack when it is empty. 
Instead of raising an error, the interpreter will read a number (character I/O is not supported implicitly) from STDIN and push it to the stack. 
This is slightly different from the n instruction in that it reads the input backwards for convenience. For example, an input of 43 54 will return [43, 54] and not [54, 43]. 
If there is no more input to take implicitly (EOF is reached or an empty line of input is given), 0 will be used instead.
Implicit input is never pushed to the stack; it is delivered straight to instructions that attempt to pop from the stack.
If an invalid input is passed implicitly, an EmptyStackError is raised.
In addition to normal implicit input, instructions that perform an action on the entire stack will implicitly dump STDIN (take lines of input until EOF/empty line) onto the stack.
Implicit output occurs at the end of a program if that program is still running. 
Thus, ending execution with the ; instruction before the end of the program will disable implicit output. 
Implicit output will print the entire stack that is under the stack pointer, with a space in between numbers.
==Commands==
This language is still under (very inactive) development, and these commands are subject to change.
In these explanations, "the stack" refers to the current stack.
===Literals and operators===
0-9 	Push the corresponding value onto the stack. You can push multi-digit numbers if you surround them in parentheses.
+ - * / %	Addition, subtraction, multiplication, division and modulo, respectively. Pop x and y off the stack, and push y operator x. Division is float division (meaning 94/u outputs 2.25 and not 2). Division by 0 raises an error.
" 	String literals; push every character found to the stack until a closing quote is hit.
' 	Character literals; push the next character to the stack.
? 	Pop x and y off the stack, and push a random integer between y and x.
= G L 	Equals, greater than and less than, respectively. Pop a number from the stack, and push 1 if the top value on the stack operator that number and 0 otherwise.
c 	Pop a number from the stack, and push the number of digits in that number.
S 	Pop a number from the stack, and push each of its digits individually. For example: [431] would become [4, 3, 1].
J 	Pop the stack, and push all of the numbers in it as a single number. For example: [4, 3, 1] would become [431].
R 	Pop X and Y from the stack, and push an inclusive range of values from Y to X to the stack.
` 	Pop a value from the stack, and push that value plus one.
, 	Pop a value from the stack, and push that value minus one.
===Stack manipulation===
:	Duplicate the top value on the stack.
p	Pop the top value of the stack.
f	Swap the top two values on the stack.
r	Reverse the current stack.
!	Pop a number from the stack. If it is 0, push 1. Otherwise, push 0.
N	Pop a number from the stack and reverse its sign, e.g. 6 becomes -6 and vice-versa.
<	Move left on the stack strip.
>	Move right on the stack strip.
y	Push the length of the stack to the stack.
a	Sort the stack from lowest to highest.
I	Pop a value off the stack, find the value on the stack with that index (Ly uses 0-indexing), and push it to the stack. If the index is out of range, an error is raised. e.g. [1, 2, 6, 4, 2] becomes [1, 2, 6, 4, 6].
~	Pop a value off the stack. If that value can be found among the remaining stack values, push 1. Otherwise, push 0.
===Input/output===
o	Pop the top of the stack and output the character with that ASCII code point.
u	Pop the top of the stack and output it as a number.
i	Read a line of input and push each character to the stack separately. Ly does not have strings, so input is taken as ASCII code points instead. e.g. abc is 97, 98, 99
n	Read one line of input input and push it to the stack as an integer. If an invalid input is given, an error is raised.
===Loops===
[	If the top value of the stack is 0 or the stack is empty, jump to the corresponding left bracket. Otherwise, do nothing.
]	If the top value of the stack is not 0 and the stack is not empty, jump to the corresponding right bracket. Otherwise, do nothing.
$	Break out of the current loop and move the execution pointer to the instruction immediately after the end of the loop.
===Miscellaneous===
s	Copy the top value on the stack to the backup cell. See The backup cell.
l	Push the current value of the backup cell.
;	End execution. This instruction is only required to end execution early; it is not required to add it at the end of every program.
===Modified commands===
In Ly, commands can be modified using the & symbol so that their effect will take place on the entire stack, as opposed to only the top value.
&+	Push the sum of the stack. The stack will not be popped in this process.
&n	Take input and write it to the stack, as a number, until EOF is reached or an empty input is given.
&o	Pop the entire stack, and print it in the correct order.
&u	Pop the entire stack, and print it, as space-separated numbers, in the correct order.
&p	Pop the entire stack.
&:	Duplicate the entire stack on top of itself.
&s	Back up the whole stack to the backup cell. The stack will not be popped in this process. A backed up stack can be loaded as normal with the l command.
==Functions==
A major feature of the Ly language is its functions.
===Defining a function===
Defining a function is simple:
 <name>{}
where <name> is a single character denoting the name of your function, and is the body of your function.
Inside a function, code is executed just like a completely separate Ly program. The only connection between a function and the main program is the input and output commands.
Instead of performing I/O from STDIN and STDOUT, a function manages I/O through the stack of the parent program. 
An example function looks like this:
 P{ns::[1-s%![l%!u;]p:l]}
This is a simple prime number checker program wrapped up in a function P that takes a single number as a parameter and pushes either 0 or 1 to the stack depending on if the number passed was a prime or not.
NOTE: If you give a function the same name as an existing function or built-in instruction, the old function/instruction will be overwritten.
===Calling functions===
Calling functions is just as simple as creating them:
 <name>
where <name> again is the name of the function.
The function is run like a normal program, with three differences:
* Whenever the function outputs, it is pushed to the stack of the parent program
* Whenever the function takes input, it is popped from the stack of the parent program
* There is no implicit input. When the parent stack is empty, taking input inside the function will yield 0.
Here is an example call of the prime number function:
 P
This will pop a number from the stack, and push 1 if it was prime and 0 otherwise.
==Examples==
All examples are using the official ly.py interpreter.
===Hello, world!===
 "Hello, world!"r[o]
 $ ./ly.py helloworld.ly
 Hello, world!
===Prime number checker===
Takes in a number as input, and outputs 1 if it is prime and 0 otherwise.
 n:s[:2-![1u;]p1-:lf%![0u;]p]
 $ ./ly.py prime.ly
 Enter program input: 7
 1
 $ ./ly.py prime.ly
 Enter program input: 8
 0
===Truth-machine===
 n:u![;]1[u1]
 $ ./ly.py truth.ly
 Enter program input: 0
 0
 $ ./ly.py truth.ly
 Enter program input: 1
 1111111111111111111111111111111111111111111111111111111111111111111111111111111...
===Fibonacci sequence===
 01[s:u+lr]
 $ ./ly.py helloworld.ly -t 0.01
 1123581321...
With newlines:
 01[s:u+52*olr]
 $ ./ly.py helloworld.ly -t 0.01
 1
 1
 2
 3
 5
 8
 13
 21
 ...
==Interpreters==
===ly.py===
The official interpreter. Hosted here.
===Online ly.py===
There are two online versions of the official ly.py interpreter currently: here and here. The latter is recommended, as it is more fully-featured and more likely to remain supported.
===crystaLyne===
Ly interpreter written by Helloman892 in Crystal. Hosted here
Category:Languages Category:Stack-based Category:2017 Category:Implemented