summaryrefslogtreecommitdiff
path: root/src/precedent.c
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2023-02-16 17:11:38 -0800
committerKai Stevenson <kai@kaistevenson.com>2023-02-16 17:11:38 -0800
commit7410d984ae270528ea4f3f16d3473acf807ef92c (patch)
treeb98ceaab29da9d791392007ad1c8e99d82e7210a /src/precedent.c
parenta2063489b84cbc47bd51aee761b3c5dd35e0195f (diff)
preprocessor works now, added some helper methods and a few more frees
Diffstat (limited to 'src/precedent.c')
-rw-r--r--src/precedent.c28
1 files changed, 13 insertions, 15 deletions
diff --git a/src/precedent.c b/src/precedent.c
index 81555b9..8be2ad3 100644
--- a/src/precedent.c
+++ b/src/precedent.c
@@ -39,7 +39,11 @@ int main(int argc, char** argv) {
char* baseDir = malloc((strlen(dirName) + 2) * sizeof(char));
strcpy(baseDir, dirName);
strcat(baseDir, "/");
- sym_t* finalSymbol = parse_symbol_final(read_file(path));
+ char* finalContent = preprocess(read_file(path), baseDir);
+ sym_t* finalSymbol = parse_symbol_final(finalContent);
+ free(pathCpy);
+ free(baseDir);
+ free(finalContent);
if (visual) {
printf("--BEGIN VISUALIZATION--\n");
write_symbol_visual(finalSymbol, 0);
@@ -47,13 +51,13 @@ int main(int argc, char** argv) {
}
init_eval();
eval(finalSymbol);
- free(dirName);
- free(pathCpy);
- free(baseDir);
return 0;
}
char* read_file(char* path) {
FILE* file = fopen(path, "r");
+ if (file == NULL) {
+ printf("file %s does not exist\n", path);
+ }
fseek(file, 0, SEEK_END);
unsigned long int length = ftell(file);
fseek(file, 0, SEEK_SET);
@@ -63,18 +67,16 @@ char* read_file(char* path) {
out[length] = '\0';
return out;
}
-/* wtf */
char* preprocess(char* str, char* baseDir) {
- char* final = str;
while (1) {
int i = 0;
while (str[i] != '@') {
i++;
if (str[i] == '\0') {
- printf("%s\n", final);
- return final;
+ return str;
}
}
+ str[i] = ' ';
i++;
char* includeName = malloc(20 * sizeof(char));
int f = 0;
@@ -84,21 +86,17 @@ char* preprocess(char* str, char* baseDir) {
i++;
f++;
}
- final = str;
includeName[f] = '\0';
char* path = malloc((strlen(baseDir) + strlen(includeName) + 1) * sizeof(char));
strcpy(path, baseDir);
strcat(path, includeName);
char* toInclude = preprocess(read_file(path), baseDir);
- char* newStr = malloc((strlen(toInclude) + strlen(final) + 1) * sizeof(char));
+ char* newStr = malloc((strlen(toInclude) + strlen(str) + 1) * sizeof(char));
strcpy(newStr, toInclude);
- strcat(newStr, final);
- /*
+ strcat(newStr, str);
+ str = newStr;
free(includeName);
free(path);
- free(str);
free(toInclude);
- */
- final = newStr;
}
}