Next: , Previous: , Up: Imperative Language Interpreter Usage Outline   [Index]


5.1.5 Complete Example (I)

The wrapped up example code is:

static int
merge_sets(struct x1f4_function_type **set)
{
    int status = 0;
    struct x1f4_function_type *function_data;
    unsigned count, trans;

    x1f4_count_functions(x1f4_e4_defaults, &count);
    x1f4_count_functions(simple_output, &trans);
    function_data = (struct x1f4_function_type *)
	malloc((count + trans + 1) * sizeof(struct x1f4_function_type));
    if (!function_data) {
	status = 1;
    } else {
	*set = function_data;
	memcpy(function_data, x1f4_e4_defaults,
	       count * sizeof(struct x1f4_function_type));
	function_data += count;
	memcpy(function_data, _libaime_e4_io,
	       (trans + 1) * sizeof(struct x1f4_function_type));
    }

    return status;
}


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;
}


static int
test_cast(void *cast, const char *name, unsigned size)
{
    return 1 ^ fwrite(name, size, 1, cast);
}


    ...

    struct x1f4_function_type *function_data;

    if (merge_sets(&function_data)) {
	...
    } else {
	struct x1f4_c1_type c1;
	struct x1f4_c1record_type c1record;
	void *program;

	c1.bcollect_set.c1record_data = &c1record;
	c1.function_set.get = select_function;
	c1.function_set.context = function_data;
	x1f4_llink_operator1s(&c1.operator1s);
	x1f4_llink_operator2s(&c1.operator2s);
	c1.variable_set.context = NULL;

	status = x1f4_init_program(&program, data, X1f4_C1_BCOLLECT, &c1);
	if (status) {
	    if (status == X1f4_C1_ALLOC_ERROR) {
		perror(argv[0]);
	    } else {
		fprintf(stderr, "%s: cannot parse `%s'\n", ..., ...);
		fprintf(stderr, "%s: ", ...);
		x1f4_stat_program(stderr, test_cast, data, &c1record, NULL);
		fprintf(stderr, "\n");
	    }
	} else {
	    status = x1f4_link_program(program);
	    if (status) {
		fprintf(stderr, "%s: cannot execute `%s'\n", ..., ...);
	    }

	    x1f4_fini_program(&program);
	}

	free(function_data);
    }

Standard output printing functions set code is to be added.

See Printing Functions Set.