Centralize/simplify file-reading.

Handle comments at the token level.
This commit is contained in:
Sage Vaillancourt 2022-04-18 11:18:38 -04:00 committed by Sage Vaillancourt
parent f458b52a17
commit 04b7e7b64a
4 changed files with 35 additions and 59 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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)

View File

@ -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