Fleshing out ObjectTableObject.
Also fleshing out forbble2. Fix _readFile() memory leak.
This commit is contained in:
parent
8e30b24e8f
commit
b26771d33c
|
@ -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,18 +47,32 @@
|
|||
(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 (= ":" word) (compile words) (
|
||||
(if (= "swap" word) (swap)
|
||||
(if (= "??" word) (pstack)
|
||||
(if (= "+" word) (twop +)
|
||||
|
@ -65,9 +80,11 @@
|
|||
(if (= "/" word) (twop /)
|
||||
(if (= "*" word) (twop *)
|
||||
(if (= "." word) (loud-pop)
|
||||
(stkadd (eval word))
|
||||
))))))))
|
||||
(_fmap (rest words))
|
||||
(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)))
|
||||
))
|
||||
)))
|
||||
|
||||
|
|
15
src/hash.c
15
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) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue