#pragma ide diagnostic ignored "misc-no-recursion" #include "pebblisp.h" #include #include #include #include "tokens.h" #ifdef STANDALONE #include "web.h" #endif /** * Inserts a variable into the environment with a given name and value. * * If `argForms` (symbol) and `argForms->forward` (value) are lists of the same * length, define each symbol element with the corresponding value element. * I.e. `(def (a b) (5 20))` would store `a` as `5` and `b` as `20`. * * @param argForms The symbol(s) and value(s) to define in the environment * @param env The environment to add the new definition to * @return The symbol(s) defined */ Object evalDefArgs(const Object* symbol, const Object* value, struct Environment* env) { const char* name = symbol->string; // Handles multi-definitions if (bothAre(TYPE_LIST, symbol, value) && listLength(symbol) == listLength(value)) { FOR_POINTERS_IN_LISTS(symbol, value) { Object finalValue = eval(P2, env); addToEnv(env, P1->string, finalValue); cleanObject(&finalValue); } return cloneObject(*symbol); } Object finalValue = eval(value, env); addToEnv(env, name, finalValue); cleanObject(&finalValue); return cloneObject(*symbol); } void printStructDef(const struct StructDef *def) { printf("%s: {\n", def->name); for (int i = 0; i < def->fieldCount; i++) { printf(" %s,\n", def->names[i]); } printf("}\n"); } /** * Add a struct to the environment with a given name and fields. * * (struct point (x y)) */ Object evalStructArgs(const Object* symbol, const Object* fields, struct Environment* env) { const char* name = symbol->string; if (!isListy(*fields)) { return errorObject(NOT_A_LIST); } struct StructDef def; //def.name = name; def.name = malloc(sizeof(char) * (strlen(name) + 1)); strcpy(def.name, name); def.fieldCount = listLength(fields); def.names = malloc(sizeof(char*) * def.fieldCount); int i = 0; FOR_POINTER_IN_LIST(fields) { def.names[i] = malloc(sizeof(char) * (strlen(POINTER->string) + 1)); strcpy(def.names[i], POINTER->string); i++; } while (env->outer) { env = env->outer; } env->structDefs[env->structCount] = def; //printStructDef(&env->structDefs[env->structCount]); env->structCount += 1; if (env->structCount == env->structCapacity) { struct StructDef* prev = env->structDefs; int prevCapacity = env->structCapacity; env->structCapacity *= 2; env->structDefs = malloc(sizeof(struct StructDef) * env->structCapacity); printf("Can malloc\n"); //memcpy(env->structDefs, prev, sizeof(struct StructDef) * prevCapacity); //printf("Can memcpy\n"); for (int i = 0; i < prevCapacity; i++) { env->structDefs[i] = prev[i]; } free(prev); } return boolObject(1); } Object evalIfArgs(const Object* argForms, struct Environment* env) { Object condition = eval(argForms, env); Object result = condition.number ? eval(argForms->forward, env) : eval(argForms->forward->forward, env); cleanObject(&condition); return result; } Object evalLambdaArgs(const Object* argForms) { return constructLambda(argForms, // Params argForms ? argForms->forward : NULL // Body ); } Object evalMapArgs(const Object* argForms, struct Environment* env) { if (!argForms) { return errorObject(NULL_MAP_ARGS); } Object lambda = eval(argForms, env); const Object* inputList = argForms->forward; Object outputList = listObject(); if (lambda.type != TYPE_LAMBDA) { return errorObject(BAD_TYPE); } if (inputList) { FOR_POINTER_IN_LIST(inputList) { // Create a new list for each element, // since lambda evaluation looks for a list Object tempList = startList(cloneObject(*POINTER)); Object* params = &lambda.lambda->params; struct Environment newEnv = envForLambda(params, &tempList, env); // Add the lambda evaluation to the list Object lambda_output = eval(&lambda.lambda->body, &newEnv); Object delisted = cloneObject(*lambda_output.list); nf_addToList(&outputList, delisted); deleteEnv(&newEnv); cleanObject(&tempList); cleanObject(&lambda_output); } } cleanObject(&lambda); return outputList; } Object evalBuiltIns(const Object* first, const Object* rest, struct Environment* env) { if (first->type != TYPE_SYMBOL) { return errorObject(NOT_A_SYMBOL); } if (strcmp(first->string, "def") == 0) { return evalDefArgs(rest, rest->forward, env); #ifndef LOW_MEM } else if (strcmp(first->string, "defe") == 0) { Object symbol = eval(rest, env); Object e = evalDefArgs(&symbol, rest->forward, env); cleanObject(&symbol); return e; #endif } else if (strcmp(first->string, "if") == 0) { return evalIfArgs(rest, env); } else if (strcmp(first->string, "fn") == 0) { return evalLambdaArgs(rest); } else if (strcmp(first->string, "map") == 0) { return evalMapArgs(rest, env); } else if (strcmp(first->string, "struct") == 0) { return evalStructArgs(rest, rest->forward, env); } return errorObject(BUILT_IN_NOT_FOUND); } /** * Bulk evaluator of Objects in a given list. Puts the results in a given array * * Note that `destArr` is a raw array, not an Object list, and that `start` is * not itself a list, but the first element *in* a list to be evaluated. This * allows more speed and flexibility in what should be evaluated. * * @param destArr The raw array to put the results into. Not an Object list! * @param start The Object element to start with * @param env The environment to use while evaluating */ void evalForms(Object* destArr, const Object* start, struct Environment* env) { int i = 0; while (start) { destArr[i] = eval(start, env); start = start->forward; i++; } } /** * Evaluates a list whose first element is a function, applying that function * * Tries to either apply the function to its parameters, or create a partial * function, if not enough parameters were passed. * * @param list The list being evaluated * @param function First element of the list, already evaluated to be a function * @param length Length of `list` - 1, to exclude the already-evaluated element * @param env The environment to evaluate in */ Object listEvalFunc(const Object* list, const Object* function, const int length, struct Environment* env) { if (length == 0) { return function->func(boolObject(0), boolObject(0), env); } Object rest[length]; evalForms(rest, list->list->forward, env); Object func_result = rest[0]; if (length == 1) { Object oneArg = errorObject(ONLY_ONE_ARGUMENT); func_result = function->func(func_result, oneArg, env); // Return a partial function if more parameters are required // Otherwise, return the function result cleanObject(&rest[0]); if (isError(func_result, ONLY_ONE_ARGUMENT)) { // These functions modify their second argument, // so we don't clean oneArg here cleanObject(&func_result); return cloneObject(*list); } else { cleanObject(&oneArg); return func_result; } } else { // With two args, will apply function once // With more than two args, apply the function repeatedly for (int i = 1; i < length; i++) { Object toClean = func_result; func_result = function->func(func_result, rest[i], env); cleanObject(&toClean); cleanObject(&rest[i]); } return func_result; } } /** * Evaluates a list whose first element is a lambda, applying that lambda * * Tries to apply the lambda to its parameters. Doesn't attempt partial * application. * * @param lambda First element of the list, already evaluated to be a lambda * @param remaining The first element after `lambda` * @param env The environment to evaluate in */ Object listEvalLambda(Object* lambda, const Object* remaining, struct Environment* env) { struct Environment newEnv = envForLambda(&lambda->lambda->params, remaining, env); Object ret = eval(&lambda->lambda->body, &newEnv); deleteEnv(&newEnv); cleanObject(lambda); Object* t = tail(&ret); if (t) { Object o = cloneObject(*t); cleanObject(&ret); return o; } return ret; } /** * Evaluates a given list, including the application of functions and lambdas * * Engages in several behaviors, depending on list contents: * - () => () * - (x y z) => (eval_x eval_y eval_z) * - (function x y) => evaluated function(x, y) * - (function ...) => evaluated function(...) applied to each arg * - (function x) => functionx() partial function * - (lambda x) => evaluated lambda(x) * * @param obj The list to be evaluated * @param env The environment to evaluate in */ Object evalList(const Object* obj, struct Environment* env) { const int evalLength = listLength(obj); if (evalLength == 0) { return cloneObject(*obj); } Object* first_form = obj->list; { // Try to eval built-ins Object builtIn = evalBuiltIns(first_form, first_form->forward, env); if (!isError(builtIn, BUILT_IN_NOT_FOUND) && !isError(builtIn, NOT_A_SYMBOL)) { return builtIn; } cleanObject(&builtIn); } //struct StructDef *def = NULL; int def = -1; if (first_form->type == TYPE_SYMBOL) { struct Environment* outerEnv = env; while (outerEnv->outer) { outerEnv = outerEnv->outer; } //printf("evalList firstElementName: %s\n", first_form->string); //printf("checking for struct `%s`\n", first_form->string); //printf("%d structs available\n", outerEnv->structCount); for (int i = 0; i < outerEnv->structCount; i++) { //printf("struct[%d] - `%s`\n", i, outerEnv->structDefs[i].name); if (strcmp(first_form->string, outerEnv->structDefs[i].name) == 0) { def = i; //printf("Found struct definition for %s!\n", first_form->string); break; } } } if (def >= 0) { Object structo = structObject(def); int i = 0; FOR_POINTER_IN_LIST(obj) { if (i != 0) { structo.structObject->fields[i - 1] = eval(POINTER, env); } i++; } return structo; } // Evaluate the list based on the first element's type Object first_eval = eval(first_form, env); switch (first_eval.type) { case TYPE_FUNC: // Passes evalLength - 1, because we skip the first form return listEvalFunc(obj, &first_eval, evalLength - 1, env); case TYPE_LAMBDA: return listEvalLambda(&first_eval, first_form->forward, env); default: { // Return list with each element evaluated Object list = listObject(); int i = 0; nf_addToList(&list, first_eval); FOR_POINTER_IN_LIST(obj) { if (i != 0) { nf_addToList(&list, eval(POINTER, env)); } i++; } return list; } } } Object eval(const Object* obj, struct Environment* env) { switch (obj->type) { case TYPE_FUNC: case TYPE_ERROR: case TYPE_OTHER: case TYPE_NUMBER: case TYPE_BOOL: case TYPE_STRING: case TYPE_STRUCT: return cloneObject(*obj); case TYPE_SYMBOL: return fetchFromEnvironment(obj->string, env); case TYPE_SLIST: { Object o = cloneObject(*obj); o.type = TYPE_LIST; return o; } case TYPE_LIST: return evalList(obj, env); case TYPE_LAMBDA: return eval(&obj->lambda->body, env); } return errorObject(BAD_TYPE); } Object catObjects(const Object obj1, const Object obj2, struct Environment* env) { Object evalObj1 = eval(&obj1, env); Object evalObj2 = eval(&obj2, env); char str1[100] = ""; char str2[100] = ""; stringObj(str1, &evalObj1); stringObj(str2, &evalObj2); cleanObject(&evalObj1); cleanObject(&evalObj2); int length = strlen(str1) + strlen(str2) + 1; Object o = newObject(TYPE_STRING); o.string = calloc(sizeof(char), length); strcat(o.string, str1); strcat(o.string, str2); return o; } Object listEquality(const Object* list1, const Object* list2) { FOR_POINTERS_IN_LISTS(list1, list2) { if (P1->type != P2->type || P1->number != P2->number) { return boolObject(0); } } return boolObject(1); } Object _basicOp(const Object* obj1, const Object* obj2, const char op, struct Environment* env) { const int n1 = obj1->number; const int n2 = obj2->number; switch (op) { case '+': if (eitherIs(TYPE_STRING, obj1, obj2)) { return catObjects(*obj1, *obj2, env); } return numberObject(n1 + n2); case '-': return numberObject(n1 - n2); case '*': return numberObject(n1 * n2); case '/': return numberObject(n1 / n2); case '%': return numberObject(n1 % n2); case '&': return boolObject(n1 != 0 && n2 != 0); case '|': return boolObject(n1 != 0 || n2 != 0); case '=': if (bothAre(TYPE_STRING, obj1, obj2)) { return boolObject(!strcmp(obj1->string, obj2->string)); } if (bothAre(TYPE_LIST, obj1, obj2)) { return listEquality(obj1, obj2); } return boolObject(n1 == n2 && areSameType(obj1, obj2)); case '>': return boolObject(n1 > n2); case '<': return boolObject(n1 < n2); } return *obj1; } Object basicOp(const Object* obj1, const Object* obj2, const char op, struct Environment* env) { if (isError(*obj2, ONLY_ONE_ARGUMENT)) { return *obj2; } int lists = (obj1->type == TYPE_LIST) + (obj2->type == TYPE_LIST); if (lists == 0) { return _basicOp(obj1, obj2, op, env); } else if (lists == 1) { // Single operand is applied to each element in list const Object* listObj = (obj1->type == TYPE_LIST) ? obj1 : obj2; const Object* singleObj = (obj1->type == TYPE_LIST) ? obj2 : obj1; Object newList = listObject(); FOR_POINTER_IN_LIST(listObj) { Object adding = eval(POINTER, env); nf_addToList(&newList, _basicOp(&adding, singleObj, op, env)); } return newList; } else { // 2 lists with the op applied to matching indices of both lists if (listLength(obj1) == listLength(obj2)) { Object newList = listObject(); FOR_POINTERS_IN_LISTS(obj1, obj2) { const Object ev1 = eval(P1, env); const Object ev2 = eval(P2, env); nf_addToList(&newList, _basicOp(&ev1, &ev2, op, env)); } return newList; } else { return errorObject(LISTS_NOT_SAME_SIZE); } } } #define BASIC_OP(_name, _char) \ Object _name(Object obj1, Object obj2, struct Environment *env) \ { \ return basicOp(&obj1, &obj2, _char, env); \ } BASIC_OP(add, '+'); BASIC_OP(sub, '-'); BASIC_OP(mul, '*'); BASIC_OP(dvi, '/'); BASIC_OP(mod, '%'); BASIC_OP(equ, '='); BASIC_OP(gth, '>'); BASIC_OP(lth, '<'); BASIC_OP(and, '&'); BASIC_OP(or, '|'); #undef BASIC_OP Object filter(Object obj1, Object obj2, struct Environment* env) { Object filteredList = listObject(); Object* filteringList = &obj2; FOR_POINTER_IN_LIST(filteringList) { Object conditional = cloneObject(obj1); nf_addToList(&conditional, *POINTER); // cloneObject()? Object result = eval(&conditional, env); cleanObject(&conditional); if (result.number == 1) { nf_addToList(&filteredList, *POINTER); } } return filteredList; } Object append(Object list, Object newElement, struct Environment* env) { Object newList = cloneObject(list); nf_addToList(&newList, cloneObject(newElement)); return newList; } Object prepend(Object list, Object newElement, struct Environment* env) { Object newList = listObject(); nf_addToList(&newList, cloneObject(newElement)); appendList(&newList, &list); return newList; } Object at(Object index, Object list, struct Environment* env) { const Object* found = itemAt(&list, index.number); if (found) { return cloneObject(*found); } else { return errorObject(INDEX_PAST_END); } } Object rest(Object list, Object ignore, struct Environment* env) { Object ret = listObject(); Object* l = &list; FOR_POINTER_IN_LIST(l) { if (POINTER == l->list) { continue; } nf_addToList(&ret, cloneObject(*POINTER)); } return ret; } Object reverse(Object _list, Object ignore, struct Environment* ignore2) { const Object* list = &_list; Object rev = listObject(); Object* tail = NULL; FOR_POINTER_IN_LIST(list) { Object* oldTail = tail; allocObject(&tail, cloneObject(*POINTER)); if (oldTail) { tail->forward = oldTail; } } rev.list = tail; return rev; } Object isNum(Object test, Object ignore, struct Environment* ignore2) { return test.type == TYPE_NUMBER ? boolObject(1) : boolObject(0); } Object isString(Object test, Object ignore, struct Environment* ignore2) { return test.type == TYPE_STRING ? boolObject(1) : boolObject(0); } // Get the int value of a string's first character Object charVal(Object test, Object ignore, struct Environment* ignore2) { return numberObject(test.string[0]); // return test.type == TYPE_STRING && test.string[0] == '\0' ? // boolObject(1) : boolObject(0); } Object isErr(Object test, Object ignore, struct Environment* ignore2) { return test.type == TYPE_ERROR ? boolObject(1) : boolObject(0); } Object charAt(Object string, Object at, struct Environment* ignore) { char* c = malloc(sizeof(char) * 2); c[0] = string.string[at.number]; c[1] = '\0'; string.string = c; return string; } #ifdef STANDALONE Object print(Object p, Object ignore, struct Environment* env) { p = cloneObject(p); p = eval(&p, env); _printObj(&p, 0); return numberObject(0); } Object addRouteO(Object path, Object textFunc, struct Environment* env) { // const char* p = malloc(sizeof(char) * strlen(path.string) + 1); // strcpy(p, path.string); // //const char* t = malloc(sizeof(char) * strlen(textFunc.string) + 1); // //strcpy(t, textFunc.string); // struct Route r; // r.path = p; // r.routeAction = cloneObject(textFunc); // r.env = env; // env->refs += 1; // //r.text = t; // addRoute(r); return numberObject(1); } Object startServer(Object path, Object textFunc, struct Environment* env) { return numberObject(1/*start(8888)*/); } Object pChar(Object c, Object i1, struct Environment* i2) { if (c.type != TYPE_NUMBER) { return errorObject(BAD_NUMBER); } printf("%c", c.number % 256); return numberObject(0); } Object printEnvO(Object i1, Object i2, struct Environment* env) { while (env->outer) { env = env->outer; } printEnv(env); return numberObject(0); } #endif Object parseEvalO(Object text, Object ignore, struct Environment* env) { if (text.type == TYPE_SYMBOL) { Object string = eval(&text, env); Object parsed = parseEval(string.string, env); cleanObject(&string); return parsed; } else if (text.type != TYPE_STRING) { return errorObject(CAN_ONLY_EVAL_STRINGS); } return parseEval(text.string, env); } #ifdef STANDALONE Object takeInput(Object i1, Object i2, struct Environment* i3) { char input[256] = ""; fgets(input, 256, stdin); return stringFromSlice(input, strlen(input) - 1); } #endif void copySlice(char* dest, struct Slice* src) { if (!dest || !src) { return; } strncpy(dest, src->text, src->length); dest[(int) src->length] = '\0'; } void debugSlice(struct Slice* s) { #ifdef DEBUG if (!s) { printf("NULL SLICE\n"); return; } printf("Debug Slice\n text:'"); for (int i = 0; i < s->length; i++) { printf("%c", s->text[i]); if (s->text[i] == '\0') { printf("NULLCHAR\n"); } } printf("'\n"); printf(" length: %d\n", s->length); #endif } Object possessive(Object structo, Object field, struct Environment* env) { if (structo.type != TYPE_STRUCT) { printf("`'s` must be used on a struct!\n"); return errorObject(NULL_PARSE); } if (!isStringy(field)) { printf("`'s` field name must be stringy! Received a "); printType(&field); printObj(&field); printf("\n"); return errorObject(NULL_PARSE); } struct StructDef structDef = global->structDefs[structo.structObject->definition]; for (int i = 0; i < structDef.fieldCount; i++) { if (strcmp(field.string, structDef.names[i]) == 0) { return cloneObject(structo.structObject->fields[i]); } } printf("Could not find field name `%s`\n", field.string); return errorObject(NULL_PARSE); } Result parse(struct Slice* slices) { struct Slice* token = slices; if (token && token->text) { struct Slice* rest = &slices[1]; if (token->text[0] == '\'' && token->text[1] == '(') { Result r = readSeq(&slices[2]); r.obj.type = TYPE_SLIST; return r; } else if (token->text[0] == '(') { // todo check for null rest return readSeq(rest); } else { // todo error on missing close paren Result r = parseAtom(token); r.slices = &r.slices[1]; return r; } } else { return (Result) {errorObject(NULL_PARSE), NULL}; } } Result readSeq(struct Slice* tokens) { Object res = listObject(); for (;;) { struct Slice* next = tokens; struct Slice* rest = next->text ? &next[1] : NULL; if (next->text[0] == ')') { return (Result) {res, rest}; } Result r = parse(tokens); if (r.obj.type == TYPE_ERROR) { return r; } nf_addToList(&res, cloneObject(r.obj)); tokens = r.slices; cleanObject(&r.obj); } } Object parseDecimal(struct Slice* s) { int num = 0; for (int i = 0; i < s->length; i++) { if (!isDigit(s->text[i])) { return errorObject(BAD_NUMBER); } num *= 10; num += s->text[i] - '0'; } return numberObject(num); } Object parseHex(struct Slice* s) { int num = 0; for (int i = 2; i < s->length; i++) { const char c = s->text[i]; if (!isHex(c)) { return errorObject(BAD_NUMBER); } num *= 16; if (isDigit(c)) { num += c - '0'; } else /* is hex */ { num += c - 'a' + 10; } } return numberObject(num); } Object parseBin(struct Slice* s) { int num = 0; for (int i = 2; i < s->length; i++) { const char c = s->text[i]; if (c != '0' && c != '1') { return errorObject(BAD_NUMBER); } num *= 2; num += c - '0'; } return numberObject(num); } Result parseAtom(struct Slice* s) { const char c = s->text[0]; if (isDigit(c)) { if (c != '0' || s->length == 1) { return (Result) {parseDecimal(s), s}; #ifndef LOW_MEM } else if (c == '0' && s->text[1] == 'x') { return (Result) {parseHex(s), s}; } else if (c == '0' && s->text[1] == 'b') { return (Result) { parseBin(s), s }; #endif } else { return (Result) { errorObject(UNSUPPORTED_NUMBER_TYPE), s}; } } else if (s->length == 1 && (c == 'T' || c == 't')) { return (Result) { boolObject(1), s}; } else if (s->length == 1 && (c == 'F' || c == 'f')) { return (Result) { boolObject(0), s}; } else if (c == '"'/* || c == '\''*/) { return (Result) { objFromSlice(s->text, s->length), s}; } else { if (s->text[s->length] == '\'' && s->text[s->length + 1] == 's') { Object possessiveFunc = newObject(TYPE_FUNC); possessiveFunc.func = &possessive; Object list = startList(possessiveFunc); Object possesser = symFromSlice(s->text, s->length); nf_addToList(&list, possesser); struct Slice* next = s + 3; Object possessed = objFromSlice(&next->text[-1], next->length + 1); nf_addToList(&list, possessed); return (Result) { list, next }; } return (Result) { symFromSlice(s->text, s->length), s}; } } Object parseEval(const char* input, struct Environment* env) { struct Error err = noError(); struct Slice* tokens = nf_tokenize(input, &err); if (err.context != NULL) { Object o = errorWithContext(err.code, err.context); free(err.context); return o; } if (!tokens->text) { return symFromSlice(" ", 1); } #ifdef DEBUG struct Slice *debug = tokens; printd("start slice\n"); if (debug) { while (debug->text) { char tok[100]; copySlice(tok, debug); printd("slice: '%s'\n", tok); debug++; } } #endif int i = 0; int parens = 0; Object obj = numberObject(0); struct Slice* tok = tokens; while (tok[i].text != NULL) { if (tok[i].text[0] == '(') { parens++; } else if (tok[i].text[0] == ')') { parens--; } if (parens == 0) { cleanObject(&obj); Object parsed = parse(tok).obj; if (parsed.type == TYPE_ERROR) { obj = parsed; // TODO Check necessity break; } if (tok[i].text[0] == ')') { // Skip `tok` past end of list that just closed tok = &tok[i + 1]; i = -1; } if (parsed.type == TYPE_SLIST) { obj = parsed; } else { obj = eval(&parsed, env); cleanObject(&parsed); } } i++; } free(tokens); return obj; } #ifdef STANDALONE int _readFile(FILE* input, struct Environment* env) { Object r = numberObject(0); char page[4096] = ""; const int LINE_MAX = 256; char line[LINE_MAX]; if (fgets(line, LINE_MAX, input)) { if (line[0] != '#' || line[1] != '!') { strncat(page, line, strlen(line) - 1); } } while (fgets(line, LINE_MAX, input)) { int i; for (i = 0; i < LINE_MAX; i++) { if (line[i] != ' ') { if (line[i] == ';') { break; } else { int j = 0; for (j = i; j < LINE_MAX; j++) { if (line[j] == ';' || line[j] == '\0') { break; } } strncat(page, line, j); strcat(page, " "); break; } } } } r = parseEval(page, env); cleanObject(&r); fclose(input); return 0; } int readFile(const char* filename, struct Environment* env) { FILE* input = fopen(filename, "r"); if (!input) { return 1; } _readFile(input, env); return 0; } Object loadFile(Object filename, Object _, struct Environment* env) { if (isStringy(filename)) { readFile(filename.string, env); return numberObject(0); } return numberObject(1); } Object systemCall(Object process, Object _, struct Environment* env) { if (isStringy(process)) { return numberObject(system(process.string)); } return numberObject(255); } void repl(struct Environment* env) { if (readFile(SCRIPTDIR "/repl.pbl", env) == 1) { fprintf(stderr, "Could not read '%s'\n", SCRIPTDIR "/repl.pbl"); fprintf(stderr, "Consider installing or reinstalling pebblisp.\n"); } } void loadArgsIntoEnv(int argc, const char* argv[], struct Environment* env) { Object args = listObject(); for (int i = 0; i < argc; i++) { nf_addToList(&args, stringFromSlice(argv[i], strlen(argv[i]))); } addToEnv(env, "args", args); } int main(int argc, const char* argv[]) { struct Environment env = defaultEnv(); global = &env; readFile(SCRIPTDIR "/lib.pbl", &env); if (argc >= 2) { FILE* file = fopen(argv[1], "r"); if (file) { // Executing a file loadArgsIntoEnv(argc, argv, &env); _readFile(file, &env); } else { // Running arguments directly as pl code Object r = numberObject(0); for (int i = 1; i < argc; i++) { r = parseEval(argv[i], &env); printAndClean(&r); } } } else { // Running a repl loadArgsIntoEnv(argc, argv, &env); repl(&env); } deleteEnv(&env); } #endif