PageRenderTime 30ms CodeModel.GetById 21ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/sandbox/pycon2009/MyUglyPrinter.py

https://github.com/tylergreen/mython
Python | 357 lines | 335 code | 8 blank | 14 comment | 2 complexity | 43e4d0c6d08194ee46e237a15b1f9450 MD5 | raw file
  1#! /usr/bin/env python
  2# ______________________________________________________________________
  3"""Module MyUglyPrinter
  4
  5Defines a not-so-pretty printer for Python abstract syntax."""
  6# ______________________________________________________________________
  7# Module imports
  8
  9from basil.lang.mython.mybuiltins import myfrontend
 10from basil.lang.mython.MyCodeGen import ASTHandler
 11from basil.lang.mython import myfront_ast
 12
 13# ______________________________________________________________________
 14# Class definition(s)
 15
 16# The methods for these were generated in part using:
 17
 18# >>> from basil.lang.mython import ASTUtils, myfront_ast
 19# >>> ASTUtils.gen_py_handlers(myfront_ast)
 20
 21# ...and then some copy/replace.  An example of using one of these is
 22# in .../basil/lang/mython/MyCodeGen.py
 23
 24class MyUglyPrinter (ASTHandler):
 25
 26
 27    def handle_children(self, node):
 28        v = [self.handle(child) for child in self.get_children(node) ]
 29        print v
 30        return "".join(v)
 31
 32    def handle_list(self, node):
 33        v = [self.handle(child) for child in node ]
 34        print v
 35        return "".join(v)
 36
 37    handle_tuple = handle_list
 38
 39    def handle_Add (self, node):
 40        return ""
 41
 42    def handle_And (self, node):
 43        return ""
 44
 45    def handle_Assert (self, node):
 46        return ""
 47
 48    def handle_Assign (self, node):
 49        return ""
 50
 51    def handle_Attribute (self, node):
 52        return ""
 53
 54    def handle_AugAssign (self, node):
 55        return ""
 56
 57    def handle_AugLoad (self, node):
 58        return ""
 59
 60    def handle_AugStore (self, node):
 61        return ""
 62
 63    def handle_BinOp (self, node):
 64        return ""
 65
 66    def handle_BitAnd (self, node):
 67        return ""
 68
 69    def handle_BitOr (self, node):
 70        return ""
 71
 72    def handle_BitXor (self, node):
 73        return ""
 74
 75    def handle_BoolOp (self, node):
 76        return ""
 77
 78    def handle_Break (self, node):
 79        return ""
 80
 81    def handle_Call (self, node):
 82        return ""
 83
 84    def handle_ClassDef (self, node):
 85        return ""
 86
 87    def handle_Compare (self, node):
 88        return ""
 89
 90    def handle_Continue (self, node):
 91        return ""
 92
 93    def handle_Del (self, node):
 94        return ""
 95
 96    def handle_Delete (self, node):
 97        return ""
 98
 99    def handle_Dict (self, node):
100        return ""
101
102    def handle_Div (self, node):
103        return ""
104
105    def handle_Ellipsis (self, node):
106        return ""
107
108    def handle_Eq (self, node):
109        return ""
110
111    def handle_Exec (self, node):
112        return ""
113
114    def handle_Expr (self, node):
115        return '%s\n' % self.handle_children(node)
116    
117    def handle_Expression (self, node):
118        return ""
119
120    def handle_ExtSlice (self, node):
121        return ""
122
123    def handle_FloorDiv (self, node):
124        return ""
125
126    def handle_For (self, node):
127        return ""
128
129    def handle_FunctionDef (self, node):
130        return ""
131
132    def handle_GeneratorExp (self, node):
133        return ""
134
135    def handle_Global (self, node):
136        return ""
137
138    def handle_Gt (self, node):
139        return ""
140
141    def handle_GtE (self, node):
142        return ""
143
144    def handle_If (self, node):
145        return ""
146
147    def handle_IfExp (self, node):
148        return ""
149
150    def handle_Import (self, node):
151        return ""
152
153    def handle_ImportFrom (self, node):
154        return ""
155
156    def handle_In (self, node):
157        return ""
158
159    def handle_Index (self, node):
160        return ""
161
162    def handle_Interactive (self, node):
163        return ""
164
165    def handle_Invert (self, node):
166        return ""
167
168    def handle_Is (self, node):
169        return ""
170
171    def handle_IsNot (self, node):
172        return ""
173
174    def handle_LShift (self, node):
175        return ""
176
177    def handle_Lambda (self, node):
178        return ""
179
180    def handle_List (self, node):
181        return ""
182
183    def handle_ListComp (self, node):
184        return ""
185
186    def handle_Load (self, node):
187        return ""
188
189    def handle_Lt (self, node):
190        return ""
191
192    def handle_LtE (self, node):
193        return ""
194
195    def handle_Mod (self, node):
196        return ""
197
198    def _handle_Module (self, node):
199        return ""
200
201    def handle_Mult (self, node):
202        return ""
203
204    def handle_Name (self, node):
205        return node.id
206
207    def handle_Not (self, node):
208        return ""
209
210    def handle_NotEq (self, node):
211        return ""
212
213    def handle_NotIn (self, node):
214        return ""
215
216    def handle_Num (self, node):
217        return ""
218
219    def handle_Or (self, node):
220        return ""
221
222    def handle_Param (self, node):
223        return ""
224
225    def handle_Pass (self, node):
226        return ""
227
228    def handle_Pow (self, node):
229        return ""
230
231    def handle_Print (self, node):
232        return ""
233
234    def handle_QuoteDef (self, node):
235        return ""
236
237    def handle_RShift (self, node):
238        return ""
239
240    def handle_Raise (self, node):
241        return ""
242
243    def handle_Repr (self, node):
244        return ""
245
246    def handle_Return (self, node):
247        return ""
248
249    def handle_Slice (self, node):
250        return ""
251
252    def handle_Store (self, node):
253        return ""
254
255    def handle_Str (self, node):
256        return ""
257
258    def handle_Sub (self, node):
259        return ""
260
261    def handle_Subscript (self, node):
262        return ""
263
264    def handle_Suite (self, node):
265        return ""
266
267    def handle_TryExcept (self, node):
268        return ""
269
270    def handle_TryFinally (self, node):
271        return ""
272
273    def handle_Tuple (self, node):
274        return ""
275
276    def handle_UAdd (self, node):
277        return ""
278
279    def handle_USub (self, node):
280        return ""
281
282    def handle_UnaryOp (self, node):
283        return ""
284
285    def handle_While (self, node):
286        return ""
287
288    def handle_With (self, node):
289        return ""
290
291    def handle_Yield (self, node):
292        return ""
293
294    def handle_alias (self, node):
295        return ""
296
297    def handle_arguments (self, node):
298        return ""
299
300    def handle_comprehension (self, node):
301        return ""
302
303    def handle_excepthandler (self, node):
304        return ""
305
306    def handle_keyword (self, node):
307        return ""
308
309
310class MyUglierPrinter(ASTHandler):
311    def __init__(self):
312        self.level = 0
313        self.indent = "  "
314        
315    def handle_children(self,node):
316        print self.indent * self.level, type(node).__name__
317        self.level += 1
318        _val = super(type(self),self).handle_children(node)
319        self.level -= 1
320        return _val
321
322
323# ______________________________________________________________________
324# Main (self-test) routine.
325
326test_strings = [
327    "None\n",
328    ]
329
330def main (*args):
331    global test_strings
332    ugly_printer = MyUglyPrinter()    
333    uglier_printer = MyUglierPrinter()    
334    def handle_source (source, filename = "<string>"):
335        ast, env = myfrontend(source, {"filename" : filename})
336        uglier_printer.handle(ast)
337        outstring = ugly_printer.handle(ast)
338        print "outstring: ", outstring
339        return myfrontend(outstring, env)[0] == ast # T(~T(T(s))) == T(s)
340    if not args:
341        for source in test_strings:
342            print "_" * 70
343            print source, handle_source(source)
344    else:
345        for arg in args:
346            source = open(arg).read()
347            print "_" * 70
348            print source, handle_source(source, arg)
349
350# ______________________________________________________________________
351
352if __name__ == "__main__":
353    import sys
354    main(*sys.argv[1:])
355
356# ______________________________________________________________________
357# End of MyUglyPrinter.py