Yourlang

From Esolang
Jump to navigation Jump to search

Yourlang is a programming language everyone can edit, created by me, User:LEOMOK. The rules:

  1. You can add your username (but you don't have to.)
  2. You can edit any feature of the language, including adding new instructions, literals or control flow structures.
  3. Instructions can be any Unicode character. Anything as long as it is not inappropiate.
  4. No low-effort instructions, such as "if top of stack is 38470923749 then end program". However, joke ones like "divide the top stack value by 0" are allowed.

The basics

Userlang is a stack-based language. Think of the stack as a giant memory array. Numbers (and other literals) will be pushed onto the stack. Operations will pop a required number of items off the stack, perform the operation and push the result. For example, the program

   50 2 +

will output 52.

Data types

Numbers

Numbers in Yourlang can be integers or floats. They are typed as a string of digits, possibly with a decimal point at the start or middle, and a minus sign at the front. Some valid number literals are:

   123
   0.464
   -125.347

The minus sign is part of a number if it is immediately followed by a digit, else it does subtraction. If you want to push a number after doing subtraction, separate the minus sign and the number with whitespace.

Exponential notation can also be used. The letter e, when directly followed by an integer literal x, pops a value y from the stack and pushes (x * 10 ** y). Examples of valid numbers formed by this construction include:

   6.02e23
   4.135e-15

Since Yourlang is a golfing language, commonly used numbers are given one-char aliases, such as:

   I=1
   D=2
   A=10
   B=11
   Z=12
   W=16
   X=64

You can also use these to indicate a numeric base, like with:

   0w9fca → 40906
   0d1010 → 9
   0xDeMt → 901025

Exponential notation may only be used with decimal literals.

Strings

In Yourlang, strings are delimited by double quotes:

   "This is a string."
   "Hello world!"
   ""

Double quotes and backslashes need to be escaped.

   "print(\"Hello!\")"
   "This is a backslash: \\"

Strings contain one character can be golfed by putting the character before a single quote:

   'a
   '1
   '=

Double quotes and backslashed need not be escaped in single-character strings.

Lists

To write a list, put its contents in square brackets.

   [1 2 3]
   ["Yourlang" "is" "cool"]
   [[0 1 2] [3 4 5]]

Lists can contain any data type, including other arrays. They can contain values of different types.

The opening bracket [ will mark the surrent size of the stack, and the closing bracket ] will merge all stack items above the marked size into an list. This means that you can execute code inside lists:

   [1 2 + 3 4 *]

is the same as

   [3 12]

Lists may also "pull in" arguments from the stack:

   5 6 [+]

will output [11].

Variables

To set the value of a variable, you type .= followed by its name. .$, followed by a variable name, pushes the value of that variable onto the stack.

Variable names can be any sequence of letters or underscores. If you want to put a letter or underscore as an instruction after a variable name, separate them with a space.

Flow control

Flow control statements are closed by a closing brace }. Flow control statements include ? (if statement) and w (while loop).

User-defined functions

To define a function, type e= followed by the name of a function. Function names have the same syntax as variable names. The function definition is ended with a closing brace }.

To run a function, you type e$ followed by the name of the function.

Instructions

In Yourlang, 0, NaN, empty strings are empty lists are considered "falsy", all other values "truthy". Division by zero returns either infinity or NaN, and does not throw an exception. Indexing starts from 0.

Type annotations:

  • num = number
  • str = string
  • lst = list
  • seq = string or list
Stack operations
Instruction Before After Description
$ x Remove the top stack value.
: x x, x Duplicate the top stack value.
@ x, y, z y, z, x Rotate the top three stack values clockwise.
\ x, y y, x Swap the top two stack values.
.: x, n x, x, x, ..., x Pop a value, and duplicate the next stack value that many times.
.@ x, y, z z, x, y Rotate the top three stack values anticlockwise.
.^ x_n, ..., x_2, x_1, n x_n Pop a value, index it into the stack, and push the value at that index.
Useful constants
Instruction Value pushed to stack
A 10
B 11
C -1
D 2
E 0
F Empty string
I 1
L Newline string
U Space string
V 1000
W 16
X 64
Y 100
Z 256
Operators
Instruction Arguments Value pushed to stack
! any x Logical not of x.
# num x A list contaning the positive integers less than x.
seq x Powerset of x.
% num x, num y x mod y, with the sign of y.
lst x, num y Every xth char/item of y.
lst y, num x Every yth char/item of x.
& num x, num y Bitwise and of x and y.
str x, str y Intersection of x and y.
lst x, lst y
( num x a - 1.
seq x Push first char/item of x, then push the rest.
) num x a + 1.
seq x Push last char/item of x, then push the rest.
* num x, num y x * y.
seq x, num y x repeated y times.
num x, seq y y repeated x times.
str x, str y Cartesian product of x and y.
lst x, lst y
+ num x, num y x + y.
str x, str y x concatenated to y.
lst x, lst y
, num x, num y A list of the integers between x (inclusive) and y (exclusive).
- num x, num y x - y.
str x, str y x with all occurrences of y removed.
lst x, lst y x with all occurrences of y, as a sublist, removed.
/ num x, num y x / y.
seq x, num y Split x at every y chars/items.
num x, seq y Split y into x pieces, distributed equally.
If not possible, the last piece has the most items.
< num x, num y 1 if x is less than y, else 0.
str x, str y Compares lexicographically.
lst x, lst y Must be all the same type. Also compares lexicographically.
seq x, num y Take first y chars/items of x.
num x, seq y Take first x chars/items of y.
= any x, any y 1 if x equals y, else 0.
NaN compares equal to NaN.
seq x, num y x[y], wrapping if out of bounds.
If y is negative, index from the end.
num x, seq y y[x].
> num x, num y 1 if x is greater than y, else 0.
str x, str y Compares lexicographically.
lst x, lst y Must be all the same type. Also compares lexicographically.
seq x, num y Drop first y chars/items of x.
num x, seq y Drop first x chars/items of y.
N str x, str y Split x by every occurence of y, dropping empty sections.
lst x, lst y
num x, num y Divmod.
[floor(x / y), x mod y].
O num x Random integer less than x.
seq x Random char/item in x.
P num x Prime factorization of x, as a list.
S seq x Sort x.
T seq x, num y Delete char/item of index y in x.
num x, num y Span.
1 + x - y.
^ num x, num y x ** y.
0 to the power of 0 is 1.
lst x, any y First index of y in x.
str x, str y First index of y in x.
_ num x x ** -1.
seq x Reverse x.
a lst x, any y Append y to x.
b num x, num y The base-y digits of x, in an array.
lst x, num y Interpret x as a base-y number.
lst y, num x Interpret y as a base-x number.
c str x, str y Split x at every occurrence of y.
lst x, lst y Split x at every occrence of y, as a sublist.
num x, num y Integer division. x // y.
g seq x, lst y Select chars/items of x corresponding to indices of y.
h any x Wrap x in an array.
j lst x, str y Join x by y.
str x, lst y All items in y with indices x or greater.
str x, lst y All items in x with indices y or greater.
k seq x Delete last char/item of x.
l seq x Length of x.
num x Decimal logarithm of x.
m lst x, any y 1 if x is a member of y, else 0.
str x, str y 1 if x contains y, else 0.
num x, num y Log base y of x.
n str x x converted to a number, 0 if invalid.
lst x Interpret x as a base-10 number.
num x Truncate x to integer.
q num x Square root of x.
seq x Flatten x.
s any x Convert to string.
For lists, join with no separator.
t lst x, num y, any z x with index y set to z.
lst x, num y, any z Insert x at index y of z.
str x, str y, str z x with every occurence of y replaced by z.
num x, num y, num z Clamp x to be between y and z.
min(y, max(x, z)).
u num x Convert to Unicode character.
str x Convert to list of codepoints.
lst x If all numbers, convert from codepoints to Unicode string.
v num x, num y yth root of x.
seq x, any y Number of times y occurs in x.
str x, str y
x num x, num y Bitwise XOR of x and y.
str x, str y Symmetric difference of x and y.
lst x, lst y
y num x Sign of x.
z num x Absolute value of x.
seq x Transpose rows and columns of x.
| num x, num y Bitwise OR of x and y.
str x, str y Union of x and y.
~ num x Bitwise NOT of x.
lst x Dump the values of x on the stack.
Extended operators
Instruction Arguments Popped values
.! num x x factorial.
seq x Permutations of x, without repeats.
.% num x, num y, num z Modular exponentation.
x ** y mod z, with the sign of z.
.& any x, any y Logical AND of x and y.
.( seq x Minimum value in x.
.) seq x Maximum value in x.
.* num x Product of digits in x.
lst x Product of all elements of x.
.+ num x Sum of digits in x.
lst x Sum of all elements of x.
.- lst x, any y Remove all instances of y (as a list item) in x.
.< num x, num y x shifted left by y bits.
seq x, num y Cyclically rotate x to the left y times.
num x, seq y Cyclically rotate y to the left x times.
.> num x, num y x shifted right by y bits.
seq x, num y Cyclically rotate x to the right y times.
num x, seq y Cyclically rotate y to the right x times.
.? any x, any y, any z y if x is truthy, else z.
.A num x, num y atan2(x, y).
.B num x Convert x to binary string.
.C num x arccos(x).
.E num x ln(x).
seq x Enumerate x.
Returns a 2D list of indices and list elements.
.G num x, num y lcm(x, y).
.H num x Convert x to hexadecimal string.
.M seq x Standard deviation of x.
.O num x Convert x to octal string.
.P seq x Permutations of x, with repeats.
.S num x arcsin(x).
str x Capitalise the first letter of x.
.T str x arctan(x).
str x Capitalise each word in x.
.[ seq x, num y, any z x padded to the left with z up to length y.
.\ seq x, num y, num z x with indices y and z swapped.
.] seq x, num y, any z x padded to the right with z up to length y.
.` seq x Run-length decode x.
.c num x cos(x).
.e num x exp(x).
.g num x, num y gcd(x, y).
.k num x, num y Binomial coefficient. x nCr y.
.l seq x Convert x to lowercase.
num x floor(x).
.m seq x Arithmetic mean of x.
.p num x, num y Count permutations. x nPr y.
.r num x round(x).
.s num x sin(x).
str x Swap the case of each letter in x.
.t num x tan(x).
str x Trim whitespace from x.
.u str x Convert x to uppercase.
num x ceil(x).
.v str x Evaluate x as Yourlang code.
.w seq x Overlapping slices of x.
.x any x, any y Logical XOR of x and y.
.{ num x, num y min(x, y).
str x, str y
lst x, lst y
.| any x, any y Logical OR of x and y.
.} num x, num y max(x, y).
str x, str y
lst x, lst y
.~ seq x Run-length encode x.
em str x, str y List containing every match of regex y in x.
er str x, str y, str z Replace all matches of regex y in with z.
es str x, str y Split x by matches of regex y.
Input and output
Instruction Arguments What it does
i Evaluate a line of input and push it onto the stack.
0 on EOF.
o any x Print x without a trailing newline.
p any x Print x with a trailing newline.
r Push a line of raw input onto the stack.
Single newline on EOF.
.i str x Push a line from file x.
.a str x, any y Convert y to a string, then write it to file x.
You can think of it as "append mode".
.o str x, any y Set the contents in file x to y.
You can think of it as "write mode".
.z Push a list containing the command line arguments.
Control flow
Instruction Arguments Description
? any x If x is truthy, run the codeblock.
; any x Else block for if statements.
w While loop.
Runs the first block while the condition in the second block is true.
d Do-while loop
Same as above, but the first block is run at least once.
f seq x For loop. Iterates through every char/item in x.
num x For loop. Iterates through every positive integer less than x.
.B Break out of loop.
.K Loop continue.
N Push loop counter. Incremented in every iteration of a loop.
Push 0 if outside a loop.
M Push the structure currently being iterated on.
If a number, same as #. If outside a for loop, push an empty list.
.X Exit the program early.
Time
Instruction Arguments Value pushed to stack
et Current time, in format [year month day h min sec weekday timezone].
eT Unix time.
Number of seconds passed since midnight on 1 Jan 1970.
ez num x Sleep for x seconds.
More constants
Instruction Value pushed to stack
eA Uppercase alphabet.
eC Uppercase consonants with Y.
eD Decimal digits 0-9.
eH Uppercase hex digits 0-9, A-F.
eI Positive infinity.
eJ Negative infinity.
eN NaN.
eQ The list ['QWERTYUIOP', 'ASDFGHJKL', 'ZXCVBNM'].
eV Uppercase vowels without Y.
eW Uppercase consonants without Y.
eY Uppercase vowels with Y.
ea Lowercase alphabet.
ec Lowercase consonants with y.
ee Euler's number.
eh Lowercase hex digits 0-9, a-f.
ep Pi.
eq The list ['qwertyuiop', 'asdfghjkl', 'ZXCVzxcvbnm'].
ev Lowercase vowels without y.
ew Lowercase consonants without y.
ey Uppercase vowels with y.
e~ A string containing lyrics of Rick Astley's Never Gonna Give You Up.