TheSingularity is an esoteric programming language created by User:HereToAnnoy that only gives the programmer one constant, called "TheSingularity", to work with.
This page is very unorganized, so if anyone could tidy it up a bit that would be great.
Rules and syntax
Comments and whitespace are not allowed in TheSingularity programs. New lines, however, are allowed.
WARNING: If you do not take proper precautions, TheSingularity will quickly melt your brain.
If your program happens to encounter an error, TheSingularity will print an error message and halt execution.
Note: The ">>" means that TheSingularity is being run from a shell, and returned values are outputted (followed by a new line). All () from outputs are for clarification only; they are not actually outputted.
>>THIS LINE OF CODE WILL CAUSE AN ERROR A black hole opens and swallows the universe. TheSingularity collapses all matter that ever existed.
The only data types allowed in TheSingularity are integers and strings.
Data is a string if it is surrounded by quotation marks. Strings are immutable in TheSingularity.
>>"Hello, World" Hello, World
Integers are harder to get to, however, because undefined variables will cause an error.
>>1 A black hole opens and swallows the universe. TheSingularity collapses all matter that ever existed.
1 is treated as a variable here, but 1 is undefined, so an error is thrown.
Integers are unbounded and can be negative.
Programmers must be mindful about what type of data they can use. A string or integer in the wrong place can be catastrophic.
In TheSingularity, variables can be an integer or string. To access a variable, simply state its name.
>>TheSingularity 1 >>1 A black hole opens and swallows the universe. TheSingularity collapses all matter that ever existed. (1 is undefined)
Variable (and function) names can be any Unicode symbols, excluding colons, semicolons, and commas.
Functions can be called in TheSingularity by first stating the name of the function and next stating its arguments split off by commas and between a colon and a semicolon.
The below function prints a string to the console:
>>TheSingularity:"Hello, World!\n"; Hello, World!
Note: Newline characters are not printed automatically. Use "\n" for a new line.
Note: Functions may not return anything - if there is nothing to return, the shell won't print it.
This function will define a variable:
>>TheSingularity:"x",TheSingularity; >>x 1
Variables can be redefined using the same function.
>>TheSingularity:"x",TheSingularity; >>x 1 >>TheSingularity:"x","Hello, World!"; >>x "Hello, World!"
Creating and using labels
Labels in TheSingularity are used to make functions and have conditional control flow. They are created like this:
If you attempt to execute a label, nothing will happen.
:TheSingularity; TheSingularity:"Hello World\n";
Labels can be jumped to using this function:
Labels must be created using integers.
Function definitions are a little bit more complicated. When you call a function, TheSingularity first has to define the argument variables. Take for instance this code:
TheSingularity must first set the argument variables. It does this (in pseudo-code):
set _1_ to "Hello, World\n" set _c_ to 1 call TheSingularity
The variables _1_-_5_ are the inputs to the function, and _c_ is the number of arguments given. The standard return variable (variable to store the returned value) is _r_, and _1_-_5_ and _c_ will be restored so they can be used further. Depending on the value of _c_ and the types of the arguments given, TheSingularity will do various different things. In this case, it will print characters to the screen.
To define a function, we can call TheSingularity's variable definition function but with an extra parameter at the end. In the below example "0" and "1" have been defined to their matching values.
TheSingularity:"defvar",0,1; TheSingularity:1; :0; TheSingularity:_1_,_2_; :1; defvar:"hw","Hello, World!\n"; TheSingularity:hw;
So, what does this example do? First, it calls the function definer.
The arguments tell the function definer to make a function called "defvar" that's code is stored between the labels 0 and 1. TheSingularity will then search for those labels and create the function. It will continue executing and reach this:
TheSingularity will jump to the line after label 1, as to not cause any unwanted calculations after label 0.
defvar:"hw","Hello, World!\n" TheSingularity:hw;
Using the new function, TheSingularity successfully defines a new variable and prints it to the console.
To do math in TheSingularity one must, obviously, call TheSingularity with a certain pattern for its arguments. In the below example, many numbers have been defined already.
>>TheSingularity:0,1,1; 2 (1+1) >>TheSingularity:1,1,1; 0 (1-1) >>TheSingularity:2,2,3; 6 (2*3) >>TheSingularity:3,3,3; 1 (floor of 3/3) >>TheSingularity:4,2,3; 8 (2 to the 3rd power) >>TheSingularity:5,3,2; 1 (3 mod 2, remainder of 3/2, NOT floor of 3/2)
If statement: TheSingularity:<val1>,<val2>,<label to jump to if val1 = val2>,<label to jump to if not>; As always, the numbers have been defined in the below examples.
TheSingularity:1,1,0,1; :0; TheSingularity:"1=1 test successful\n"; TheSingularity:2; :1; TheSingularity:"1=1 test failed\n"varaible :2;
1=1 test successful
Any TheSingularity program can receive input, but whether or not the program uses the input is up to the programmer.
When the program starts, the argument variables (_1_-_5_, _c_) are set as the inputs of the program (usually integers).
Conversions and type of
The following function will convert integers to strings and can get the types of variables, but, however, strings cannot be converted back into integers.
>>TheSingularity:"x",1; >>TheSingularity:x,"string"; "1" (string containing the integer) >>TheSingularity:1,"type"; int (gets the type of the input)
All examples are subject to optimization.
- Quine (by error)
A black hole opens and swallows the universe. TheSingularity collapses all matter that ever existed.
Note: The program ends in a new line
- Cat program - Accepts one string
TheSingularity:"1",TheSingularity; TheSingularity:"0",TheSingularity:1,1,1;; TheSingularity:_c_,1,0,1; :0; TheSingularity:_1_; :1; TheSingularity:"\n";
- 99 bottles of beer (100% OPTIMIZABLE)
TheSingularity:"1",TheSingularity; TheSingularity:"0",TheSingularity:1,1,1;; TheSingularity:"$",0,1; TheSingularity:1; :0; TheSingularity:"_r_",TheSingularity:_1_,_2_,_3_;; :1; TheSingularity:"2",$:0,1,1;; TheSingularity:"3",$:0,1,2;; TheSingularity:"x",$:2,3,$;2,3,$:0,2,$:2,3,3;;;;; TheSingularity:"A"," bottles of beer on the wall,\n"; TheSingularity:"B"," bottles of beer.\n"; TheSingularity:"C","Take one down, pass it around,\n"; TheSingularity:"D"," bottles of beer on the wall.\n"; TheSingularity:"y",TheSingularity:x,"string";; :2; TheSingularity:y; TheSingularity:A; TheSingularity:y; TheSingularity:B; TheSingularity:C; TheSingularity:"x",$:1,x,1;; TheSingularity:"y",TheSingularity:x,"string";; TheSingularity:y; TheSingularity:D; TheSingularity:x,1,3,2; :3; TheSingularity:"One bottle of beer on the wall,\nOne bottle of beer.\n"; TheSingularity:C; TheSingularity:"No"; TheSingularity:A; TheSingularity:"No"; TheSingularity:B; TheSingularity:"Go to the store, buy some more,\n99" TheSingularity:D;