Load script arguments into `args` list

For example, running "./do_math.pl 5 + 5" would define args as a list of
( /usr/bin/pl ./do_math.pl 5 + 5 ), where each of the five elements is a
string object. The arguments will also be loaded when opening the repl,
but NOT when writing  pl code directly as command-line arguments (i.e.
'./pl "(* 9 9)"').
This commit is contained in:
Sage Vaillancourt 2021-07-10 22:27:24 -04:00
parent 96a47590f1
commit f89417ab9e
1 changed files with 27 additions and 8 deletions

View File

@ -756,11 +756,7 @@ Object parseEval(const char *input, struct Environment *env)
} }
#ifdef STANDALONE #ifdef STANDALONE
int readFile(const char *filename, struct Environment *env) { int _readFile(FILE *input, struct Environment *env) {
FILE *input = fopen(filename, "r");
if(!input) {
return 1;
}
Object r = numberObject(0); Object r = numberObject(0);
char page[4096] = ""; char page[4096] = "";
const unsigned LINE_MAX = 256; const unsigned LINE_MAX = 256;
@ -797,6 +793,14 @@ int readFile(const char *filename, struct Environment *env) {
fclose(input); fclose(input);
return 0; 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) { Object loadFile(Object filename, Object _, struct Environment *env) {
if (isStringy(filename)) { if (isStringy(filename)) {
@ -806,20 +810,33 @@ Object loadFile(Object filename, Object _, struct Environment *env) {
return numberObject(1); return numberObject(1);
} }
void repl(struct Environment *env) void repl(struct Environment *env) {
{
if (readFile(SCRIPTDIR "/repl.pbl", env) == 1) { if (readFile(SCRIPTDIR "/repl.pbl", env) == 1) {
fprintf(stderr, "Could not read '%s'\n", SCRIPTDIR "/repl.pbl"); fprintf(stderr, "Could not read '%s'\n", SCRIPTDIR "/repl.pbl");
fprintf(stderr, "Consider installing or reinstalling pebblisp.\n"); 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[]) int main(int argc, const char* argv[])
{ {
struct Environment env = defaultEnv(); struct Environment env = defaultEnv();
readFile(SCRIPTDIR "/lib.pbl", &env); readFile(SCRIPTDIR "/lib.pbl", &env);
FILE *file = fopen(argv[1], "r");
if(argc >= 2) { 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); Object r = numberObject(0);
for(int i = 1; i < argc; i++) { for(int i = 1; i < argc; i++) {
r = parseEval(argv[i], &env); r = parseEval(argv[i], &env);
@ -827,6 +844,8 @@ int main(int argc, const char* argv[])
} }
} }
} else { } else {
// Running a repl
loadArgsIntoEnv(argc, argv, &env);
repl(&env); repl(&env);
} }
deleteEnv(&env); deleteEnv(&env);