Clean up printf cruft. Begin adding doc comments

This commit is contained in:
= 2020-05-08 05:32:08 +01:00
parent 3843e34a51
commit 73bfffcf55
4 changed files with 33 additions and 110 deletions

View File

@ -190,14 +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) (+ 69 a)))", &env);
Object obj = parseEval("(def ad (fn (a) (* 3 a)))", &env);
// Object obj2 = parseEval("(map ad (1 50 99))", &env);
// printObj(&obj);
// printObj(&obj2);
// cleanObject(&obj);
cleanObject(&obj);
// cleanObject(&obj2);
if(1)
return;
if(persist_exists(current_code)) {
persist_read_string(current_code, mytext, SMAX_LENGTH);
updateText();

View File

@ -17,23 +17,31 @@
#define printf(...) APP_LOG(APP_LOG_LEVEL_DEBUG, __VA_ARGS__)
#endif
/**
* Returns the length of a given list Object
* @param listObj The list to get the length of
* @return Length of the list if non-null and of the list type. Otherwise -1
*/
int listLength(const Object *listObj)
{
if(!listObj || listObj->type != TYPE_LIST)
return -1;
Object *t = listObj->list;
if(t == NULL)
return 0;
int len = 1;
while(t->forward != NULL) {
t = t->forward;
Object *march = listObj->list;
int len = 0;
while(march) {
march = march->forward;
len++;
}
return len;
}
/**
* Returns a pointer to the Object at the given index in a given list
* @param listObj The list to fetch an Object from
* @param n The index to to fetch from the list
* @return A pointer to the Object, if it is found, or NULL on an error
*/
Object *itemAt(const Object *listObj, int n)
{
if(!listObj || listObj->type != TYPE_LIST)
@ -41,10 +49,9 @@ Object *itemAt(const Object *listObj, int n)
Object *march = listObj->list;
for(int i = 0; i < n; i++) {
march = march->forward;
if(march == NULL)
return NULL;
march = march->forward;
}
return march;
}
@ -55,11 +62,11 @@ Object *tail(const Object *listObj)
if(!listObj || listObj->type != TYPE_LIST)
return NULL;
Object *t = listObj->list;
while(t->forward != NULL) {
t = t->forward;
Object *march = listObj->list;
while(march->forward != NULL) {
march = march->forward;
}
return t;
return march;
}
/**
@ -95,9 +102,9 @@ inline int isEmpty(const Object *obj)
void allocObject(Object **spot, const Object src)
{
printf("ALLOC\n");
if(!spot)
return;
*spot = malloc(sizeof(struct Object));
printf("END ALLOC\n");
**spot = src;
(*spot)->forward = NULL;
}
@ -157,13 +164,10 @@ void nf_addToList(Object *dest, const Object src)
void printErr(const Object *obj)
{
//#ifdef STANDALONE
if(!obj || obj->type != TYPE_ERROR)
return;
printf("%s\n", errorText[(int)(obj->err)]);
//#endif
return;
}
void stringList(char *dest, const Object *obj)
@ -173,7 +177,6 @@ void stringList(char *dest, const Object *obj)
const Object *tail = obj->list;
while(tail != NULL) {
printf("%s\n", dest);
strcat(dest, " ");
char tok[90] = "";
stringObj(tok, tail);
@ -370,9 +373,7 @@ 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;
}

View File

@ -79,9 +79,11 @@ int getType(const Object *obj);
int isEmpty(const Object *obj);
Object *tail(const Object *listObj);
void insertIntoList(Object *dest, int ind, const Object src);
void replaceListing(Object *list, int i, const Object src);
void nf_addToList(Object *dest, Object src);
void deleteList(const Object *dest);
int listLength(const Object *listObj);
Object *itemAt(const Object *listObj, int n);
void copyList(Object *dest, const Object *src);

View File

@ -109,9 +109,7 @@ 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);
@ -131,22 +129,13 @@ Object evalLambdaArgs(const Object *arg_forms)
return constructLambda(arg_forms, arg_forms->forward);
}
//void evalMapArgs(Object *newList, const Object *arg_forms,
Object evalMapArgs(Object *newList, const Object *arg_forms,
struct Environment *env)
Object evalMapArgs(const Object *arg_forms, struct Environment *env)
{
if(!arg_forms)
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);
printf("oldList\n");
printObj(oldList);
printf("END oldList\n");
if(lambda.type != TYPE_LAMBDA || oldList->type != TYPE_LIST) {
return errorObject(BAD_TYPE);
@ -164,42 +153,22 @@ Object evalMapArgs(Object *newList, const Object *arg_forms,
listBack[0].forward = NULL;
tempList.list = listBack;
printObj(&tempList);
printf("END &tempList\n");
//nf_addToList(&tempList, *oldElement);
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(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;
printf("END of evalMapArgs forward-setting");
//printf("return list\n");
return list;
}
struct Environment envForLambda(const Object *params, const Object *arg_forms,
struct Environment *outer)
{
printd("\n#####################");
printf("envForLambda()\n");
printd("envForLambda()\n");
debugObj(arg_forms);
int length = listLength(params);
@ -213,35 +182,20 @@ 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("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");
// printEnv(&env);
// printd("END envForLambda()\n\n");
return env;
}
@ -255,16 +209,7 @@ Object evalBuiltIns(const Object *first, const Object *rest,
} else if(strcmp(first->name, "fn") == 0) {
return evalLambdaArgs(rest);
} else if(strcmp(first->name, "map") == 0) {
printf("MAP BUILT-IN\n");
int length = listLength(rest->forward);
Object newList = listObject();
//Object listBack[length];
//newList.list = listBack;
Object ll = evalMapArgs(NULL, rest, env);
printf("print evalMapArgs() obj\n");
printObj(&ll);
printf("MAP BUILT-IN\n");
return ll;
return evalMapArgs(rest, env);
}
return errorObject(BUILT_IN_NOT_FOUND);
@ -272,19 +217,16 @@ 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");
printObj(obj);
printd("eval():\n");
debugObj(obj);
switch(obj->type) {
case TYPE_NUMBER:
case TYPE_BOOL:
@ -313,30 +255,23 @@ 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;
@ -366,12 +301,9 @@ Result result(Object obj, struct Slice *slices)
void addToEnv(struct Environment *env, const char *name, const Object obj)
{
int i;
//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) {
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;
@ -386,18 +318,14 @@ 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);
printf("END ALLOC\n");
strncpy(env->strings[i], name, strlen(name) + 1);
env->objects[i] = obj;
}
@ -411,8 +339,8 @@ void printEnv(struct Environment *env)
for(int i = 0; i < env->size; i++) {
if(env->strings[i] == NULL)
return;
printf("env[%d]: '%s'\n ", i, env->strings[i]);
printObj(&env->objects[i]);
printd("env[%d]: '%s'\n ", i, env->strings[i]);
debugObj(&env->objects[i]);
}
}
@ -482,17 +410,13 @@ 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);
@ -521,9 +445,7 @@ 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;
}