-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotomc.py
61 lines (47 loc) · 1.68 KB
/
rotomc.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
53
54
55
56
57
58
59
60
61
import os
import argparse
from rotom_cmp.semantics.ast import Program
from rotom_cmp.parsing.parser import parser
from rotom_cmp.codegen.js_transpiler import JavascriptTranspiler
from rotom_cmp.utils.rich_logger import RichLogger
from transpile import transpile
def build_arg_parser():
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument(
"file",
type=argparse.FileType(mode="r", encoding="utf-8"),
help="Source .rotom file",
)
arg_parser.add_argument(
"--output_dir",
type=str,
help="Output directory of the transpiled file",
)
arg_parser.add_argument(
"--verbosity", type=int, default=1, help="Handle verbosity levels (0 or 1)"
)
return arg_parser
if __name__ == "__main__":
arg_parser = build_arg_parser()
args = arg_parser.parse_args()
file = args.file.name
verbosity = args.verbosity
output_dir = args.output_dir or os.path.splitext(file)[0]
transpiled_filepath = output_dir + ".js"
logger = RichLogger()
if verbosity:
logger.log_info("1/2 Transpiling...", bold=True)
js_code, errors = transpile(file)
if errors:
logger.log_error(f"Errors found: {len(errors)}", bold=True)
for error in errors:
logger.log_error(" - " + error)
else:
if verbosity:
logger.log_success("1/2 Transpilation succeded", bold=True)
logger.log_info("2/2 Writing js file...", bold=True)
with open(transpiled_filepath, "w") as fd:
fd.write(js_code)
if verbosity:
logger.log_success("2/2 🥳 Finished!!!\n", bold=True)
os.system(f"node {transpiled_filepath}")