Skip to content

Literal Values

Socratic_Phoenix edited this page Aug 21, 2017 · 6 revisions

<-- Back | Next -->


Literal Values

Numbers

Shnap has two numbers types, integers and decimals. Both types have arbitrary precision. The precision of integers automatically increases or decreases based on operations, but decimals have their precisions estimated based on what operation is being applied, with a guaranteed precision of 32 decimal digits. A literal number is a sign (-, +, or nothing), followed by digits, optionally followed by a . and more digits, optional followed by e or E and a signed exponent, optionally followed by i or d. If a number is suffixed with i, it is converted to an integer, and if it is followed by d, it is converted to a decimal.

Examples (literal -> value):

5 -> integer 5
5.2 -> decimal 5.2
5e2 -> decimal 500
5e2i -> integer 500
-5 -> integer -5
3215d -> decimal 3215
53.21i -> integer 53
5e-2 -> decimal 0.05
5e-2i -> integer 0
Strings and Characters

There are 3 ways to represent text in Shnap, a raw string, a string, or a character.

A raw string is three quotes, followed by a string, followed by three quotes. A raw string uses the escape character \ and has only one escape, the quote, and any sequence of quotes less than 3 in length is read literally. A string is a quote, followed by an escapable string, followed by quote. A character is a ' followed by a single character (either literally, or escaped), followed by a '. The following escape codes are supported in strings and characters:

Escape Meaning
\u#### A unicode character, where #### is a hexadecimal number representing the code point.
\0 the null character
\b the backspace character
\t the tab character
\n the new line character
\f the form feed character
\r the carriage return character
\" the double quote character
\' the single quote character
\\ the backslash character

Note: any invalid escapes are simply read literally, so "\\k" is the same as "\k"

Examples:

'c'
'\0'
'\uFFFF'
//Shnap supports the full unicode range in its characters, but the \u format only
//supports the BMP. Using surrogate pairs, any character point can be represented:
'\uD800\uDc00'
"5\n\r"
//Shnap strings can span multiple lines
"Hello
World"
//The below \n is interpreted literally as \n
"""Hi\n,how are you?"""

Booleans

Booleans are represented literally as true and false. They function as numbers with the values 0 and 1.

Absent values

Null is represented literally as null and void is represented literally as void. These objects are known as "absent values". Every instance of null is the same object, and every instance of void is the same object. The difference between null and void is this: null represents a valid query, but an absent value, and void represents an invalid query. For example, referencing a variable that does not exist returns void, whereas calling a function that exists, with the proper parameters, may return null. Additionally, a function may return void if it does not have a return statement.

Arrays

Array literals are represented as a [ followed by a comma-separated list of values, followed by a ]. You can also create arrays with any size (which are populated with null) with the newArray builtin.

Examples:

[1, 2, 3]
[1 + 2, 4, null]
newArray(10)
Clone this wiki locally