Next: , Up: Crash Introduction   [Index]


3.1.1 Parsing An Expression

To parse an expression the evaluator must be provided its textual representation, the function, operator and variable sets in use and the expression termination syntax.

status = x1f4_init_expression(&expression, text, flags, &attributes);

See x1f4_init_expression.

The x1f4_init_expression function builds the parsed expression representation from its text textual representation and stores the result in expression.

Parsed expression representations are opaque structures and application wise typeless. Only references to parsed expression representations are application available. For convenience the C type of the parsed expression representation references is void *.

Hence, the expression declaration may well be:

void *expression;

The rest of x1f4_init_expression input is provided via the attributes record. The flags parameter hints x1f4_init_expression that it must pay attention to certain attributes fields. In this simple example it is assumed 0.

The C type of the attributes record is struct x1f4_e4_type. The attributes record is thus declared as:

struct x1f4_e4_type attributes;

See struct x1f4_e4_type.

The minimal additional input for expression parsing is the function, operator and variable sets in use and the expression termination syntax.

The variables set is specified via a variable look up function. An additional variable look up context parameter can be specified and will be passed the variable look up function as parameter.

See Variables.

If no variables are allowed the variable look up function may be coded as:

static int
select_variable(const char *f, unsigned length, const void *context,
                const struct x1f4_variable_type **variable, void **state)
{
    return 1;
}

The corresponding attributes fields may be set as:

attributes.variable_set.get = select_variable;
attributes.variable_set.context = NULL;

The functions set specification is very much alike the variable set specification.

The functions set is specified via a function look up function. An additional function look up context parameter can be specified and will be passed the function look up function as parameter.

See Functions.

Assuming the libaime functions set and the simplest function look up strategy, the function look up function may be coded as:

static int
select_function(const char *f, unsigned length, const void *context,
                const struct x1f4_function_type **function)
{
    int status = X1f4_E4_PARSE_ERROR;
    const struct x1f4_function_type *function_data;

    function_data = context;
    if (function_data) {
        while (function_data->name) {
            if (length == function_data->length
                && !memcmp((void *) f, function_data->name, length)) {
                break;
            }
            function_data++;
        }
        if (function_data->name) {
            status = 0;
            *function = function_data;
        }
    }

    return status;
}

See struct x1f4_function_type.

The corresponding attributes fields can be set as:

attributes.function_set.get = select_function;
attributes.function_set.context = x1f4_e4_defaults;

See x1f4_e4_defaults.

See Mathematical And Text Generalities Functions.

To use the prefix unary and infix binary libaime operators sets only the attributes fields can be set as:

x1f4_llink_operator1s(&attributes.operator1s);
x1f4_llink_operator2s(&attributes.operator2s);

See x1f4_llink_operator1s.

See Prefix Unary Operators.

See C Prefix Unary Operators Set.

See x1f4_llink_operator2s.

See Infix Binary Operators.

See C Infix Binary Operators Set.

Finally, to specify that the textual expression representation completes with the first zero byte, the attributes record terminator field has to be set as:

attributes.terminator = 0;

Next: Typing An Expression, Up: Crash Introduction   [Index]