Data Literals
String
PPL supports three kinds of Unicode string literals:
-
Single quoted string literal
The single quoted string literal works like the classical string literal used in many popular programming languages (C, Java, …)
The string is delimited by a pair of quotes (
"
) and some characters must be escaped with a backslash (\
).Examples:
"Hello" // Hello " " // <space> "\"" // " "'" // ' "\t1" // <horizontal tab>1 "\r\n" // <carriage return><line feed> "Hello\\\nTom" // Hello\<line feed>Tom "\u0038\u0039\u002f there" // hi? there
-
Triple-apostrophed string literal
The triple-apostrophised string literal is delimited by a pair of three apostrophes (
'''
). Characters within the string are not escaped and the string can contain new lines.Examples:
'''Hello''' '''string containing 3 lines line 2 line 3''' '''Characters like \ and " don't need to be escaped! (very useful when defining regular expressions, file paths, SQL statements, etc.'''
-
Triple-quoted string literal
The triple-quoted string literal is delimited by a pair of three quotes (
"""
). It extends the possibilities of the the triple-apostrophed string literal by also supporting string interpolation. Expressions can be embedded in the string by surrounding them with{{
and}}
.Example:
const name = "Einstein" const quote = "Try not to become a man of success, but rather try to become a man of value." const message = """{{name.to_upper_case}} said: {{quote}}""" assert message =v '''EINSTEIN said: Try not to become a man of success, but rather try to become a man of value.'''
Character
A character literal consists of one Unicode character or one escape character delimited by two apostrophes ('
). It works like in Java.
Examples:
'a' // a ' ' // <space> '"' // " '\'' // ' '\t' // <horizontal tab> '\u002f' // ?
Number
There are two number literals:
-
Integer literal
An integer literal consists of an optional minus sign (
-
), followed by one or more digits.By default, integer literals are 32 bits integers. If you need a 64 bits integer then the literal must be suffixed by
L
.Examples:
0 1 123 -2000 // 32 bits -2000L // 64 bits
PPL distinguishes between signed and unsigned integers, as well as integers that can or cannot be zero. The following graph shows the type inheritance for 64 bits integers:
signed_int_64 ^ ^ zero_neg_64 zero_pos_64 ^ ^ neg_64 pos_64
Type compatibility is ensured by the compiler. For example, a positive integer is compatible to a signed integer, but the inverse is not true.
-
Floating point literal
A floating point literal consists of an optional minus sign (
-
), followed by one or more digits, followed by a fraction, optionally followed by a positive or negative exponent. The dot (.
) is used as decimal separator. The exponent specifies a power of 10 by which the number is to be multiplied.Examples:
123.45 -123.45 0.0 1.0 0.1 1.0e3 // 1000 1.0e-3 // 0.001 -0.1e-3 // -0.0001
Yes_no (Boolean)
A yes_no literal has two possible values:
yes no
Note: Other languages use the term boolean
, instead of yes_no
, and the possible values are true
and false
, instead of yes
and no
. But the concept is the same.
Null
The null literal consists of the keyword:
null
List Literal
A list literal consists of an enumeration of arbitrary complex expressions, separated by a comma, and delimited by square brackets ([
and ]
).
The type of elements in the list is inferred by the compiler, but can be specified explicitly if desired.
Example:
const list = ["A", "B", "c".to_upper_case] assert list.size =v 3 assert list.first =v "A" assert list.get(2) =v "B" assert list.last =v "C" assert list.to_long_string =v "[A, B, C]" // The type can be specified explicitly: const yes_no_list list<yes_no> = [yes, no, yes, yes]
Map Literal
A map literal consists of an enumeration of key:value
pairs, separated by a comma, and delimited by square brackets ([
and ]
).
The key/value types are inferred by the compiler, but they can be specified explicitly if desired.
Example:
const map = [ "A": 1, "B": 2, "c".to_upper_case: 1+1+1 ] assert map.size =v 3 assert map.get ( "C" ) =v 3 assert map.to_long_string =v "[A: 1, B: 2, C: 3]" // The type can be specified explicitly: const map2 map<key:string, value:yes_no> = [ "A": yes, "B": no, "C": yes ]