From bd6b26331bc55fb5d5e1610d2d313fab410a0b15 Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Tue, 19 Apr 2022 16:53:36 -0400 Subject: [PATCH] A crumb of cleanup. --- src/examples/forbble2.pbl | 18 ++++++++---------- src/hash.c | 9 ++++----- src/hash.h | 14 ++++++++++++-- src/object.c | 2 -- src/object.h | 2 ++ 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/examples/forbble2.pbl b/src/examples/forbble2.pbl index 0d6167f..aaf976b 100644 --- a/src/examples/forbble2.pbl +++ b/src/examples/forbble2.pbl @@ -59,9 +59,7 @@ (def compile (fn (words) ( (def name (second words)) (def code (get-code (rest (rest words)))) - (prnl (cat "name:" name " code: " code)) (h-insert dictionary name code) - (prnl (h-get dictionary name)) ))) (def not (fn (bool) @@ -80,30 +78,30 @@ (def noterr (fn (e) (not (iserr e)))) -(def _fmap (fn (words) ( - ;(prnl (cat "fmap: " word)) +(def fmap (fn (words) ( (def word (at 0 words)) (if (iserr word) () ( ; Define a user function (if (= ":" word) (compile words) ( ; Or read a user-defined function - (if (noterr (h-get dictionary word)) (_fmap (h-get dictionary word)) ( + (if (noterr (h-get dictionary word)) (fmap (h-get dictionary word)) ( ; Or check the operations table (if (noterr (switch word operations)) () ( ; Or evaluate - (if (noterr (eval word)) (stkadd (eval word)) ( + (def evaluated-word (eval word)) + (if (noterr evaluated-word) (stkadd evaluated-word) ( ; Or add as a string (stkadd word) )))))) - (_fmap (rest words)) + (fmap (rest words)) )) )) ))) -(def fmap (fn (text) ( +(def feval (fn (text) ( (def words (get-words text)) - (_fmap words) + (fmap words) ))) (def esc (ch 27)) @@ -113,7 +111,7 @@ ; Override the normal REPL prompt (set prompt fprompt) (set preprocess (fn (text) ( - (fmap text) + (feval text) (prn nl) "" ; Have the underlying REPL do nothing ))) diff --git a/src/hash.c b/src/hash.c index 722791d..e69e82a 100644 --- a/src/hash.c +++ b/src/hash.c @@ -46,7 +46,7 @@ void deleteTable(struct ObjectTable* table) #ifndef HASHLESS_ENV -static size_t hash(const char* str, struct ObjectTable* table) +size_t hash(const char* str, unused const struct ObjectTable* table) { size_t hash = 5381; char c; @@ -125,6 +125,9 @@ static size_t hash(unused const char* str, struct ObjectTable* table) #endif +/** + * \return The hash value of the given name + */ size_t addStripped(struct ObjectTable* table, char* name, struct StrippedObject object) { extendTable(table); @@ -146,10 +149,6 @@ size_t addStripped(struct ObjectTable* table, char* name, struct StrippedObject return initial; } -/// -/// \param table -/// \param name Should be a new allocation -/// \param object Should already be cloned size_t addToTable(struct ObjectTable* table, char* name, Object object) { return addStripped(table, name, (struct StrippedObject) { diff --git a/src/hash.h b/src/hash.h index 52d81bd..22d3e1e 100644 --- a/src/hash.h +++ b/src/hash.h @@ -11,10 +11,20 @@ struct ObjectTable { struct ObjectTable buildTable(size_t capacity); -/// Returns the hash of the inserted object +/// Hash the given string +/// \param str The string to hash +/// \param table The table the hash should be generated for (currently ignored by the actual hashing implementation) +/// \return The hash value of the given string +size_t hash(const char* str, const struct ObjectTable* table); + +/// \param table Where to insert the object +/// \param name Should be a new allocation +/// \param object Should already be cloned +/// \return The hash value of the name used to insert size_t addToTable(struct ObjectTable* table, char* name, Object object); -struct StrippedObject* getWithHash(struct ObjectTable* table, size_t hash); +/// \todo +struct StrippedObject* getWithHash(struct ObjectTable* table, const char* name, size_t hash); struct StrippedObject* getFromTable(struct ObjectTable* table, const char* name); diff --git a/src/object.c b/src/object.c index 0ab62ab..84d18ed 100644 --- a/src/object.c +++ b/src/object.c @@ -776,8 +776,6 @@ inline Object symFromSlice(const char* string, int len) return o; } -/// Creates a stringy object with room for a reference-count prefix and trailing null byte. -/// Thus, withLen(3, TYPE_STRING) will actually allocate a 5-byte "string". inline Object withLen(size_t len, enum Type type) { Object o = newObject(type); diff --git a/src/object.h b/src/object.h index 5865658..f21593c 100644 --- a/src/object.h +++ b/src/object.h @@ -289,6 +289,8 @@ Object stringFromSlice(const char* string, int len); Object symFromSlice(const char* string, int len); +/// Creates a stringy object with room for a reference-count prefix and trailing null byte. +/// Thus, withLen(3, TYPE_STRING) will actually allocate a 5-byte "string". Object withLen(size_t len, enum Type type); Object boolObject(int b);