VARIABLES DESCRIPTION

Back to:
index

Educational compiler supports INTEGER, CHAR, and BOOLEAN variable types (all standard types, except REAL) and ARRAY of them. Examples of typical variable descriptions, available in "COMPAS", are exemplified below:

  • VAR a: INTEGER;
  • VAR a,b,c: INTEGER;
  • VAR c,d: CHAR;
  • VAR e,f: BOOLEAN;
  • VAR g,h: ARRAY ['A'..'F'] OF INTEGER;
  • VAR i,j: ARRAY [BOOLEAN] OF CHAR;
  • VAR k,l: ARRAY [1..8] OF BOOLEAN;
  • CONST m=1; n=5; VAR o: ARRAY [m..n] OF INTEGER;
When "COMPAS" begins to analyse the list of variables, VAR word is already skipped. So the next thing compiler does, is to get variable list until ":" symbol. "COMPAS" checks, that variable's name is correct (my compiler "knows" only names from one latin letter) and unique (other variable or constant can't have the same name). Only "," or ":" can be after variable name.

If list reading was successful, the variable type is determined then: compiler compares the text after ":" with INTEGER, CHAR, BOOLEAN and ARRAY reserved words. If the result is positive, the definite amount of bytes is allocated for each variable (the size of variable is determined by its type):

  • 1 byte for CHAR or BOOLEAN
  • 2 bytes for INTEGER
  • MaxIndex-MinIndex+1 bytes for ARRAY OF CHAR or ARRAY OF BOOLEAN
  • (MaxIndex-MinIndex+1)*2 bytes for ARRAY OF INTEGER
And of course "COMPAS" always check, is there enough memory for the variable. It's especially actual for arrays, which can be large.

"COMPAS" creates variables from high memory address (towards the program), so for statement
VAR a,b: INTEGER
variable's a address will be higher, than variable b.

As you can assume, the most complex is the case of array. When "COMPAS" sees "[" symbol, it jumps to the special part of program, which decodes array index type. Index bounds can be set by value or CONST of correspondent type. "COMPAS" distinguishes this situations, looking at the first symbol: number or "-" shows, that it's not a constant, but a number. The case of boolean index is checked apart.

Notice, that "COMPAS" always remembers the address of zero-index element, even when it's not exist (e.g. in the case ARRAY [3..5]). It's more simple to calculate array elements address in this case.

And one more important thing. "COMPAS" doesn't initialize variables by any value. I specially did so to underline, that the programmer must take care about initial values. If it seems strange to you, note, that compiler can't know what value is better for your case: 0, 1 or any other. The practice to determine initial variable's value yourself is better from all sides. Your program will always work correctly even when computer will cyclical repeat this piece of program many times. If it's not enough argument, remember, that local variables in procedures are not initialized even in "regardful" Borland Pascal, althogh it initializes global variables. I hope I have assured you to initialize variables yourself always.



Back to:
index top