Calculon
Calculon is a new, esoteric language written by User:Grulf. It is purely mathematical and only allows input and output of numeric values, currently with only 4 digits after the comma.
Language overview
Calculon's syntax is best described with an example program:
5 get sub peek
This program takes a number from the input, call it n, performs the calculation n - 5 and prints it. As you can see, commands are separated by whitespace. Any numerical value written will get pushed onto the stack, in this case "5". The command "get" requires the user to input any number and pushes it onto the stack. Then, the command "sub" pops two values from the stack, following the LIFO-principle and therefore subtracting the bottom value from the top value of the stack, and pushes the resulting value onto it, which the command "peek" prints out without modifying anything.
The following commands are currently implemented in the language (which is still in progress):
Command | Description |
---|---|
add | Pops two values from the stack and adds them, pushing the resulting value onto the stack. |
sub | Pops two values from the stack and subtracts the bottom value from the top value, pushing the resulting value onto the stack. |
mul | Pops two values from the stack and multiplies them, pushing the resulting value onto the stack. |
div | Pops two values from the stack and divides the top value by the bottom value, pushing the resulting value onto the stack. |
sqrt | Pops a value from the stack and pushes its square root onto it. |
sq | Pops a value from the stack and pushes the value squared onto it. |
get | Pushes the validated user input onto the stack. |
peek | Prints the first value of the stack, without modifying anything. |
cond | Pops a value from the stack and compares it with the next value on the stack. If they are equal, the other value is also popped and the code until the identifier "end" is executed. If not, the following code until the identifier "end" is ignored and only one value has been popped. |
end | Marks the end of a condition. |
setr | Pops a value from the stack and sets the internal loop variable to it. |
repeat | If the internal loop variable is above zero, repeat all statements since the last "setr". |
Example programs
10 33 100 108 114 119 32 44 111 108 101 72 peek setr peek setr peek peek setr peek setr peek setr peek setr peek setr peek setr peek setr peek setr peek setr peek setr
Prints the number sequence "72 101 108 111 44 32 119 114 108 100 33 10", which is "Hello, world!" in ASCII codes.
0 0 div peek
Divides zero by zero and prints the result.
(Stub)
Interpreter
The interpreter was originally written in Ruby, but then ported to C++, where the language underwent some major changes. The C++-Interpreter currently has 184 lines and the source will soon be available for download. It will be distributed under the WTFPL-License.