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; continue;
} }
add_history(buf); add_history(buf);
if ((buf[0] == 'c' && buf[1] == 'd')) {
if (buf[0] == 'c' && buf[1] == 'd' && buf[2] == ' ') {
char* oldBuf = buf; char* oldBuf = buf;
buf = malloc(sizeof(char) * strlen(buf + 6)); buf = malloc(sizeof(char) * strlen(buf + 3) + 8);
sprintf(buf, "(cd \"%s\")", oldBuf + 3); sprintf(buf, "(cd \"%s\")", oldBuf + 3);
free(oldBuf); free(oldBuf);
} } else if (buf[0] == '?' && (buf[1] == ' ' || buf[1] == '\0')) {
if ((buf[0] == '?' && (buf[1] == ' ' || buf[1] == '\0'))) {
char* oldBuf = buf; char* oldBuf = buf;
buf = malloc(sizeof(char) * strlen(buf + 3)); buf = malloc(sizeof(char) * (strlen(buf) + 3));
sprintf(buf, "(%s)", oldBuf); sprintf(buf, "(%s)", oldBuf);
free(oldBuf); free(oldBuf);
} }
Object o = parseEval(buf, env); Object o = parseEval(buf, env);
if (isFuncy(o) || isError(o, DID_NOT_FIND_SYMBOL)) { if (isFuncy(o) || isError(o, DID_NOT_FIND_SYMBOL)) {
cleanObject(&o);
system(buf); system(buf);
free(buf); } else {
write_history(settings.historyFile);
continue;
}
free(buf);
size_t length; size_t length;
char* output = stringObj(&o, &length); char* output = stringObj(&o, &length);
cleanObject(&o);
printColored(output); printColored(output);
free(output); free(output);
printf("\n"); printf("\n");
}
write_history(settings.historyFile); write_history(settings.historyFile);
cleanObject(&o);
free(buf);
} }
} }
void loadArgsIntoEnv(int argc, const char* argv[], struct Environment* env) void loadArgsIntoEnv(int argc, const char* argv[], struct Environment* env)
{ {
Object args = listObject(); BuildListNamed(args);
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
nf_addToList(&args, nullTerminated(argv[i])); addToList(args, nullTerminated(argv[i]));
} }
addToEnv(env, "args", args); addToEnv(env, "args", args);
} }

View File

@ -28,6 +28,9 @@
_element = _element->forward) _element = _element->forward)
#define POINTER _element #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 #ifdef PBL_PLATFORM_APLITE
#define LOW_MEM #define LOW_MEM
#endif #endif

View File

@ -93,7 +93,7 @@ Object mapO(Object* params, int length, struct Environment* env)
return errorObject(BAD_TYPE); return errorObject(BAD_TYPE);
} }
Object outputList = listObject(); BuildListNamed(outputList);
FOR_POINTER_IN_LIST(inputList) { FOR_POINTER_IN_LIST(inputList) {
// Create a new list for each element, // Create a new list for each element,
// since lambda evaluation looks for a list // 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); struct Environment newEnv = envForLambda(lambdaParams, &tempInput, listLength(lambdaParams), env);
// Add the lambda evaluation to the list // Add the lambda evaluation to the list
Object lambda_output = eval(&lambda.lambda->body, &newEnv); addToList(outputList, eval(&lambda.lambda->body, &newEnv));
nf_addToList(&outputList, lambda_output);
deleteEnv(&newEnv); deleteEnv(&newEnv);
cleanObject(&tempInput); cleanObject(&tempInput);
} }

View File

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

View File

@ -54,14 +54,14 @@ Object filter(Object* params, unused int length, struct Environment* env)
Object condition = params[0]; Object condition = params[0];
Object list = params[1]; Object list = params[1];
Object filtered = listObject(); BuildListNamed(filtered);
FOR_POINTER_IN_LIST(&list) { FOR_POINTER_IN_LIST(&list) {
Object cloned = cloneObject(condition); Object cloned = cloneObject(condition);
Object first_eval = eval(&cloned, env); Object first_eval = eval(&cloned, env);
Object testee = cloneObject(*POINTER); Object testee = cloneObject(*POINTER);
Object e = funcyEval(&first_eval, &testee, 1, env); Object e = funcyEval(&first_eval, &testee, 1, env);
if (e.number) { 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(&e);
cleanObject(&cloned); cleanObject(&cloned);