Big mess, but something in there got `map` working

This commit is contained in:
= 2020-05-08 03:37:54 +01:00
parent a286943984
commit 3843e34a51
3 changed files with 80 additions and 40 deletions

View File

@ -190,12 +190,12 @@ void code_window_load(Window *window)
window_stack_push(s_code_window, true); window_stack_push(s_code_window, true);
// If possible, load the previous code text // If possible, load the previous code text
Object obj = parseEval("(def ad (fn (a) (+ 1 a)))", &env); // Object obj = parseEval("(def ad (fn (a) (+ 69 a)))", &env);
Object obj2 = parseEval("(map ad (1 2 3))", &env); // Object obj2 = parseEval("(map ad (1 50 99))", &env);
printObj(&obj); // printObj(&obj);
printObj(&obj2); // printObj(&obj2);
cleanObject(&obj); // cleanObject(&obj);
cleanObject(&obj2); // cleanObject(&obj2);
if(1) if(1)
return; return;
if(persist_exists(current_code)) { if(persist_exists(current_code)) {

View File

@ -95,7 +95,9 @@ inline int isEmpty(const Object *obj)
void allocObject(Object **spot, const Object src) void allocObject(Object **spot, const Object src)
{ {
printf("ALLOC\n");
*spot = malloc(sizeof(struct Object)); *spot = malloc(sizeof(struct Object));
printf("END ALLOC\n");
**spot = src; **spot = src;
(*spot)->forward = NULL; (*spot)->forward = NULL;
} }
@ -368,7 +370,9 @@ inline Object symbolObject()
inline Object lambdaObject() inline Object lambdaObject()
{ {
Object o = newObject(TYPE_LAMBDA); Object o = newObject(TYPE_LAMBDA);
printf("ALLOC\n");
o.lambda = malloc(sizeof(struct Lambda)); o.lambda = malloc(sizeof(struct Lambda));
printf("END ALLOC\n");
return o; return o;
} }

View File

@ -109,7 +109,9 @@ Object evalDefArgs(const Object *arg_forms, struct Environment *env)
const Object *first_form = &arg_forms[0]; const Object *first_form = &arg_forms[0];
const char *name = first_form->name; const char *name = first_form->name;
printf("evalDefArgs eval\n");
Object second_eval = eval(first_form->forward, env); Object second_eval = eval(first_form->forward, env);
printf("END evalDefArgs eval\n");
addToEnv(env, name, second_eval); addToEnv(env, name, second_eval);
@ -129,14 +131,16 @@ Object evalLambdaArgs(const Object *arg_forms)
return constructLambda(arg_forms, arg_forms->forward); return constructLambda(arg_forms, arg_forms->forward);
} }
//Object evalMapArgs(Object *newList, const Object *arg_forms, //void evalMapArgs(Object *newList, const Object *arg_forms,
void evalMapArgs(Object *newList, const Object *arg_forms, Object evalMapArgs(Object *newList, const Object *arg_forms,
struct Environment *env) struct Environment *env)
{ {
if(!arg_forms) if(!arg_forms)
return; // errorObject(NULL_MAP_ARGS); return errorObject(NULL_MAP_ARGS);
printf("evalMapArgs eval1\n");
const Object lambda = eval(&arg_forms[0], env); const Object lambda = eval(&arg_forms[0], env);
printf("END evalMapArgs eval1\n");
Object *oldList = (&arg_forms[0])->forward; Object *oldList = (&arg_forms[0])->forward;
printf("lambda\n"); printf("lambda\n");
printObj(&lambda); printObj(&lambda);
@ -145,10 +149,10 @@ void evalMapArgs(Object *newList, const Object *arg_forms,
printf("END oldList\n"); printf("END oldList\n");
if(lambda.type != TYPE_LAMBDA || oldList->type != TYPE_LIST) { if(lambda.type != TYPE_LAMBDA || oldList->type != TYPE_LIST) {
return; // errorObject(BAD_TYPE); return errorObject(BAD_TYPE);
} }
// Object list = listObject(); Object list = listObject();
int i = 0; int i = 0;
Object *oldElement = oldList->list; Object *oldElement = oldList->list;
@ -166,24 +170,29 @@ void evalMapArgs(Object *newList, const Object *arg_forms,
struct Environment newEnv = struct Environment newEnv =
envForLambda(&lambda.lambda->params, &tempList, env); envForLambda(&lambda.lambda->params, &tempList, env);
printf("evalMapArgs eval2\n");
const Object evalLambda = eval(&lambda.lambda->body, &newEnv); const Object evalLambda = eval(&lambda.lambda->body, &newEnv);
printf("END evalMapArgs eval2\n");
// cleanObject(&tempList); // Don't let tempList linger // cleanObject(&tempList); // Don't let tempList linger
//nf_addToList(&list, evalLambda); //nf_addToList(newList, evalLambda);
newList[i++] = evalLambda; nf_addToList(&list, evalLambda);
printf("VVVVVVnewList[%d]\n", i - 1); //newList[i++] = evalLambda;
printObj(&newList[i-1]); //printf("VVVVVVnewList[%d]\n", i - 1);
printf("^^^^^^newList[%d]\n", i - 1); //printObj(&newList[i-1]);
//printf("^^^^^^newList[%d]\n", i - 1);
oldElement = oldElement->forward; oldElement = oldElement->forward;
} }
printf("END of evalMapArgs loop");
for(int j = 0; j < i - 1; j++) { //for(int j = 0; j < i - 1; j++) {
newList[j].forward = &newList[j+1]; // newList[j].forward = &newList[j+1];
} //}
newList[i-1].forward = NULL; //newList[i-1].forward = NULL;
printf("END of evalMapArgs forward-setting");
//printf("return list\n"); //printf("return list\n");
//return list; return list;
} }
struct Environment envForLambda(const Object *params, const Object *arg_forms, struct Environment envForLambda(const Object *params, const Object *arg_forms,
@ -204,30 +213,35 @@ struct Environment envForLambda(const Object *params, const Object *arg_forms,
if(length == 0) if(length == 0)
return env; return env;
printf("ALLOC\n");
env.strings = calloc(sizeof(char*), length + 1); env.strings = calloc(sizeof(char*), length + 1);
env.objects = malloc(sizeof(Object) * length + 1); env.objects = malloc(sizeof(Object) * length + 1);
printf("END ALLOC\n");
Object vs[length]; Object vs[length];
printf("eval_forms");
eval_forms(vs, arg_forms, outer); eval_forms(vs, arg_forms, outer);
printf("WHICH/ONE\n"); // printf("WHICH/ONE\n");
for(int i = 0; i < length; i++) { // for(int i = 0; i < length; i++) {
//printObj(&vs[i]); // //printObj(&vs[i]);
printObj(&arg_forms[i]); // printObj(&arg_forms[i]);
} // }
printf("END WHICH/ONE\n"); // printf("END WHICH/ONE\n");
printf("addToEnv in envForLambda\n");
for(int i = 0; i < length; i++) { for(int i = 0; i < length; i++) {
const char *n = itemAt(params, i)->name; const char *n = itemAt(params, i)->name;
printf("envForLambda eval\n");
addToEnv(&env, n, eval(&arg_forms[i], outer)); // May not need eval? addToEnv(&env, n, eval(&arg_forms[i], outer)); // May not need eval?
printf("END envForLambda eval\n");
// SHOULD BE `vs` not arg_forms??? // SHOULD BE `vs` not arg_forms???
} // Something is segfaulting, anyway } // Something is segfaulting, anyway
// env.strings[length] = NULL; // env.strings[length] = NULL;
printf("end of addToEnv in envForLambda\n");
printd("envForLambda env IT BROKE HERE()\n"); // printEnv(&env);
printd("envForLambda length=%d\n", length); // printd("END envForLambda()\n\n");
printEnv(&env);
printd("END envForLambda()\n\n");
return env; return env;
} }
@ -246,11 +260,11 @@ Object evalBuiltIns(const Object *first, const Object *rest,
Object newList = listObject(); Object newList = listObject();
//Object listBack[length]; //Object listBack[length];
//newList.list = listBack; //newList.list = listBack;
evalMapArgs(listBack, rest, env); Object ll = evalMapArgs(NULL, rest, env);
printf("print evalMapArgs() obj\n"); printf("print evalMapArgs() obj\n");
printObj(&newList); printObj(&ll);
printf("MAP BUILT-IN\n"); printf("MAP BUILT-IN\n");
return newList; return ll;
} }
return errorObject(BUILT_IN_NOT_FOUND); return errorObject(BUILT_IN_NOT_FOUND);
@ -258,16 +272,19 @@ Object evalBuiltIns(const Object *first, const Object *rest,
void eval_forms(Object *destList, const Object *src, struct Environment *env) void eval_forms(Object *destList, const Object *src, struct Environment *env)
{ {
printf("THIS IS EVAL_FROMS WE ARE OUT HERE AND WE ARE ####################n");
int length = listLength(src) - 1; // Not counting first_form int length = listLength(src) - 1; // Not counting first_form
for(int i = 0; i < length; i++) { // Evaluates all in list for(int i = 0; i < length; i++) { // Evaluates all in list
printf("eval_forms eval\n");
destList[i] = eval(itemAt(src, i + 1), env); // Skip the first destList[i] = eval(itemAt(src, i + 1), env); // Skip the first
printf("END eval_forms eval\n");
} }
} }
Object eval(const Object *obj, struct Environment *env) Object eval(const Object *obj, struct Environment *env)
{ {
printf("eval():\n"); printf("eval():\n");
debugObj(obj); printObj(obj);
switch(obj->type) { switch(obj->type) {
case TYPE_NUMBER: case TYPE_NUMBER:
case TYPE_BOOL: case TYPE_BOOL:
@ -296,23 +313,30 @@ Object eval(const Object *obj, struct Environment *env)
} }
} }
printf("first_eval eval\n");
Object first_eval = eval(first_form, env); Object first_eval = eval(first_form, env);
printf("END first_eval eval\n");
if(first_eval.type == TYPE_FUNC) { if(first_eval.type == TYPE_FUNC) {
int length = listLength(obj) - 1; // Not counting first_form int length = listLength(obj) - 1; // Not counting first_form
Object rest[length]; Object rest[length];
printf("eval_forms");
eval_forms(rest, obj, env); eval_forms(rest, obj, env);
printf("end eval_forms");
Object func_eval = rest[0]; Object func_eval = rest[0];
for(int i = 1; i < length; i++) { for(int i = 1; i < length; i++) {
func_eval = first_eval.func(func_eval, rest[i]); func_eval = first_eval.func(func_eval, rest[i]);
} }
// deleteList(obj); // Decreases indirectly lost memory, but fails on Pebble // deleteList(obj); // Decreases indirectly lost memory, but fails on Pebble
printf("returning func_eval\n");
return func_eval; return func_eval;
} else if (first_eval.type == TYPE_LAMBDA) { } else if (first_eval.type == TYPE_LAMBDA) {
struct Environment newEnv = struct Environment newEnv =
envForLambda(&first_eval.lambda->params, first_form->forward, env); envForLambda(&first_eval.lambda->params, first_form->forward, env);
printf("first_eval.type == TYPE_LAMBDA eval\n");
Object ret = eval(&first_eval.lambda->body, &newEnv); Object ret = eval(&first_eval.lambda->body, &newEnv);
printf("END first_eval.type == TYPE_LAMBDA eval\n");
deleteEnv(&newEnv); deleteEnv(&newEnv);
return ret; return ret;
@ -345,7 +369,9 @@ void addToEnv(struct Environment *env, const char *name, const Object obj)
//printf("sizeof(char)=%d * strlen(name)=%d\n", (int)sizeof(char), (int)strlen(name)); //printf("sizeof(char)=%d * strlen(name)=%d\n", (int)sizeof(char), (int)strlen(name));
for(i = 0; i < env->size ; i++) { for(i = 0; i < env->size ; i++) {
if(env->strings[i] == NULL) { if(env->strings[i] == NULL) {
env->strings[i] = malloc(strlen(name) + 1); printf("ALLOC\n");
env->strings[i] = calloc(sizeof(char), strlen(name) + 1);
printf("END ALLOC\n");
strncpy(env->strings[i], name, strlen(name)); strncpy(env->strings[i], name, strlen(name));
env->objects[i] = obj; env->objects[i] = obj;
return; return;
@ -360,29 +386,33 @@ void addToEnv(struct Environment *env, const char *name, const Object obj)
printd("Reallocating environment\n"); printd("Reallocating environment\n");
const int inc = 5; const int inc = 5;
env->size += inc; env->size += inc;
printf("ALLOC\n");
env->strings = realloc(env->strings, sizeof(char*) * env->size); env->strings = realloc(env->strings, sizeof(char*) * env->size);
env->objects = realloc(env->objects, sizeof(Object) * env->size); env->objects = realloc(env->objects, sizeof(Object) * env->size);
printf("END ALLOC\n");
for(int j = 0; j < inc; j++) { for(int j = 0; j < inc; j++) {
env->strings[i + j] = NULL; env->strings[i + j] = NULL;
} }
printf("ALLOC\n");
env->strings[i] = malloc(strlen(name) + 1); env->strings[i] = malloc(strlen(name) + 1);
strncpy(env->strings[i], name, strlen(name)); printf("END ALLOC\n");
strncpy(env->strings[i], name, strlen(name) + 1);
env->objects[i] = obj; env->objects[i] = obj;
} }
void printEnv(struct Environment *env) void printEnv(struct Environment *env)
{ {
if(!env) { if(!env) {
printd("NULL env\n"); printf("NULL env\n");
return; return;
} }
for(int i = 0; i < env->size; i++) { for(int i = 0; i < env->size; i++) {
if(env->strings[i] == NULL) if(env->strings[i] == NULL)
return; return;
printd("env[%d]: '%s'\n ", i, env->strings[i]); printf("env[%d]: '%s'\n ", i, env->strings[i]);
debugObj(&env->objects[i]); printObj(&env->objects[i]);
} }
} }
@ -452,13 +482,17 @@ void deleteEnv(struct Environment *e)
struct Environment defaultEnv() { struct Environment defaultEnv() {
struct Environment e; struct Environment e;
e.outer = NULL; e.outer = NULL;
printf("ALLOC\n");
e.strings = calloc(sizeof(char*), MAX_ENV_ELM); e.strings = calloc(sizeof(char*), MAX_ENV_ELM);
printf("END ALLOC\n");
e.size = MAX_ENV_ELM; e.size = MAX_ENV_ELM;
// for(int i = 0; i < MAX_ENV_ELM; i++) { // for(int i = 0; i < MAX_ENV_ELM; i++) {
// e.strings[i] = NULL; // e.strings[i] = NULL;
// } // }
printf("ALLOC\n");
e.objects = malloc(sizeof(Object) * MAX_ENV_ELM); e.objects = malloc(sizeof(Object) * MAX_ENV_ELM);
printf("END ALLOC\n");
addFunc("+", &add, &e); addFunc("+", &add, &e);
addFunc("-", &sub, &e); addFunc("-", &sub, &e);
@ -487,7 +521,9 @@ Object parseEval(const char *input, struct Environment *env)
#endif #endif
Object parsed = parse(tokens).obj; Object parsed = parse(tokens).obj;
free(tokens); free(tokens);
printf("parseEval eval\n");
Object ret = eval(&parsed, env); Object ret = eval(&parsed, env);
printf("END parseEval eval\n");
cleanObject(&parsed); cleanObject(&parsed);
return ret; return ret;
} }