From f6fa7bba675f78365f7f31d44fb96b52b11dfa77 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Wed, 18 Dec 2019 22:59:08 -0500 Subject: added identifier IDs --- blockarr.c | 9 +++++++++ identifiers.c | 30 ++++++++++++++++++++++-------- types.h | 4 ++-- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/blockarr.c b/blockarr.c index 1d8590d..cd4d343 100644 --- a/blockarr.c +++ b/blockarr.c @@ -39,6 +39,15 @@ static void *block_arr_add(BlockArr *arr) { } } +static U64 block_arr_len(BlockArr *arr) { + ArrBlock *last_block; + if (!arr->blocks) return 0; + last_block = arr->blocks + (arr_len(arr->blocks) - 1); + U64 len = (arr_len(arr->blocks)-1) * (((U64)1)<lg_block_sz); + len += last_block->n; + return len; +} + static inline void *block_arr_get(BlockArr *arr, size_t index) { size_t block_index = index >> arr->lg_block_sz; ArrBlock *block = &arr->blocks[block_index]; diff --git a/identifiers.c b/identifiers.c index c8c5047..ef277e9 100644 --- a/identifiers.c +++ b/identifiers.c @@ -41,6 +41,7 @@ static Identifier ident_new(Identifiers *ids, Identifier parent, unsigned char i if (parent) tree->depth = (uint16_t)(parent->depth + 1); tree->index_in_parent = index_in_parent; + tree->id = block_arr_len(&ids->trees); return tree; } @@ -84,19 +85,32 @@ static Identifier ident_insert(Identifiers *ids, char **s) { tree->children[c_high] = ident_new(ids, tree, c_high); } tree = tree->children[c_high]; - (*s)++; + ++(*s); } } static void fprint_ident(FILE *out, Identifier id) { - assert(id); - if (id->parent == NULL) return; /* at root */ - fprint_ident(out, id->parent->parent); /* to go up one character, we need to go to the grandparent */ - int c_low = id->parent->index_in_parent; - int c_high = id->index_in_parent; - int c = ident_uchar_to_char(c_low + (c_high << 4)); - fputc(c, out); + Identifier i = id; + size_t chars = 0; + while (i->parent) { + i = i->parent->parent; /* to go up one character, we need to go to the grandparent */ + ++chars; + } + printf("%lu:",(unsigned long)id->id); + char *s = malloc(chars + 1); + char *p = s + chars; + i = id; + *p-- = '\0'; + while (i->parent) { + int c_low = i->parent->index_in_parent; + int c_high = i->index_in_parent; + char c = (char)ident_uchar_to_char(c_low + (c_high << 4)); + *p-- = c; + i = i->parent->parent; + } + fprintf(out, "%s", s); + free(s); } static void print_ident(Identifier id) { diff --git a/types.h b/types.h index fef7492..015ec9a 100644 --- a/types.h +++ b/types.h @@ -103,8 +103,7 @@ typedef struct ArrBlock { typedef struct BlockArr { size_t item_sz; int lg_block_sz; - /* NOTE: dynamic array tends to over-allocate, so we're using our own */ - ArrBlock *blocks; + ArrBlock *blocks; } BlockArr; typedef struct HashTable { @@ -174,6 +173,7 @@ typedef struct IdentTree { /* zero value is an empty trie */ uint16_t depth; unsigned char index_in_parent; /* index of this in .parent.children */ + U64 id; /* unique integer associated with this identifier - also index in Identifiers.trees */ struct IdentTree *parent; struct IdentTree *children[TREE_NCHILDREN]; IdentDecl *decls; /* array of declarations of this identifier */ -- cgit v1.2.3