Basic Classes and Literal Forms

Sather - A Language Tutorial
Prev | Chapter 12. Built-in classes | Next

# 12.5. Basic Classes and Literal Forms

The basic Sather classes such as integers and booleans are not treated specially by the compiler. However, they do have language support in the form of convenient literal forms that permit easy specification of values. These literal forms all have a concrete type derived from the syntax; typing of literals is not dependent on context. Each of these basic classes also has a default void initial value.

Type

  Description

`BOOL`

  Initial Value: `false` - Immutable objects which represent boolean values.

`CHAR`

  Initial Value: `'\0'` - Immutable objects which represent characters. The number of bits in a '`CHAR`' object is less than or equal to the number in an '`INT`' object.

`STR`

  Initial Value: `""` (= `void`) - Reference objects which represent strings for characters. '`void`' is a representation for the null string.

`INT`

  Initial Value: `0` - Immutable objects which represent efficient integers. The size is defined by the implementation but must be at least 32 bits. Bit operations are supported in addition to numerical operations.

`INTI`

  Initial Value: `0i` - Reference objects which represent infinite precision integers.

`FLT`

  Initial Value: `0.0` - Immutable objects which represent single precision floating point values as defined by the IEEE-754-1985 standard.

`FLTD`

  Initial Value: `0.0d` - Immutable objects for double precision floating point values.

## 12.5.1. Booleans and the BOOL class

Examples:

    a:BOOL := true;
    b ::= false;
    c:BOOL := a.and(b);
    if a.and(b).or(d) then
    end;

`BOOL` objects represent boolean values (See also unnamedlink). The two possible values are represented by the boolean literal expressions: '`true`' and '`false`'. Boolean objects support the standard logical operations. Note that these logical operations are evaluated in the standard way, and not short-circuited. The Sather expressions "and" and "or" provide a short circuit logical operations.

    if b.has_value and b.get_value > 3 then
       -- The short circuit and will only evaluate b.get_value
       -- if b.has_value is true
    end;

## 12.5.2. Characters and the CHAR class

Examples:

    c:CHAR := 'a';
    new_line:CHAR := '\n';
    code_16:CHAR := '\016';

`CHAR` objects represent characters. Character literal expressions begin and end with single quote marks. These may enclose either any single ISO-Latin-1 printing character except single quote or backslash or an escape code starting with a backslash.

- `'\a'` is an alert such as a bell

- `'\b'` is the backspace character

- `'\f'` is the form feed character

- `'\n'` is the newline character

- `'\r'` is the carriage return character

- `'\t'` is the horizontal tab character

- `'\v'` is the vertical tab character

- `'\\'` is the backslash character

- `'\''` is the single quote character

- `'\"'` is the double quote character

A backslash followed by one or more octal digits represents the character whose octal representation is given. A backslash followed by any other character is that character. The mapping of escape codes to other characters is defined by the Sather implementation.

## 12.5.3. The string class, STR

Examples:

    s:STR := "a string literal"
    d:STR := "concat" "enation\015"
             -- d  is  "concatenation\015"

`STR` objects represent strings. String literal expressions begin and end with double quote marks. A backslash starts an escape sequence as with character literals. All successive octal digits following a backslash are taken to define a single character. Individual string literals may not extend beyond a single line, but successive string literals are concated together. Thus, a break in a string literal can also be used to force the end of an octal encoded character. For example: `"\0367"` is a one character string, while `"\03""67"` is a three character string. Such segments may be separated by comments and whitespace.

## 12.5.4. Integers and the INT class

Examples:

    a:INT := 14;
    b:INT := -4532;
    c:INT := 39_8322_983_298;
    binary:INT := 0b101011;
    bin:INT := -0b_101010_1010;
    octal:ITN := 0o37323;
    hex_num:INT:= 0x_ea_75_67;

`INT` objects represent machine integers. Integer literals can be represented in four bases: binary is base 2, octal is base 8, decimal is base 10 and hexadecimal is base 16. These are indicated by the prefixes: '`0b`', '`0o`', nothing, and '`0x`' respectively. Underscores may be used within integer literals to improve readability and are ignored. `INT` literals are only legal if they are in the representable range of the Sather implementation, which is at least 32 bits.

Underscores may be used to separate the digits of an integer to improve readability - this may be particularly useful for very long binary numbers.

## 12.5.5. Infinite precision integers and the INTI class

Examples:

    b:INTI := -4532i;
    infinite_hex:INTI := 0o373254i;

Infinite precision integers are are implemetned by the INTI class and supported by a literal form which is essentially the same as that of integers, but with a trailing 'i'. All the standard arithmetic operations are defined on infinite precision integers.

## 12.5.6. Floating point numbers: the FLT and FLTD classes

Examples:

    f:FLT := 12.34;
    fd:FLTD := 3.498_239e-8d;

Syntax:

flt_literal_expression ==> [`-`] decimal_int `.` decimal_int [`e` [`-`] decimal_int] [ `d` ]

`FLT` and `FLTD` objects represent floating point numbers according to the single and double representations defined by the IEEE-754-1985 standard). A floating point literal is of type `FLT` unless suffixed by 'd' designating a `FLTD` literal. The optional '`e`' portion is used to specify a power of 10 by which to multiply the decimal value. Underscores may be used within floating point literals to improve readability and are ignored. Literal values are only legal if they are within the range specified by the IEEE standard.

Sather does not do implicit type coercions (such as promoting an integer to floating point when used in a floating point context.) Types must instead be promoted explicitly by the programmer. This avoids a number of portability and precision issues (for example, when an integer can't be represented by the floating point representation.

The following two expressions are equivalent. In the first, the '`d`' is a literal suffix denoting the type. In the second, '`3.14`' is the literal and '`.fltd`' is an explicit conversion.

    3.14d      -- A double precision literal
    3.14.fltd  -- Single, but converted

Prev | Home | Next
Object Finalization: `$FINALIZE` | Up | Library Conventions