Correcting some REPL behavior.

Add BuildListNamed() macro for optimized list creation.
This commit is contained in:
Sage Vaillancourt 2022-04-11 20:43:45 -04:00
parent 5a34bcbfbd
commit 48b1f3c54b
5 changed files with 25 additions and 25 deletions

View File

@ -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;
}
free(buf);
} else {
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);
}

View File

@ -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

View File

@ -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);
}

View File

@ -21,6 +21,7 @@ Object _name(Object* params, int length, struct Environment* env)
struct TypeCheck {
int (* checkFunc)(Object);
const char* name;
};

View File

@ -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);