-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_ir.py
84 lines (76 loc) · 2.92 KB
/
convert_ir.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
import argparse
import os
import yaml
import json
from yaml_to_classes import load_yaml_file
from json_to_ir import load_json_file
def save_yaml_file(header, output_file):
yaml_data = {
'header': header.name,
'macros': [macro.name for macro in header.macros],
'types': [type_.name for type_ in header.types],
'enums': [
{
'name': enum.name,
'values': enum.values
} for enum in header.enumerations
],
'functions': [
{
'return_type': func.return_type,
'name': func.name,
'arguments': [arg for arg in func.arguments],
'guard': func.guard,
'attributes': func.attributes
} for func in header.functions
],
'includes': [include.name for include in header.includes]
}
with open(output_file, 'w') as f:
yaml.safe_dump(yaml_data, f)
def save_json_file(header, output_file):
json_data = {
'header': header.name,
'macros': [macro.name for macro in header.macros],
'types': [type_.name for type_ in header.types],
'enums': [
{
'name': enum.name,
'values': enum.values
} for enum in header.enumerations
],
'functions': [
{
'return_type': func.return_type,
'name': func.name,
'arguments': [arg for arg in func.arguments],
'guard': func.guard,
'attributes': func.attributes
} for func in header.functions
],
'includes': [include.name for include in header.includes]
}
with open(output_file, 'w') as f:
json.dump(json_data, f, indent=4)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert between YAML and JSON with IR representation")
parser.add_argument("input_file", help="Path to the input YAML or JSON file")
parser.add_argument("output_file", help="Path to the output YAML or JSON file")
parser.add_argument("--format", choices=['yaml', 'json'], help="Output format (yaml or json)", required=True)
args = parser.parse_args()
input_ext = os.path.splitext(args.input_file)[1].lower()
output_ext = os.path.splitext(args.output_file)[1].lower()
if input_ext == '.yaml':
header = load_yaml_file(args.input_file)
elif input_ext == '.json':
header = load_json_file(args.input_file)
else:
raise ValueError("Input file must be a .yaml or .json file")
if args.format == 'yaml':
save_yaml_file(header, args.output_file)
elif args.format == 'json':
save_json_file(header, args.output_file)
else:
raise ValueError("Output format must be 'yaml' or 'json'")
#command line argument: python convert_ir.py path/to/input_file.yaml path/to/output_file.json --format json