From 492460f37e61b5c86269daa40b25d6f439ef264e Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Wed, 4 Aug 2021 11:16:00 +0100 Subject: [PATCH] Compiles and tests, but there are still major problems. --- src/arith/integer.h | 3 ++ src/memory/consspaceobject.c | 67 ++++++++++++++++++++++++++---------- src/memory/consspaceobject.h | 2 +- src/memory/hashmap.c | 24 ++++++++++--- src/memory/hashmap.h | 2 ++ src/memory/vectorspace.h | 6 ++++ 6 files changed, 81 insertions(+), 23 deletions(-) diff --git a/src/arith/integer.h b/src/arith/integer.h index f0117f5..4ce58d5 100644 --- a/src/arith/integer.h +++ b/src/arith/integer.h @@ -11,6 +11,9 @@ #ifndef __integer_h #define __integer_h +#include +#include + struct cons_pointer make_integer(int64_t value, struct cons_pointer more); struct cons_pointer add_integers(struct cons_pointer a, diff --git a/src/memory/consspaceobject.c b/src/memory/consspaceobject.c index 080158d..c240c4d 100644 --- a/src/memory/consspaceobject.c +++ b/src/memory/consspaceobject.c @@ -24,13 +24,30 @@ #include "intern.h" #include "print.h" #include "stack.h" +#include "vectorspace.h" /** - * True if the tag on the cell at this `pointer` is this `tag`, else false. + * True if the tag on the cell at this `pointer` is this `tag`, or, if the tag + * of the cell is `VECP`, if the tag of the vectorspace object indicated by the + * cell is this `tag`, else false. */ bool check_tag( struct cons_pointer pointer, char *tag ) { - struct cons_space_object cell = pointer2cell( pointer ); - return strncmp( &cell.tag.bytes[0], tag, TAGLENGTH ) == 0; + bool result = false; + struct cons_space_object cell = pointer2cell( pointer ); + + result = strncmp( &cell.tag.bytes[0], tag, TAGLENGTH ) == 0; + + if ( !result ) { + // if ( vectorpointp( pointer ) ) { <<< this line blows up! + // // struct vector_space_object *vec = pointer_to_vso( pointer ); + + // // if ( vec != NULL ) { + // // result = strncmp( &vec->header.tag.bytes[0], tag, TAGLENGTH ) == 0; + // // } + // } + } + + return result; } /** @@ -72,14 +89,22 @@ void dec_ref( struct cons_pointer pointer ) { * @return As a Lisp string, the tag of the object which is at that pointer. */ struct cons_pointer c_type( struct cons_pointer pointer ) { - struct cons_pointer result = NIL; - struct cons_space_object cell = pointer2cell( pointer ); + struct cons_pointer result = NIL; + struct cons_space_object cell = pointer2cell( pointer ); + + if ( strncmp( (char *)&cell.tag.bytes, VECTORPOINTTAG, TAGLENGTH ) == 0 ) { + struct vector_space_object *vec = pointer_to_vso( pointer ); for ( int i = TAGLENGTH - 1; i >= 0; i-- ) { - result = make_string( ( wchar_t ) cell.tag.bytes[i], result ); + result = make_string( (wchar_t)vec->header.tag.bytes[i], result ); } + } else { + for ( int i = TAGLENGTH - 1; i >= 0; i-- ) { + result = make_string( (wchar_t)cell.tag.bytes[i], result ); + } + } - return result; + return result; } /** @@ -227,16 +252,19 @@ struct cons_pointer make_nlambda( struct cons_pointer args, } /** - * Return a hash value for this string. + * Return a hash value for this string like thing. * * What's important here is that two strings with the same characters in the * same order should have the same hash value, even if one was created using * `"foobar"` and the other by `(append "foo" "bar")`. I *think* this function * has that property. I doubt that it's the most efficient hash function to * have that property. - */ -uint32_t calculate_hash( wint_t c, struct cons_pointer ptr) { -struct cons_space_object *cell = &pointer2cell(ptr); + * + * returns 0 for things which are not string like. + */ +uint32_t calculate_hash(wint_t c, struct cons_pointer ptr) +{ + struct cons_space_object *cell = &pointer2cell(ptr); uint32_t result = 0; switch (cell->tag.value) @@ -244,13 +272,16 @@ struct cons_space_object *cell = &pointer2cell(ptr); case KEYTV: case STRINGTV: case SYMBOLTV: - if (nilp(ptr)) { - result =(uint32_t) c; - } else { - result = ((uint32_t)c * - cell->payload.string.hash) & - 0xffffffff; - } + if (nilp(ptr)) + { + result = (uint32_t)c; + } + else + { + result = ((uint32_t)c * + cell->payload.string.hash) & + 0xffffffff; + } } return result; diff --git a/src/memory/consspaceobject.h b/src/memory/consspaceobject.h index 7bf34de..486efe2 100644 --- a/src/memory/consspaceobject.h +++ b/src/memory/consspaceobject.h @@ -21,7 +21,7 @@ #include #include "io/fopen.h" -#include "memory/conspage.h" +// #include "memory/conspage.h" /** diff --git a/src/memory/hashmap.c b/src/memory/hashmap.c index fcd69e4..edabb89 100644 --- a/src/memory/hashmap.c +++ b/src/memory/hashmap.c @@ -10,6 +10,7 @@ #include "arith/integer.h" #include "memory/consspaceobject.h" #include "memory/hashmap.h" +#include "memory/vectorspace.h" /** * Get the hash value for the cell indicated by this `ptr`; currently only @@ -45,17 +46,32 @@ struct cons_pointer lisp_get_hash(struct stack_frame *frame, } /** - * Make a hashmap with this number of buckets. + * Make a hashmap with this number of buckets, using this `hash_fn`. If + * `hash_fn` is `NIL`, use the standard hash funtion. */ -struct cons_pointer make_hashmap( uint32_t n_buckets) { +struct cons_pointer make_hashmap( uint32_t n_buckets, struct cons_pointer hash_fn) { struct cons_pointer result = make_vso(HASHTAG, (sizeof(struct cons_pointer) * (n_buckets + 1)) + (sizeof(uint32_t) * 2)); - // TODO: fill in the payload! - struct hashmap_payload *payload = (struct hashmap_payload *) &pointer_to_vso(result)->payload; + payload->hash_fn = hash_fn; + payload->n_buckets = n_buckets; + for (int i = 0; i < n_buckets; i++) { + payload->buckets[i] = NIL; + } + + return result; +} + +struct cons_pointer clone_hashmap(struct cons_pointer ptr) { + struct cons_pointer result = NIL; + + if (hashmapp(ptr)) { + + } + return result; } \ No newline at end of file diff --git a/src/memory/hashmap.h b/src/memory/hashmap.h index b834f5a..813211b 100644 --- a/src/memory/hashmap.h +++ b/src/memory/hashmap.h @@ -10,6 +10,8 @@ #ifndef __psse_hashmap_h #define __psse_hashmap_h +#include "arith/integer.h" +#include "memory/conspage.h" #include "memory/consspaceobject.h" #include "memory/vectorspace.h" diff --git a/src/memory/vectorspace.h b/src/memory/vectorspace.h index 22b0d88..15740ac 100644 --- a/src/memory/vectorspace.h +++ b/src/memory/vectorspace.h @@ -28,18 +28,24 @@ #define HASHTAG "HASH" #define HASHTV 0 +#define hashmapp(conspoint)((check_tag(conspoint,HASHTAG))) + /* * a namespace (i.e. a binding of names to values, implemented as a hashmap) */ #define NAMESPACETAG "NMSP" #define NAMESPACETV 0 +#define namespacep(conspoint)(check_tag(conspoint,NAMESPACETAG)) + /* * a vector of cons pointers. */ #define VECTORTAG "VECT" #define VECTORTV 0 +#define vectorp(conspoint)(check_tag(conspoint,VECTORTAG)) + /** * given a pointer to a vector space object, return the object. */