summaryrefslogtreecommitdiff
path: root/comp
diff options
context:
space:
mode:
authorthing1 <thing1@Thing1LAP.my.domain>2024-09-05 18:20:24 +0100
committerthing1 <thing1@Thing1LAP.my.domain>2024-09-05 18:20:24 +0100
commit9b83439d9d0204a5092772ae46ef43cea42dbc7c (patch)
tree1553eb58a8dbaa2f12bfa286880579b6b84edbda /comp
parentc818366f33527bbd0d23d7ff983bb1eaf82fa34b (diff)
added some nice stuff
Diffstat (limited to 'comp')
-rw-r--r--comp/lucas-standen-NEA/code2/Makefile2
-rw-r--r--comp/lucas-standen-NEA/code2/comp.c100
-rw-r--r--comp/lucas-standen-NEA/code2/comp.h3
-rw-r--r--comp/lucas-standen-NEA/code2/gdb.corebin0 -> 10729440 bytes
-rwxr-xr-xcomp/lucas-standen-NEA/code2/samplebin15464 -> 6432 bytes
-rw-r--r--comp/lucas-standen-NEA/code2/sample.zpy9
-rw-r--r--comp/lucas-standen-NEA/code2/tokenizer.c5
-rwxr-xr-xcomp/lucas-standen-NEA/code2/zpybin0 -> 24216 bytes
-rw-r--r--comp/lucas-standen-NEA/code2/zpy.c3
-rw-r--r--comp/lucas-standen-NEA/code2/zpy.corebin0 -> 299160 bytes
-rwxr-xr-xcomp/lucas-standen-NEA/code2/zpy.sh2
11 files changed, 75 insertions, 49 deletions
diff --git a/comp/lucas-standen-NEA/code2/Makefile b/comp/lucas-standen-NEA/code2/Makefile
index 50e44c2..0ebc2c5 100644
--- a/comp/lucas-standen-NEA/code2/Makefile
+++ b/comp/lucas-standen-NEA/code2/Makefile
@@ -1,4 +1,4 @@
-CC = gcc
+CC = cc
CFLAGS = -O0 -ggdb
all: _zpy _parser _tokenizer _comp _util _debug
diff --git a/comp/lucas-standen-NEA/code2/comp.c b/comp/lucas-standen-NEA/code2/comp.c
index c11fdd8..4bc67e9 100644
--- a/comp/lucas-standen-NEA/code2/comp.c
+++ b/comp/lucas-standen-NEA/code2/comp.c
@@ -12,11 +12,11 @@ char *names[] = {
"if", // takes a condition // 4
"endif", // takes no args // 5
"elif", // same as if, but only executes if the prev statment didnt // 6
- "else", // its else! //
+ "else", // its else! // 7
"for", // takes a iterator and type, a condition, and an increment // 8
"endfor", // takes no args // 9
"write", // takes an int and puts it on the screen // 10
- "symbol",
+ "symbol", // takes a name and return type and args
"+",
"-",
"*",
@@ -33,7 +33,7 @@ char *names[] = {
"return",
};
-void vartypeToC(char *str){
+void vartypeToC(char *str, FILE *f){
char *name = malloc(strlen(str));
char *type = malloc(strlen(str));
@@ -57,12 +57,12 @@ void vartypeToC(char *str){
}
type[j] = '\0';
- printf("%s %s", type, name);
+ fprintf(f, "%s %s", type, name);
free(type);
free(name);
}
-char *getVarName(char *exp){
+char *getVarName(char *exp, FILE *f){
char *out = malloc(strlen(exp));
memcpy(out, exp, strlen(exp));
char *pos = strchr(out, ':');
@@ -70,68 +70,90 @@ char *getVarName(char *exp){
return out;
}
-void reversepolishToC(astNode *exp){
- printf("%s ", exp->args[0]);
- if (exp->func[0] == '=') printf("==");
- else printf("%s", exp->func);
- printf(" %s", exp->args[1]);
+void reversepolishToC(astNode *exp, FILE *f){
+ fprintf(f, "%s ", exp->args[0]);
+ if (exp->func[0] == '=') fprintf(f, "==");
+ else fprintf(f, "%s", exp->func);
+ fprintf(f, " %s", exp->args[1]);
}
-void compile(astNode *node){
+void compile(astNode *node, FILE *f){
if (strcmp(names[0], node->func) == 0){
- printf("%s %s(", node->args[1], node->args[0]);
+ fprintf(f, "%s %s(", node->args[1], node->args[0]);
int i = 2;
while (node->args[i] != NULL){
- vartypeToC(node->args[i]);
+ vartypeToC(node->args[i], f);
i++;
}
- printf("){\n");
+ fprintf(f, "){\n");
}
else if (strcmp(names[1], node->func) == 0){
- printf("\n}\n");
+ fprintf(f, "\n}\n");
}
else if (strcmp(names[2], node->func) == 0){
- printf("const ");
- vartypeToC(node->args[0]);
- printf(" = %s;\n", node->args[1]);
+ fprintf(f, "const ");
+ vartypeToC(node->args[0], f);
+ fprintf(f, " = %s;\n", node->args[1]);
}
else if (strcmp(names[3], node->func) == 0){
- vartypeToC(node->args[0]);
- printf(" = %s;\n", node->args[1]);
+ vartypeToC(node->args[0], f);
+ fprintf(f, " = %s;\n", node->args[1]);
}
else if (strcmp(names[4], node->func) == 0){
- printf("if (");
- reversepolishToC(node->children[0]);
- printf("){\n");
+ fprintf(f, "if (");
+ reversepolishToC(node->children[0], f);
+ fprintf(f, "){\n");
}
else if (strcmp(names[5], node->func) == 0){
- printf("\n}\n");
+ fprintf(f, "\n}\n");
}
else if (strcmp(names[6], node->func) == 0){
- printf("\n}\n");
- printf("else if (");
- reversepolishToC(node->children[0]);
- printf("){\n");
+ fprintf(f, "\n}\n");
+ fprintf(f, "else if (");
+ reversepolishToC(node->children[0], f);
+ fprintf(f, "){\n");
}
else if (strcmp(names[7], node->func) == 0){
- printf("\n}\n");
- printf("else{");
+ fprintf(f, "\n}\n");
+ fprintf(f, "else{");
}
else if (strcmp(names[8], node->func) == 0){
- printf("for (");
- vartypeToC(node->args[0]);
- printf(" = 0;");
- reversepolishToC(node->children[1]);
- printf("; %s+=%s){", getVarName(node->args[0]), node->args[2]);
+ fprintf(f, "for (");
+ vartypeToC(node->args[0], f);
+ fprintf(f, " = 0;");
+ reversepolishToC(node->children[1], f);
+ fprintf(f, "; %s+=%s){", getVarName(node->args[0], f), node->args[2]);
}
else if (strcmp(names[9], node->func) == 0){
- printf("\n}\n");
+ fprintf(f, "\n}\n");
}
else if (strcmp(names[10], node->func) == 0){
- printf("printf(\"");
- printf("%%d\\n");
- printf("\", %s);", node->args[0]);
+ fprintf(f, "printf(\"");
+ fprintf(f, "%%d\\n");
+ fprintf(f, "\", %s);", node->args[0]);
}
+ else if (strcmp(names[11], node->func) == 0){
+ fprintf(f, "%s %s(", node->args[1], node->args[0]);
+ int i = 2;
+ while (node->args[i] != NULL){
+ vartypeToC(node->args[i], f);
+ i++;
+ }
+ fprintf(f, ");\n");
+ }
+ else if (strcmp(names[12], node->func) == 0){
+ reversepolishToC(node->children[0], f);
+ }
+ else {
+ fprintf(f, "%s(", node->func);
+ int i = 0;
+ while (node->args[i] != NULL){
+ fprintf(f, "%s", node->args[i]);
+ i++;
+ }
+ fprintf(f, ");\n");
+ }
}
+
diff --git a/comp/lucas-standen-NEA/code2/comp.h b/comp/lucas-standen-NEA/code2/comp.h
index 840cd16..b04056f 100644
--- a/comp/lucas-standen-NEA/code2/comp.h
+++ b/comp/lucas-standen-NEA/code2/comp.h
@@ -1,2 +1,3 @@
#include "tokenizer.h"
-void compile(astNode *node);
+#include <stdio.h>
+void compile(astNode *node, FILE *f);
diff --git a/comp/lucas-standen-NEA/code2/gdb.core b/comp/lucas-standen-NEA/code2/gdb.core
new file mode 100644
index 0000000..046f004
--- /dev/null
+++ b/comp/lucas-standen-NEA/code2/gdb.core
Binary files differ
diff --git a/comp/lucas-standen-NEA/code2/sample b/comp/lucas-standen-NEA/code2/sample
index 08dda04..d026169 100755
--- a/comp/lucas-standen-NEA/code2/sample
+++ b/comp/lucas-standen-NEA/code2/sample
Binary files differ
diff --git a/comp/lucas-standen-NEA/code2/sample.zpy b/comp/lucas-standen-NEA/code2/sample.zpy
index c8eaba1..b4bfacb 100644
--- a/comp/lucas-standen-NEA/code2/sample.zpy
+++ b/comp/lucas-standen-NEA/code2/sample.zpy
@@ -1,9 +1,6 @@
+(let a:int (+ 65 2))
+(symbol putchar int c:int)
(defun main int)
-(let i:int 10)
-(let a:int 10)
-
-(if (= i a))
-(write a)
-(endif)
+(putchar a)
(endfun)
diff --git a/comp/lucas-standen-NEA/code2/tokenizer.c b/comp/lucas-standen-NEA/code2/tokenizer.c
index 10c70a7..6275a7a 100644
--- a/comp/lucas-standen-NEA/code2/tokenizer.c
+++ b/comp/lucas-standen-NEA/code2/tokenizer.c
@@ -38,6 +38,11 @@ int readuntil(char *src, char c, char *dst){ // returns how many chars read, wil
astNode *tokenize(char *line){ // asume the first set of brackets have been stripped
astNode *head = malloc(sizeof(astNode));
+ head->func = NULL;
+ for (int i = 0; i < 8; i++){
+ head->args[i] = NULL;
+ head->children[i] = NULL;
+ }
int depth = 0;
int argCount = 0;
diff --git a/comp/lucas-standen-NEA/code2/zpy b/comp/lucas-standen-NEA/code2/zpy
new file mode 100755
index 0000000..2534f0c
--- /dev/null
+++ b/comp/lucas-standen-NEA/code2/zpy
Binary files differ
diff --git a/comp/lucas-standen-NEA/code2/zpy.c b/comp/lucas-standen-NEA/code2/zpy.c
index 69eb6c3..6e22b7b 100644
--- a/comp/lucas-standen-NEA/code2/zpy.c
+++ b/comp/lucas-standen-NEA/code2/zpy.c
@@ -20,11 +20,12 @@ int main(int argc, char **argv){
if (stringTokens == NULL)
die("couldn't parse file, is it formated properly?");
+
for (int i = 0; i < stringTokens->count; i++){
stringTokens->strs[i]++;
stringTokens->strs[i][strlen(stringTokens->strs[i]) - 1] = '\0';
astNode *line = tokenize(stringTokens->strs[i]);
- compile(line);
+ compile(line, stdout);
}
diff --git a/comp/lucas-standen-NEA/code2/zpy.core b/comp/lucas-standen-NEA/code2/zpy.core
new file mode 100644
index 0000000..a9fb131
--- /dev/null
+++ b/comp/lucas-standen-NEA/code2/zpy.core
Binary files differ
diff --git a/comp/lucas-standen-NEA/code2/zpy.sh b/comp/lucas-standen-NEA/code2/zpy.sh
index 72259c2..7dc663e 100755
--- a/comp/lucas-standen-NEA/code2/zpy.sh
+++ b/comp/lucas-standen-NEA/code2/zpy.sh
@@ -1,2 +1,2 @@
#!/bin/sh
-./zpy $1 | cc -x c - -Wno-implicit -Wno-builtin-declaration-mismatch -o $2
+./zpy $1 | cc -x c - -Wno-implicit -Wno-missing-declarations -o $2