From 7d3856203d28281e3ffc6b365cc55b1d192a5599 Mon Sep 17 00:00:00 2001 From: standenboy Date: Thu, 25 Apr 2024 08:45:36 +0100 Subject: started cw --- comp/cw/code/parser/Makefile | 12 ++++++++ comp/cw/code/parser/parser | Bin 0 -> 20880 bytes comp/cw/code/parser/parser.c | 32 +++++++++++++++++++ comp/cw/code/parser/parser.o | Bin 0 -> 2256 bytes comp/cw/code/parser/preprocessor.c | 24 +++++++++++++++ comp/cw/code/parser/preprocessor.h | 5 +++ comp/cw/code/parser/preprocessor.o | Bin 0 -> 1504 bytes comp/cw/code/parser/readfile.c | 61 +++++++++++++++++++++++++++++++++++++ comp/cw/code/parser/readfile.h | 1 + comp/cw/code/parser/readfile.o | Bin 0 -> 2208 bytes comp/cw/code/parser/sample.zpy | 15 +++++++++ 11 files changed, 150 insertions(+) create mode 100644 comp/cw/code/parser/Makefile create mode 100755 comp/cw/code/parser/parser create mode 100644 comp/cw/code/parser/parser.c create mode 100644 comp/cw/code/parser/parser.o create mode 100644 comp/cw/code/parser/preprocessor.c create mode 100644 comp/cw/code/parser/preprocessor.h create mode 100644 comp/cw/code/parser/preprocessor.o create mode 100644 comp/cw/code/parser/readfile.c create mode 100644 comp/cw/code/parser/readfile.h create mode 100644 comp/cw/code/parser/readfile.o create mode 100644 comp/cw/code/parser/sample.zpy (limited to 'comp/cw/code/parser') diff --git a/comp/cw/code/parser/Makefile b/comp/cw/code/parser/Makefile new file mode 100644 index 0000000..9325d95 --- /dev/null +++ b/comp/cw/code/parser/Makefile @@ -0,0 +1,12 @@ +all: readfile preprocesor parser + cc readfile.o preprocessor.o parser.o -o parser + +readfile: + cc -c readfile.c -o readfile.o +preprocesor: + cc -c preprocessor.c -o preprocessor.o +parser: parser.c + cc -c parser.c -o parser.o + +clean: + rm -rf *.o parser diff --git a/comp/cw/code/parser/parser b/comp/cw/code/parser/parser new file mode 100755 index 0000000..9d996a8 Binary files /dev/null and b/comp/cw/code/parser/parser differ diff --git a/comp/cw/code/parser/parser.c b/comp/cw/code/parser/parser.c new file mode 100644 index 0000000..3162841 --- /dev/null +++ b/comp/cw/code/parser/parser.c @@ -0,0 +1,32 @@ +#include +#include +#include + +#include "preprocessor.h" + +int main(int argc, char **argv){ + if (argc <= 1){ + printf("no args given!\n"); + return 1; + } + char *code = preprocessor(argc, argv); + if (code == NULL){ + printf("falled to open file\n"); + return 1; + } + for (int i = 0; i < strlen(code); i++){ + if (code[i] == '{'){ + printf("\nopen-scope\n"); + } else if (code[i] == '}'){ + printf("\nclose-scope\n"); + }else { + if (code[i] == '(') + printf("\n"); + else if (code[i] != ')') + printf("%c", code[i]); + } + } + printf("\n"); + free(code); + return 0; +} diff --git a/comp/cw/code/parser/parser.o b/comp/cw/code/parser/parser.o new file mode 100644 index 0000000..baccd37 Binary files /dev/null and b/comp/cw/code/parser/parser.o differ diff --git a/comp/cw/code/parser/preprocessor.c b/comp/cw/code/parser/preprocessor.c new file mode 100644 index 0000000..92c6127 --- /dev/null +++ b/comp/cw/code/parser/preprocessor.c @@ -0,0 +1,24 @@ +#include +#include + +#include "readfile.h" + +char *preprocessor(int argc, char **argv){ + char *buf = readfile(argv[1]); + if (buf == NULL) + return NULL; + + int i = 0; + + while (buf[i] != '\0'){ + if (buf[i] == '\n'){ + buf[i] = ' '; + } + if (buf[i] == '\t'){ + buf[i] = ' '; + } + i++; + } + + return buf; +} diff --git a/comp/cw/code/parser/preprocessor.h b/comp/cw/code/parser/preprocessor.h new file mode 100644 index 0000000..855240f --- /dev/null +++ b/comp/cw/code/parser/preprocessor.h @@ -0,0 +1,5 @@ +#include +#include +#include + +char *preprocessor(int argc, char **argv); diff --git a/comp/cw/code/parser/preprocessor.o b/comp/cw/code/parser/preprocessor.o new file mode 100644 index 0000000..fad2c87 Binary files /dev/null and b/comp/cw/code/parser/preprocessor.o differ diff --git a/comp/cw/code/parser/readfile.c b/comp/cw/code/parser/readfile.c new file mode 100644 index 0000000..391d5a5 --- /dev/null +++ b/comp/cw/code/parser/readfile.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include + +bool instring = false; + +char *readfile(char *filepath){ + FILE *fptr; + fptr = fopen(filepath, "r"); + + if (fptr == NULL) + return NULL; + int size = 10; + char *buf = malloc(size); + char c; + + int i = 0; + + buf = buf+i; + buf[0] = '{'; + buf = buf-i; + + i++; + + while ((c = getc(fptr)) != EOF){ + if (i > size + 1){ + size = size + 10; + buf = realloc(buf, size); + } + if (c == '"'){ + if (instring == false) + instring = true; + else + instring = false; + } + + if (c == '!' && instring == false){ + while ((c = getc(fptr)) != EOF && c != '\n'){} + } + buf = buf+i; + buf[0] = c; + buf = buf-i; + i++; + + } + + buf = buf+i; + buf[0] = '}'; + buf = buf-i; + + i++; + + buf = buf+i; + buf[0] = '\0'; + buf = buf-i; + + fclose(fptr); + + return buf; +} diff --git a/comp/cw/code/parser/readfile.h b/comp/cw/code/parser/readfile.h new file mode 100644 index 0000000..12d98ec --- /dev/null +++ b/comp/cw/code/parser/readfile.h @@ -0,0 +1 @@ +char *readfile(char *filepath); diff --git a/comp/cw/code/parser/readfile.o b/comp/cw/code/parser/readfile.o new file mode 100644 index 0000000..32bc31d Binary files /dev/null and b/comp/cw/code/parser/readfile.o differ diff --git a/comp/cw/code/parser/sample.zpy b/comp/cw/code/parser/sample.zpy new file mode 100644 index 0000000..e289d9d --- /dev/null +++ b/comp/cw/code/parser/sample.zpy @@ -0,0 +1,15 @@ +{ +(let fib:function +(defun num:i32 i32 + (if (< num 2) + (return num) + ) + (else + (return (+ (fib (- num 1)) (fib (- num 2)) )) + ) +)) +} +(let a:i32 (fib 5)) +(const str[]:char "hello!") +! returns the 5th fib number +(const str[]:char "hello!") -- cgit v1.2.3