diff --git a/src/main.c b/src/main.c index 60aca9f..44b77d8 100644 --- a/src/main.c +++ b/src/main.c @@ -101,43 +101,40 @@ void repl(struct Environment* env) continue; } add_history(buf); - if ((buf[0] == 'c' && buf[1] == 'd')) { + + if (buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' ') { char* oldBuf = buf; - buf = malloc(sizeof(char) * strlen(buf + 6)); + buf = malloc(sizeof(char) * strlen(buf + 3) + 8); sprintf(buf, "(cd \"%s\")", oldBuf + 3); free(oldBuf); - } - if ((buf[0] == '?' && (buf[1] == ' ' || buf[1] == '\0'))) { + } else if (buf[0] == '?' && (buf[1] == ' ' || buf[1] == '\0')) { char* oldBuf = buf; - buf = malloc(sizeof(char) * strlen(buf + 3)); + buf = malloc(sizeof(char) * (strlen(buf) + 3)); sprintf(buf, "(%s)", oldBuf); free(oldBuf); } + Object o = parseEval(buf, env); if (isFuncy(o) || isError(o, DID_NOT_FIND_SYMBOL)) { - cleanObject(&o); system(buf); - free(buf); - write_history(settings.historyFile); - continue; + } else { + size_t length; + char* output = stringObj(&o, &length); + printColored(output); + free(output); + printf("\n"); } - free(buf); - - size_t length; - char* output = stringObj(&o, &length); - cleanObject(&o); - printColored(output); - free(output); - printf("\n"); write_history(settings.historyFile); + cleanObject(&o); + free(buf); } } void loadArgsIntoEnv(int argc, const char* argv[], struct Environment* env) { - Object args = listObject(); + BuildListNamed(args); for (int i = 0; i < argc; i++) { - nf_addToList(&args, nullTerminated(argv[i])); + addToList(args, nullTerminated(argv[i])); } addToEnv(env, "args", args); } diff --git a/src/object.h b/src/object.h index 0f21e1b..08fb8b7 100644 --- a/src/object.h +++ b/src/object.h @@ -28,6 +28,9 @@ _element = _element->forward) #define POINTER _element +#define BuildListNamed(LIST) Object LIST = listObject(); Object** LIST ## _LIST_ITEM = &(LIST.list) +#define addToList(LIST, OBJECT) allocObject(LIST ## _LIST_ITEM, OBJECT); LIST ## _LIST_ITEM = &(*LIST ## _LIST_ITEM)->forward + #ifdef PBL_PLATFORM_APLITE #define LOW_MEM #endif diff --git a/src/pebblisp.c b/src/pebblisp.c index 9e09e79..4adafc1 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -93,7 +93,7 @@ Object mapO(Object* params, int length, struct Environment* env) return errorObject(BAD_TYPE); } - Object outputList = listObject(); + BuildListNamed(outputList); FOR_POINTER_IN_LIST(inputList) { // Create a new list for each element, // since lambda evaluation looks for a list @@ -103,8 +103,7 @@ Object mapO(Object* params, int length, struct Environment* env) struct Environment newEnv = envForLambda(lambdaParams, &tempInput, listLength(lambdaParams), env); // Add the lambda evaluation to the list - Object lambda_output = eval(&lambda.lambda->body, &newEnv); - nf_addToList(&outputList, lambda_output); + addToList(outputList, eval(&lambda.lambda->body, &newEnv)); deleteEnv(&newEnv); cleanObject(&tempInput); } diff --git a/src/pebblisp.h b/src/pebblisp.h index 9eb75e6..9f3dfad 100644 --- a/src/pebblisp.h +++ b/src/pebblisp.h @@ -20,7 +20,8 @@ _Static_assert(array_length(_name ## Tests) % 2 == 0, "Array of test strings mus Object _name(Object* params, int length, struct Environment* env) struct TypeCheck { - int (*checkFunc)(Object); + int (* checkFunc)(Object); + const char* name; }; diff --git a/src/plfunc.c b/src/plfunc.c index a145e10..092aff9 100644 --- a/src/plfunc.c +++ b/src/plfunc.c @@ -54,14 +54,14 @@ Object filter(Object* params, unused int length, struct Environment* env) Object condition = params[0]; Object list = params[1]; - Object filtered = listObject(); + BuildListNamed(filtered); FOR_POINTER_IN_LIST(&list) { Object cloned = cloneObject(condition); Object first_eval = eval(&cloned, env); Object testee = cloneObject(*POINTER); Object e = funcyEval(&first_eval, &testee, 1, env); if (e.number) { - nf_addToList(&filtered, testee); // May need to re-clone testee? + addToList(filtered, testee); // May need to re-clone testee? } cleanObject(&e); cleanObject(&cloned);