systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
hcode2verilog.py
Go to the documentation of this file.
1from parselib.grammar import lark_grammar
2from parselib.utils import tidify
3from parselib.transforms import VerilogTranslator
4import sys
5import traceback
6import logging
7import argparse
8import pathlib
9logging.basicConfig(level=logging.DEBUG)
10
11from lark import logger
12from pprint import pprint
13
14logger.setLevel(logging.DEBUG)
15
16__file_input = None
17
19 exc_type, exc_value, exc_traceback = sys.exc_info()
20 print("***** print_exception:")
21 # exc_type below is ignored on 3.5 and later
22 traceback.print_exception(exc_type, exc_value, exc_traceback, file=sys.stdout, limit=-60)
23
24 stack_summary = list(traceback.walk_tb(exc_traceback))
25 if stack_summary:
26 f, lineno = stack_summary[-1]
27 if 'tree' in f.f_locals:
28 print("While processing the following tree node (the format might differ from hNode representation).")
29 if __file_input:
30 print("When parsing file: ", __file_input)
31 print("Line: ", f.f_locals['tree'].meta.line, ", Column: ", f.f_locals['tree'].meta.column)
32 print(f.f_locals['tree'].pretty())
33 print(f.f_locals['tree'].meta)
34 exit(retcode)
35
36
37def translate_text(file_content):
38 t = lark_grammar.parse(file_content)
39
40 try:
41 x = VerilogTranslator
42 return x.translate(t)
43 except Exception as e:
45
46
47def main():
48 global __file_input
49
50 parser = argparse.ArgumentParser()
51 parser.add_argument('input', type=str, help='Input file name (normally the _hdl.txt file)')
52 parser.add_argument('--output', type=str, help='The outpuf filename')
53 parser.add_argument('-f', '--force', action="store_true",
54 help='Whether the script overwrites resultant file')
55 args = parser.parse_args()
56 filename = args.input
57 __file_input = filename
58 outputname = filename + ".sv"
59 if args.output is not None:
60 outputname = args.output
61 if not args.force and pathlib.Path(outputname).exists():
62 raise FileExistsError("File {} exists".format(outputname))
63 with open(filename, 'r') as f:
64 file_content = f.read()
65 try:
66 t = lark_grammar.parse(file_content)
67 except Exception as e:
68 print(e)
69 exit(2) # This code has specific meanings, should be tested in the verilog tests
70 try:
71 x = VerilogTranslator
72 res = x.translate(t)
73 except Exception as e:
75
76 with open(outputname, 'w+') as f:
77 f.writelines(res)
78 f.write("\n\n")
79 # print('Result: ', res)
80
81
82if __name__ == '__main__':
83 main()
84 # l.parse('')
_handle_exception_and_exit(e, retcode=3)
translate_text(file_content)