aboutsummaryrefslogtreecommitdiffstats
path: root/libnormalform_apply_transformation__.c
blob: 03af62c3b64e0193d160c7776cd8ad2f85750bd2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* 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:domain-view>",
		.builtin = LIBNORMALFORM_DOMAIN_VIEW
	};
	static struct libnormalform_transformer image_view = {
		.transform = &image_view_transform,
		.deallocate = NULL,
		.user_data = NULL,
		.identifier = "<builtin:image-view>",
		.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