Skip to content

Literals

Integer Literals

You can just write decimal integers as they are e.g 12345. This will take on the type NInt however if the number of bits required to represent the literal goes over the size of NInt the literal will take on the type Int512. If the number of bits is still higher than the size of Int512 then the literal will take on the type Int which is an arbitrary length integer.

Gambol provides the following types in the standard library to represent integers:

Int8, Int16, Int32, Int64, Int128, Int256, Int512 UInt8, UInt16, UInt32, UInt64, UInt128, UInt256, UInt512 NInt, Int

Custom integer types

You can specify a type after the literal which has to be one of the internal integer types or your own type. In the latter case, your type must provide a parse function (not method) taking a string and a base and returning an instance of that type.

type MyType {

    fun parse(s String, base Int32) -> MyType { ... }
}

12345 MyType

you can also write the integer in 3 other bases:

  • Binary: 0b11000000111001
  • Octal: 0o30071
  • Hexadecimal: 0x123AB43 or 0x123ab43

Real Literals

To write real numbers you can use the decimal point e.g. 12345.99. This will take on the type NFloat. If the number of bits required to represent the number goes over NFloat's precision then it will take on the type Float64. If the number of bits required is still higher it will take on the type Float which is an arbitrary precision float type with precision set high enough to represent the number accurately.

Gambol provides the following floating point types in the standard library to represent real numbers:

Float32, Float64, NFloat, Float

Custom real types

Just like integer literals you can specify your own type and if it is not one of Gambol provided types it must provide a parse function taking a String and returning an instance of that type.

You can also express real literals in scientific notation e.g. 123e3 or 123.55E-44. Positive and negative infinity are represented as +Inf and -Inf. Not a number as NaN.

Boolean Literals

There are only two true and false representing the type Bool.

String Literals

String literals in Gambol are enclosed in backquotes and can span multiple lines of text.

`hello
world`

To interpolate strings you have to start with a string literal and alternate between expressions and string literals like so:

`John's age is ` 44 ` and his height is ` 6 ` feet`

Gambol will pass the expressions in between to the str function in the global module to get a string representation.

for regular strings like above the following escape sequences are defined:

Sequence Description Representation
\` backquote byte 0x60 in ASCII encoding
\\ backslash byte 0x5c in ASCII encoding
\a audible bell byte 0x07 in ASCII encoding
\b backspace byte 0x08 in ASCII encoding
\f form feed byte 0x0c in ASCII encoding
\n new line byte 0x0a in ASCII encoding
\r carriage return byte 0x0d in ASCII encoding
\t horizontal tab byte 0x09 in ASCII encoding
\v vertical tab byte 0x0b in ASCII encoding
\nnn octal value 1 to 3 digit unicode code unit
\xnn hex value 2 digit unicode code unit
\unn or \Unnnn hex value 4 or 8 digit unicode code point

You can also specify raw strings like below

print(r`this is \n a raw string \u55`)

which will produce this is \n a raw string \u55

only the below two escape sequences are defined for raw strings:

Sequence Description Representation
\` backquote byte 0x60 in ASCII encoding
\\ backslash byte 0x5c in ASCII encoding
Custom string types

If you have your own string type e.g. a UnicodeString type you can instantiate your type with a string literal by providing a type function called parse that takes a String and returns your type. To instantiate a string literal with a type other than String just add the type after the literal.

Doc Strings

Some elements like functions and types allow you to add a documentation string on them. Doc strings are just like multiline string literals except going to a new line once in a doc string won't actually insert a new line:

    d`This is the documentation
    for my fun.

    it takes no parameter and returns 12`
    fun my_fun() { 12 }

import mirror

f = mirror.Function(my_fun)
print(f.doc_string)

output

This is the documentation for my fun.
it takes no parameter and returns 12

You can also document modules by placing a module doc string at the beginning of the modules file:

m`This is a module doc string

This module demonstrates the use of doc strings`

import mirror
m = mirror.Module(ast_ptr: __module__)
print(m.doc_string)

output

This is a module doc string

This module demonstrates the use of doc strings

Note

Unlike the regular doc strings the module doc string literal respects new lines