summaryrefslogtreecommitdiff
path: root/precedent.c
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2023-02-16 02:00:58 -0800
committerKai Stevenson <kai@kaistevenson.com>2023-02-16 14:25:52 -0800
commit10a1f7e4ad1bc90436f58269a52cddfd0587be33 (patch)
tree6a4aa46f802ec804b3e409994f920bfa2eb12f71 /precedent.c
parent4b68d60daf495cef5eb22a521ce809d798418aa5 (diff)
added a preprocessor with include statements except it doesn't work
Diffstat (limited to 'precedent.c')
-rw-r--r--precedent.c54
1 files changed, 53 insertions, 1 deletions
diff --git a/precedent.c b/precedent.c
index b609fff..81555b9 100644
--- a/precedent.c
+++ b/precedent.c
@@ -1,6 +1,8 @@
#include <stdlib.h>
+#include <ctype.h>
#include <stdio.h>
#include <string.h>
+#include <libgen.h>
#include "precedent.h"
#include "hashtable.h"
#include "parse.h"
@@ -29,7 +31,15 @@ int main(int argc, char** argv) {
printf("usage: precedent <file> [--visualize] [--version] [--help]\n");
return 0;
}
- sym_t* finalSymbol = parse_symbol_final(read_file(argv[1]));
+ /* dirname may modify path, copy first */
+ char* path = argv[1];
+ char* pathCpy = malloc((strlen(path) + 1) * sizeof(char));
+ strcpy(pathCpy, path);
+ char* dirName = dirname(pathCpy);
+ char* baseDir = malloc((strlen(dirName) + 2) * sizeof(char));
+ strcpy(baseDir, dirName);
+ strcat(baseDir, "/");
+ sym_t* finalSymbol = parse_symbol_final(read_file(path));
if (visual) {
printf("--BEGIN VISUALIZATION--\n");
write_symbol_visual(finalSymbol, 0);
@@ -37,6 +47,9 @@ int main(int argc, char** argv) {
}
init_eval();
eval(finalSymbol);
+ free(dirName);
+ free(pathCpy);
+ free(baseDir);
return 0;
}
char* read_file(char* path) {
@@ -50,3 +63,42 @@ 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;
+ }
+ }
+ i++;
+ char* includeName = malloc(20 * sizeof(char));
+ int f = 0;
+ while (isalnum(str[i]) || str[i] == '/' || str[i] == '.' || str[i] == '_') {
+ includeName[f] = str[i];
+ str[i] = ' ';
+ 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));
+ strcpy(newStr, toInclude);
+ strcat(newStr, final);
+ /*
+ free(includeName);
+ free(path);
+ free(str);
+ free(toInclude);
+ */
+ final = newStr;
+ }
+}