aboutsummaryrefslogtreecommitdiffstats
path: root/src/parser.c
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-30 10:13:11 +0200
committerBad Diode <bd@badd10de.dev>2021-10-30 10:13:11 +0200
commitaf3f2ebc42bf9dc64fc581b9f8f79e98895cb417 (patch)
tree5065fe2cc5ac04a7dcd2558f97e01222721d0664 /src/parser.c
parentf9a6691243d59915dad8785a321ca021bb27de27 (diff)
downloadbdl-af3f2ebc42bf9dc64fc581b9f8f79e98895cb417.tar.gz
bdl-af3f2ebc42bf9dc64fc581b9f8f79e98895cb417.zip
Add hashtable for Environment tracking
Diffstat (limited to 'src/parser.c')
-rw-r--r--src/parser.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/parser.c b/src/parser.c
index 2215fad..e16fc4a 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -4,6 +4,15 @@
4static Object **objects = NULL; 4static Object **objects = NULL;
5static Root *roots = NULL; 5static Root *roots = NULL;
6 6
7
8uint64_t
9symbol_hash(const HashTable *table, void *key) {
10 Object *obj = key;
11 uint64_t hash = _xor_shift_hash(obj->text, array_size(obj->text));
12 hash = _fibonacci_hash(hash, table->shift_amount);
13 return hash;
14}
15
7Token 16Token
8peek_token(const Parser *parser) { 17peek_token(const Parser *parser) {
9 return parser->tokens[parser->current]; 18 return parser->tokens[parser->current];
@@ -391,6 +400,16 @@ parse(Token *tokens, Errors *errors) {
391 array_push(roots, root); 400 array_push(roots, root);
392 } 401 }
393 402
403 // TEST INSERTION/LOOKUP.
404 HashTable *table = ht_init(symbol_hash, object_equal);
405 ht_insert(table, &roots[0][0], &roots[1][0]);
406 ht_insert(table, &roots[2][0], &roots[3][0]);
407 Object *val = ht_lookup(table, &roots[2][0]);
408 object_display(val);
409 val = ht_lookup(table, &roots[0][0]);
410 object_display(val);
411 ht_free(table);
412
394 // Perform semantic analysis. 413 // Perform semantic analysis.
395 // TODO: Check that symbols are defined before usage. 414 // TODO: Check that symbols are defined before usage.
396 // TODO: Remove unnecessary statements. 415 // TODO: Remove unnecessary statements.
@@ -522,3 +541,37 @@ object_display(Object *obj) {
522 } 541 }
523 return; 542 return;
524} 543}
544
545bool
546object_equal(Object *a, Object *b) {
547 if (a == NULL || b == NULL || a->type != b->type) {
548 return false;
549 }
550 switch (a->type) {
551 case OBJ_TYPE_TRUE:
552 case OBJ_TYPE_FALSE: {
553 return true;
554 } break;
555 case OBJ_TYPE_FIXNUM: {
556 return a->fixnum == b->fixnum;
557 } break;
558 case OBJ_TYPE_SYMBOL:
559 case OBJ_TYPE_STRING: {
560 if (array_size(a->text) != array_size(b->text)) {
561 return false;
562 }
563 for (size_t i = 0; i < array_size(a->text); i++) {
564 if (a->text[i] != b->text[i]) {
565 return false;
566 }
567 }
568 } break;
569 case OBJ_TYPE_LAMBDA: {
570 // return a->closure == b.closure;
571 } break;
572 default: {
573 return false;
574 } break;
575 }
576 return true;
577}