CodeGeneration.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. from __future__ import absolute_import
  2. from .Visitor import VisitorTransform
  3. from .Nodes import StatListNode
  4. class ExtractPxdCode(VisitorTransform):
  5. """
  6. Finds nodes in a pxd file that should generate code, and
  7. returns them in a StatListNode.
  8. The result is a tuple (StatListNode, ModuleScope), i.e.
  9. everything that is needed from the pxd after it is processed.
  10. A purer approach would be to separately compile the pxd code,
  11. but the result would have to be slightly more sophisticated
  12. than pure strings (functions + wanted interned strings +
  13. wanted utility code + wanted cached objects) so for now this
  14. approach is taken.
  15. """
  16. def __call__(self, root):
  17. self.funcs = []
  18. self.visitchildren(root)
  19. return (StatListNode(root.pos, stats=self.funcs), root.scope)
  20. def visit_FuncDefNode(self, node):
  21. self.funcs.append(node)
  22. # Do not visit children, nested funcdefnodes will
  23. # also be moved by this action...
  24. return node
  25. def visit_Node(self, node):
  26. self.visitchildren(node)
  27. return node