-class ExpanderMixin:
- '''Mixin class that provides a bunch of methods to expand C expressions
- from the specifications.'''
-
- __structs = None
- __indices = None
-
- def expand(self, expr):
- # Expand a C expression, replacing certain variables
- if not isinstance(expr, basestring):
- return expr
- variables = {}
-
- if self.__structs is not None:
- variables['self'] = '(%s)' % self.__structs[0]
- if self.__indices is not None:
- variables['i'] = self.__indices[0]
-
- expandedExpr = expr.format(**variables)
- if expandedExpr != expr and 0:
- sys.stderr.write(" %r -> %r\n" % (expr, expandedExpr))
- return expandedExpr
-
- def visitMember(self, structInstance, member_type, *args, **kwargs):
- self.__structs = (structInstance, self.__structs)
- try:
- return self.visit(member_type, *args, **kwargs)
- finally:
- _, self.__structs = self.__structs
-
- def visitElement(self, element_index, element_type, *args, **kwargs):
- self.__indices = (element_index, self.__indices)
- try:
- return self.visit(element_type, *args, **kwargs)
- finally:
- _, self.__indices = self.__indices
-
-