aboutsummaryrefslogtreecommitdiffstats
path: root/src/analyse.c
blob: 9510f5a69dda060c76b1d69d5f63264a9becb679 (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
#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;
	struct parse_deftype_s *dtp, *tmp_dtp;
	struct parse_var_s *vtp, *tmp_vtp;
	struct analyse_tree_s *cur;
	unsigned i;

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

	if (pr.deftypes != NULL) {
		HASH_ITER(hh, pr.deftypes, dtp, tmp_dtp) {
			cur = &(ar.deftype_tree);

			/* walk down the tree, creating nodes when necessary */
			for (i = 0; dtp->name[i] != '\0'; i++) {
				if (cur->branch_count == 0
						|| cur->branch_chars[cur->branch_count - 1]
						!= dtp->name[i]
				) {
					cur->branch_count++;
					cur->branch_chars[cur->branch_count - 1] = dtp->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, dtp->name);
		}
	}

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

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

			/* walk down the tree, creating nodes when necessary */
			for (i = 0; vtp->name[i] != '\0'; i++) {
				if (cur->branch_count == 0
						|| cur->branch_chars[cur->branch_count - 1]
						!= vtp->name[i]
				) {
					cur->branch_count++;
					cur->branch_chars[cur->branch_count - 1] = vtp->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, vtp->name);
		}
	}

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