Next: Reference Variables, Previous: Defining Variables, Up: Variables
The following very simple variable look up method makes use of a record holding two array references, the first to the variable definitions list and the second to the addresses at which the values of the variables are stored.
The record is defined as:
struct context_type { struct x1f4_variable_type *variable_data; void **state; };
The variable definitions list is assumed to have one extra element for which
the name
field is NULL
, marking the end of list.
The look up function uses sequential search to identify the variable.
static int select_variable(const char *f, unsigned length, const void *context, const struct x1f4_variable_type **variable, void **state) { int status = X1f4_E4_PARSE_ERROR; const struct context_type *context_data; const struct x1f4_variable_type *variable_data; context_data = context; variable_data = context_data->variable_data; if (variable_data) { while (variable_data->name) { if (length == variable_data->length && !memcmp((void *) f, variable_data->name, length)) { break; } variable_data++; } if (variable_data->name) { status = 0; *state = context_data->state [variable_data - context_data->variable_data]; *variable = variable_data; } } return status; }
Setting up the expression parser becomes:
struct context_type context; struct x1f4_attributes_type attributes; ... attributes.variable_set.get = select_variable; attributes.variable_set.context = context; ... status = x1f4_init_expression(..., ..., ..., &attributes);
See x1f4_init_expression.