Expressions

From Esolang
Jump to navigation Jump to search

Expressions is an esoteric programming language created by User:DigitalDetective47 based around re‐defining the results of calculations. It has a syntax similar to that of non‐esoteric programming languages.

Program structure

Each command within the program must start with #<line number>. All line numbers must be non‐negative integers. A line that does not start with a # is considered a continuation of the previous line. All ASCII whitespace is ignored except for a space after the line number, or whitespace characters inside strings; #s are used to seperate commands. The program counter starts at 0. At each “tick”, the instruction with the line number equal to the program counter is executed, and 1 is added to it after each instruction (PC = PC + 1). If the program counter is larger than the greatest line number, the program terminates. If there is no instruction with the value of the program counter, it is simply incremented without executing an instruction. If there are multiple instructions with line numbers matching the program counter, one is selected at random to be executed.

Commands

There are two data types in Expressions: int and str. To specify an integer, type its digit representation. For negative integers, simply place the minus sign before the digits. Integers in Expressions can be positive, negative, or zero, and can be of unbounded magnitude. To specify a string, enclose the text in double quotes ("). Line breaks within string literals are not allowed; use \n instead. If two string literals are placed next to each other, they are merged into a single string literal at compile time (Components are not overwritten). This feature is intended only to allow long strings to be broken across multiple lines. Escape sequences cannot be split between multiple segments of strings. Only ASCII characters are allowed. There are five allowed escape codes for strings. \n is a newline (ASCII 10), \" is a quotation mark, \h is a hash sign, \\ is a backslash, and \x<2-digit hex code> is the hex code specified, as a character. \x escapes greater than 127 are considered illegal escape sequences, and will cause a syntax error. The most important operation in Expressions is the assignment operator (=). It can link either a value or calculation to a value. For example, 5 = 7 would make the program automatically convert any 5s it creates into 7s. As an example of assigning calculations, 9 + 10 = 21 makes the program return 21 any time it evaluates 9 + 10. Note that in this case, 10 + 9 would still return 19, as all calculation overrides are order‐dependent. Value references always take priority over calculation references. For example, if the program executes 5 + 4 = 8, and then 4 = 9, 5 + 4 and 5 + 9 will both return 14. If 5 + 4 = 8 is then executed again, 5 + 4 and 5 + 9 will now return 8. As stated above, the incrementation of the program counter specifically adds 1 on the right. Here is a list of binary operators:

List of binary operators
Symbol Meaning Input types Output type
< If the left number is less than the right number, return 1. Otherwise, return 0. int, int int
> If the left number is greater than the right number, return 1. Otherwise, return 0. int, int int
+ Return the sum of the two numbers. int, int int
+ Concatenate the two strings. str, str str
- Return the difference of the two numbers. int, int int
* Return the product of the two numbers. int, int int
* Repeat the string the number of times specified by the number. str, int str
/ Return the quotient of the two numbers, rounded towards 0. Crashes if the denominator is zero. int, int int
% Return the modulo of the two numbers with the same sign as /. Crashes if the denominator is zero. int, int int
^ Return the exponentiation of the two numbers, rounded towards 0. Crashes if zero is raised to a non‐positive power. int, int int

Operator precedence is handled using PEMDAS, with modulo having the same precedence as multiplication and division, and comparisons having a lower precedence than addition. Note however that a negative sign binds to its integer literal, so -2 ^ 2 defaults to 4, not -4. Arithmetic‐ Parentheses can be used to manipulate the order of operator evaluation. There are also a few named functions that use parentheses to take arguments. Here is a list of them:

List of named functions
Name Meaning Input types Output type
char Return a one‐character string with the character code given by the input number. If the given number is not in the range from 0 to 127, it is wrapped to be inside that range. int str
code Return the character code for the given one‐character string. Crashes if the string is longer than one character. str int
input(int) Get an integer from the user and return it. None int
input(str) Get a string from the user and return it. The trailing newline is stripped. None str
print Print the given integer in base 10. int None
print Print the given string. A trailing newline is not automatically added. str None
substr Return a one‐character string with the character in the string at the position of the integer (Starts at 0). str, int str

Examples

Hello, world!

#0 print("Hello, world!")

Truth-machine

#0 "var/userInput" = input(int)
#1 2 + 1 = 3 - "var/userInput"
#2 print("var/userInput")

Cat program

#0 1 + 1 = 1
#1 print(input(str) + "\n")

99 bottles of beer

#0 7 + 1 = 8 * (-99 * -1 < 4)
#1 print(-99 * -1)
#2 print(" bottles of beer on the wall,\n")
#3 print(-99 * -1)
#4 print(" bottles of beer.\nTake one down, pass it around,\n")
#5 -99 = -99 + 1
#6 print(-99)
#7 print(" bottles of beer on the wall.\n\n")
#8 print("2 bottles of beer on the wall,\n2 bottles of beer.\n"
"Take one down, pass it around,\n1 bottle of beer on the wall.\n\n"
"1 bottle of beer on the wall,\n1 bottle of beer.\n"
"Take one down, pass it around,\nNo bottles of beer on the wall.\n\n"
"No bottles of beer on the wall,\nNo bottles of beer.\n"
"Go to the store, buy some more,\n99 bottles of beer on the wall.")