diff --git a/src/calc.c b/src/calc.c index 8299c72..af891aa 100644 --- a/src/calc.c +++ b/src/calc.c @@ -190,12 +190,12 @@ void code_window_load(Window *window) window_stack_push(s_code_window, true); // If possible, load the previous code text - Object obj = parseEval("(def ad (fn (a) (+ 1 a)))", &env); - Object obj2 = parseEval("(map ad (1 2 3))", &env); - printObj(&obj); - printObj(&obj2); - cleanObject(&obj); - cleanObject(&obj2); + // Object obj = parseEval("(def ad (fn (a) (+ 69 a)))", &env); + // Object obj2 = parseEval("(map ad (1 50 99))", &env); + // printObj(&obj); + // printObj(&obj2); + // cleanObject(&obj); + // cleanObject(&obj2); if(1) return; if(persist_exists(current_code)) { diff --git a/src/object.c b/src/object.c index ec30b2f..4670bde 100644 --- a/src/object.c +++ b/src/object.c @@ -95,7 +95,9 @@ inline int isEmpty(const Object *obj) void allocObject(Object **spot, const Object src) { + printf("ALLOC\n"); *spot = malloc(sizeof(struct Object)); + printf("END ALLOC\n"); **spot = src; (*spot)->forward = NULL; } @@ -368,7 +370,9 @@ inline Object symbolObject() inline Object lambdaObject() { Object o = newObject(TYPE_LAMBDA); + printf("ALLOC\n"); o.lambda = malloc(sizeof(struct Lambda)); + printf("END ALLOC\n"); return o; } diff --git a/src/pebblisp.c b/src/pebblisp.c index 76933de..f7bcee1 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -109,7 +109,9 @@ Object evalDefArgs(const Object *arg_forms, struct Environment *env) const Object *first_form = &arg_forms[0]; const char *name = first_form->name; + printf("evalDefArgs eval\n"); Object second_eval = eval(first_form->forward, env); + printf("END evalDefArgs eval\n"); addToEnv(env, name, second_eval); @@ -129,14 +131,16 @@ Object evalLambdaArgs(const Object *arg_forms) 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) { 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); + printf("END evalMapArgs eval1\n"); Object *oldList = (&arg_forms[0])->forward; printf("lambda\n"); printObj(&lambda); @@ -145,10 +149,10 @@ void evalMapArgs(Object *newList, const Object *arg_forms, printf("END oldList\n"); 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; Object *oldElement = oldList->list; @@ -166,24 +170,29 @@ void evalMapArgs(Object *newList, const Object *arg_forms, struct Environment newEnv = envForLambda(&lambda.lambda->params, &tempList, env); + printf("evalMapArgs eval2\n"); const Object evalLambda = eval(&lambda.lambda->body, &newEnv); + printf("END evalMapArgs eval2\n"); // cleanObject(&tempList); // Don't let tempList linger - //nf_addToList(&list, evalLambda); - newList[i++] = evalLambda; - printf("VVVVVVnewList[%d]\n", i - 1); - printObj(&newList[i-1]); - printf("^^^^^^newList[%d]\n", i - 1); + //nf_addToList(newList, evalLambda); + nf_addToList(&list, evalLambda); + //newList[i++] = evalLambda; + //printf("VVVVVVnewList[%d]\n", i - 1); + //printObj(&newList[i-1]); + //printf("^^^^^^newList[%d]\n", i - 1); oldElement = oldElement->forward; } + printf("END of evalMapArgs loop"); - for(int j = 0; j < i - 1; j++) { - newList[j].forward = &newList[j+1]; - } - newList[i-1].forward = NULL; + //for(int j = 0; j < i - 1; j++) { + // newList[j].forward = &newList[j+1]; + //} + //newList[i-1].forward = NULL; + printf("END of evalMapArgs forward-setting"); //printf("return list\n"); - //return list; + return list; } 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) return env; + printf("ALLOC\n"); env.strings = calloc(sizeof(char*), length + 1); env.objects = malloc(sizeof(Object) * length + 1); + printf("END ALLOC\n"); Object vs[length]; + printf("eval_forms"); eval_forms(vs, arg_forms, outer); - printf("WHICH/ONE\n"); - for(int i = 0; i < length; i++) { - //printObj(&vs[i]); - printObj(&arg_forms[i]); - } - printf("END WHICH/ONE\n"); + // printf("WHICH/ONE\n"); + // for(int i = 0; i < length; i++) { + // //printObj(&vs[i]); + // printObj(&arg_forms[i]); + // } + // printf("END WHICH/ONE\n"); + printf("addToEnv in envForLambda\n"); for(int i = 0; i < length; i++) { const char *n = itemAt(params, i)->name; + printf("envForLambda eval\n"); addToEnv(&env, n, eval(&arg_forms[i], outer)); // May not need eval? + printf("END envForLambda eval\n"); // SHOULD BE `vs` not arg_forms??? } // Something is segfaulting, anyway // env.strings[length] = NULL; + printf("end of addToEnv in envForLambda\n"); - printd("envForLambda env IT BROKE HERE()\n"); - printd("envForLambda length=%d\n", length); - printEnv(&env); - printd("END envForLambda()\n\n"); + // printEnv(&env); + // printd("END envForLambda()\n\n"); return env; } @@ -246,11 +260,11 @@ Object evalBuiltIns(const Object *first, const Object *rest, Object newList = listObject(); //Object listBack[length]; //newList.list = listBack; - evalMapArgs(listBack, rest, env); + Object ll = evalMapArgs(NULL, rest, env); printf("print evalMapArgs() obj\n"); - printObj(&newList); + printObj(&ll); printf("MAP BUILT-IN\n"); - return newList; + return ll; } 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) { + printf("THIS IS EVAL_FROMS WE ARE OUT HERE AND WE ARE ####################n"); int length = listLength(src) - 1; // Not counting first_form 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 + printf("END eval_forms eval\n"); } } Object eval(const Object *obj, struct Environment *env) { printf("eval():\n"); - debugObj(obj); + printObj(obj); switch(obj->type) { case TYPE_NUMBER: 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); + printf("END first_eval eval\n"); if(first_eval.type == TYPE_FUNC) { int length = listLength(obj) - 1; // Not counting first_form Object rest[length]; + printf("eval_forms"); eval_forms(rest, obj, env); + printf("end eval_forms"); Object func_eval = rest[0]; for(int i = 1; i < length; i++) { func_eval = first_eval.func(func_eval, rest[i]); } // deleteList(obj); // Decreases indirectly lost memory, but fails on Pebble + printf("returning func_eval\n"); return func_eval; } else if (first_eval.type == TYPE_LAMBDA) { struct Environment newEnv = 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); + printf("END first_eval.type == TYPE_LAMBDA eval\n"); deleteEnv(&newEnv); 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)); for(i = 0; i < env->size ; i++) { 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)); env->objects[i] = obj; return; @@ -360,29 +386,33 @@ void addToEnv(struct Environment *env, const char *name, const Object obj) printd("Reallocating environment\n"); const int inc = 5; env->size += inc; + printf("ALLOC\n"); env->strings = realloc(env->strings, sizeof(char*) * env->size); env->objects = realloc(env->objects, sizeof(Object) * env->size); + printf("END ALLOC\n"); for(int j = 0; j < inc; j++) { env->strings[i + j] = NULL; } + printf("ALLOC\n"); 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; } void printEnv(struct Environment *env) { if(!env) { - printd("NULL env\n"); + printf("NULL env\n"); return; } for(int i = 0; i < env->size; i++) { if(env->strings[i] == NULL) return; - printd("env[%d]: '%s'\n ", i, env->strings[i]); - debugObj(&env->objects[i]); + printf("env[%d]: '%s'\n ", i, env->strings[i]); + printObj(&env->objects[i]); } } @@ -452,13 +482,17 @@ void deleteEnv(struct Environment *e) struct Environment defaultEnv() { struct Environment e; e.outer = NULL; + printf("ALLOC\n"); e.strings = calloc(sizeof(char*), MAX_ENV_ELM); + printf("END ALLOC\n"); e.size = MAX_ENV_ELM; // for(int i = 0; i < MAX_ENV_ELM; i++) { // e.strings[i] = NULL; // } + printf("ALLOC\n"); e.objects = malloc(sizeof(Object) * MAX_ENV_ELM); + printf("END ALLOC\n"); addFunc("+", &add, &e); addFunc("-", &sub, &e); @@ -487,7 +521,9 @@ Object parseEval(const char *input, struct Environment *env) #endif Object parsed = parse(tokens).obj; free(tokens); + printf("parseEval eval\n"); Object ret = eval(&parsed, env); + printf("END parseEval eval\n"); cleanObject(&parsed); return ret; }