From: Elijah Cohen Date: Mon, 22 Jul 2024 17:28:36 +0000 (-0500) Subject: shoddy dict implemented properly X-Git-Url: https://git.eli173.com/?a=commitdiff_plain;h=acdffdb31d8f8f5610a7188b261fe4112ea5691a;p=klapaucius shoddy dict implemented properly --- diff --git a/src/dict.c b/src/dict.c index 7ff7bb5..fc2053e 100644 --- a/src/dict.c +++ b/src/dict.c @@ -3,8 +3,7 @@ #include "sexpr.h" #include "dict.h" -#define HT_SIZE (2<<16) - +#include // okay, doing an incredibly /incredibly/ naive dict implementation for now, // proper hash tables come later @@ -17,23 +16,19 @@ Sexpr* init_dict() { Sexpr* append_to_dict(Sexpr* dict, Sexpr* key, Sexpr* value) { // assumes dict well-formed, returns new dict + // puts new things on the front of the dict, makes it so looking up gets the newest stuff first Sexpr* new = cons(key, value); return cons(new, dict); } - - - - -uint64_t hash(char* s) { - uint64_t hash = 0; - uint64_t p = 31; - uint64_t p_to_pow = 1; - uint64_t m = HT_SIZE; - if(s==NULL) return hash; - while(*s != '\0') { - hash = (hash + p_to_pow*(*s - 'A'+1)) % m; - p_to_pow = (p_to_pow*p) % m; +Sexpr* lookup(Sexpr* dict, Sexpr* key) { + // assumes dict well-formed + Sexpr* node = dict; + while(node->type != NIL) { + if(equal(key, car(car(node)))->type == T) { + return cdr(car(node)); + } + node = cdr(node); } - return hash; + return from_nil(); } diff --git a/src/dict.h b/src/dict.h index 612078a..fe1aeea 100644 --- a/src/dict.h +++ b/src/dict.h @@ -9,12 +9,8 @@ // key type is string, value type is sexpr, hash range is uint64_t -typedef struct KV_Pair { - uint64_t key; - Sexpr* val; -} KV_Pair_t; - -uint64_t hash(char* s); - +Sexpr* init_dict(); +Sexpr* append_to_dict(Sexpr* dict, Sexpr* key, Sexpr* value); +Sexpr* lookup(Sexpr* dict, Sexpr* key); #endif diff --git a/src/sexpr.c b/src/sexpr.c index 01286af..126b124 100644 --- a/src/sexpr.c +++ b/src/sexpr.c @@ -14,6 +14,14 @@ Sexpr* from_nil() { return ret; } +Sexpr* from_t() { + Sexpr* ret = malloc(sizeof(Sexpr)); + ret->type = T; + ret->value.t = NULL; + return ret; +} + + Sexpr* from_sym(char* s) { Sexpr* ret = malloc(sizeof(Sexpr)); ret->type = SYM; @@ -46,6 +54,24 @@ Sexpr* cdr(Sexpr* s) { return s->value.c->cdr; } +Sexpr* equal(Sexpr* a, Sexpr* b) { + // need to change later with proper builtins and so forth + if(a->type != b->type) + return from_nil(); + Sexpr_Type t = a->type; + if(t == NIL) + return from_t(); + if(t == T) + return from_t(); + + if(t == SYM) { + return strcmp(a->value.s, b->value.s) == 0 ? from_t() : from_nil(); + } + // leaving everything else off for... reasons + return from_nil(); +} + + Sexpr* reverse(Sexpr* s) { if(s->type != CONS) { diff --git a/src/sexpr.h b/src/sexpr.h index 4bc42d3..335c4b9 100644 --- a/src/sexpr.h +++ b/src/sexpr.h @@ -6,6 +6,8 @@ Sexpr* from_nil(); +Sexpr* from_t(); + Sexpr* from_sym(char* s); Sexpr* from_uint(uint64_t u); @@ -18,6 +20,8 @@ Sexpr* cdr(Sexpr* s); Sexpr* reverse(Sexpr* s); +Sexpr* equal(Sexpr* a, Sexpr* b); + char* sprint_sexpr(Sexpr* s); #endif diff --git a/src/test.c b/src/test.c index 01277d0..550e6fb 100644 --- a/src/test.c +++ b/src/test.c @@ -5,6 +5,7 @@ #include "types.h" #include "sexpr.h" #include "parser.h" +#include "dict.h" @@ -66,11 +67,33 @@ void test_parser() { printf("_: %s\n", sprint_sexpr(parse("(a b (3 2 (5) c) d (e f) g)"))); } +void test_eq() { + printf("testing eq\n"); + printf("t: %s\n", sprint_sexpr(from_t())); + Sexpr* a = from_sym("test"); + Sexpr* b = from_sym("test"); + printf("%s\n", sprint_sexpr(equal(a, b))); +} + +void test_dict() { + printf("testing dict\n"); + Sexpr* d = init_dict(); + d = append_to_dict(d, from_sym("hello"), from_sym("world")); + d = append_to_dict(d, from_sym("asdf"), from_uint(544)); + printf("lookup hello: %s\n", sprint_sexpr(lookup(d, from_sym("hello")))); + printf("lookup asdf: %s\n", sprint_sexpr(lookup(d, from_sym("asdf")))); + + +} + + void run_tests(){ - test_basics(); - test2(); - test_parsing(); - test_parser(); + //test_basics(); + //test2(); + //test_parsing(); + //test_parser(); + //test_eq(); + test_dict(); } int main() {