Fleshing out ObjectTableObject.

Also fleshing out forbble2.
Fix _readFile() memory leak.
This commit is contained in:
Sage Vaillancourt 2022-04-18 22:17:17 -04:00
parent 8e30b24e8f
commit b26771d33c
3 changed files with 45 additions and 22 deletions

View File

@ -2,6 +2,7 @@
; Initialize an empty stack ; Initialize an empty stack
(def stack ()) (def stack ())
(def dictionary (table))
(def stkadd (fn (a) (def stkadd (fn (a)
"Add the given value to the stack" "Add the given value to the stack"
@ -46,28 +47,44 @@
(prnl (pop)) (prnl (pop))
))) )))
(def compile (fn (words) ( (def get-code (fn (words) (
(def name (at 0 words)) (def next (first words))
(set words (rest words)) (prnl (cat "next: " next))
(def name ()) (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) ( (def _fmap (fn (words) (
;(prnl (cat "fmap: " word)) ;(prnl (cat "fmap: " word))
(def word (at 0 words)) (def word (at 0 words))
(if (iserr word) () ( (if (iserr word) () (
(if (= ":" word) (compile (words)) (if (= ":" word) (compile words) (
(if (= "swap" word) (swap) (if (= "swap" word) (swap)
(if (= "??" word) (pstack) (if (= "??" word) (pstack)
(if (= "+" word) (twop +) (if (= "+" word) (twop +)
(if (= "-" word) (twop -) (if (= "-" word) (twop -)
(if (= "/" word) (twop /) (if (= "/" word) (twop /)
(if (= "*" word) (twop *) (if (= "*" word) (twop *)
(if (= "." word) (loud-pop) (if (= "." word) (loud-pop)
(stkadd (eval word)) (if (not (iserr (h-get dictionary word))) (_fmap (h-get dictionary word))
)))))))) (if (not (iserr (eval word))) (stdadd (eval word))
(_fmap (rest words)) (stkadd word)
))))))))))
(_fmap (rest words)))
)) ))
))) )))

View File

@ -131,7 +131,7 @@ size_t addStripped(struct ObjectTable* table, char* name, struct StrippedObject
while (table->elements[h].symbol) { while (table->elements[h].symbol) {
h = (h + 1) % table->capacity; 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].symbol = name;
table->elements[h].object = object; table->elements[h].object = object;
table->count += 1; table->count += 1;
@ -161,7 +161,7 @@ Object buildHashTable(Object* params, int length, struct Environment* env)
Object table = newObject(TYPE_HASH_TABLE); Object table = newObject(TYPE_HASH_TABLE);
table.table = malloc(sizeof(struct ObjectTableObject)); table.table = malloc(sizeof(struct ObjectTableObject));
table.table->table = buildTable(capacity); table.table->table = buildTable(capacity);
table.table->refs = 2; table.table->refs = 1;
return table; return table;
} }
@ -170,16 +170,21 @@ Object addToHashTable(Object* params, int length, struct Environment* env)
Object table = params[0]; Object table = params[0];
Object name = params[1]; Object name = params[1];
Object add = params[2]; Object add = params[2];
eprintf("Adding `%s`\n", name.string); //eprintf("Adding `%s`\n", table.string);
addToTable(&table.table->table, name.string, cloneObject(add)); //eprintf("Adding `%s`\n", strdup(name.string));
addToTable(&table.table->table, strdup(name.string), cloneObject(add));
return numberObject(0); return numberObject(0);
} }
Object getFromHashTable(Object* params, int length, struct Environment* env) Object getFromHashTable(Object* params, int length, struct Environment* env)
{ {
//eprintf("getFromHashTable()\n");
struct ObjectTable* table = &params[0].table->table; struct ObjectTable* table = &params[0].table->table;
//eprintf("*table = %p\n", table);
//eprintf("*table->capacity = %d\n", table->capacity);
for (int i = 0; i < table->capacity; i++) { 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(&params[0].table->table, params[1].string); struct StrippedObject* fetched = getFromTable(&params[0].table->table, params[1].string);
if (fetched) { if (fetched) {

View File

@ -653,6 +653,7 @@ int _readFile(FILE* input, struct Environment* env)
fclose(input); fclose(input);
Object r = parseEval(fileText, env); Object r = parseEval(fileText, env);
cleanObject(&r); cleanObject(&r);
free(fileText - 1);
return 0; return 0;
} }