HANDBOOK / LIBRARY REFERENCE MANUAL /

# Implementing Types and Predicates in C

A type that is is defined without functors or a procedure that is defined without rules must be implemented in C.

Abstract types may be implemented as values of type `long` or as pointers.

For example, if a Gentle specification contains the declaration

    'type' IDENT

this type may be implemented in a C module as

    typedef struct IDENTSTRUCT *IDENT;

which defines values of type `IDENT` as references to structures.

Abstract procedures are implemented by C routines that take `long` arguments as input parameters and use `(long *)` arguments to realize output parameters.

For example, if a Gentle specifications defines a procedure as

    'action' Max (INT, INT -> INT)

it may be implemented in C as

    void Max (x, y, ref_result)
       long x;
       long y;
       long *ref_result;
    {
       *ref_result = (x > y ? x : y);
    }

The Gentle type `INT` corresponds to the C type `long`. Gentle terms and the type `POS` may also be treated as `long`. The Gentle type `STRING` corresponds to `(char *)`.

A `'condition'` must be implemented as a function returning `int` that yields 1 if the call succeeds and 0 if it fails.

For example, a procedure introduced by

    'condition' HasMeaning (IDENT -> MEANING)

may be implemented by

    int HasMeaning (id, ref_meaning)
       IDENT id;
       long *ref_meaning;
    {
       if (id->meaning == 0)
          return 0;
       *ref_meaning = id->meaning;
       return 1;
    }