-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile.py
More file actions
61 lines (44 loc) · 1.44 KB
/
compile.py
File metadata and controls
61 lines (44 loc) · 1.44 KB
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
53
54
55
56
57
58
59
60
61
import sys
import logging
import glob
import argparse
import os
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format="%(message)s")
HERE = os.path.dirname(os.path.realpath(__file__))
assert os.path.exists(HERE) and os.path.isdir(HERE)
for file in glob.glob(os.path.join(HERE, "temper.out/py/*")):
sys.path.append(file)
import ebrew
def read_file(args, k):
name = args[0]
with open(name, "rb") as f:
text = f.read().decode("utf-8")
k(ebrew.unstring(text))
def compile(target: str, code: str) -> str:
env = ebrew.Env()
chars = []
def putchar(args, k):
chars.append(chr(int(args[0].to_string())))
k(args[0])
env.add("read-file", read_file)
env.add("putchar", putchar)
with open(os.path.join(HERE, "src/ebrew.eb")) as f:
env.source(f.read())
env.call("main-lang", [ebrew.string(target), ebrew.string(code)])
return "".join(chars)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("input", type=argparse.FileType("r"))
parser.add_argument("-o", "--output", type=argparse.FileType("w"), required=True)
parser.add_argument("-t", "--target", choices=['inc', 'min', 'c'], default = 'inc')
args = parser.parse_args()
code = args.input.read()
args.input.close()
output = compile(
target = args.target,
code = code,
)
args.output.write(output)
args.output.close()
if __name__ == "__main__":
main()