aboutsummaryrefslogtreecommitdiffstats
path: root/src/analyse.c
blob: 416b10a45ec6e15fdf6895216bf66a49de01c330 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "err.h"
#include "analyse.h"

#include <assert.h>
#include <string.h>

struct analyse_result_s analyse(struct parse_result_s pr)
{
	struct analyse_result_s ar = {
		.uses_hash = false,
		.uses_array = false,
	};
	struct parse_deftype_s *dcur, *dtmp;
	struct parse_var_s *vcur, *vtmp;
	struct analyse_tree_s *cur;
	unsigned i;


	/***************
	 * BUILD TREES *
	 ***************/

	ar.deftype_tree.branch_count = 0;
	ar.deftype_tree.is_terminal = false;

	if (pr.deftypes != NULL) {
		HASH_ITER(hh, pr.deftypes, dcur, dtmp) {
			if (!dcur->is_used)
				continue;

			cur = &(ar.deftype_tree);

			/* walk down the tree, creating nodes when necessary */
			for (i = 0; dcur->name[i] != '\0'; i++) {
				if (cur->branch_count == 0
						|| cur->branch_chars[cur->branch_count - 1]
						!= dcur->name[i]
				) {
					cur->branch_count++;
					cur->branch_chars[cur->branch_count - 1] = dcur->name[i];
					TRYALLOC(cur->branches[cur->branch_count - 1], 1);
					cur = cur->branches[cur->branch_count - 1];
					cur->branch_count = 0;
					cur->is_terminal = false;
				} else {
					cur = cur->branches[cur->branch_count - 1];
				}
			}

			cur->is_terminal = true;
			strcpy(cur->name, dcur->name);
		}
	}

	ar.var_tree.branch_count = 0;
	ar.var_tree.is_terminal = false;

	if (pr.vars != NULL) {
		HASH_ITER(hh, pr.vars, vcur, vtmp) {
			cur = &(ar.var_tree);

			/* walk down the tree, creating nodes when necessary */
			for (i = 0; vcur->name[i] != '\0'; i++) {
				if (cur->branch_count == 0
						|| cur->branch_chars[cur->branch_count - 1]
						!= vcur->name[i]
				) {
					cur->branch_count++;
					cur->branch_chars[cur->branch_count - 1] = vcur->name[i];
					TRYALLOC(cur->branches[cur->branch_count - 1], 1);
					cur = cur->branches[cur->branch_count - 1];
					cur->branch_count = 0;
					cur->is_terminal = false;
				} else {
					cur = cur->branches[cur->branch_count - 1];
				}
			}

			cur->is_terminal = true;
			strcpy(cur->name, vcur->name);
		}
	}


	/************************
	 * CALCULATE USED TYPES *
	 ************************/

	for (i = PARSE_TYPE_BOOL; i < PARSE_TYPE_HASH_DEFTYPE; i++)
		ar.uses_type[i] = false;

	HASH_ITER(hh, pr.deftypes, dcur, dtmp) {
		if (!dcur->is_used)
			continue;

		for (i = 0; i < dcur->member_list_len; i++) {
			if (dcur->member_type_list[i] >= PARSE_TYPE_HASH_BOOL) {
				ar.uses_hash = true;
				ar.uses_type[dcur->member_type_list[i]
					- PARSE_TYPE_HASH_BOOL] = true;
			} else if (dcur->member_type_list[i] >= PARSE_TYPE_ARRAY_BOOL) {
				ar.uses_array = true;
				ar.uses_type[dcur->member_type_list[i]
					- PARSE_TYPE_ARRAY_BOOL] = true;
			}

			ar.uses_type[dcur->member_type_list[i]] = true;
		}
	}

	HASH_ITER(hh, pr.vars, vcur, vtmp) {
		if (vcur->type >= PARSE_TYPE_HASH_BOOL) {
			ar.uses_hash = true;
			ar.uses_type[vcur->type - PARSE_TYPE_HASH_BOOL] = true;
		} else if (vcur->type >= PARSE_TYPE_ARRAY_BOOL) {
			ar.uses_array = true;
			ar.uses_type[vcur->type - PARSE_TYPE_ARRAY_BOOL] = true;
		}

		if (vcur->type == PARSE_TYPE_DEFTYPE
				|| vcur->type == PARSE_TYPE_ARRAY_DEFTYPE 
				|| vcur->type == PARSE_TYPE_HASH_DEFTYPE
		) {
			continue;
		}

		ar.uses_type[vcur->type] = true;
	}

	return ar;
}

static void sub_recurse_free(struct analyse_tree_s *t)
{
	unsigned i;

	for (i = 0; i < t->branch_count; i++) {
		sub_recurse_free(t->branches[i]);
		free(t->branches[i]);
	}
}

void analyse_result_wipe(struct analyse_result_s *r)
{
	assert(r != NULL);

	sub_recurse_free(&(r->deftype_tree));
	sub_recurse_free(&(r->var_tree));
}