Mercurial > hg > CbC > CbC_gcc
annotate CbC-scripts/make_headers.py @ 150:26042f4007d5 current
fix examples
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 23 May 2020 07:51:47 +0900 |
parents | f9b1a53df341 |
children |
rev | line source |
---|---|
26 | 1 #!/usr/bin/env python3.0 |
2 | |
27
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
3 |
26 | 4 import sys |
5 import re | |
6 import getopt | |
7 | |
8 reserved_words = [ "if", "for", "switch", "return", "while", "else", ] | |
9 | |
27
f9b1a53df341
implemented indirect sibcall for ppc.
kent@teto.cr.ie.u-ryukyu.ac.jp
parents:
26
diff
changeset
|
10 PATTERN = "([a-zA-Z_][\w\s]*\**)\s([a-zA-Z_]\w*)\s*\(([^{/;]*)\)\s*\{" |
26 | 11 # TODO: 関数パラメータ内にコメントがあると正しく動かない! |
12 # TODO: int * const * とか大丈夫? | |
13 PROG = re.compile(PATTERN, re.S) | |
14 | |
15 omit_static=False | |
16 add_extern="" | |
17 | |
18 def truncate_comments(data): | |
19 pass | |
20 | |
21 def check_reserved_word(decl): | |
22 """ return true if decl's type and name is not reserved word. """ | |
23 | |
24 if decl["name"] in reserved_words or decl["type"] in reserved_words: | |
25 return False | |
26 return True | |
27 | |
28 def read_decls(file): | |
29 declarators = [] | |
30 | |
31 # open the file and read all lines into a string. | |
32 try: | |
33 fo = open(file, 'r') | |
34 lines = fo.readlines() | |
35 data = "".join(lines) | |
36 truncate_comments(data) | |
37 except IOError: | |
38 print("cannot read file %s" % file) | |
39 return None | |
40 | |
41 # find all matched strings. | |
42 # moiter is iterator of MatchObject. | |
43 moiter = PROG.finditer(data) | |
44 for mo in moiter: | |
45 tmp = { "type": mo.group(1), | |
46 "name": mo.group(2), | |
47 "parms": mo.group(3), | |
48 "offset": mo.start() } | |
49 if check_reserved_word(tmp): | |
50 declarators.append(tmp) | |
51 | |
52 return declarators | |
53 | |
54 def debug_print(decl): | |
55 for (key,value) in list(decl.items()): | |
56 if isinstance(value, str): | |
57 decl[key] = value.replace("\n"," ").replace("\t"," ") | |
58 | |
59 print("Type:\t{0:s}".format(decl["type"])) | |
60 print("Name:\t{0:s}".format(decl["name"])) | |
61 print("Params:\t{0:s}".format(decl["parms"])) | |
62 print("offset:\t{0:d}".format(decl["offset"])) | |
63 print("") | |
64 | |
65 def format_print(decl, file): | |
66 for (key,value) in list(decl.items()): | |
67 if isinstance(value, str): | |
68 decl[key] = value.replace("\n"," ").replace("\t"," ") | |
69 | |
70 print("/* defined in file {0:s} at offset {1:d} */".format(file,decl["offset"])) | |
71 print("{3:s}{0:s} {1:s} ({2:s});".format(decl["type"],decl["name"],decl["parms"], add_extern)) | |
72 print("") | |
73 | |
74 def getoptions(): | |
75 global omit_static, add_extern | |
76 | |
77 try: | |
78 opts, args = getopt.getopt(sys.argv[1:], 'se', [ 'omit-static', 'add-extern' ]) | |
79 except getopt.GetoptError as err: | |
80 print(err) | |
81 usage() | |
82 sys.exit(2) | |
83 | |
84 for opt,a in opts: | |
85 if opt in ("-s", "--omit-static"): | |
86 omit_static=True | |
87 elif opt in ("-e", "--add-extern"): | |
88 add_extern="extern " | |
89 else: | |
90 print("unhandled option {0}".format(opt)) | |
91 usage() | |
92 | |
93 return args | |
94 | |
95 def usage(): | |
96 print( """\ | |
97 Usage: {0:s} OPION... [FILE]... | |
98 OPTIONS: | |
99 -s, --omit-static omit static functions | |
100 -e, --add-extern add extern to all function declarations | |
101 """.format(sys.argv[0])) | |
102 | |
103 def main(): | |
104 | |
105 # option handling. | |
106 args = getoptions() | |
107 | |
108 for file in args: | |
109 # read function declaration from each file. | |
110 decls = read_decls(file) | |
111 if decls==None or len(decls)==0: | |
112 # no function found. | |
113 print("{0} have no function definition!".format(file)) | |
114 continue | |
115 | |
116 for decl in decls: | |
117 if omit_static and 0 <= decl["type"].find("static"): | |
118 # static function is ignored. | |
119 continue | |
120 #debug_print(decl) | |
121 format_print(decl, file) | |
122 | |
123 main() | |
124 |