diff options
Diffstat (limited to 'comp/lucas-standen-NEA/code2/comp.c')
-rw-r--r-- | comp/lucas-standen-NEA/code2/comp.c | 93 |
1 files changed, 50 insertions, 43 deletions
diff --git a/comp/lucas-standen-NEA/code2/comp.c b/comp/lucas-standen-NEA/code2/comp.c index 3c47cbb..402f797 100644 --- a/comp/lucas-standen-NEA/code2/comp.c +++ b/comp/lucas-standen-NEA/code2/comp.c @@ -3,6 +3,7 @@ #include <string.h> #include "tokenizer.h" +#include "appendsnprintf.h" #define MAXOUTLEN 512 @@ -35,7 +36,7 @@ char *names[] = { "return", }; -void vartypeToC(char *str, char *out){ +char *vartypeToC(char *str, char *out){ char *name = malloc(strlen(str)); char *type = malloc(strlen(str)); @@ -59,9 +60,12 @@ void vartypeToC(char *str, char *out){ } type[j] = '\0'; - snprintf(out, MAXOUTLEN, "%s%s %s", out, type, name); + char *outbuf = malloc(64); + outbuf = appendsnprintf(outbuf, MAXOUTLEN, "%s %s", type, name); + out = appendsnprintf(out, MAXOUTLEN, "%s", outbuf); free(type); free(name); + return out; } char *getVarName(char *exp){ @@ -72,90 +76,93 @@ char *getVarName(char *exp){ return out; } -void reversepolishToC(astNode *exp, char *out){ - snprintf(out, MAXOUTLEN, "%s ", exp->args[0]); - if (exp->func[0] == '=') snprintf(out, MAXOUTLEN, "=="); - else snprintf(out, MAXOUTLEN, "%s", exp->func); - snprintf(out, MAXOUTLEN, " %s", exp->args[1]); +char *reversepolishToC(astNode *exp, char *out){ + out = appendsnprintf(out, MAXOUTLEN, "%s ", exp->args[0]); + if (exp->func[0] == '=') out = appendsnprintf(out, MAXOUTLEN, "=="); + else out = appendsnprintf(out, MAXOUTLEN, "%s", exp->func); + out = appendsnprintf(out, MAXOUTLEN, " %s", exp->args[1]); + return out; } -void compile(astNode *node, char *out){ +char *compile(astNode *node){ + char *out = malloc(MAXOUTLEN); if (strcmp(names[0], node->func) == 0){ - snprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]); + out = appendsnprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]); int i = 2; while (node->args[i] != NULL){ - vartypeToC(node->args[i], out); + out = vartypeToC(node->args[i], out); i++; } - snprintf(out, MAXOUTLEN, "%s){\n", out); + out = appendsnprintf(out, MAXOUTLEN, "){\n"); } else if (strcmp(names[1], node->func) == 0){ - snprintf(out, MAXOUTLEN, "}\n"); + out = appendsnprintf(out, MAXOUTLEN, "}\n"); } else if (strcmp(names[2], node->func) == 0){ - snprintf(out, MAXOUTLEN, "const "); - vartypeToC(node->args[0], out); - snprintf(out, MAXOUTLEN, " = %s;\n", node->args[1]); + out = appendsnprintf(out, MAXOUTLEN, "const "); + out = vartypeToC(node->args[0], out); + out = appendsnprintf(out, MAXOUTLEN, " = %s;\n", node->args[1]); } else if (strcmp(names[3], node->func) == 0){ - vartypeToC(node->args[0], out); - snprintf(out, MAXOUTLEN, " = %s;\n", node->args[1]); + out = vartypeToC(node->args[0], out); + out = appendsnprintf(out, MAXOUTLEN, " = %s;\n", node->args[1]); } else if (strcmp(names[4], node->func) == 0){ - snprintf(out, MAXOUTLEN, "if ("); - reversepolishToC(node->children[0], out); - snprintf(out, MAXOUTLEN, "){\n"); + out = appendsnprintf(out, MAXOUTLEN, "if ("); + out = reversepolishToC(node->children[0], out); + out = appendsnprintf(out, MAXOUTLEN, "){\n"); } else if (strcmp(names[5], node->func) == 0){ - snprintf(out, MAXOUTLEN, "}\n"); + out = appendsnprintf(out, MAXOUTLEN, "}\n"); } else if (strcmp(names[6], node->func) == 0){ - snprintf(out, MAXOUTLEN, "}\n"); - snprintf(out, MAXOUTLEN, "else if ("); - reversepolishToC(node->children[0], out); - snprintf(out, MAXOUTLEN, "){\n"); + out = appendsnprintf(out, MAXOUTLEN, "}\n"); + out = appendsnprintf(out, MAXOUTLEN, "else if ("); + out = reversepolishToC(node->children[0], out); + out = appendsnprintf(out, MAXOUTLEN, "){\n"); } else if (strcmp(names[7], node->func) == 0){ - snprintf(out, MAXOUTLEN, "}\n"); - snprintf(out, MAXOUTLEN, "else{"); + out = appendsnprintf(out, MAXOUTLEN, "}\n"); + out = appendsnprintf(out, MAXOUTLEN, "else{"); } else if (strcmp(names[8], node->func) == 0){ - snprintf(out, MAXOUTLEN, "for ("); - vartypeToC(node->args[0], out); - snprintf(out, MAXOUTLEN, " = 0;"); - reversepolishToC(node->children[1], out); - snprintf(out, MAXOUTLEN, "; %s+=%s){", getVarName(node->args[0]), node->args[2]); + out = appendsnprintf(out, MAXOUTLEN, "for ("); + out = vartypeToC(node->args[0], out); + out = appendsnprintf(out, MAXOUTLEN, " = 0;"); + out = reversepolishToC(node->children[1], out); + out = appendsnprintf(out, MAXOUTLEN, "; %s+=%s){", getVarName(node->args[0]), node->args[2]); } else if (strcmp(names[9], node->func) == 0){ - snprintf(out, MAXOUTLEN, "}\n"); + out = appendsnprintf(out, MAXOUTLEN, "}\n"); } else if (strcmp(names[10], node->func) == 0){ - snprintf(out, MAXOUTLEN, "printf(\""); - snprintf(out, MAXOUTLEN, "%%d\\n"); - snprintf(out, MAXOUTLEN, "\", %s);", node->args[0]); + out = appendsnprintf(out, MAXOUTLEN, "printf(\""); + out = appendsnprintf(out, MAXOUTLEN, "%%d\\n"); + out = appendsnprintf(out, MAXOUTLEN, "\", %s);", node->args[0]); } else if (strcmp(names[11], node->func) == 0){ - snprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]); + out = appendsnprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]); int i = 2; while (node->args[i] != NULL){ - vartypeToC(node->args[i], out); + out = vartypeToC(node->args[i], out); i++; } - snprintf(out, MAXOUTLEN, ");\n"); + out = appendsnprintf(out, MAXOUTLEN, ");\n"); } else if (strcmp(names[12], node->func) == 0){ - reversepolishToC(node->children[0], out); + out = reversepolishToC(node->children[0], out); } else { - snprintf(out, MAXOUTLEN, "%s(", node->func); + out = appendsnprintf(out, MAXOUTLEN, "%s(", node->func); int i = 0; while (node->args[i] != NULL){ - snprintf(out, MAXOUTLEN, "%s", node->args[i]); + out = appendsnprintf(out, MAXOUTLEN, "%s", node->args[i]); i++; } - snprintf(out, MAXOUTLEN, ");\n"); + out = appendsnprintf(out, MAXOUTLEN, ");\n"); } + return out; } |