Skip to content

Expressions

Rui Ventura edited this page May 15, 2017 · 8 revisions

An expression is an algebraic representation of an amount: every expression has a type and returns a value.

There are primitive expressions and expressions that are a result of the evaluation of operators.

The table below shows the relative precedences of operators: it is the same for operators in the same line and the following lines have less priority than the ones before. Most of the operators adhere to the C language's semantic (except when explicitly indicated otherwise). Like C, boolean values are 0 (zero) (false value), and different from zero (true value).

Type Operators Associativity Operands Semantic
primary ()[] Non associative - parenthesis, indexing, memory allocation
unary +-? Non associative - identity and symmetry, address of expression
multiplicative */% Left Integers, reals C (% for integers only)
additive +- Left Integers, reals, pointers C (in case of pointers, the result is: (i) an offset; (ii) address difference, when - is applied to two pointers of the same type (the result is number of objects of the pointer type in between them).
comparative < > <= >= Left Integers, reals C
equality == != Left Integers, reals, pointers C
logical not ~ Non associative Integers C
logical and & Left Integers C: the 2nd argument is only evaluated if the 1st isn't false
logical or | Left Integers C: the 2nd argument is only evaluated if the 1st isn't true
assignment = Right All types The rhs expression's value is stored in the memory position indicated by the lvalue (lhs operand). Integer values can be assigned to real lvalues (automatic conversion). In other cases, both types must match.

Primitive expressions

See Literal expressions and function invocations.

Identifiers

An identifier is an expression if it has been declared. An identifier can denote a variable or a constant.

An identifier is the simplest case of a left-value. In other words, it is an entity that can be used on the left side of an assignment. A function's return value is defined by a special left-value (also see definitions regarding a function's body).

Reading

The reading of an integer or a real value is achieved through the @ expression, returning the read value, according to the expected type (integer or real). In case it is used as the argument of printing operators (! or !!), an integer is expected to be read.

Examples: a = @ (read value assigned to a), f(@) (read value assigned to function f's argument), @!! (reading and printing).

Parenthesis

An expression in between parenthesis has the expression's value and can change the priority of operators. Such an expression can't be used as a left-value (also see indexing expression).

Expressions as a result of operator evaluation

Indexing

Indexing returns the value of a memory position referenced by a pointer. It consists of a pointer expression followed by the offset enclosed in square brackets. The result of index is a left-value.

Example (accessing the memory position pointed to by p): p[0]

Identity and symmetry

The identity (+) and symmetry (-) operators can be used on integers or reals. They have the same meaning as they do in the C language.

Memory allocation

The memory allocation expression [] returns the pointer that references the zone in memory, in the current function's stack, containing enough space for the number of objects indicated by its integer argument.

Example (allocating array with 5 reals, pointed to by p): [real] p = [5]

Address of expression

The ? suffix operator is used on left-values, returning the respective address.

Example (address of a): a?

Clone this wiki locally