-
Notifications
You must be signed in to change notification settings - Fork 3
/
generator.rb
278 lines (240 loc) · 7.77 KB
/
generator.rb
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
require 'tempfile'
class Generator
def self.filter_key
nil
end
def initialize
@type_size_map = {
"uint8_t" => 1,
"uint16_t" => 2,
"uint32_t" => 4,
"uint64_t" => 8,
"int8_t" => 1,
"int16_t" => 2,
"int32_t" => 4,
"int64_t" => 8,
"char" => 1,
"Point" => 4,
"ProcPtr" => 4,
"void" => 0,
"double" => 8
}
@fancy_comments = false
@expand_common = true
end
def size_of_type(type)
return nil if not type
return @type_size_map[type] if @type_size_map[type]
return 4 if type =~ /[*\[\]]/
return nil
end
def starredtext(str, align, lchar:'*', rchar:'*')
maxlinelen = str.lines.map{|s| s.rstrip.length}.max
str.each_line do |s|
s.rstrip!
c = (75 - s.length)
b = case align
when "center" then c/2
when "left" then c - 1
when "right" then 1
end
a = c - b
a = 0 if a < 0
b = 0 if b < 0
@out << " #{lchar}#{' '*a}#{s}#{' '*b}#{rchar}\n"
end
end
def starline
@out << "/#{'*'*77}/\n"
end
def box(title, comment=nil, order:1)
if title or comment then
@out << "/#{'*'*77}\n"
#print " *#{' '*75}*\n"
starredtext(title, 'center') if title
@out << " *#{' '*75}*\n" if title and comment
starredtext(comment, 'left') if comment
@out << " #{'*'*77}/\n"
else
starline
end
end
def document_dependencies(header)
out = ""
if header.included.size > 0 then
out << "Needs:\n"
colwidth = header.included.map(&:size).max + 4 + 2 + 4
maxlinelen = 70
header.included.each do |inc|
line = " " * 4 + inc + ".h"
line << " " * (colwidth - line.length)
indent = line.length
whys = header.included_why[inc].to_a
whys.each_index do |idx|
why = whys[idx]
last = (idx == whys.size - 1)
if line.length + why.size + (last ? 0 : 1) > maxlinelen
out << line << "\n"
line = " " * indent
end
line << why
line << ", " unless last
end
out << line << "\n"
end
end
return out
end
def declare_members(members)
members.each do |member|
sub = (member["struct"] or member["union"])
if sub then
@out << (if member["struct"] then "struct" else "union" end) << "{"
declare_members sub
@out << "} " << member["name"] << ";"
elsif member["common"]
if @expand_common then
declare_members($global_name_map[member["common"]]["common"]["members"])
else
@out << member["common"] << ";"
end
else
@out << decl(member["type"], member["name"]) << ";"
@out << " // " << member["comment"].rstrip << "\n" if member["comment"]
end
end
end
def convert_expression(expr)
return expr
end
def declare_enum(value)
@out << "typedef " if value["name"]
@out << "enum {"
value["values"].each do |val|
@out << val["name"]
if val["value"] then
@out << " = " << convert_expression(val["value"].to_s) << ","
else
@out << ","
end
@out << " // " << val["comment"].rstrip if val["comment"]
@out << "\n"
if val["old_name"] then
@out << "#if OLDROUTINENAMES\n"
@out << "#define #{val["old_name"]} #{val["name"]}\n"
@out << "#endif\n"
end
end
@out << "}"
@out << value["name"] if value["name"]
@out << ";"
end
def declare_typedef(value)
@out << "typedef "
@out << decl(value["type"], value["name"])
@out << ";"
sz = size_of_type(value["type"])
@type_size_map[value["name"]] = sz if sz
end
def generate_preamble(header)
title = "\n" + header.name + ".h\n" + "="*(header.name.length+2) + "\n"
titlecomment = nil
if header.included.size > 0 then
titlecomment = document_dependencies(header)
titlecomment << "\n\n"
else
title << "\n"
end
box(title, titlecomment)
@out << "\n\n"
end
def generate_postamble(header)
end
def generate_comment(key, value)
if @fancy_comments then
box(value["name"], value["comment"])
else
return unless value["comment"]
value["comment"].each_line do |l|
@out << "// " << l.rstrip << "\n"
end
end
end
def declare_verbatim(value)
@out << value.strip << "\n"
end
def generate_header(header)
@out = ""
generate_preamble(header)
header.data.each do |item|
key, value = main_elem(item)
make_api_ifdef item["api"] do
generate_comment(key, value)
case key
when "enum"
declare_enum(value)
when "struct", "union"
declare_struct_union(key, value)
when "dispatcher"
declare_dispatcher(value)
when "typedef"
declare_typedef(value)
when "function"
declare_function(value)
when "lowmem"
declare_lowmem(value)
when "funptr"
@type_size_map[value["name"]] = 4
declare_funptr(value)
when "verbatim"
declare_verbatim(value)
when "executor_only"
declare_verbatim(value["code"])
end
@out << "\n"
end
@out << "\n"
end
generate_postamble(header)
return @out
end
def make_api_ifdef(api)
yield
end
def formatted_file(name, &block)
if not @format_command then
if system('clang-format --version', [:out, :err]=>File::NULL) then
@format_command = "clang-format"
elsif system('uncrustify --version', [:out, :err]=>File::NULL) then
@format_command = "uncrustify -q -c uncrustify.cfg"
elsif system('astyle --version', [:out, :err]=>File::NULL) then
@format_command = "astyle --style=allman --max-code-length=79 --mode=c"
end
end
#IO.popen("#{@format_command} > \"#{name}\"", "w", &block)
tmp = Tempfile.new('multiverse-tmp')
block.call(tmp)
tmp.close
if @format_command then
newtext = ""
IO.popen("#{@format_command}", "r", :in => tmp.path) do |pipe|
newtext = pipe.read
end
else
newtext = File.read(tmp.path)
end
tmp.unlink
filtered = ""
newtext.each_line do |line|
if line =~ /^\/\/ clang-format o.*/ then
else
filtered += line
end
end
newtext = filtered
if !File.readable?(name) || File.read(name) != newtext then
File.write(name, newtext)
print "#{name}\n"
end
end
end