From cd9d103d12020ecfa838ee0d4533e08ea650a582 Mon Sep 17 00:00:00 2001 From: mtardy Date: Wed, 24 Jun 2020 18:06:05 +0200 Subject: [PATCH] Add draft for correct symbol table implementation --- draft.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 draft.c diff --git a/draft.c b/draft.c new file mode 100644 index 0000000..18bcee0 --- /dev/null +++ b/draft.c @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#define SYMBOL_TABLE_CHUNK 4 + +typedef struct symbol_t { + char *ident; + bool defined; + int value; +} symbol_t; + + +typedef struct table_t { + symbol_t **array; + int size; + int capacity; +} table_t; + +table_t table; + +typedef struct bsearch_t { + int pos; + bool found; +} bsearch_t; + +bsearch_t binary_search(symbol_t *arr[], int l, int r, char *ident) +{ + while (r >= l) { + int mid = l + (r - l) / 2; + int cmpres = strcmp(arr[mid]->ident, ident); + if (cmpres > 0) { + r = mid - 1; + } else if (cmpres < 0) { + l = mid + 1; + } else { + bsearch_t res = { mid, true }; + return res; + } + } + bsearch_t res = { l, false }; + return res; +} + +int insert(table_t *table, symbol_t *element, int pos) +{ + if (pos < 0 || pos > table->size) { + return -1; + } + + if (table->size >= table->capacity) { + table->array = realloc(table->array, sizeof(symbol_t *) * (table->capacity + SYMBOL_TABLE_CHUNK)); + if (table->array == NULL) { + printf("Error: running out of memory\n"); + exit(1); + } + table->capacity += SYMBOL_TABLE_CHUNK; + } + + for (int i = table->size; i > pos; i--) { + table->array[i] = table->array[i-1]; + } + table->array[pos] = element; + return ++(table->size); +} + +symbol_t *intern(char *ident, bool create) +{ + bsearch_t res = binary_search(table.array, 0, table.size - 1, ident); + printf("pos:%d\n", res.pos); + if (create) { + symbol_t *new_symbol = malloc(sizeof(symbol_t)); + new_symbol->ident = strdup(ident); + new_symbol->defined = false; + if (res.found) { + free(table.array[res.pos]); + table.array[res.pos] = new_symbol; + } else { + printf("insert:%d\n", insert(&table, new_symbol, res.pos)); + } + return new_symbol; + } else { + if (res.found) { + return table.array[res.pos]; + } else { + return NULL; + } + } +} + +void init_table() +{ + table.array = malloc(sizeof(symbol_t *) * SYMBOL_TABLE_CHUNK); + if (table.array == NULL) { + printf("Error: running out of memory\n"); + exit(1); + } + table.size = 0; + table.capacity = SYMBOL_TABLE_CHUNK; +} + +int main() +{ + init_table(); + char line[256]; + intern("chaussure\n", true); + while (fgets(line, sizeof(line), stdin)) { + printf("intern:%p\n", intern(line, true)); + printf("after size:%d\n", table.size); + printf("after capacity:%d\n", table.capacity); + printf("\n"); + for (int i = 0; i < table.size; i++) { + printf("%d.%s", i, table.array[i]->ident); + } + printf("\n"); + } +} \ No newline at end of file