-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2016_12_transpiler_test.py
executable file
·52 lines (43 loc) · 1.47 KB
/
aoc2016_12_transpiler_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python2
import sys, os, re, argparse, subprocess
CFILE = "a.c"
EXEFILE = "./a.out"
COMPILE = ["cc", "-O9", CFILE, "-o", EXEFILE]
OPS = {
"cpy": "B = A",
"inc": "A++",
"dec": "A--",
"jnz": "if (A) ip += B - 1",
}
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("infile", default="input.txt", nargs='?',
help="input program file name")
parser.add_argument("-c", metavar="N", type=int, default=0,
help="set initial value of c [default: %(default)s]")
parser.add_argument("-k", "--keep", action='store_true',
help="keep C output (%s)" % CFILE)
args = parser.parse_args()
c = open("a.c", 'w')
print >>c, "#include <stdio.h>"
print >>c, "int ip, a, b, c = %d, d;" % args.c
print >>c, "int main(void) {"
print >>c, " for (;;) {"
print >>c, " switch(ip) {"
addr = 0
for line in open(args.infile):
ins = line.strip().split()
print >>c, " case %d: %s; break;" % (addr, OPS[ins[0]].replace('A', ins[1]).replace('B', ins[2] if len(ins)>2 else "?"))
addr += 1
print >>c, " default: printf(\"%d\\n\", a); return 0;"
print >>c, " }"
print >>c, " ip++;"
print >>c, " }"
print >>c, "}"
c.close()
res = subprocess.call(COMPILE)
if res:
sys.exit(res)
if not args.keep:
os.unlink(CFILE)
sys.exit(subprocess.call([EXEFILE]))