CONSTANTS DESCRIPTION

Back to:
index

Educational compiler supports constants of INTEGER, CHAR, and BOOLEAN types (all standard types, except REAL). You can use ordinary and typed constants. Examples of typical constants descriptions, available in "COMPAS", are exemplified below:
    ordinary constants:
  • CONST a = 23;
  • CONST b = 'k';
  • CONST c = TRUE;
    typed constants:
  • CONST d: INTEGER = 6;
  • CONST e: CHAR = 'j';
  • CONST f: BOOLEAN = FALSE;
The very important difference between these two variants is the following. Ordinary constant is just a signature in program, which compiler replaces by its concrete value. Looking after translation on the result executable code, You can't point the places, where constant was used. Typed constant is a kind of variable, so it occupies some memory. The above fact has significant effect: you never can change ordinary constant value while execution of your program, but easily can revise typed one.

When "COMPAS" begins to analyse constant statement, CONST word is already skipped. So compiler reads constant name and checks it: is it correct (my compiler "knows" only names from one latin letter) and unique (other variable or constant can't have the same name).

Next symbol determines, what kind of constant we have: "=" shows, that it's ordinary, ":" - typed.

For ordinary constant its type is determined according to the value after "=" symbol. If it begins from "-" or number - constant has INTEGER type, from " ' " - CHAR; FALSE and TRUE text tells, that it's BOOLEAN.

All information about the constant (name, type and value) is placed into special table. Compiler uses it to replace constants by their values. You may ask, what's role of constant type - it is need for more reliable control of the program: e.g. compiler get possibility to compare types of constant and expression, to which it is substituted.

Let's consider typed constants. Their analysis has some difference. Constant type is determined after ":" and compiler compares it with the type of right part value. If they are the same, "COMPAS" creates typed constant.

Typed constant is not a real constant; it's better to call it initialized variable. So compiler must allocate memory for it (2 bytes for INTEGER type and 1 byte otherwise). "COMPAS" places typed constants into program area, because this gives possibility to save their values together with program code.

Remember typed constant as an alternative way to define variable. In the case of type constant you'll be sure in definite start value.



Back to:
index top