blob: 411ba4e75907accd1f3e2c4f839cb782ba2a495d (
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
|
// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de>
// SPDX-License-Identifier: MIT
#include <stdlib.h>
#include <lib/hashmap.h>
#include <map.h>
#include <parse.h>
static struct hashmap *all_terms;
static void deref_term(struct term *term)
{
if (term->type == ABS) {
deref_term(hashmap_get(all_terms, term->u.abs.term));
} else if (term->type == APP) {
deref_term(hashmap_get(all_terms, term->u.app.lhs));
deref_term(hashmap_get(all_terms, term->u.app.rhs));
}
// TODO: remove from hashmap?
if (--term->refs == 0)
free(term);
}
static void hashmap_free_term(void *item)
{
struct term *term = *(struct term **)item;
free(term);
}
struct term *map_get(hash_t hash)
{
struct term **handle = hashmap_get(all_terms, hash);
if (!handle)
return 0;
return *handle;
}
void map_set(struct term *term, hash_t hash)
{
hashmap_set(all_terms, &term, hash);
}
void map_initialize(void)
{
all_terms = hashmap_new(sizeof(struct term *), 0, hashmap_free_term);
}
void map_destroy(void)
{
hashmap_free(all_terms);
}
|