From 10a1f7e4ad1bc90436f58269a52cddfd0587be33 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Thu, 16 Feb 2023 02:00:58 -0800 Subject: added a preprocessor with include statements except it doesn't work --- Makefile | 6 ++--- doc/boolean.pcdt | 14 ---------- doc/example.pcdt | 2 -- doc/internal_functions_dictionary | 6 +++++ doc/pcdt_examples/boolean.pcdt | 18 +++++++++++++ doc/pcdt_examples/example.pcdt | 2 ++ doc/pcdt_examples/helloworld.pcdt | 3 +++ doc/pcdt_examples/includetest.pcdt | 2 ++ eval.c | 3 ++- parse.c | 2 +- precedent.c | 54 +++++++++++++++++++++++++++++++++++++- precedent.h | 1 + 12 files changed, 91 insertions(+), 22 deletions(-) delete mode 100644 doc/boolean.pcdt delete mode 100644 doc/example.pcdt create mode 100644 doc/internal_functions_dictionary create mode 100644 doc/pcdt_examples/boolean.pcdt create mode 100644 doc/pcdt_examples/example.pcdt create mode 100644 doc/pcdt_examples/helloworld.pcdt create mode 100644 doc/pcdt_examples/includetest.pcdt diff --git a/Makefile b/Makefile index 3d5b3ed..bf83693 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ -CFLAGS=-I. -pthread -Wall -Wextra -Wpedantic -std=c99 -O3 -lm -CFLAGS+= -Wshadow -Wstrict-prototypes -Wundef -Wpointer-arith -Wcast-align -Wstrict-overflow=5 -Wswitch-default -CFLAGS+= -Wunreachable-code -Wswitch-enum -Wformat=2 -g +CFLAGS=-I. -pthread -Wall -Wextra -Wpedantic -O3 -lm +CFLAGS+= -Wshadow -Wstrict-prototypes -Wundef -Wpointer-arith -Wcast-align -Wstrict-overflow=5 -Wswitch-default +CFLAGS+= -Wunreachable-code -Wswitch-enum -Wformat=2 -Wc11-extensions -g PREFIX ?= /usr BINDIR ?= ${PREFIX}/bin diff --git a/doc/boolean.pcdt b/doc/boolean.pcdt deleted file mode 100644 index 81b1f62..0000000 --- a/doc/boolean.pcdt +++ /dev/null @@ -1,14 +0,0 @@ -store(1; xv;) -store(1; yv;) - -store( -eq(xv() yv()) -res;) - -if(res() -print(values equal;) -) - -if(not(res()) -print(values not;) -) diff --git a/doc/example.pcdt b/doc/example.pcdt deleted file mode 100644 index 68d0a84..0000000 --- a/doc/example.pcdt +++ /dev/null @@ -1,2 +0,0 @@ -store(hello;myVar;) -print(myVar()) diff --git a/doc/internal_functions_dictionary b/doc/internal_functions_dictionary new file mode 100644 index 0000000..b7fe7d6 --- /dev/null +++ b/doc/internal_functions_dictionary @@ -0,0 +1,6 @@ +root() -- executes all arguments. returns the eval of the last symbol +store() -- stores a pointer to the first argument under the name specified by the eval of the second argument +eq() -- returns 1 if the eval of all the arguments is the same, 0 if different. accepts any number of arguments +if() -- if the eval of the first argument is 1, executes all the other arguments +not() -- if the eval of the first argument is 1, returns 0, and vice versa +print() -- prints the eval of the first argument to the console, and a newline character diff --git a/doc/pcdt_examples/boolean.pcdt b/doc/pcdt_examples/boolean.pcdt new file mode 100644 index 0000000..e6becd6 --- /dev/null +++ b/doc/pcdt_examples/boolean.pcdt @@ -0,0 +1,18 @@ +store(14; var1;) +store(12; var2;) + +store( +eq(var1() var2()) +res;) + +print(var1()) + +if(res() +print(equals;) +) + +if(not(res()) +print(does not equal;) +) + +print(var2()) diff --git a/doc/pcdt_examples/example.pcdt b/doc/pcdt_examples/example.pcdt new file mode 100644 index 0000000..68d0a84 --- /dev/null +++ b/doc/pcdt_examples/example.pcdt @@ -0,0 +1,2 @@ +store(hello;myVar;) +print(myVar()) diff --git a/doc/pcdt_examples/helloworld.pcdt b/doc/pcdt_examples/helloworld.pcdt new file mode 100644 index 0000000..6ad1398 --- /dev/null +++ b/doc/pcdt_examples/helloworld.pcdt @@ -0,0 +1,3 @@ +store(print(hello world;) HelloWorldFunc;) +HelloWorldFunc() +HelloWorldFunc() diff --git a/doc/pcdt_examples/includetest.pcdt b/doc/pcdt_examples/includetest.pcdt new file mode 100644 index 0000000..81aa285 --- /dev/null +++ b/doc/pcdt_examples/includetest.pcdt @@ -0,0 +1,2 @@ +@helloworld.pcdt +HelloWorldFunc() diff --git a/eval.c b/eval.c index 05fd9b3..b1d4e0a 100644 --- a/eval.c +++ b/eval.c @@ -66,11 +66,12 @@ sym_t* try_eval_internal(sym_t* symbol) { /* first arg is value, remaining args are names */ /* TODO: recursion for multiple pairs */ char* k = symtos(eval(symbol->args[1])); - sym_t* v = eval(symbol->args[0]); + sym_t* v = symbol->args[0]; pair_t* p = init_pair(k, (void*)v); hashtable_add_pair(ht, p); return &trueSym; } + /* TODO: release()/free() method? */ else if (hash == get_hash("eq")) { litType_t type = NO_TYPE; void* lastVal = NULL; diff --git a/parse.c b/parse.c index 469b492..27ee417 100644 --- a/parse.c +++ b/parse.c @@ -63,7 +63,7 @@ symset_t* parse_symbols(char** str) { /* TODO: float */ /* string */ if (isalpha(symbolName[0])) { - symbolName = realloc(symbolName, i * sizeof(char)); + //symbolName = realloc(symbolName, i * sizeof(char)); symbol->name = symbolName; symbol->litType = STRING; } diff --git a/precedent.c b/precedent.c index b609fff..81555b9 100644 --- a/precedent.c +++ b/precedent.c @@ -1,6 +1,8 @@ #include +#include #include #include +#include #include "precedent.h" #include "hashtable.h" #include "parse.h" @@ -29,7 +31,15 @@ int main(int argc, char** argv) { printf("usage: precedent [--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; + } +} diff --git a/precedent.h b/precedent.h index a1302d4..8a5110e 100644 --- a/precedent.h +++ b/precedent.h @@ -2,5 +2,6 @@ #include "hashtable.h" #ifndef PRECEDENT char* read_file(char* path); +char* preprocess(char* str, char* baseDir); #define PRECEDENT #endif -- cgit v1.3.1