From 6c1e4bbfe920feda675d8ea3a13281742d55c334 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Thu, 5 Jan 2023 22:29:35 -0800 Subject: hash table bug finally fixed forever --- src/hash_table.c | 61 +++++++++++++++++++++++++++++++++++++------------------- src/main.c | 29 +++++++++++++++++++++++---- src/parser.c | 5 ++++- src/visitor.c | 3 ++- 4 files changed, 71 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/hash_table.c b/src/hash_table.c index 99508e9..7a978e2 100644 --- a/src/hash_table.c +++ b/src/hash_table.c @@ -35,38 +35,55 @@ sl_list_t *init_sl_list() { } /* TODO: fix segfault bug */ +/* void sl_list_add(sl_list_t *l, char *key, ast_t *value) { */ +/* sl_node_t *cur = l->head; */ +/* bool modified = false; */ +/* if (l->head == NULL) { */ +/* l->head = init_sl_node(key, value); */ +/* l->size++; */ +/* return; */ +/* } */ + +/* for (int i = 0; i < l->size - 1; i++) { */ +/* if (strcmp(cur->value->key, key) == 0) { */ +/* cur->value->value = value; */ +/* modified = true; */ +/* break; */ +/* } */ +/* cur = cur->next; */ +/* } */ + +/* if (strcmp(cur->value->key, key) == 0) { */ +/* cur->value->value = value; */ +/* modified = true; */ +/* } */ + +/* if (!modified) { */ +/* cur->next = init_sl_node(key, value); */ +/* l->size++; */ +/* } */ +/* } */ + void sl_list_add(sl_list_t *l, char *key, ast_t *value) { + printf("sl list add %s\n", key); sl_node_t *cur = l->head; - bool modified = false; - if (l->head == NULL) { + if (cur == NULL) { l->head = init_sl_node(key, value); l->size++; - return; - } - - for (int i = 0; i < l->size - 1; i++) { - if (strcmp(cur->value->key, key) == 0) { - cur->value->value = value; - modified = true; - break; + } else { + while (cur->next != NULL) { + cur = cur->next; } - cur = cur->next; - } - - if (strcmp(cur->value->key, key) == 0) { - cur->value->value = value; - modified = true; - } - - if (!modified) { cur->next = init_sl_node(key, value); + printf("sl list cur->next %s\n", cur->next->value->key); l->size++; } } - ast_t *sl_list_get(sl_list_t *l, char *key) { sl_node_t *cur = l->head; for (int i = 0; i < l->size; i++) { + if (cur == NULL) + return NULL; if (strcmp(cur->value->key, key) == 0) return cur->value->value; cur = cur->next; @@ -97,7 +114,7 @@ hash_table_t *init_hash_table(int size) { if (h == NULL) die("malloc on hash table"); h->size = size; - h->buckets = malloc(sizeof(sl_list_t *)); + h->buckets = malloc(size * sizeof(sl_list_t *)); if (h->buckets == NULL) die("malloc on buckets"); for (int i = 0; i < h->size; i++) @@ -106,6 +123,7 @@ hash_table_t *init_hash_table(int size) { } void hash_table_add(hash_table_t *h, char *key, ast_t *value) { + printf("entering here? %s\n", key); sl_list_t *l = h->buckets[hash(key, h->size)]; sl_list_add(l, key, value); } @@ -116,6 +134,7 @@ ast_t *hash_table_get(hash_table_t *h, char *key) { } bool hash_table_exists(hash_table_t *h, char *key) { + printf("entering here? %s\n", key); sl_list_t *l = h->buckets[hash(key, h->size)]; return sl_list_exists(l, key); } diff --git a/src/main.c b/src/main.c index f418925..56608c1 100644 --- a/src/main.c +++ b/src/main.c @@ -78,14 +78,35 @@ int main(int argc, char **argv) { /* print(res); */ /* DONE: TEST NON-BUILTIN FUNCTIONS (stack frame) */ - lexer_t *lexer = init_lexer( - "(bind hello 3) ((lambda (x y) (+ x y)) (+ hello 4) 4) (+ 3 4)"); + lexer_t *lexer = init_lexer("(bind my_func (lambda (x y) (+ x " + "y))) (my_func (+ 3 4) 4) (+ 3 4)"); + printf("%ld\n", hash("my_func", 100)); + printf("%ld\n", hash("my_func", 100)); + printf("debug 1\n"); parser_t *parser = init_parser(lexer); + printf("debug 2\n"); visitor_t *visitor = init_visitor(parser); + printf("debug 3\n"); ast_t *root = eval(visitor); + printf("debug 4\n"); print_root(root); - /* ast_t *res = root->subnodes[0]; */ - /* print(res); */ + printf("debug 5\n"); + ast_t *res = root->subnodes[0]; + print(res); + + /* lexer_t *lexer = init_lexer("((lambda (x y) (+ x y)) (+ 3 4) 4) (+ 3 + * 4)"); */ + /* printf("debug 1\n"); */ + /* parser_t *parser = init_parser(lexer); */ + /* printf("debug 2\n"); */ + /* visitor_t *visitor = init_visitor(parser); */ + /* printf("debug 3\n"); */ + /* ast_t *root = eval(visitor); */ + /* printf("debug 4\n"); */ + /* print_root(root); */ + /* printf("debug 5\n"); */ + /* ast_t *res = root->subnodes[0]; */ + /* print(res); */ /* TODO: TEST REPL POSSIBILITY */ /* printf("Welcome to the NXS REPL.\n"); */ diff --git a/src/parser.c b/src/parser.c index b5fc57e..ba9788a 100644 --- a/src/parser.c +++ b/src/parser.c @@ -129,13 +129,14 @@ void parse_bind(parser_t *parser) { parser_error(parser); token_t *t = parser->tokens[parser->i]; char *name = t->value; + printf("%s\n", name); parser_move(parser); ast_t *expr = parse_expr(parser); /* unevaluated expr will be evaluated when hash table transfers to visitor JIT */ if (expr == NULL) parser_error(parser); hash_table_add(parser->symbol_table, name, expr); - + printf("after add\n"); if (parser->tokens[parser->i]->type != TOKEN_RPAREN) parser_error(parser); @@ -153,6 +154,7 @@ ast_t *parse_list(parser_t *parser) { while (current_token->type != TOKEN_RPAREN) { if (current_token->type == TOKEN_ID) { if (strcmp(current_token->value, "lambda") == 0 && first_entry) { + printf("lambda here\n"); return parse_function(parser); } else if (strcmp(current_token->value, "bind") == 0 && first_entry) { parse_bind(parser); @@ -221,6 +223,7 @@ ast_t *parse_all(parser_t *parser) { while (t != NULL) { cur = parse_expr(parser); if (cur == NULL) { + printf("this is happening\n"); t = parser->tokens[parser->i]; continue; } diff --git a/src/visitor.c b/src/visitor.c index 135c4d5..4a4d87f 100644 --- a/src/visitor.c +++ b/src/visitor.c @@ -68,7 +68,7 @@ bool is_built_in(ast_t *e) { ast_t *eval_symbol(visitor_t *v, ast_t *e) { /* hash_table_t *lmao = stack_peek(v->stack_frame); */ hash_table_t *h = stack_peek(v->stack_frame); - + printf("%s\n", e->string_value); if (is_built_in(e)) return e; /* first, it looks in the stack frame for a variable */ @@ -87,6 +87,7 @@ ast_t *eval_symbol(visitor_t *v, ast_t *e) { hash_table_add(v->eval_table, e->string_value, eval); return eval; } else { + printf("this eval error\n"); eval_error(v, e); return NULL; } -- cgit