summaryrefslogtreecommitdiff
path: root/comp/lucas-standen-NEA/code2
diff options
context:
space:
mode:
authorthing1 <thing1@seacrossedlovers.xyz>2024-10-07 11:46:38 +0100
committerthing1 <thing1@seacrossedlovers.xyz>2024-10-07 11:46:38 +0100
commit69c8e84587d934545bdb9d21b292463428ebee93 (patch)
treef0fa8e2cacc5454e595d1b64fba03d1acca67f38 /comp/lucas-standen-NEA/code2
parentef8cf00bbf9f74eb3b6aabc1d99f5358e81741c7 (diff)
did some work to the compiler to make it have error msgs
Diffstat (limited to 'comp/lucas-standen-NEA/code2')
-rw-r--r--comp/lucas-standen-NEA/code2/Makefile2
-rw-r--r--comp/lucas-standen-NEA/code2/TODO1
-rw-r--r--comp/lucas-standen-NEA/code2/comp.c54
-rw-r--r--comp/lucas-standen-NEA/code2/comp.h3
-rw-r--r--comp/lucas-standen-NEA/code2/examples/Makefile4
-rw-r--r--comp/lucas-standen-NEA/code2/examples/syntaxerr.zpy3
-rw-r--r--comp/lucas-standen-NEA/code2/examples/tmp.zpy.c2
-rwxr-xr-xcomp/lucas-standen-NEA/code2/zpybin37072 -> 42536 bytes
-rw-r--r--comp/lucas-standen-NEA/code2/zpy.c4
9 files changed, 66 insertions, 7 deletions
diff --git a/comp/lucas-standen-NEA/code2/Makefile b/comp/lucas-standen-NEA/code2/Makefile
index b5a1aed..126bed6 100644
--- a/comp/lucas-standen-NEA/code2/Makefile
+++ b/comp/lucas-standen-NEA/code2/Makefile
@@ -1,7 +1,7 @@
CC = cc
CFLAGS = -O0 -ggdb
-zpy:
+zpy: zpy.c comp.c parser.c tokenizer.c util.c
${CC} *.c -c ${CFLAGS}
${CC} *.o -o zpy ${CFLAGS}
cd stdlib && make
diff --git a/comp/lucas-standen-NEA/code2/TODO b/comp/lucas-standen-NEA/code2/TODO
deleted file mode 100644
index 2e7e13d..0000000
--- a/comp/lucas-standen-NEA/code2/TODO
+++ /dev/null
@@ -1 +0,0 @@
-make the defunptr function
diff --git a/comp/lucas-standen-NEA/code2/comp.c b/comp/lucas-standen-NEA/code2/comp.c
index 7d97cdc..087c536 100644
--- a/comp/lucas-standen-NEA/code2/comp.c
+++ b/comp/lucas-standen-NEA/code2/comp.c
@@ -2,6 +2,8 @@
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
+#include <signal.h>
+#include <unistd.h>
#include "tokenizer.h"
#include "util.h"
@@ -9,6 +11,18 @@
#define MAXOUTLEN 512
+pid_t pid;
+int linecount = 0;
+char *currentLine;
+char *errmsg;
+
+void errorhandle(int type){
+ fprintf(stderr, "err:%d (%s)\n", linecount, currentLine);
+ fprintf(stderr, "%s\n", errmsg);
+
+ exit(1);
+}
+
char *names[] = {
"defun", // takes a func name, func return type, and args // 0
"endfun", // takes no args // 1
@@ -125,11 +139,19 @@ astNode *processChildren(astNode *node){
return node;
}
+void checkNULL(void *value, char *msg){
+ if (value == NULL) {
+ errmsg = msg;
+ kill(pid, SIGSEGV);
+ }
+}
char *compile(astNode *node){
char *out = calloc(0, MAXOUTLEN);
node = processChildren(node);
if (strcmp(names[0], node->func) == 0){
+ checkNULL(node->args[0], "expected func name");
+ checkNULL(node->args[1], "expected return type");
out = appendsnprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]);
int i = 2;
while (node->args[i] != NULL){
@@ -140,17 +162,21 @@ char *compile(astNode *node){
out = appendsnprintf(out, MAXOUTLEN, "){\n");
}
else if (strcmp(names[1], node->func) == 0){
-
out = appendsnprintf(out, MAXOUTLEN, "}\n");
}
else if (strcmp(names[2], node->func) == 0){
+ checkNULL(node->args[0], "expected var name");
+ checkNULL(node->args[1], "expected var value");
out = vartypeToC(node->args[0], out);
out = appendsnprintf(out, MAXOUTLEN, " = %s;\n", node->args[1]);
}
else if (strcmp(names[3], node->func) == 0){
+ checkNULL(node->args[0], "expected var name");
+ checkNULL(node->args[1], "expected var value");
out = appendsnprintf(out, MAXOUTLEN, "%s = %s;\n", node->args[0], node->args[1]);
}
else if (strcmp(names[4], node->func) == 0){
+ checkNULL(node->args[0], "expected sub expression");
out = appendsnprintf(out, MAXOUTLEN, "if (%s", node->args[0]);
out = appendsnprintf(out, MAXOUTLEN, "){\n");
}
@@ -158,6 +184,7 @@ char *compile(astNode *node){
out = appendsnprintf(out, MAXOUTLEN, "}\n");
}
else if (strcmp(names[6], node->func) == 0){
+ checkNULL(node->args[0], "expected sub expression");
out = appendsnprintf(out, MAXOUTLEN, "}else if (%s", node->args[0]);
out = appendsnprintf(out, MAXOUTLEN, "){\n");
}
@@ -166,6 +193,10 @@ char *compile(astNode *node){
out = appendsnprintf(out, MAXOUTLEN, "else{");
}
else if (strcmp(names[8], node->func) == 0){
+ checkNULL(node->args[0], "expected iterator");
+ checkNULL(node->args[1], "expected iterator value");
+ checkNULL(node->args[2], "expected condition");
+ checkNULL(node->args[3], "expected iterator increment");
out = appendsnprintf(out, MAXOUTLEN, "for (");
out = vartypeToC(node->args[0], out);
out = appendsnprintf(out, MAXOUTLEN, " = %s;", node->args[1]);
@@ -176,6 +207,8 @@ char *compile(astNode *node){
out = appendsnprintf(out, MAXOUTLEN, "}\n");
}
else if (strcmp(names[10], node->func) == 0){
+ checkNULL(node->args[0], "expected symbol type");
+ checkNULL(node->args[1], "expected symbol name");
out = appendsnprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]);
int i = 2;
while (node->args[i] != NULL){
@@ -186,6 +219,7 @@ char *compile(astNode *node){
out = appendsnprintf(out, MAXOUTLEN, ");\n");
}
else if (strcmp(names[21], node->func) == 0){
+ checkNULL(node->args[0], "expected exit code");
out = appendsnprintf(out, MAXOUTLEN, "exit(%s);\n", node->args[0]);
}
else if (strcmp(names[22], node->func) == 0){
@@ -200,13 +234,16 @@ char *compile(astNode *node){
break;
}
}
+ checkNULL(node->args[0], "expected return value");
out = appendsnprintf(out, MAXOUTLEN, "return %s;\n", node->args[0]);
}
else if (strcmp(names[23], node->func) == 0){
+ checkNULL(node->args[0], "expected alloc size");
out = appendsnprintf(out, MAXOUTLEN, "malloc(%s)", node->args[0]);
neededmemptr = true;
}
else if (strcmp(names[24], node->func) == 0){
+ checkNULL(node->args[0], "expected type name");
out = appendsnprintf(out, MAXOUTLEN, "typedef struct %s %s;\n", node->args[0], node->args[0]);
out = appendsnprintf(out, MAXOUTLEN, "typedef struct %s {", node->args[0]);
structname = node->args[0];
@@ -216,12 +253,16 @@ char *compile(astNode *node){
structname = NULL;
}
else if (strcmp(names[26], node->func) == 0){
+ checkNULL(node->args[0], "expected variable definition");
out = vartypeToC(node->args[0], out);
}
else if (strcmp(names[27], node->func) == 0){
+ checkNULL(node->args[0], "expected variable type");
out = appendsnprintf(out, MAXOUTLEN, "sizeof(%s)", node->args[0]);
}
else if (strcmp(names[28], node->func) == 0){
+ checkNULL(node->args[0], "expected function ptr type");
+ checkNULL(node->args[0], "expected function ptr name");
out = appendsnprintf(out, MAXOUTLEN, "%s (*%s)", node->args[1], node->args[0]);
int i = 2;
while (node->args[i] != NULL){
@@ -236,12 +277,14 @@ char *compile(astNode *node){
else {
// arithmetic operators and comparitors
for (int i = 0; i < 9; i++){
+ checkNULL(node->func, "expected func name");
if (strcmp(names[11+i], node->func) == 0){
out = reversepolishToC(node, out);
goto end;
}
}
+ checkNULL(node->func, "expected func name");
out = appendsnprintf(out, MAXOUTLEN, "%s(", node->func);
int i = 0;
while (node->args[i] != NULL){
@@ -255,7 +298,13 @@ end:
return out;
}
-void Compile(astNode *line, FILE *f){
+void CompilerInit(){
+ signal(SIGSEGV, &errorhandle);
+ pid = getpid();
+}
+
+void Compile(astNode *line, FILE *f, char* strline){
+ currentLine = strline;
char *code = compile(line);
if (neededmemptr == true){
tofree[freeptr] = line->args[0];
@@ -265,5 +314,6 @@ void Compile(astNode *line, FILE *f){
int len = strlen(code);
if (code[len-2] == ';' || code[len-2] == '{' || code[len-2] == '}') fprintf(f, "%s", code);
else fprintf(f, "%s;\n", code);
+ linecount++;
free(code);
}
diff --git a/comp/lucas-standen-NEA/code2/comp.h b/comp/lucas-standen-NEA/code2/comp.h
index c2f1942..303c3ec 100644
--- a/comp/lucas-standen-NEA/code2/comp.h
+++ b/comp/lucas-standen-NEA/code2/comp.h
@@ -2,5 +2,6 @@
#include <stdio.h>
#define MAXOUTLEN 512
-void Compile(astNode *node, FILE *f);
+void CompilerInit();
+void Compile(astNode *node, FILE *f, char *strline);
diff --git a/comp/lucas-standen-NEA/code2/examples/Makefile b/comp/lucas-standen-NEA/code2/examples/Makefile
index cbdcda1..18a9e64 100644
--- a/comp/lucas-standen-NEA/code2/examples/Makefile
+++ b/comp/lucas-standen-NEA/code2/examples/Makefile
@@ -3,5 +3,7 @@ all:
zpy spaceinvaders.zpy -o spaceinvaders -f -lraylib -f -lm -i raylib.h
zpy fib_example.zpy -o fib_example -f -ggdb
zpy str_example.zpy -o str_example -f -ggdb
+
+ zpy syntaxerr.zpy -o syntaxerr
clean:
- rm -rf fib_example raylib_example str_example spaceinvaders
+ rm -rf fib_example raylib_example str_example spaceinvaders tmp.zpy.c
diff --git a/comp/lucas-standen-NEA/code2/examples/syntaxerr.zpy b/comp/lucas-standen-NEA/code2/examples/syntaxerr.zpy
new file mode 100644
index 0000000..c74d53b
--- /dev/null
+++ b/comp/lucas-standen-NEA/code2/examples/syntaxerr.zpy
@@ -0,0 +1,3 @@
+(defun main int)
+ (return)
+(endfun)
diff --git a/comp/lucas-standen-NEA/code2/examples/tmp.zpy.c b/comp/lucas-standen-NEA/code2/examples/tmp.zpy.c
new file mode 100644
index 0000000..2305f24
--- /dev/null
+++ b/comp/lucas-standen-NEA/code2/examples/tmp.zpy.c
@@ -0,0 +1,2 @@
+#include <zpylib.h>
+int main(){
diff --git a/comp/lucas-standen-NEA/code2/zpy b/comp/lucas-standen-NEA/code2/zpy
index d153a2f..dac40be 100755
--- a/comp/lucas-standen-NEA/code2/zpy
+++ 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 35357f2..923d5c8 100644
--- a/comp/lucas-standen-NEA/code2/zpy.c
+++ b/comp/lucas-standen-NEA/code2/zpy.c
@@ -69,6 +69,8 @@ int main(int argc, char **argv){
if (stringTokens == NULL)
die("couldn't parse file, is it formated properly?");
+ CompilerInit();
+
fprintf(fout, "#include <zpylib.h>\n");
for (int i = 0; i < libcount; i++){
fprintf(fout, "#include <%s>\n", libs[i]);
@@ -79,7 +81,7 @@ int main(int argc, char **argv){
stringTokens->strs[i][strlen(stringTokens->strs[i]) - 1] = '\0';
astNode *line = tokenize(stringTokens->strs[i]);
- Compile(line, fout);
+ Compile(line, fout, stringTokens->strs[i]);
}
fclose(fout);