Next: , Previous: Typing An Expression, Up: Crash Introduction


2.1.3 Evaluating An Expression

Translating the expression into a single value can be done as:

status = x1f4_link_expression(x1f4_expression, context, &output);

See x1f4_link_expression.

The context parameter is the expression evaluation context and it is passed to the C functions implementing the syntax allowed functions and operators. It is not used by the expression evaluator, nor by the functions implementing the libx1f4i0 function and operator sets. Thus, in this example, it may well be set to NULL (or any value).

See Functions.

See Prefix Unary Operators.

See Infix Binary Operators.

The third x1f4_link_expression parameter indicates a memory location to which the result of the expression evaluation should be stored. Enough space to store the result, of which the type can be determined via x1f4_type_expression, should be available at the specified location.

Since this examples uses only the intrinsic types the minimum memory requirements for result storage can be determined via union usage. Thus, the:

union state_type {
    X1f4_E4_C_MODE mode;
    X1f4_E4_C_REAL real;
    X1f4_E4_C_TEXT text;
};

See C Types.

type is large enough to hold any computed value. In the above x1f4_link_expression function call example, output was assumed declared as:

union state_type output;

Note however, that the result will stored at the address indicated, and thus accessing the result via the union state_type members will produce undefined results.

In plain C, to access the expression evaluation results, one may cast the result storage address to the corresponding pointer type and read the result from the first location. Like in:

if (type == X1f4_E4_MODE) {
    *(X1f4_E4_C_MODE *) &output
} else {
    if (type == X1f4_E4_REAL) {
        *(X1f4_E4_C_REAL *) &output
    } else {
        if (type == X1f4_E4_TEXT) {
            *(X1f4_E4_C_TEXT *) &output
        } else {
            no result, the expression evaluated void
        }
    }
}