Advertisement
sconetto

Python - C Compiler

Nov 7th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.82 KB | None | 0 0
  1. import click
  2.  
  3. @click.command()
  4. @click.argument('brainf_ck',type=click.File('r'))
  5. @click.option('-o', nargs=1, type=click.File('w'))
  6.  
  7. def CCompiler(brainf_ck, o):
  8.  
  9.     bfck_dict = {
  10.         ">": "\t++ptr;\n",
  11.         "<": "\t--ptr;\n",
  12.         "+": "\t++(*ptr);\n",
  13.         "-": "\t--(*ptr);\n",
  14.         ".": """\tprintf("%c",(*ptr));\n""",
  15.         ",": """\tscanf("%c",ptr);\n""",
  16.         "[": """\twhile(*ptr) {\n\t""",
  17.         "]": "\t\n}\n",
  18.     }
  19.  
  20.     CInit = """#include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. int main(void) {
  24.     char *tape = malloc(sizeof(char)*40000);
  25.     char *ptr = &tape[0];
  26. """
  27.  
  28.     source = brainf_ck.read()
  29.     for data in source:
  30.         if data in bfck_dict:
  31.             CInit = CInit + bfck_dict[data]
  32.  
  33.     CInit = CInit + """\tprintf("\\n");\n\treturn 0;\n}"""
  34.  
  35.     o.write(CInit)
  36.     o.flush()
  37.  
  38. if __name__ == "__main__":
  39.     op = CCompiler()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement