#include "sexpr.h"
#include "dict.h"
-#define HT_SIZE (2<<16)
-
+#include <stdio.h>
// okay, doing an incredibly /incredibly/ naive dict implementation for now,
// proper hash tables come later
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();
}
// 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
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;
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) {
Sexpr* from_nil();
+Sexpr* from_t();
+
Sexpr* from_sym(char* s);
Sexpr* from_uint(uint64_t u);
Sexpr* reverse(Sexpr* s);
+Sexpr* equal(Sexpr* a, Sexpr* b);
+
char* sprint_sexpr(Sexpr* s);
#endif
#include "types.h"
#include "sexpr.h"
#include "parser.h"
+#include "dict.h"
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() {