Constants

Language reference ››
Parent Previous Next

See also: Pseudo-SET and IN operator, System constants and pseudo variables, EEPROM constants

 

PMP supports the declaration of symbols of type constant that may be simple values, strings or arrays.  

Constants must be declared in one or more CONST sections.


constants-declaration = CONST constant-declaration { "," constant-declaration } .
constant-declaration = simple-constant-declaration | array-constant-declaration .
simple-constant-declaration = identifier [ ":" simple-type | type-identifier ] "=" constant-expression ";" .
array-constant-declaration = identifier ":" ARRAY [ "[" range-expression "]" ] OF simple-type "=" "(" constant-expression { "," constant-expression } ")" ";" .
range-expression = ( ordinal-constant-expression ".." ordinal-constant-expression ) | range-type . 


<constant-expression>:

Any valid numeric, character or string expression that may be computed at this point.


<simple-type>:

May be BOOLEAN, BYTE, SHORTINT, WORD, LONGWORD (DWORD) or LONGINT and also SINGLE or REAL for PIC16+. 

Simple-typed constants syntax is for convenience only, it forces a range checking and a cast in computed expressions; such a variable declaration does not generate initialization code or variable allocation as in TP or Delphi (these strange "assignable / variable constants").


<type-identifier>:

An identifier of a previously defined TYPE. The resulting type should be <simple-type> or an ARRAY type. String types are not allowed, only direct string literals may be used.


<value-expression>:

A valid numeric constant expression that may be computed at this time. If the value does not match <simple-type>, a truncation occurs and a warning is produced.

 

NEW! (V2.0): Typed constants may be generated in EEPROM. They are generated with initial values as EEPROM VAR variables, but they are read only. They may be modified manually with a programming device or by software through the EEWRITE procedure. See also: EEPROM constants.


NEW! (V2.1): In extended syntax mode, an initialized constant ARRAY may be declared without bounds. The actual bounds will be 0..(<Number of elements> - 1). The high bound may be retrieved by the HIGH() built-in pseudo function.


Expressions may be any valid expression that may be evaluated by the compiler at this point; PMP uses 32-bit arithmetic for computing integer constant expressions and 64-bit doubles for computing floating point expressions:

 

CONST
  ConstOne = (123 + 4) * $10;
  ConstTwo = ConstOne - 1;
  ConstThree = 'hello ' + 'world'; // string made by concatenation
  ConstThreeCrLf = ConstThree + 13 + 10; // string with CR/LF
  ConstFour = 4;
  ConstFive = PI * 1.234E-3; // defaults to REAL type
  ConstSix: single = PI / 4.0; { force SINGLE precision, but computed as
                                 REAL (due to the expression that starts
                                 with PI that is REAL) }
  MyConstArray: ARRAY[1..ConstFour] OF INTEGER = (1, 1 SHL 1, 4, 2 * 4);
  MyFloatArray: ARRAY[1..ConstFour] OF REAL = (1.0, 1 SHL 1, 4.0, 2 * 4);
  MyConstArray2: ARRAY OF INTEGER = (1, 4, 2);  // define an initialized array [0..2] of integer
TYPE
  tMyConstArray = ARRAY[1..4] OF INTEGER;
CONST
  MyConstArray2: tMyConstArray = (10, 20, 30, 40);
{$SPACE EEPROM}
CONST
  EE_Const_Array: ARRAY[1..2] OF BYTE = (1, 1); // EEPROM constant ARRAY
  EE_Const_String: STRING[10] = 'abcd'; // EEPROM constant STRING, 10 chars max, filled with 4 chars.

 

PMP is a single pass compiler. Constants have to be declared prior to their use.