diff options
Diffstat (limited to 'comp/lucas-standen-NEA/code2/comp.c')
-rw-r--r-- | comp/lucas-standen-NEA/code2/comp.c | 73 |
1 files changed, 42 insertions, 31 deletions
diff --git a/comp/lucas-standen-NEA/code2/comp.c b/comp/lucas-standen-NEA/code2/comp.c index 087c536..d49148e 100644 --- a/comp/lucas-standen-NEA/code2/comp.c +++ b/comp/lucas-standen-NEA/code2/comp.c @@ -11,11 +11,21 @@ #define MAXOUTLEN 512 +// globals for memory management +char *tofree[256]; +int freeptr = 0; +bool neededmemptr = false; + +// globals for structs +char *structname; + +// globals for errors pid_t pid; int linecount = 0; char *currentLine; char *errmsg; +//# error handler that will be triggered by signal when there is a syntax error and will print information void errorhandle(int type){ fprintf(stderr, "err:%d (%s)\n", linecount, currentLine); fprintf(stderr, "%s\n", errmsg); @@ -63,17 +73,10 @@ char *names[] = { "defunptr" // takes the same stuff as defun but outputs it in fuction ptr form }; -// function prototype for recursion +// function prototype for recursion of the compile function char *compile(astNode *node); -// globals for memory management -char *tofree[256]; -int freeptr = 0; -bool neededmemptr = false; - -// globals for stucts -char *structname; - +//# this function will convert the zpy style variable defintion, to the c style char *vartypeToC(char *str, char *out){ char *name = malloc(strlen(str)); char *type = malloc(strlen(str)); @@ -106,6 +109,7 @@ char *vartypeToC(char *str, char *out){ return out; } +//# this function will give the user the variable name, without its type, which is useful for translation char *getVarName(char *exp){ char *out = malloc(strlen(exp)); memcpy(out, exp, strlen(exp)); @@ -114,13 +118,14 @@ char *getVarName(char *exp){ return out; } +//# this function will give the user the type, without its name, which is useful for translation char *getVarType(char *exp){ char *out = malloc(strlen(exp)); char *pos = strchr(exp, ':')+1; memcpy(out, pos, strlen(pos) + 1); return out; } - +//# this will convert mathmatical expressions, to the c style of maths char *reversepolishToC(astNode *exp, char *out){ out = appendsnprintf(out, MAXOUTLEN, "%s ", exp->args[0]); if (exp->func[0] == '=') out = appendsnprintf(out, MAXOUTLEN, "=="); @@ -129,6 +134,7 @@ char *reversepolishToC(astNode *exp, char *out){ return out; } +//# this is a recursive loop that will call the compile function recursivly to flatten the tree astNode *processChildren(astNode *node){ for (int i = 0; i < 8; i++){ if (node->children[i] != NULL){ @@ -139,6 +145,7 @@ astNode *processChildren(astNode *node){ return node; } +//# this function will check if the value given is null, if it is, it will cause a sig segv and set the error msg void checkNULL(void *value, char *msg){ if (value == NULL) { errmsg = msg; @@ -146,10 +153,11 @@ void checkNULL(void *value, char *msg){ } } +//# this function will do the bulk of converting from zpy into c code char *compile(astNode *node){ char *out = calloc(0, MAXOUTLEN); node = processChildren(node); - if (strcmp(names[0], node->func) == 0){ + if (strcmp(names[0], node->func) == 0){ // converting function definitions 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]); @@ -161,38 +169,38 @@ char *compile(astNode *node){ } out = appendsnprintf(out, MAXOUTLEN, "){\n"); } - else if (strcmp(names[1], node->func) == 0){ + else if (strcmp(names[1], node->func) == 0){ // converting ending function definitions out = appendsnprintf(out, MAXOUTLEN, "}\n"); } - else if (strcmp(names[2], node->func) == 0){ + else if (strcmp(names[2], node->func) == 0){ // converting variable declarations 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){ + else if (strcmp(names[3], node->func) == 0){ // converting vairable reasignments 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){ + else if (strcmp(names[4], node->func) == 0){ // converting if statments checkNULL(node->args[0], "expected sub expression"); out = appendsnprintf(out, MAXOUTLEN, "if (%s", node->args[0]); out = appendsnprintf(out, MAXOUTLEN, "){\n"); } - else if (strcmp(names[5], node->func) == 0){ + else if (strcmp(names[5], node->func) == 0){ // converting ending if statments out = appendsnprintf(out, MAXOUTLEN, "}\n"); } - else if (strcmp(names[6], node->func) == 0){ + else if (strcmp(names[6], node->func) == 0){ // converting elif (else if) statments checkNULL(node->args[0], "expected sub expression"); out = appendsnprintf(out, MAXOUTLEN, "}else if (%s", node->args[0]); out = appendsnprintf(out, MAXOUTLEN, "){\n"); } - else if (strcmp(names[7], node->func) == 0){ + else if (strcmp(names[7], node->func) == 0){ // converting else statments out = appendsnprintf(out, MAXOUTLEN, "}\n"); out = appendsnprintf(out, MAXOUTLEN, "else{"); } - else if (strcmp(names[8], node->func) == 0){ + else if (strcmp(names[8], node->func) == 0){ // converting for loop statments checkNULL(node->args[0], "expected iterator"); checkNULL(node->args[1], "expected iterator value"); checkNULL(node->args[2], "expected condition"); @@ -203,10 +211,10 @@ char *compile(astNode *node){ out = appendsnprintf(out, MAXOUTLEN, "%s", node->args[2]); out = appendsnprintf(out, MAXOUTLEN, "; %s+=%s){", getVarName(node->args[0]), node->args[3]); } - else if (strcmp(names[9], node->func) == 0){ + else if (strcmp(names[9], node->func) == 0){ // converting end for loop statments out = appendsnprintf(out, MAXOUTLEN, "}\n"); } - else if (strcmp(names[10], node->func) == 0){ + else if (strcmp(names[10], node->func) == 0){ // converting symbol definition statments 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]); @@ -218,7 +226,7 @@ char *compile(astNode *node){ } out = appendsnprintf(out, MAXOUTLEN, ");\n"); } - else if (strcmp(names[21], node->func) == 0){ + else if (strcmp(names[21], node->func) == 0){ //converting exit statments checkNULL(node->args[0], "expected exit code"); out = appendsnprintf(out, MAXOUTLEN, "exit(%s);\n", node->args[0]); } @@ -234,33 +242,33 @@ char *compile(astNode *node){ break; } } - checkNULL(node->args[0], "expected return value"); + checkNULL(node->args[0], "expected return value"); // converting return statments out = appendsnprintf(out, MAXOUTLEN, "return %s;\n", node->args[0]); } - else if (strcmp(names[23], node->func) == 0){ + else if (strcmp(names[23], node->func) == 0){ // converting memory allocation statments 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){ + else if (strcmp(names[24], node->func) == 0){ // converting struct definiton statments 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]; } - else if (strcmp(names[25], node->func) == 0){ + else if (strcmp(names[25], node->func) == 0){ // converting struct end statments out = appendsnprintf(out, MAXOUTLEN, "} %s", structname); structname = NULL; } - else if (strcmp(names[26], node->func) == 0){ + else if (strcmp(names[26], node->func) == 0){ // converting variable definition statments checkNULL(node->args[0], "expected variable definition"); out = vartypeToC(node->args[0], out); } - else if (strcmp(names[27], node->func) == 0){ + else if (strcmp(names[27], node->func) == 0){ // converting sizeof statments checkNULL(node->args[0], "expected variable type"); out = appendsnprintf(out, MAXOUTLEN, "sizeof(%s)", node->args[0]); } - else if (strcmp(names[28], node->func) == 0){ + else if (strcmp(names[28], node->func) == 0){ // converting function pointer definition staments 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]); @@ -274,7 +282,7 @@ char *compile(astNode *node){ out = appendsnprintf(out, MAXOUTLEN, ")"); } - else { + else { // arithmetic operators and comparitors for (int i = 0; i < 9; i++){ checkNULL(node->func, "expected func name"); @@ -284,7 +292,8 @@ char *compile(astNode *node){ } } - checkNULL(node->func, "expected func name"); + // converting user defined function calls + checkNULL(node->func, "expected func name"); out = appendsnprintf(out, MAXOUTLEN, "%s(", node->func); int i = 0; while (node->args[i] != NULL){ @@ -298,11 +307,13 @@ end: return out; } +//# this function sets up the signal handler for the error msgs void CompilerInit(){ signal(SIGSEGV, &errorhandle); pid = getpid(); } +//# the exposed compiler function that will fully process an astNode tree void Compile(astNode *line, FILE *f, char* strline){ currentLine = strline; char *code = compile(line); |