diff --git a/src/pebblisp.c b/src/pebblisp.c index 5a55dcd..28d88fe 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -756,11 +756,7 @@ Object parseEval(const char *input, struct Environment *env) } #ifdef STANDALONE -int readFile(const char *filename, struct Environment *env) { - FILE *input = fopen(filename, "r"); - if(!input) { - return 1; - } +int _readFile(FILE *input, struct Environment *env) { Object r = numberObject(0); char page[4096] = ""; const unsigned LINE_MAX = 256; @@ -797,6 +793,14 @@ int readFile(const char *filename, struct Environment *env) { 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)) { @@ -806,20 +810,33 @@ Object loadFile(Object filename, Object _, struct Environment *env) { return numberObject(1); } -void repl(struct Environment *env) -{ +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(); readFile(SCRIPTDIR "/lib.pbl", &env); + FILE *file = fopen(argv[1], "r"); if(argc >= 2) { - if(readFile(argv[1], &env) != 0) { + 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); @@ -827,6 +844,8 @@ int main(int argc, const char* argv[]) } } } else { + // Running a repl + loadArgsIntoEnv(argc, argv, &env); repl(&env); } deleteEnv(&env);