modula-2 home

  Home  
  Tutorial  
  Win32 API  
  Reference  
  Projects  
 

 

Expressions - Operands

Operands can be any of the following:

  • Integer constants
  • Real constants
  • Complex constants
  • String constants
  • Set constructors
  • Variable designators
  • Procedure designators
  • Function calls
  • Type casts

Each operand has a type associated with it that determines what operators can apply to it.

For Integer constants, Real constants, Complex constants see Type declarations: basic types.

Character and string constants

String constants are specified by enclosing a string of characters in either single or double quotes.  A string constant with N characters has the type:

ARRAY [0..N-1] OF CHAR

String constants with exactly one character are also compatible with the CHAR type.

String constants are compatible with ARRAYs that have an element type of CHAR.

Note: for ease of interfacing with operating systems and other languages, string constants are always followed in memory by a byte containing the NUL character (0 decimal).

A string constant with no characters (represented by two adjacent quotes, "") is allocated one byte and has the value NUL.  If this string is passed to a procedure expecting an ARRAY OF CHAR, the upper bound will be zero, which indicates a string with one character.  In this case, the one character is NUL.

You can use set, array and record constants in expressions using the same syntax for the value as used in constant declarations.

SET, ARRAY and RECORD value constructors

This creates a constant value from expressions that cannot be computed at compile time. A value constructor can only be used in expressions that are part of a statement.

SET, ARRAY and RECORD value constructors have the same syntax as SET, ARRAY and RECORD constants, however you can use non compile time values for any of the elements in the constructor.

BITSET{x..y}
Matrix{{InitialValue BY 10}BY 10}

If you use the BY keyword in an ARRAY value constructor, the BY repeat expression must still be a compile time constant.

Variable designators

A variable designator refers to the current value of a variable.  The syntax of a variable designator is:

[modulename.]variablename{qualifier}

If the modulename is specified, the variablename exported from that module is used.  Otherwise, the variablename is interpreted in the current scope.

Qualifier is:

[ field | subscript | dereference | subarray | coercion ]

Field qualifier

The field qualifier is specified by:

designator.identifier

The field qualifier can be applied to a variable designator that refers to a record type.  The new designator refers to the field of the record named by the identifier.

Subscript qualifier

The subscript qualifier is specified by:

designator[expression{,expression}]

The subscript qualifier can be applied to a variable designator that is an array type.  The resulting designator refers to the component of the array selected by the index values specified by the expressions.

An expression of the form:

A[e1, e2]

is equivalent to:

A[e1][e2]

Dereference qualifier

Dereference qualifierThe dereference qualifier is specified by:

designator^

The dereference qualifier can be applied to any designator that refers to a pointer type.  The resulting designator refers to the object pointed to by the pointer variable.

Procedure designators

A procedure designator is simply the name of the procedure.  A procedure designator refers to the address of the procedure and can only be passed as a parameter or assigned to a procedure variable.

Function calls

A procedure designator followed by parentheses is a function call.  The procedure must be a function procedure; that is, its declaration must include a return type.

A function call is specified by:

FunctionName([expression{,expression}])

The list of expressions are the actual parameters of the function.  The actual parameters are matched in order with the formal parameters.

The requirements for the actual parameters depend on how the formal parameter was declared:

  • For value parameters, the expression must result in a type assignable to the type of the formal parameter.
  • For VAR parameters, the expression must be a variable designator having the same type as the formal parameter.
  • For open array parameters, the actual parameter must be an array variable having the same element type as the formal parameter, or an open array parameter of the same type.
  • For procedure type parameters, the actual parameter can be either a variable of a compatible procedure type, or the name of a procedure compatible with the procedure type.

Formal parameter types are discussed in depth in the Procedures topic in this document.

The value of a function call is the value returned on the return statement in the function procedure.

Type cast

You can force an expression to be interpreted as a type other than its usual type by using a type cast.

A type cast takes the following form:

CAST(TypeName, expression)

The expression is evaluated, then forced to be interpreted as the type specified by TypeName.  No conversion of data is done by type transfers. The raw data is interpreted as the new type. The CAST function is imported from the SYSTEM module.

This means that type transfers are dependent on the data representation used by an implementation, and are therefore not transportable.

For this reason, type conversion procedures are preferred when applicable.  See the Standard Procedures chapter of this manual for information about type conversion procedures.

Type transfers from a shorter to a longer type are allowed, however a warning is generated because the excess bits are undefined.

Example:

VAR
   X : BITSET;
BEGIN
   X := BITSET{1, 2, 4};
   WriteCard(CAST(CARDINAL16, X), 0);

The value written is 22, because the BITSET has bits 1, 2, and 4 set. When interpreted as a CARDINAL this makes the number 22.


Source:

  • Stony Brook Modula-2 documentation. Used with permission. Note: Stony-Brook M2 offers an extended syntax with features not described here. Stony Brook M2 users are encouraged to visit the Stony Brook website and to consult the Stony Brook help system.