contrib/pep8analysis: minor clean up
[nit.git] / contrib / pep8analysis / src / ast / rich_instructions.nit
1 # Pep/8 instructions are not reserved as keywords. It is common
2 # that the identifier of an instruction will be used for a label.
3 # For this reason, we cannot create precise instruction nodes with
4 # the parser.
5 #
6 # This module manually creates the expected subclass with an additionnal
7 # OOP hiearchy.
8
9 module rich_instructions
10
11 import backbone
12 intrude import parser
13 import ast_base
14
15 redef class AnalysisManager
16 redef fun build_ast(filename)
17 do
18 var ast = super
19 if ast != null then
20 #fun enrich_ast( ast : AListing ) do
21 for line in ast.n_lines do
22 if line isa AInstructionLine then
23 line.enrich
24 end
25 end
26 end
27 return ast
28 end
29 end
30
31 redef class AInstructionLine
32 # TODO move to AnalysisManager as private?
33 private fun enrich
34 do
35 var instr = n_instruction
36 var new_instr : AInstruction
37
38 var id = instr.n_id.text.to_upper
39 if instr isa ABinaryInstruction then
40 if id == "BR" then
41 new_instr = new ABrInstruction.from(instr)
42 else if id == "BRLE" then
43 new_instr = new ABrleInstruction.from(instr)
44 else if id == "BRLT" then
45 new_instr = new ABrltInstruction.from(instr)
46 else if id == "BREQ" then
47 new_instr = new ABreqInstruction.from(instr)
48 else if id == "BRNE" then
49 new_instr = new ABrneInstruction.from(instr)
50 else if id == "BRGE" then
51 new_instr = new ABrgeInstruction.from(instr)
52 else if id == "BRGT" then
53 new_instr = new ABrgtInstruction.from(instr)
54 else if id == "BRV" then
55 new_instr = new ABrvInstruction.from(instr)
56 else if id == "BRC" then
57 new_instr = new ABrcInstruction.from(instr)
58 else if id == "CALL" then
59 new_instr = new ACallInstruction.from(instr)
60 else if id == "NOP" then
61 new_instr = new ABinaryNopInstruction.from(instr)
62 else if id == "DECI" then
63 new_instr = new ADeciInstruction.from(instr)
64 else if id == "DECO" then
65 new_instr = new ADecoInstruction.from(instr)
66 else if id == "STRO" then
67 new_instr = new AStroInstruction.from(instr)
68 else if id == "CHARI" then
69 new_instr = new AChariInstruction.from(instr)
70 else if id == "CHARO" then
71 new_instr = new ACharoInstruction.from(instr)
72 else if id == "ADDSP" then
73 new_instr = new AAddspInstruction.from(instr)
74 else if id == "SUBSP" then
75 new_instr = new ASubspInstruction.from(instr)
76 else if id.has_prefix("ADD") then
77 new_instr = new AAddInstruction.from(instr)
78 else if id.has_prefix("SUB") then
79 new_instr = new ASubInstruction.from(instr)
80 else if id.has_prefix("AND") then
81 new_instr = new AAndInstruction.from(instr)
82 else if id.has_prefix("OR") then
83 new_instr = new AOrInstruction.from(instr)
84 else if id.has_prefix("CP") then
85 new_instr = new ACpInstruction.from(instr)
86 else if id.has_prefix("LDBYTE") then
87 new_instr = new ALdbyteInstruction.from(instr)
88 else if id.has_prefix("LD") then
89 new_instr = new ALdInstruction.from(instr)
90 else if id.has_prefix("STBYTE") then
91 new_instr = new AStbyteInstruction.from(instr)
92 else if id.has_prefix("ST") then
93 new_instr = new AStInstruction.from(instr)
94 else
95 # error
96 print "error {instr.location}: invalid instruction {id} with data access"
97 abort
98 end
99 else if instr isa AUnaryInstruction then
100 if id == "STOP" then
101 new_instr = new AStopInstruction.from(instr)
102 else if id == "RETTR" then
103 new_instr = new ARettrInstruction.from(instr)
104 else if id == "MOVSPA" then
105 new_instr = new AMovspaInstruction.from(instr)
106 else if id == "MOVFLGA" then
107 new_instr = new AMovflgaInstruction.from(instr)
108 else if id.has_prefix("NOT") then
109 new_instr = new ANotInstruction.from(instr)
110 else if id.has_prefix("NEG") then
111 new_instr = new ANegInstruction.from(instr)
112 else if id.has_prefix("ASL") then
113 new_instr = new AAslInstruction.from(instr)
114 else if id.has_prefix("ASR") then
115 new_instr = new AAsrInstruction.from(instr)
116 else if id.has_prefix("ROL") then
117 new_instr = new ARolInstruction.from(instr)
118 else if id.has_prefix("ROR") then
119 new_instr = new ARorInstruction.from(instr)
120 else if id.has_prefix("NOP") then
121 new_instr = new AUnaryNopInstruction.from(instr)
122 else if id.has_prefix("RET") then
123 new_instr = new ARetInstruction.from(instr)
124 else
125 # error
126 print "error {instr.location}: invalid instruction {id} without data access"
127 abort
128 end
129 else abort
130
131 # TODO check, one of those 2 might not be necessary
132 replace_child( instr, new_instr )
133 #n_instruction = new_instr
134 end
135 end
136
137 #
138 # Support classes
139 #
140 abstract class ARichBinaryInstruction
141 super ABinaryInstruction
142
143 init from( src: ABinaryInstruction ) do
144 _n_operand = src.n_operand
145 _n_id = src.n_id
146 location = src.location
147 parent = src.parent
148 end
149 end
150
151 abstract class ARichUnaryInstruction
152 super AUnaryInstruction
153
154 init from( src: AUnaryInstruction ) do
155 _n_id = src.n_id
156 _location = src.location
157 parent = src.parent
158 end
159 end
160
161 #
162 # Categories
163 #
164 abstract class ABranchInstruction
165 super ARichBinaryInstruction
166 init from( src ) do super
167 end
168
169 class ABrInstruction
170 super ABranchInstruction
171 init from( src ) do super
172 end
173 class ABrleInstruction
174 super ABranchInstruction
175 init from( src ) do super
176 end
177 class ABrltInstruction
178 super ABranchInstruction
179 init from( src ) do super
180 end
181 class ABreqInstruction
182 super ABranchInstruction
183 init from( src ) do super
184 end
185 class ABrneInstruction
186 super ABranchInstruction
187 init from( src ) do super
188 end
189 class ABrgeInstruction
190 super ABranchInstruction
191 init from( src ) do super
192 end
193 class ABrgtInstruction
194 super ABranchInstruction
195 init from( src ) do super
196 end
197 class ABrvInstruction
198 super ABranchInstruction
199 init from( src ) do super
200 end
201 class ABrcInstruction
202 super ABranchInstruction
203 init from( src ) do super
204 end
205 class ACallInstruction
206 super ABranchInstruction
207 init from( src ) do super
208 end
209
210
211 abstract class ANopInstruction
212 super AInstruction
213 end
214 class ABinaryNopInstruction
215 super ANopInstruction
216 super ARichBinaryInstruction
217 init from( src ) do super
218 end
219 class AUnaryNopInstruction
220 super ANopInstruction
221 super ARichUnaryInstruction
222 init from( src ) do super
223 end
224
225
226 abstract class AOutputInstruction
227 super ARichBinaryInstruction
228 init from( src ) do super
229 end
230 abstract class AInputInstruction
231 super ARichBinaryInstruction
232 init from( src ) do super
233 end
234 # TODO add category for ADecInstruction?
235 class ADeciInstruction
236 super AInputInstruction
237 init from( src ) do super
238 end
239 class ADecoInstruction
240 super AOutputInstruction
241 init from( src ) do super
242 end
243
244 class AStroInstruction
245 super AOutputInstruction
246 init from( src ) do super
247 end
248
249 class AChariInstruction
250 super AInputInstruction
251 init from( src ) do super
252 end
253 class ACharoInstruction
254 super AOutputInstruction
255 init from( src ) do super
256 end
257
258
259 abstract class AStackInstruction
260 super ARichBinaryInstruction
261 init from( src ) do super
262 end
263 class AAddspInstruction
264 super AStackInstruction
265 init from( src ) do super
266 end
267 class ASubspInstruction
268 super AStackInstruction
269 init from( src ) do super
270 end
271
272 # Misc
273 class AStopInstruction
274 super ARichUnaryInstruction
275 init from( src ) do super
276 end
277
278 class ARettrInstruction
279 super ARichUnaryInstruction
280 init from( src ) do super
281 end
282
283 abstract class AMovInstruction
284 super ARichUnaryInstruction
285 init from( src ) do super
286 end
287 class AMovspaInstruction
288 super AMovInstruction
289 init from( src ) do super
290 end
291 class AMovflgaInstruction
292 super AMovInstruction
293 init from( src ) do super
294 end
295
296
297 class ANotInstruction
298 super ARichUnaryInstruction
299 init from( src ) do super
300 end
301
302 class ANegInstruction
303 super ARichUnaryInstruction
304 init from( src ) do super
305 end
306
307 abstract class AShiftInstruction
308 super ARichUnaryInstruction
309 init from( src ) do super
310 end
311 class AAslInstruction
312 super AShiftInstruction
313 init from( src ) do super
314 end
315 class AAsrInstruction
316 super AShiftInstruction
317 init from( src ) do super
318 end
319 class ARolInstruction
320 super AShiftInstruction
321 init from( src ) do super
322 end
323 class ARorInstruction
324 super AShiftInstruction
325 init from( src ) do super
326 end
327
328 class ARetInstruction
329 super ARichUnaryInstruction
330 init from( src ) do super
331 end
332
333
334 # TODO find a better name
335 abstract class AArithmeticInstruction
336 super ARichBinaryInstruction
337 init from( src ) do super
338 end
339 class AAddInstruction
340 super AArithmeticInstruction
341 init from( src ) do super
342 end
343 class ASubInstruction
344 super AArithmeticInstruction
345 init from( src ) do super
346 end
347 class AAndInstruction
348 super AArithmeticInstruction
349 init from( src ) do super
350 end
351 class AOrInstruction
352 super AArithmeticInstruction
353 init from( src ) do super
354 end
355
356 class ACpInstruction
357 super ARichBinaryInstruction
358 init from( src ) do super
359 end
360
361
362 abstract class ALoadInstruction
363 super ARichBinaryInstruction
364 init from( src ) do super
365 end
366 class ALdInstruction
367 super ALoadInstruction
368 init from( src ) do super
369 end
370 class ALdbyteInstruction
371 super ALoadInstruction
372 init from( src ) do super
373 end
374
375 abstract class AStoreInstruction
376 super ARichBinaryInstruction
377 init from( src ) do super
378 end
379 class AStInstruction
380 super AStoreInstruction
381 init from( src ) do super
382 end
383 class AStbyteInstruction
384 super AStoreInstruction
385 init from( src ) do super
386 end