Literals - The Nitrate Programming Language

Integer Literals

Overview

Nitrate integer literals can be specified in several bases, including binary, octal, decimal, and hexadecimal. The base of the integer is determined by the prefix of the token. For example, 0b1010 is a binary number, 0o12 is an octal number, 42 is a decimal number, and 0x2A is a hexadecimal number. Integer literals may contain underscores to improve readability, such as 1_000_000 or 0x0B671B17_60B5_4C29_AADA_3DA1C701EA96.

PrefixBaseExampleDescription
0b20b100010Binary number
0o80o42Octal number
0d100d34Explicit decimal number
1034Implicit decimal number
0x160x22Hexadecimal number

Formal Definition

<integer_digit_separator> ::= "_"
<binary_char> ::= [0-1]
<octal_char> ::= [0-7]
<hex_char> ::= ([0-9] | [a-f] | [A-F])
<binary_literal_body> ::= <binary_char> | <binary_literal_body> <integer_digit_separator>* <binary_char>
<octal_literal_body> ::= <octal_char> | <octal_literal_body> <integer_digit_separator>* <octal_char>
<decimal_literal_body> ::= [0-9] | <decimal_literal_body> <integer_digit_separator>* [0-9]
<hex_literal_body> ::= <hex_char> | <hex_literal_body> <integer_digit_separator>* <hex_char>
<binary_literal> ::= "0b" <binary_literal_body>
<octal_literal> ::= "0o" <octal_literal_body>
<explicit_decimal_literal> ::= "0d" <decimal_literal_body>
<implicit_decimal_literal> ::= <decimal_literal_body>
<decimal_literal> ::= <explicit_decimal_literal> | <implicit_decimal_literal>
<hex_literal> ::= "0x" <hex_literal_body>
<integer_lit> ::= <binary_literal> | <octal_literal> | <decimal_literal> | <hex_literal>

Float Literals

Overview

Nitrate float literals (or numbers with a decimal point) can be denoted in decimal form. Floats may contain underscores to improve readability, such as 3.141_592_653_589_793 or 2_718_281_828.459_045. Floats use scientific notation to represent very large or very small numbers, such as 6.022e23 or 1.602e-19. Fractional exponents are also supported, such as 4.891e9.189.

Formal Definition

<float_digit_separator> ::= "_"
<float_part> ::= <implicit_decimal_literal> | <implicit_decimal_literal> <float_digit_separator>* "." <float_digit_separator>* <implicit_decimal_literal>
<float_mantissa> ::= <float_part>
<float_exponent> ::= "e" <float_part> | "e-" <float_part>
<float_lit> ::= <float_mantissa> <float_exponent> | <float_mantissa>

String Literals

Overview

Nitrate string literals are sequences of characters enclosed in double quotes, such as "Hello, World!". Special characters can be escaped using a backslash, such as "\n" for a newline character or "\t" for a tab character. Unicode characters can be represented using a backslash followed by a u, such as "\u{1F600}" for the grinning face emoji 😁. Arbitrary bytes can be escaped using the hexadecimal \xXX pattern.

Formal Definition

<basic_char> ::= [0-9] | [a-f] | [A-F] | "~" | "@" | "#" | "%" | "_" | "$" | "&" | "'" | "-" | "+" | "/" 
<hex_char> ::= [0-9] | [a-f] | [A-F]
<escape_sequence> ::= "\\" ("n" | "t" | "r" | "h" | "v" | "b" | "f" | "0" | "\"" | "'" | "\\" | "x" <hex_char> <hex_char> | "u" "{" <hex_char>* "}")
<basic_string_part> ::= <basic_char>* | <escape_sequence>*
<string_lit> ::= "\"" <basic_string_part> "\""

Character Literals

Overview

Nitrate character literals are single characters enclosed in single quotes, such as 'a', '1', or '!'. Special characters can be escaped using a backslash, such as '\n' for a newline character or '\t' for a tab character.

Formal Definition

<basic_char_part> ::= <basic_char> | <escape_sequence>
<char_lit> ::= "'" <basic_char_part> "'"