/* See LICENSE file for copyright and license details. */ #include "common.h" #ifndef TEST /** * Transformation function for `LIBNORMALFORM_DOMAIN_VIEW`, * see `.transform` of `libnormalform_transformer` for * more information * * Outputs the `.key` for `input`; `input` has the type * `struct libnormalform_mapping *` */ static void * domain_view_transform(void *user_data, void *input) { struct libnormalform_mapping *kvpair = input; (void) user_data; return kvpair->key; } /** * Transformation function for `LIBNORMALFORM_IMAGE_VIEW`, * see `.transform` of `libnormalform_transformer` for * more information * * Outputs the `.value` for `input`; `input` has the type * `struct libnormalform_mapping *` */ static void * image_view_transform(void *user_data, void *input) { struct libnormalform_mapping *kvpair = input; (void) user_data; return kvpair->value; } int (libnormalform_apply_transformation__)(struct expression *this, enum libnormalform_builtin_transformer transformer) { /* Only .builtin is actually needed, but the others * could be useful for debugging by the user */ static struct libnormalform_transformer domain_view = { .transform = &domain_view_transform, .deallocate = NULL, .user_data = NULL, .identifier = "", .builtin = LIBNORMALFORM_DOMAIN_VIEW }; static struct libnormalform_transformer image_view = { .transform = &image_view_transform, .deallocate = NULL, .user_data = NULL, .identifier = "", .builtin = LIBNORMALFORM_IMAGE_VIEW }; size_t i; struct expression *new, **term_singleton; switch (this->type) { case LIBNORMALFORM_CONJUNCTION: case LIBNORMALFORM_DISJUNCTION: case LIBNORMALFORM_EXCLUSIVE_DISJUNCTION: /* transformations are morphism */ for (i = 0; i < this->nterms; i++) if (libnormalform_apply_transformation__(this->terms[i], transformer)) return -1; break; case LIBNORMALFORM_TRANSFORMATION: case LIBNORMALFORM_FUNCTION: case LIBNORMALFORM_NEGATED_FUNCTION: new = malloc(sizeof(*new)); if (!new) return -1; term_singleton = malloc(sizeof(*term_singleton)); if (!term_singleton) { free(new); return -1; } *new = *this; *this = EXPRESSION_INIT(LIBNORMALFORM_TRANSFORMATION); this->nterms = 1; this->terms = term_singleton; this->terms[0] = new; switch (transformer) { case LIBNORMALFORM_DOMAIN_VIEW: this->user_item = &domain_view; break; case LIBNORMALFORM_IMAGE_VIEW: this->user_item = &image_view; break; default: case LIBNORMALFORM_NOT_BUILT_IN: abort(); } break; case LIBNORMALFORM_VARIABLE: case LIBNORMALFORM_NEGATED_VARIABLE: /* variables are input-independent leafs */ case LIBNORMALFORM_FOR_ALL: case LIBNORMALFORM_NEGATED_FOR_ALL: case LIBNORMALFORM_FOR_ANY: case LIBNORMALFORM_NEGATED_FOR_ANY: case LIBNORMALFORM_FOR_ONE: case LIBNORMALFORM_NEGATED_FOR_ONE: /* qualifiers reset input */ default: break; } return 0; } #else TODO_TEST #endif