Literal Expressions
Num Literals
Nonnegative real numbers can be written in the decimal system: as integers, with a decimal point, or in exponential notation.
| Program | Type | Value | Error |
|---|---|---|---|
123 | Num | 123 | |
1.23 | Num | 1.23 | |
01.23 | Num | 1.23 | |
.23 | Num | 0.23 | |
1. | Num | 1 | |
1.23e2 | Num | 123 | |
123E2 | Num | 12300 | |
123E+2 | Num | 12300 | |
1e-1 | Num | 0.1 | |
.1e0 | Num | 0.1 | |
0010e-2 | Num | 0.1 | |
0e+5 | Num | 0 |
There are no literals for negative, infinity, or NaN values. But they can be created using the builtin -, inf, and nan funcers. For details, see Math Funcers.
| Program | Type | Value | Error |
|---|---|---|---|
-1 | Num | -1 | |
-0010e-2 | Num | -0.1 | |
-0 | Num | -0 | |
inf | Num | inf | |
-inf | Num | -inf | |
nan | Num | nan |
Str Literals
Strings are sequences of bytes. String literals are delimited by double quotes. Characters inside generally represent the byte sequence that is their UTF-8 encoding. For example:
arepresents the byte sequence61(hexadecimal notation; UTF-8 encoding of Latin Small Letter A)~represents the byte sequence7E(UTF-8 encoding of Tilde)abcrepresents the byte sequence61 62 63日本語represents the byte sequenceE6 97 A5 E6 9C AC E8 AA 9E
There are, however, the following exceptions:
\arepresents the byte sequence07(UTF-8 encoding of Bell character)\brepresents the byte sequence08(UTF-8 encoding of Backspace)\frepresents the byte sequence0C(UTF-8 encoding of Form feed)\nrepresents the byte sequence0A(UTF-8 encoding of Line feed)\rrepresents the byte sequence0D(UTF-8 encoding of Carriage return)\trepresents the byte sequence09(UTF-8 encoding of Horizontal tab)\vrepresents the byte sequence09(UTF-8 encoding of Vertical tab)\\represents the byte sequence5C(UTF-8 encoding of Backslash)\"represents the byte sequence22(UTF-8 encoding of Quotation mark)\followed by three octal digits in the range from000to3ff(inclusive) represents the corresponding byte\xfollowed by two hexadecimal digits represents the corresponding byte\ufollowed by four hexadecimal digits represents the UTF-8 encoding of the corresponding code point, if defined\Ufollowed by eight hexadecimal digits represents the UTF-8 encoding of the corresponding code point, if defined{{represents the byte sequence7B(UTF-8 encoding of Left curly bracket)}}represents the byte sequence7D(UTF-8 encoding of Right curly bracket){, followed by a Bach expression, followed by}, represents the UTF-8 encoding of what the expression evaluates to- Other uses of
\,",{, or}inside the delimiting quotes are invalid, as is the line feed character
| Program | Type | Value | Error |
|---|---|---|---|
"a" | Str | "a" | |
"\a" | Str | "\a" | |
"\"\\a\"" | Str | "\"\\a\"" | |
"\141" | Str | "a" | |
"\x61" | Str | "a" | |
"\u65e5\u672c\u8a9e" | Str | "日本語" | |
"\U000065e5\U0000672c\U00008a9e" | Str | "日本語" | |
"{{}}" | Str | "{{}}" | |
"1 + 1 = {1 +1}" | Str | "1 + 1 = 2" | |
"{ {a: 1 +1} }" | Str | "{{a: 2}}" |
Arr Literals
Array literals are delimited by square brackets. Inside, a comma-separated sequence of Bach expressions represents the elements. An expression representing an array can be appended as a suffix using a semicolon.
| Program | Type | Value | Error |
|---|---|---|---|
[] | Arr<> | [] | |
[1] | Arr<Num> | [1] | |
[1, 2, 3] | Arr<Num, Num, Num> | [1, 2, 3] | |
[1, "a"] | Arr<Num, Str> | [1, "a"] | |
[[1, 2], ["a", "b"]] | Arr<Arr<Num, Num>, Arr<Str, Str>> | [[1, 2], ["a", "b"]] | |
[1 +1] | Arr<Num> | [2] | |
[1;[]] | Arr<Num> | [1] | |
[1, 2;[3, 4]] | Arr<Num, Num, Num, Num> | [1, 2, 3, 4] | |
[3, 4] =rest [1, 2;rest] | Arr<Num, Num, Num, Num> | [1, 2, 3, 4] | |
[1, 2;[1, 2] each(+2)] | Arr<Num, Num, Num...> | [1, 2, 3, 4] |
Obj Literals
Obj literals are delimited by curly braces. Inside, each elements consists of a key, followed by a colon, followed by a value. Elements are separated by commas. The order of elements does not matter. Keys are always strings, but they can be written as identifiers or number literals; they are converted to strings automatically.
| Program | Type | Value | Error |
|---|---|---|---|
{} | Obj<Void> | {} | |
{"a": 1} | Obj<a: Num, Void> | {a: 1} | |
{a: 1} | Obj<a: Num, Void> | {a: 1} | |
{1: "a"} | Obj<1: Str, Void> | {1: "a"} | |
{"1": "a"} | Obj<1: Str, Void> | {1: "a"} | |
{a: 1, b: "c"} | Obj<a: Num, b: Str, Void> | {a: 1, b: "c"} | |
{b: 1 +1} | Obj<b: Num, Void> | {b: 2} |