From b26771d33c4655848c1a0086e0f97e3a6d8d7d8f Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Mon, 18 Apr 2022 22:17:17 -0400 Subject: [PATCH] Fleshing out ObjectTableObject. Also fleshing out forbble2. Fix _readFile() memory leak. --- src/examples/forbble2.pbl | 49 ++++++++++++++++++++++++++------------- src/hash.c | 17 +++++++++----- src/pebblisp.c | 1 + 3 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/examples/forbble2.pbl b/src/examples/forbble2.pbl index 54b0c08..71aad6e 100644 --- a/src/examples/forbble2.pbl +++ b/src/examples/forbble2.pbl @@ -2,6 +2,7 @@ ; Initialize an empty stack (def stack ()) +(def dictionary (table)) (def stkadd (fn (a) "Add the given value to the stack" @@ -46,28 +47,44 @@ (prnl (pop)) ))) -(def compile (fn (words) ( - (def name (at 0 words)) - (set words (rest words)) - (def name ()) +(def get-code (fn (words) ( + (def next (first words)) + (prnl (cat "next: " next)) + (if (iserr next) () + (if (= "$" next) () + (pre (get-code (rest words)) next))) ))) +(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) + (if bool F T) +)) + (def _fmap (fn (words) ( ;(prnl (cat "fmap: " word)) (def word (at 0 words)) (if (iserr word) () ( - (if (= ":" word) (compile (words)) - (if (= "swap" word) (swap) - (if (= "??" word) (pstack) - (if (= "+" word) (twop +) - (if (= "-" word) (twop -) - (if (= "/" word) (twop /) - (if (= "*" word) (twop *) - (if (= "." word) (loud-pop) - (stkadd (eval word)) - )))))))) - (_fmap (rest words)) + (if (= ":" word) (compile words) ( + (if (= "swap" word) (swap) + (if (= "??" word) (pstack) + (if (= "+" word) (twop +) + (if (= "-" word) (twop -) + (if (= "/" word) (twop /) + (if (= "*" word) (twop *) + (if (= "." word) (loud-pop) + (if (not (iserr (h-get dictionary word))) (_fmap (h-get dictionary word)) + (if (not (iserr (eval word))) (stdadd (eval word)) + (stkadd word) + )))))))))) + (_fmap (rest words))) )) ))) @@ -82,4 +99,4 @@ (fmap text) (prn nl) "" ; Have the underlying REPL do nothing -))) \ No newline at end of file +))) diff --git a/src/hash.c b/src/hash.c index 34a8413..bbfe6d3 100644 --- a/src/hash.c +++ b/src/hash.c @@ -131,7 +131,7 @@ size_t addStripped(struct ObjectTable* table, char* name, struct StrippedObject while (table->elements[h].symbol) { h = (h + 1) % table->capacity; } - eprintf("adding at %d: `%s`\n", h, name); + //eprintf("adding at %ld: `%s`\n", h, name); table->elements[h].symbol = name; table->elements[h].object = object; table->count += 1; @@ -161,7 +161,7 @@ Object buildHashTable(Object* params, int length, struct Environment* env) Object table = newObject(TYPE_HASH_TABLE); table.table = malloc(sizeof(struct ObjectTableObject)); table.table->table = buildTable(capacity); - table.table->refs = 2; + table.table->refs = 1; return table; } @@ -170,16 +170,21 @@ Object addToHashTable(Object* params, int length, struct Environment* env) Object table = params[0]; Object name = params[1]; Object add = params[2]; - eprintf("Adding `%s`\n", name.string); - addToTable(&table.table->table, name.string, cloneObject(add)); + //eprintf("Adding `%s`\n", table.string); + //eprintf("Adding `%s`\n", strdup(name.string)); + addToTable(&table.table->table, strdup(name.string), cloneObject(add)); return numberObject(0); } Object getFromHashTable(Object* params, int length, struct Environment* env) { + //eprintf("getFromHashTable()\n"); struct ObjectTable* table = ¶ms[0].table->table; + //eprintf("*table = %p\n", table); + //eprintf("*table->capacity = %d\n", table->capacity); for (int i = 0; i < table->capacity; i++) { - eprintf("[%d] %s\n", i, table->elements[i].symbol); + //eprintf("for i = %d\n", i); + //eprintf("[%d] %s\n", i, table->elements[i].symbol); } struct StrippedObject* fetched = getFromTable(¶ms[0].table->table, params[1].string); if (fetched) { @@ -188,4 +193,4 @@ Object getFromHashTable(Object* params, int length, struct Environment* env) throw(DID_NOT_FIND_SYMBOL, "Hash table does not contain %s", params[1].string); } -#endif \ No newline at end of file +#endif diff --git a/src/pebblisp.c b/src/pebblisp.c index ccef47b..8b44f26 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -653,6 +653,7 @@ int _readFile(FILE* input, struct Environment* env) fclose(input); Object r = parseEval(fileText, env); cleanObject(&r); + free(fileText - 1); return 0; }