diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/map.c | 14 | ||||
-rw-r--r-- | src/parse.c | 31 | ||||
-rw-r--r-- | src/term.c | 40 |
3 files changed, 46 insertions, 39 deletions
@@ -9,20 +9,6 @@ static struct hashmap *all_terms; -static void deref_term(struct term *term) -{ - if (term->type == ABS) { - deref_term(term->u.abs.term); - } else if (term->type == APP) { - deref_term(term->u.app.lhs); - deref_term(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; diff --git a/src/parse.c b/src/parse.c index 5445aea..b10a698 100644 --- a/src/parse.c +++ b/src/parse.c @@ -10,25 +10,6 @@ static size_t max_depth = 0; -static struct term *new_term(term_type_t type, hash_t hash, size_t depth) -{ - struct term *term = malloc(sizeof(*term)); - if (!term) - fatal("out of memory!\n"); - term->type = type; - term->refs = 1; - term->hash = hash; - term->depth = depth; - return term; -} - -static void update_term(struct term *term, size_t depth) -{ - term->refs++; - if (depth < term->depth) // lower depths are more important - term->depth = depth; -} - static struct term_handle abs_blc(char **term, size_t depth) { term_type_t res_type = ABS; @@ -37,9 +18,9 @@ static struct term_handle abs_blc(char **term, size_t depth) struct term *res_term; if ((res_term = map_get(res))) { - update_term(res_term, depth); + term_refer(res_term, depth); } else { - res_term = new_term(res_type, res, depth); + res_term = term_new(res_type, res, depth); res_term->u.abs.term = inner.term; map_set(res_term, res); } @@ -58,9 +39,9 @@ static struct term_handle app_blc(char **term, size_t depth) struct term *res_term; if ((res_term = map_get(res))) { - update_term(res_term, depth); + term_refer(res_term, depth); } else { - res_term = new_term(res_type, res, depth); + res_term = term_new(res_type, res, depth); res_term->u.app.lhs = lhs.term; res_term->u.app.rhs = rhs.term; map_set(res_term, res); @@ -76,9 +57,9 @@ static struct term_handle var_blc(int index, size_t depth) struct term *res_term; if ((res_term = map_get(res))) { - update_term(res_term, depth); + term_refer(res_term, depth); } else { - res_term = new_term(res_type, res, depth); + res_term = term_new(res_type, res, depth); res_term->u.var.index = index; map_set(res_term, res); } diff --git a/src/term.c b/src/term.c new file mode 100644 index 0000000..b2202cc --- /dev/null +++ b/src/term.c @@ -0,0 +1,40 @@ +// Copyright (c) 2023, Marvin Borner <dev@marvinborner.de> +// SPDX-License-Identifier: MIT + +#include <stdlib.h> + +#include <log.h> +#include <term.h> + +struct term *term_new(term_type_t type, hash_t hash, size_t depth) +{ + struct term *term = malloc(sizeof(*term)); + if (!term) + fatal("out of memory!\n"); + term->type = type; + term->refs = 1; + term->hash = hash; + term->depth = depth; + return term; +} + +void term_refer(struct term *term, size_t depth) +{ + term->refs++; + if (depth < term->depth) // lower depths are more important + term->depth = depth; +} + +/* void deref_term(struct term *term) */ +/* { */ +/* if (term->type == ABS) { */ +/* deref_term(term->u.abs.term); */ +/* } else if (term->type == APP) { */ +/* deref_term(term->u.app.lhs); */ +/* deref_term(term->u.app.rhs); */ +/* } */ + +/* // TODO: remove from hashmap? */ +/* if (--term->refs == 0) */ +/* free(term); */ +/* } */ |