From 2cc076fa43c60961c657dce32f932cda397f92be Mon Sep 17 00:00:00 2001 From: FelixBrendel Date: Thu, 3 Oct 2019 16:52:33 +0200 Subject: [PATCH] updated to use type-extensible hashmaps --- src/built_ins.cpp | 1 + src/error.cpp | 2 +- src/ftb | 2 +- src/memory.cpp | 2 +- src/structs.cpp | 31 ++++++++++++++++++++++++++++++- 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/built_ins.cpp b/src/built_ins.cpp index 9b35af7..8a370e2 100644 --- a/src/built_ins.cpp +++ b/src/built_ins.cpp @@ -426,6 +426,7 @@ proc load_built_ins_into_environment() -> void { define_special((define definee (:doc "") . body), "TODO") { fetch(definee, doc, body); + // print_hm(get_current_environment()->hm); try assert_type(doc, Lisp_Object_Type::String); // if no doc string, we dont have to store it diff --git a/src/error.cpp b/src/error.cpp index 4b54fed..124c63e 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -16,7 +16,7 @@ proc create_error(const char* c_file_name, int c_file_line, Lisp_Object* type, S delete_error(); if (Globals::breaking_on_errors) { - // debug_break(); + debug_break(); } // visualize_lisp_machine(); diff --git a/src/ftb b/src/ftb index 8545932..2f6954b 160000 --- a/src/ftb +++ b/src/ftb @@ -1 +1 @@ -Subproject commit 854593273fdf8039d8ac1ac7150527fca47818ef +Subproject commit 2f6954b520f0175e801a78dbfb00ab026f75c1bf diff --git a/src/memory.cpp b/src/memory.cpp index 1668042..2c75230 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -387,7 +387,7 @@ namespace Memory { if (parent) append_to_array_list(&env->parents, parent); - env->hm = create_hashmap(); + env->hm = create_String_hashmap(); return env; } diff --git a/src/structs.cpp b/src/structs.cpp index 7b9008e..797831e 100644 --- a/src/structs.cpp +++ b/src/structs.cpp @@ -2,12 +2,41 @@ struct Lisp_Object; struct String; struct Environment; +// ARRAY LISTS define_array_list(Lisp_Object*, Lisp_Object); define_array_list(Environment*, Environment); define_array_list(String*, String); define_array_list(int, Int); define_array_list(void*, Void_Ptr); +// HASH MAPS +inline bool hm_objects_match(char* a, char* b) { + return strcmp(a, b) == 0; +} + +inline bool hm_objects_match(void* a, void* b) { + return a == b; +} + +u32 hm_hash(void* ptr) { + return ((unsigned long long)ptr * 2654435761) % 4294967296; +} + +u32 hm_hash(char* str) { + u32 value = str[0] << 7; + int i = 0; + while (str[i]) { + value = (10000003 * value) ^ str[i++]; + } + return value ^ i; +} + +define_hash_map(char*, String); +define_hash_map(void*, Void_Ptr); +#define for_str_hash_map(hm) __for_hm_generator(char*, hm) +#define for_ptr_hash_map(hm) __for_hm_generator(void*, hm) + +// STRUCTS enum struct Thread_Type { Main, GarbageCollection @@ -111,7 +140,7 @@ struct Arguments { struct Environment { Environment_Array_List parents; - StringHashMap* hm; + String_Hash_Map* hm; // int capacity; // int next_index;