pep8analysis: enable analyzing from stream
[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, stream)
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 parent = src.parent
147
148 init
149 _location = src.location
150 end
151 end
152
153 abstract class ARichUnaryInstruction
154 super AUnaryInstruction
155
156 init from( src: AUnaryInstruction ) do
157 _n_id = src.n_id
158 parent = src.parent
159
160 init
161 _location = src.location
162 end
163 end
164
165 #
166 # Categories
167 #
168 abstract class ABranchInstruction
169 super ARichBinaryInstruction
170 init from( src ) do super
171 end
172
173 class ABrInstruction
174 super ABranchInstruction
175 init from( src ) do super
176 end
177 class ABrleInstruction
178 super ABranchInstruction
179 init from( src ) do super
180 end
181 class ABrltInstruction
182 super ABranchInstruction
183 init from( src ) do super
184 end
185 class ABreqInstruction
186 super ABranchInstruction
187 init from( src ) do super
188 end
189 class ABrneInstruction
190 super ABranchInstruction
191 init from( src ) do super
192 end
193 class ABrgeInstruction
194 super ABranchInstruction
195 init from( src ) do super
196 end
197 class ABrgtInstruction
198 super ABranchInstruction
199 init from( src ) do super
200 end
201 class ABrvInstruction
202 super ABranchInstruction
203 init from( src ) do super
204 end
205 class ABrcInstruction
206 super ABranchInstruction
207 init from( src ) do super
208 end
209 class ACallInstruction
210 super ABranchInstruction
211 init from( src ) do super
212 end
213
214
215 abstract class ANopInstruction
216 super AInstruction
217 end
218 class ABinaryNopInstruction
219 super ANopInstruction
220 super ARichBinaryInstruction
221 init from( src ) do super
222 end
223 class AUnaryNopInstruction
224 super ANopInstruction
225 super ARichUnaryInstruction
226 init from( src ) do super
227 end
228
229
230 abstract class AOutputInstruction
231 super ARichBinaryInstruction
232 init from( src ) do super
233 end
234 abstract class AInputInstruction
235 super ARichBinaryInstruction
236 init from( src ) do super
237 end
238 # TODO add category for ADecInstruction?
239 class ADeciInstruction
240 super AInputInstruction
241 init from( src ) do super
242 end
243 class ADecoInstruction
244 super AOutputInstruction
245 init from( src ) do super
246 end
247
248 class AStroInstruction
249 super AOutputInstruction
250 init from( src ) do super
251 end
252
253 class AChariInstruction
254 super AInputInstruction
255 init from( src ) do super
256 end
257 class ACharoInstruction
258 super AOutputInstruction
259 init from( src ) do super
260 end
261
262
263 abstract class AStackInstruction
264 super ARichBinaryInstruction
265 init from( src ) do super
266 end
267 class AAddspInstruction
268 super AStackInstruction
269 init from( src ) do super
270 end
271 class ASubspInstruction
272 super AStackInstruction
273 init from( src ) do super
274 end
275
276 # Misc
277 class AStopInstruction
278 super ARichUnaryInstruction
279 init from( src ) do super
280 end
281
282 class ARettrInstruction
283 super ARichUnaryInstruction
284 init from( src ) do super
285 end
286
287 abstract class AMovInstruction
288 super ARichUnaryInstruction
289 init from( src ) do super
290 end
291 class AMovspaInstruction
292 super AMovInstruction
293 init from( src ) do super
294 end
295 class AMovflgaInstruction
296 super AMovInstruction
297 init from( src ) do super
298 end
299
300
301 class ANotInstruction
302 super ARichUnaryInstruction
303 init from( src ) do super
304 end
305
306 class ANegInstruction
307 super ARichUnaryInstruction
308 init from( src ) do super
309 end
310
311 abstract class AShiftInstruction
312 super ARichUnaryInstruction
313 init from( src ) do super
314 end
315 class AAslInstruction
316 super AShiftInstruction
317 init from( src ) do super
318 end
319 class AAsrInstruction
320 super AShiftInstruction
321 init from( src ) do super
322 end
323 class ARolInstruction
324 super AShiftInstruction
325 init from( src ) do super
326 end
327 class ARorInstruction
328 super AShiftInstruction
329 init from( src ) do super
330 end
331
332 class ARetInstruction
333 super ARichUnaryInstruction
334 init from( src ) do super
335 end
336
337
338 # TODO find a better name
339 abstract class AArithmeticInstruction
340 super ARichBinaryInstruction
341 init from( src ) do super
342 end
343 class AAddInstruction
344 super AArithmeticInstruction
345 init from( src ) do super
346 end
347 class ASubInstruction
348 super AArithmeticInstruction
349 init from( src ) do super
350 end
351 class AAndInstruction
352 super AArithmeticInstruction
353 init from( src ) do super
354 end
355 class AOrInstruction
356 super AArithmeticInstruction
357 init from( src ) do super
358 end
359
360 class ACpInstruction
361 super ARichBinaryInstruction
362 init from( src ) do super
363 end
364
365
366 abstract class ALoadInstruction
367 super ARichBinaryInstruction
368 init from( src ) do super
369 end
370 class ALdInstruction
371 super ALoadInstruction
372 init from( src ) do super
373 end
374 class ALdbyteInstruction
375 super ALoadInstruction
376 init from( src ) do super
377 end
378
379 abstract class AStoreInstruction
380 super ARichBinaryInstruction
381 init from( src ) do super
382 end
383 class AStInstruction
384 super AStoreInstruction
385 init from( src ) do super
386 end
387 class AStbyteInstruction
388 super AStoreInstruction
389 init from( src ) do super
390 end