From 04b7e7b64aa3e7921f347681d0f1376f5138e39a Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Mon, 18 Apr 2022 11:18:38 -0400 Subject: [PATCH] Centralize/simplify file-reading. Handle comments at the token level. --- src/pebblisp.c | 63 ++++++++++++++++++++++---------------------------- src/pebblisp.h | 2 ++ src/plfunc.c | 24 ------------------- src/tokens.c | 5 ++++ 4 files changed, 35 insertions(+), 59 deletions(-) diff --git a/src/pebblisp.c b/src/pebblisp.c index 7f7d21b..78f8418 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -589,6 +589,31 @@ Object typeCheck(const char* funcName, Object* params, int length, #ifdef STANDALONE +char* readFileToString(FILE* input) +{ + size_t capacity = 128; + char* string = malloc(sizeof(char) * (capacity + 1)); + string[0] = 1; // Set refCount + + int c; + int i = 1; // Skip refCount + + while ((c = fgetc(input)) != EOF) { + string[i] = c; + i++; + if (i == capacity) { + char* prev = string; + capacity *= 2; + string = malloc(sizeof(char) * capacity); + memcpy(string, prev, sizeof(char) * (capacity / 2)); + free(prev); + } + } + string[i] = '\0'; + + return string + 1; // Offset past refCount +} + /// Returns 1 if the file could not be opened. Otherwise, 0 int readFile(const char* filename, struct Environment* env) { @@ -602,43 +627,11 @@ int readFile(const char* filename, struct Environment* env) 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] != '!') { - strcat(page, line); - } - } - int isQuote = 0; - 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; - for (j = i; j < LINE_MAX; j++) { - if (line[j] == '"') { - isQuote = !isQuote; - } else if (line[j] == '\0' || (!isQuote && line[j] == ';')) { - break; - } - } - strncat(page, line, j); - strcat(page, " "); - break; - } - } - } - } - - r = parseEval(page, env); + char* fileText = readFileToString(input); + fclose(input); + Object r = parseEval(fileText, env); cleanObject(&r); - fclose(input); return 0; } diff --git a/src/pebblisp.h b/src/pebblisp.h index 079992e..12b5973 100644 --- a/src/pebblisp.h +++ b/src/pebblisp.h @@ -102,6 +102,8 @@ if (FAILED) { \ #ifdef STANDALONE +char* readFileToString(FILE* input); + int _readFile(FILE* input, struct Environment* env); int readFile(const char* filename, struct Environment* env); diff --git a/src/plfunc.c b/src/plfunc.c index 6561f64..c6e4ad9 100644 --- a/src/plfunc.c +++ b/src/plfunc.c @@ -518,30 +518,6 @@ Object systemCall(Object* params, unused int length, unused struct Environment* return numberObject(255); } -char* readFileToString(FILE* input) -{ - size_t capacity = 128; - char* string = malloc(sizeof(char) * capacity); - int c; - int i = 1; // Skip refCount - - string[0] = 1; // Set refCount - while ((c = fgetc(input)) != EOF) { - string[i] = c; - i++; - if (i == capacity) { - char* prev = string; - capacity *= 2; - string = malloc(sizeof(char) * capacity); - memcpy(string, prev, sizeof(char) * (capacity / 2)); - free(prev); - } - } - string[i] = '\0'; - - return string + 1; // Offset past refCount -} - Object readFileToObject(Object* params, unused int length, unused struct Environment* env) { checkTypes(readFileToObject) diff --git a/src/tokens.c b/src/tokens.c index 0c6433e..6b31f48 100644 --- a/src/tokens.c +++ b/src/tokens.c @@ -88,6 +88,11 @@ struct Slice* nf_tokenize(const char* input, struct Error* err) if (isSingle(input[i])) { i++; + } else if (input[i] == ';') { + while (input[i] && input[i] != '\n') { + i++; + } + continue; } else if (input[i] == '"') { if (input[i + 1] == '"' && input[i + 2] == '"') { // Triple-quoted block