All known memory leaks handled!

Watch the commented `if` -127,7 +137,10 @ Object eval for weird behavior
This commit is contained in:
= 2020-05-16 04:22:08 +01:00
parent f488e29c54
commit efb151d0ef
2 changed files with 19 additions and 5 deletions

View File

@ -3,7 +3,7 @@
#define MAX_TOK_LEN 10 // 11 #define MAX_TOK_LEN 10 // 11
#define MAX_TOK_CNT 128 // 128 #define MAX_TOK_CNT 128 // 128
#define MAX_ENV_ELM 15 // 50 #define MAX_ENV_ELM 25 // 50
#define FOR_POINTER_IN_LIST(_list) \ #define FOR_POINTER_IN_LIST(_list) \
for(Object *_element = _list->list; \ for(Object *_element = _list->list; \

View File

@ -50,12 +50,21 @@ Object evalMapArgs(const Object *arg_forms, struct Environment *env)
FOR_POINTER_IN_LIST(inputList) { FOR_POINTER_IN_LIST(inputList) {
// Create a new list for each element, // Create a new list for each element,
// since lambda evaluation looks for a list // since lambda evaluation looks for a list
const Object tempList = startList(*POINTER); Object tempList;
if(POINTER->type == TYPE_LIST) {
Object tl = listObject();
copyList(&tl, POINTER);
tempList = startList(tl);
} else {
tempList = startList(*POINTER);
}
struct Environment newEnv = struct Environment newEnv =
envForLambda(&lambda.lambda->params, &tempList, env); envForLambda(&lambda.lambda->params, &tempList, env);
// Add the lambda evaluation to the list // Add the lambda evaluation to the list
nf_addToList(&list, eval(&lambda.lambda->body, &newEnv)); nf_addToList(&list, eval(&lambda.lambda->body, &newEnv));
deleteEnv(&newEnv);
cleanObject(&tempList);
} }
return list; return list;
@ -93,8 +102,9 @@ Object eval(const Object *obj, struct Environment *env)
case TYPE_NUMBER: case TYPE_NUMBER:
case TYPE_BOOL: case TYPE_BOOL:
return *obj; // Return as is return *obj; // Return as is
case TYPE_STRING: case TYPE_STRING:
return copyString(*obj); // Return as is return copyString(*obj);
case TYPE_SYMBOL: case TYPE_SYMBOL:
return fetchFromEnvironment(obj->name, env); return fetchFromEnvironment(obj->name, env);
@ -127,7 +137,10 @@ Object eval(const Object *obj, struct Environment *env)
Object func_eval = rest[0]; Object func_eval = rest[0];
for(int i = 1; i < length; i++) { for(int i = 1; i < length; i++) {
Object toClean = func_eval;
func_eval = first_eval.func(func_eval, rest[i], env); func_eval = first_eval.func(func_eval, rest[i], env);
//if(i != length - 1)
cleanObject(&toClean);
cleanObject(&rest[i]); cleanObject(&rest[i]);
} }
@ -364,7 +377,7 @@ Object parseEval(const char *input, struct Environment *env)
int i = 0; int i = 0;
int parens = 0; int parens = 0;
Object obj; Object obj = numberObject(0);
struct Slice *tok = tokens; struct Slice *tok = tokens;
if(tok[i].text[0] != '(') { if(tok[i].text[0] != '(') {
Object parsed = parse(tok).obj; Object parsed = parse(tok).obj;
@ -377,6 +390,7 @@ Object parseEval(const char *input, struct Environment *env)
} else if(tok[i].text[0] == ')') { } else if(tok[i].text[0] == ')') {
parens--; parens--;
if(parens == 0) { if(parens == 0) {
cleanObject(&obj);
Object parsed = parse(tok).obj; Object parsed = parse(tok).obj;
tok = &tok[i + 1]; tok = &tok[i + 1];
i = -1; i = -1;
@ -418,6 +432,6 @@ int main(int argc, const char* argv[])
} else { } else {
repl(&env); repl(&env);
} }
// deleteEnv(&env); deleteEnv(&env);
} }
#endif #endif