From 94868542678358870cc1cf836f8bb28658abf20b Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Thu, 29 Oct 2020 11:18:06 -0400 Subject: [PATCH] Basic file execution --- src/pebblisp.c | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/pebblisp.c b/src/pebblisp.c index 441ba59..7138054 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -549,6 +549,29 @@ Object parseEval(const char *input, struct Environment *env) } #ifdef STANDALONE +int read_file(const char *filename, struct Environment *env) { + FILE *input = fopen(filename, "r"); + if(!input) { + return 1; + } + Object r = numberObject(0); + char buf[256]; + if(fgets(buf, 256, input)){ + if(buf[0] != '#' || buf[1] != '!') { + r = parseEval(buf, env); + printAndClean(&r); + } + } + while(fgets(buf, 256, input)) { + if(buf[0] != ';') { + r = parseEval(buf, env); + printAndClean(&r); + } + } + fclose(input); + return 0; +} + void repl(struct Environment *env) { char input[200] = ""; @@ -564,10 +587,12 @@ int main(int argc, const char* argv[]) { struct Environment env = defaultEnv(); if(argc >= 2) { - Object r = numberObject(0); - for(int i = 1; i < argc; i++) { - r = parseEval(argv[i], &env); - printAndClean(&r); + if(read_file(argv[1], &env) != 0) { + Object r = numberObject(0); + for(int i = 1; i < argc; i++) { + r = parseEval(argv[i], &env); + printAndClean(&r); + } } } else { repl(&env);