PageRenderTime 706ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/sandbox/pycon2009/MyUglyPrinter.py

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