Skip to content

Instantly share code, notes, and snippets.

@eraserhd
Created August 24, 2014 17:58
Show Gist options
  • Select an option

  • Save eraserhd/db4dea693a31a5db87c8 to your computer and use it in GitHub Desktop.

Select an option

Save eraserhd/db4dea693a31a5db87c8 to your computer and use it in GitHub Desktop.
A simple compiler
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *f;
char op;
int a, b;
scanf("%c %d %d", &op, &a, &b);
f = fopen("/tmp/temp.c", "w");
fprintf(f, "#include <stdio.h>\n");
fprintf(f, "#include <stdlib.h>\n");
fprintf(f, "int main(int argc, char *argv[]) {\n");
switch (op) {
case '+': fprintf(f,"printf(\"%%d\", %d + %d);\n", a, b); break;
case '-': fprintf(f,"printf(\"%%d\", %d - %d);\n", a, b); break;
}
fprintf(f, "exit(0);\n");
fprintf(f, "}\n");
fclose(f);
system("cc -o /tmp/temp /tmp/temp.c");
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment