diff options
Diffstat (limited to 'comp/lucas-standen-NEA/code2/comp.c')
-rw-r--r-- | comp/lucas-standen-NEA/code2/comp.c | 90 |
1 files changed, 46 insertions, 44 deletions
diff --git a/comp/lucas-standen-NEA/code2/comp.c b/comp/lucas-standen-NEA/code2/comp.c index 4bc67e9..3c47cbb 100644 --- a/comp/lucas-standen-NEA/code2/comp.c +++ b/comp/lucas-standen-NEA/code2/comp.c @@ -4,6 +4,8 @@ #include "tokenizer.h" +#define MAXOUTLEN 512 + char *names[] = { "defun", // takes a func name, func return type, and args // 0 "endfun", // takes no args // 1 @@ -33,7 +35,7 @@ char *names[] = { "return", }; -void vartypeToC(char *str, FILE *f){ +void vartypeToC(char *str, char *out){ char *name = malloc(strlen(str)); char *type = malloc(strlen(str)); @@ -57,12 +59,12 @@ void vartypeToC(char *str, FILE *f){ } type[j] = '\0'; - fprintf(f, "%s %s", type, name); + snprintf(out, MAXOUTLEN, "%s%s %s", out, type, name); free(type); free(name); } -char *getVarName(char *exp, FILE *f){ +char *getVarName(char *exp){ char *out = malloc(strlen(exp)); memcpy(out, exp, strlen(exp)); char *pos = strchr(out, ':'); @@ -70,90 +72,90 @@ char *getVarName(char *exp, FILE *f){ return out; } -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 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]); } -void compile(astNode *node, FILE *f){ +void compile(astNode *node, char *out){ if (strcmp(names[0], node->func) == 0){ - fprintf(f, "%s %s(", node->args[1], node->args[0]); + snprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]); int i = 2; while (node->args[i] != NULL){ - vartypeToC(node->args[i], f); + vartypeToC(node->args[i], out); i++; } - fprintf(f, "){\n"); + snprintf(out, MAXOUTLEN, "%s){\n", out); } else if (strcmp(names[1], node->func) == 0){ - fprintf(f, "\n}\n"); + snprintf(out, MAXOUTLEN, "}\n"); } else if (strcmp(names[2], node->func) == 0){ - fprintf(f, "const "); - vartypeToC(node->args[0], f); - fprintf(f, " = %s;\n", node->args[1]); + snprintf(out, MAXOUTLEN, "const "); + vartypeToC(node->args[0], out); + snprintf(out, MAXOUTLEN, " = %s;\n", node->args[1]); } else if (strcmp(names[3], node->func) == 0){ - vartypeToC(node->args[0], f); - fprintf(f, " = %s;\n", node->args[1]); + vartypeToC(node->args[0], out); + snprintf(out, MAXOUTLEN, " = %s;\n", node->args[1]); } else if (strcmp(names[4], node->func) == 0){ - fprintf(f, "if ("); - reversepolishToC(node->children[0], f); - fprintf(f, "){\n"); + snprintf(out, MAXOUTLEN, "if ("); + reversepolishToC(node->children[0], out); + snprintf(out, MAXOUTLEN, "){\n"); } else if (strcmp(names[5], node->func) == 0){ - fprintf(f, "\n}\n"); + snprintf(out, MAXOUTLEN, "}\n"); } else if (strcmp(names[6], node->func) == 0){ - fprintf(f, "\n}\n"); - fprintf(f, "else if ("); - reversepolishToC(node->children[0], f); - fprintf(f, "){\n"); + snprintf(out, MAXOUTLEN, "}\n"); + snprintf(out, MAXOUTLEN, "else if ("); + reversepolishToC(node->children[0], out); + snprintf(out, MAXOUTLEN, "){\n"); } else if (strcmp(names[7], node->func) == 0){ - fprintf(f, "\n}\n"); - fprintf(f, "else{"); + snprintf(out, MAXOUTLEN, "}\n"); + snprintf(out, MAXOUTLEN, "else{"); } else if (strcmp(names[8], node->func) == 0){ - 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]); + 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]); } else if (strcmp(names[9], node->func) == 0){ - fprintf(f, "\n}\n"); + snprintf(out, MAXOUTLEN, "}\n"); } else if (strcmp(names[10], node->func) == 0){ - fprintf(f, "printf(\""); - fprintf(f, "%%d\\n"); - fprintf(f, "\", %s);", node->args[0]); + snprintf(out, MAXOUTLEN, "printf(\""); + snprintf(out, MAXOUTLEN, "%%d\\n"); + snprintf(out, MAXOUTLEN, "\", %s);", node->args[0]); } else if (strcmp(names[11], node->func) == 0){ - fprintf(f, "%s %s(", node->args[1], node->args[0]); + snprintf(out, MAXOUTLEN, "%s %s(", node->args[1], node->args[0]); int i = 2; while (node->args[i] != NULL){ - vartypeToC(node->args[i], f); + vartypeToC(node->args[i], out); i++; } - fprintf(f, ");\n"); + snprintf(out, MAXOUTLEN, ");\n"); } else if (strcmp(names[12], node->func) == 0){ - reversepolishToC(node->children[0], f); + reversepolishToC(node->children[0], out); } else { - fprintf(f, "%s(", node->func); + snprintf(out, MAXOUTLEN, "%s(", node->func); int i = 0; while (node->args[i] != NULL){ - fprintf(f, "%s", node->args[i]); + snprintf(out, MAXOUTLEN, "%s", node->args[i]); i++; } - fprintf(f, ");\n"); + snprintf(out, MAXOUTLEN, ");\n"); } } |