forked from ytanay/thinglang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.py
37 lines (25 loc) · 1005 Bytes
/
compile.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
import os
import argparse
from thinglang.utils.source_context import SourceContext
from thinglang import pipeline
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"sourcefile", help="The input thing source file to compile.")
return parser.parse_args()
def compile(target_source_file_path):
source_context = SourceContext.wrap(
str(open(target_source_file_path, "rb").read(), encoding="utf-8"))
compilation_result = pipeline.compile(source_context)
return compilation_result.bytes()
def main():
source_file = parse_args().sourcefile
print(f"Compiling {source_file}...")
compiled_bytecode = compile(source_file)
target_file_path = os.path.splitext(source_file)[0] + ".tlc"
print(f"Compilation successful, writing bytecode to {target_file_path}")
with open(target_file_path, "wb") as bytecode_file:
bytecode_file.write(compiled_bytecode)
print(f"Done! :)")
if __name__ == "__main__":
main()