OEP

147

Title

ENUM support

Summary

Allow enumerations for constants.

Owner

Mario Schweigler

Status

Proposed

Date-Proposed

2006-03-31

Keywords

language syntax enum

A useful feature (which would probably be fairly easy to implement) would be C-like enumerations for constants. The syntax could be like this:

VAL <type>
  ENUM <start> STEP <step>
    <name-list>
:

<type> would be any 'enumeratable' type such as INT or BYTE. The syntax of the second line would be similar to existing replicators, except that the FOR part, which denotes the number of replications, is not needed, since for ENUMs, this would be implicit by the number of names in the body of the ENUM. So, <start> would be the start value and <step> the 'distance' between two successive values. If the STEP part is omitted, a default distance of 1 would be used. <name-list> would be a list of names for constants.

Using an ENUM, the follwing existing occam-pi code:

VAL INT PONYC.RESULT.ALLOC.OK IS 0:
VAL INT PONYC.RESULT.ALLOC.ILLEGALNCTNAME IS -1:
VAL INT PONYC.RESULT.ALLOC.CHANTYPEMISMATCH IS -2:
VAL INT PONYC.RESULT.ALLOC.X2XTYPEMISMATCH IS -3:
VAL INT PONYC.RESULT.ALLOC.X2XCOUNTMISMATCH IS -4:

could be written as:

VAL INT
  ENUM 0 STEP -1
    PONYC.RESULT.ALLOC.OK
    PONYC.RESULT.ALLOC.ILLEGALNCTNAME
    PONYC.RESULT.ALLOC.CHANTYPEMISMATCH
    PONYC.RESULT.ALLOC.X2XTYPEMISMATCH
    PONYC.RESULT.ALLOC.X2XCOUNTMISMATCH
:

Another useful feature would be allowing prefixes for ENUMs. In the above example for instance, all constants start with PONYC.RESULT.ALLOC. --- it would therefore be useful to have an ENUM syntax like:

VAL <type> <prefix>
  ENUM <start> STEP <step>
    <suffix-list>
:

where <prefix> is a prefix that would be prepended to the names in the body of the ENUM. (Of course, the <prefix> could be omitted, which would give us the earlier syntax.) So, the above code example could be simplified to:

VAL INT PONYC.RESULT.ALLOC.
  ENUM 0 STEP -1
    OK
    ILLEGALNCTNAME
    CHANTYPEMISMATCH
    X2XTYPEMISMATCH
    X2XCOUNTMISMATCH
:

It would also be useful to be able to find out about the number of constants in a (possibly large) ENUM. So, if an ENUM contains a prefix, we could use the SIZE operator to determine its size. For the above example,

SIZE PONYC.RESULT.ALLOC.

would return 5. Since ENUMs are constant, KRoC could evaluate SIZE <enum-prefix> expressions at compile-time.

OEP/147 (last edited 2007-09-27 01:18:23 by ats1)