summaryrefslogtreecommitdiff
path: root/comp/lucas-standen-NEA/code
diff options
context:
space:
mode:
authorstandenboy <standenboy@seacrossedlovers.xyz>2024-05-06 09:32:48 +0100
committerstandenboy <standenboy@seacrossedlovers.xyz>2024-05-06 09:32:48 +0100
commit319e7ea0a724cd97041c1aaf1281c4ca6aa688d1 (patch)
tree7411f4636ce9ce9733760666f3311a17b6e4d17c /comp/lucas-standen-NEA/code
parent0ca35b27de52a5d3acf5f2eb877a440c1103e928 (diff)
added a load of stuff, and fixed a git conflict
Diffstat (limited to 'comp/lucas-standen-NEA/code')
-rw-r--r--comp/lucas-standen-NEA/code/Makefile6
-rw-r--r--comp/lucas-standen-NEA/code/parser/Makefile12
-rw-r--r--comp/lucas-standen-NEA/code/parser/parser.c32
-rw-r--r--comp/lucas-standen-NEA/code/parser/preprocessor.c24
-rw-r--r--comp/lucas-standen-NEA/code/parser/preprocessor.h5
-rw-r--r--comp/lucas-standen-NEA/code/parser/readfile.c61
-rw-r--r--comp/lucas-standen-NEA/code/parser/readfile.h1
-rw-r--r--comp/lucas-standen-NEA/code/parser/sample.zpy15
-rw-r--r--comp/lucas-standen-NEA/code/proto/ast/Makefile4
-rwxr-xr-xcomp/lucas-standen-NEA/code/proto/ast/ast (renamed from comp/lucas-standen-NEA/code/parser/parser)bin20880 -> 20792 bytes
-rw-r--r--comp/lucas-standen-NEA/code/proto/ast/ast.c147
-rw-r--r--comp/lucas-standen-NEA/code/proto/ast/astg.c45
-rw-r--r--comp/lucas-standen-NEA/code/proto/ast/astg.h19
13 files changed, 215 insertions, 156 deletions
diff --git a/comp/lucas-standen-NEA/code/Makefile b/comp/lucas-standen-NEA/code/Makefile
deleted file mode 100644
index a30526c..0000000
--- a/comp/lucas-standen-NEA/code/Makefile
+++ /dev/null
@@ -1,6 +0,0 @@
-all:
- cd ads; make
- cd parser; make
-
-tests: all
- cd ads; make tests
diff --git a/comp/lucas-standen-NEA/code/parser/Makefile b/comp/lucas-standen-NEA/code/parser/Makefile
deleted file mode 100644
index 9325d95..0000000
--- a/comp/lucas-standen-NEA/code/parser/Makefile
+++ /dev/null
@@ -1,12 +0,0 @@
-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/lucas-standen-NEA/code/parser/parser.c b/comp/lucas-standen-NEA/code/parser/parser.c
deleted file mode 100644
index 3162841..0000000
--- a/comp/lucas-standen-NEA/code/parser/parser.c
+++ /dev/null
@@ -1,32 +0,0 @@
-#include <string.h>
-#include <stdio.h>
-#include <stdbool.h>
-
-#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/lucas-standen-NEA/code/parser/preprocessor.c b/comp/lucas-standen-NEA/code/parser/preprocessor.c
deleted file mode 100644
index 92c6127..0000000
--- a/comp/lucas-standen-NEA/code/parser/preprocessor.c
+++ /dev/null
@@ -1,24 +0,0 @@
-#include <stdbool.h>
-#include <string.h>
-
-#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/lucas-standen-NEA/code/parser/preprocessor.h b/comp/lucas-standen-NEA/code/parser/preprocessor.h
deleted file mode 100644
index 855240f..0000000
--- a/comp/lucas-standen-NEA/code/parser/preprocessor.h
+++ /dev/null
@@ -1,5 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-
-char *preprocessor(int argc, char **argv);
diff --git a/comp/lucas-standen-NEA/code/parser/readfile.c b/comp/lucas-standen-NEA/code/parser/readfile.c
deleted file mode 100644
index 391d5a5..0000000
--- a/comp/lucas-standen-NEA/code/parser/readfile.c
+++ /dev/null
@@ -1,61 +0,0 @@
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <stdbool.h>
-
-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/lucas-standen-NEA/code/parser/readfile.h b/comp/lucas-standen-NEA/code/parser/readfile.h
deleted file mode 100644
index 12d98ec..0000000
--- a/comp/lucas-standen-NEA/code/parser/readfile.h
+++ /dev/null
@@ -1 +0,0 @@
-char *readfile(char *filepath);
diff --git a/comp/lucas-standen-NEA/code/parser/sample.zpy b/comp/lucas-standen-NEA/code/parser/sample.zpy
deleted file mode 100644
index e289d9d..0000000
--- a/comp/lucas-standen-NEA/code/parser/sample.zpy
+++ /dev/null
@@ -1,15 +0,0 @@
-{
-(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!")
diff --git a/comp/lucas-standen-NEA/code/proto/ast/Makefile b/comp/lucas-standen-NEA/code/proto/ast/Makefile
new file mode 100644
index 0000000..e11e20f
--- /dev/null
+++ b/comp/lucas-standen-NEA/code/proto/ast/Makefile
@@ -0,0 +1,4 @@
+all: astg ast.c
+ cc ast.c astg.o -o ast
+astg: astg.c
+ cc astg.c -c -o astg.o
diff --git a/comp/lucas-standen-NEA/code/parser/parser b/comp/lucas-standen-NEA/code/proto/ast/ast
index 9d996a8..b4b4f9f 100755
--- a/comp/lucas-standen-NEA/code/parser/parser
+++ b/comp/lucas-standen-NEA/code/proto/ast/ast
Binary files differ
diff --git a/comp/lucas-standen-NEA/code/proto/ast/ast.c b/comp/lucas-standen-NEA/code/proto/ast/ast.c
new file mode 100644
index 0000000..80d198d
--- /dev/null
+++ b/comp/lucas-standen-NEA/code/proto/ast/ast.c
@@ -0,0 +1,147 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "astg.h"
+
+void getBrackets(char *in, int bpos, char *out){ // gets the content of the brackets that open at bpos
+ if (in[0] != '(' && in[0] != '[')
+ out = NULL;
+
+ char *input = malloc(strlen(in) + 1);
+ char *Pinput = input;
+ memcpy(input, in, strlen(in) + 1);
+
+ int i = 0;
+ while (i != bpos){
+ input++;
+ i++;
+ }
+ i = 0;
+ int depth = 0;
+ while (input[0] != '\0'){
+ out[i] = input[0];
+ if (input[0] == '(' || input[0] == '[')
+ depth++;
+ if (input[0] == ')' || input[0] == ']')
+ depth--;
+ if (depth == 0){
+ i++;
+ break;
+ }
+
+ input++;
+ i++;
+ }
+
+ out[i] = '\0';
+ free(Pinput);
+}
+
+int getContents(char *brackets){
+ int i = 0;
+ char *num = malloc(strlen(brackets));
+ while (brackets[0] != '\0'){
+ if (brackets[0] != '[' && brackets[0] != ']'){
+ num[i] = brackets[0];
+ i++;
+ }
+ brackets++;
+ }
+ num[i] = '\0';
+ int out = atoi(num);
+ free(num);
+ return out;
+}
+
+ast_node *genAst(char *expression){
+ ast_node *out = malloc(sizeof(ast_node));
+
+ /* take the expression
+ * get the first operation from it
+ * get the first number from after the expression
+ * if that number is another expression{
+ * grab everything inside its braket and then call this function on it
+ * }
+ * get the second number from the expression
+ * if that number is another expression{
+ * grab everything inside its braket and then call this function on it
+ * }
+ * execute the output
+ */
+
+ int i = 0, j = 0;
+ int currentTokenVal = -1;
+
+ char *currentToken = malloc(strlen(expression)+1);
+ while (expression[i] != '\0'){
+ if (expression[i] == '(' || expression[i] == '['){
+ getBrackets(expression, i, currentToken);
+ currentTokenVal++;
+
+ int depth = 0;
+
+ if (i != 0){
+ while (expression[i] != '\0'){
+ if (expression[i] == '(' || expression[i] == '[')
+ depth++;
+ if (expression[i] == ')' || expression[i] == ']')
+ depth--;
+ if (depth == 0){
+ break;
+ }
+ i++;
+ }
+ }
+
+ if (currentTokenVal == 0){
+ switch (currentToken[1]) {
+ case '+':
+ out->operation= ADD;
+ break;
+ case '-':
+ out->operation = SUB;
+ break;
+ case '*':
+ out->operation = MUL;
+ break;
+ case '/':
+ out->operation = DIV;
+ break;
+ }
+ }
+
+ if (currentTokenVal == 1){
+ if (currentToken[0] == '(')
+ out->left = genAst(currentToken);
+ else {
+ out->realLeft = getContents(currentToken);
+ out->left = NULL;
+ }
+ } else if (currentTokenVal == 2){
+ if (currentToken[0] == '(')
+ out->right= genAst(currentToken);
+ else {
+ out->realRight = getContents(currentToken);
+ out->right = NULL;
+ }
+ }
+ }
+ i++;
+ }
+ free(currentToken);
+
+ return out;
+}
+
+
+int main(int argc, char **argv){
+ if (argc < 2)
+ exit(1);
+
+ ast_node *head = genAst(argv[1]);
+
+ printf("%d\n", exec(head));
+
+ freeAst(head);
+}
diff --git a/comp/lucas-standen-NEA/code/proto/ast/astg.c b/comp/lucas-standen-NEA/code/proto/ast/astg.c
new file mode 100644
index 0000000..fc6fc8f
--- /dev/null
+++ b/comp/lucas-standen-NEA/code/proto/ast/astg.c
@@ -0,0 +1,45 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+typedef struct ast_node ast_node;
+
+typedef enum op {
+ ADD = 0,
+ SUB = 1,
+ MUL = 2,
+ DIV = 3,
+} op;
+
+typedef struct ast_node {
+ op operation;
+ int realLeft;
+ int realRight;
+ ast_node *right;
+ ast_node *left;
+} ast_node;
+
+int exec(ast_node *exp){
+ if (exp->left != NULL)
+ exp->realLeft = exec(exp->left);
+ if (exp->right != NULL)
+ exp->realRight = exec(exp->right);
+
+ if (exp->operation == ADD)
+ return exp->realLeft+ exp->realRight;
+ if (exp->operation == SUB)
+ return exp->realLeft - exp->realRight;
+ if (exp->operation == MUL)
+ return exp->realLeft * exp->realRight;
+ if (exp->operation == DIV)
+ return exp->realLeft/ exp->realRight;
+ return 0;
+}
+
+void freeAst(ast_node *head){
+ if (head->left != NULL)
+ freeAst(head->left);
+ if (head->right != NULL)
+ freeAst(head->left);
+ free(head);
+}
diff --git a/comp/lucas-standen-NEA/code/proto/ast/astg.h b/comp/lucas-standen-NEA/code/proto/ast/astg.h
new file mode 100644
index 0000000..16250b2
--- /dev/null
+++ b/comp/lucas-standen-NEA/code/proto/ast/astg.h
@@ -0,0 +1,19 @@
+typedef struct ast_node ast_node;
+
+typedef enum op {
+ ADD = 0,
+ SUB = 1,
+ MUL = 2,
+ DIV = 3,
+} op;
+
+typedef struct ast_node {
+ op operation;
+ int realLeft;
+ int realRight;
+ ast_node *right;
+ ast_node *left;
+} ast_node;
+
+int exec(ast_node *exp);
+void freeAst(ast_node *head);