summaryrefslogtreecommitdiffstats
path: root/tools/brainfuck/bf_compile.py
blob: 453ae774c5c6c7788f22a3d31e5d5ac5b7590c18 (plain) (blame)
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
#!/usr/bin/env python
import sys

if len(sys.argv) < 2:
    print("Give me filename...")
    exit()

bytecode = {
    '>': b"\x00",
    '<': b"\x02",
    '+': b"\x07",
    '-': b"\x06",
    '.': b"\x05",
    ',': b"\x04",
    '[': b"\x03",
    ']': b"\x01",
}

src_buff = open(sys.argv[1]).read()
bin_file = open(sys.argv[1]+".bin", mode="wb")

for c in src_buff:
    try:
        b = bytecode[c]
        bin_file.write(b)
    except KeyError:
        # Ignore whitespace, etc.
        pass

bin_file.close()