aboutsummaryrefslogtreecommitdiff
path: root/src/parse.c
diff options
context:
space:
mode:
authorMarvin Borner2023-05-27 00:37:19 +0200
committerMarvin Borner2023-05-27 00:37:19 +0200
commitac039e6fcbdec3dc6c8e28013e1b3a20068c84ee (patch)
treee0b9db6a303e1369054b478cc44a8ea7d3d7d44d /src/parse.c
parent90a0366ba0556314b8624a3f46c667eaf5824e4c (diff)
Basic schedule initialization
Diffstat (limited to 'src/parse.c')
-rw-r--r--src/parse.c53
1 files changed, 29 insertions, 24 deletions
diff --git a/src/parse.c b/src/parse.c
index 24d2339..5445aea 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -8,9 +8,9 @@
#include <map.h>
#include <log.h>
-static int max_depth = 0;
+static size_t max_depth = 0;
-static struct term *new_term(term_type_t type, hash_t hash, int depth)
+static struct term *new_term(term_type_t type, hash_t hash, size_t depth)
{
struct term *term = malloc(sizeof(*term));
if (!term)
@@ -22,63 +22,68 @@ static struct term *new_term(term_type_t type, hash_t hash, int depth)
return term;
}
-static hash_t abs_blc(char **term, int depth)
+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;
- hash_t inner_hash = parse_blc(term, depth + 1);
- hash_t res = hash((uint8_t *)&res_type, sizeof(res_type), inner_hash);
+ struct term_handle inner = parse_blc(term, depth + 1);
+ hash_t res = hash((uint8_t *)&res_type, sizeof(res_type), inner.hash);
struct term *res_term;
if ((res_term = map_get(res))) {
- res_term->refs++;
- res_term->depth = (res_term->depth + depth) / 2;
+ update_term(res_term, depth);
} else {
res_term = new_term(res_type, res, depth);
- res_term->u.abs.term = inner_hash;
+ res_term->u.abs.term = inner.term;
map_set(res_term, res);
}
- return res;
+ return (struct term_handle){ .term = res_term, .hash = res };
}
-static hash_t app_blc(char **term, int depth)
+static struct term_handle app_blc(char **term, size_t depth)
{
term_type_t res_type = APP;
- hash_t lhs_hash = parse_blc(term, depth + 1);
- hash_t rhs_hash = parse_blc(term, depth + 1);
+ struct term_handle lhs = parse_blc(term, depth + 1);
+ struct term_handle rhs = parse_blc(term, depth + 1);
- hash_t res = hash((uint8_t *)&res_type, sizeof(res_type), lhs_hash);
- res = hash((uint8_t *)&res, sizeof(res), rhs_hash);
+ hash_t res = hash((uint8_t *)&res_type, sizeof(res_type), lhs.hash);
+ res = hash((uint8_t *)&res, sizeof(res), rhs.hash);
struct term *res_term;
if ((res_term = map_get(res))) {
- res_term->refs++;
- res_term->depth = (res_term->depth + depth) / 2;
+ update_term(res_term, depth);
} else {
res_term = new_term(res_type, res, depth);
- res_term->u.app.lhs = lhs_hash;
- res_term->u.app.rhs = rhs_hash;
+ res_term->u.app.lhs = lhs.term;
+ res_term->u.app.rhs = rhs.term;
map_set(res_term, res);
}
- return res;
+ return (struct term_handle){ .term = res_term, .hash = res };
}
-static hash_t var_blc(int index, int depth)
+static struct term_handle var_blc(int index, size_t depth)
{
term_type_t res_type = VAR;
hash_t res = hash((uint8_t *)&res_type, sizeof(res_type), index);
struct term *res_term;
if ((res_term = map_get(res))) {
- res_term->refs++;
- res_term->depth = (res_term->depth + depth) / 2;
+ update_term(res_term, depth);
} else {
res_term = new_term(res_type, res, depth);
res_term->u.var.index = index;
map_set(res_term, res);
}
- return res;
+
+ return (struct term_handle){ .term = res_term, .hash = res };
}
int parse_get_max_depth(void)
@@ -88,7 +93,7 @@ int parse_get_max_depth(void)
// TODO: bit parse_bblc
// TODO: BLoC support (needs entry reference index sth two passes)
-hash_t parse_blc(char **term, int depth)
+struct term_handle parse_blc(char **term, size_t depth)
{
if (depth > max_depth)
max_depth = depth;