-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (47 loc) · 1.93 KB
/
main.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
62
63
64
import getopt
import sys
from os.path import join
from typing import List
from parser.run_one_test_case import run_test_case
from transformers.decaf_transformer import DecafTransformer
def parent_path(dir: str) -> str:
jj = '/'.join(dir.split('/')[:-1])
if jj:
jj += '/'
return jj
def run(input_file_address: str) -> bool:
with open(input_file_address) as input_file:
data = input_file.read()
parse_tree = run_test_case(data, parent_path(
input_file_address), raise_exception=True)
tac_code = DecafTransformer().transform(parse_tree)
return tac_code
def eval_test():
file_folder = 'tests/'
test_folder = 'CastFunctions/'
file_name = 't091-expr-1.d'
tac_code = run(join(file_folder, test_folder, file_name))
output_file_addr = "out.asm"
with open(output_file_addr, "w") as output_file:
print(tac_code, file=output_file)
def get_sys_attrs(argv, short_attrs: List[str] = [], long_attrs: List[str] = []) -> List:
short_attrs_string: str = ':'.join(short_attrs) + ':'
long_attrs_list = list(map(lambda l_attr: f"{l_attr}=", long_attrs))
try:
opts, args = getopt.getopt(argv, f"h{short_attrs_string}", long_attrs_list)
except getopt.GetoptError:
print('main.py -a <inputfile> -b <inputfile> -o <outputfile>')
sys.exit(2)
# opts based on run.sh, should be sth like: [('-i', 'G1/1.d'), ('-o', 'G1/1.s')]
return list(map(lambda attr: attr[1], opts))
def eval_global_test(argv):
in_dir = 'tests'
out_dir = 'out'
input_file_local_addr, output_file_local_addr = get_sys_attrs(argv=argv, short_attrs=['i', 'o'])
tac_code = run(join(in_dir, input_file_local_addr))
output_file_addr = join(out_dir, output_file_local_addr)
with open(output_file_addr, "w") as output_file:
print(tac_code, file=output_file)
if __name__ == '__main__':
# eval_test()
eval_global_test(argv=sys.argv[1:])