class KlassVisitor(ASTVisitor): """ An AST visitor for classes. """ ########################################################################### # 'object' interface. ########################################################################### def __init__(self, klass): """ Creates a new visitor. """ self.klass = klass # Factories used to create klasses, functions and assignments from # AST nodes. self._function_factory = FunctionFactory() self._assign_factory = AssignFactory() return ########################################################################### # 'ASTVisitor' interface. ########################################################################### def visitFunction(self, node): """ Visits a function node. """ function = self._function_factory.from_ast(self.klass, node) self.klass.locals[node.name] = function self.klass.methods[node.name] = function return def visitAssign(self, node): """ Visits an assignment node. """ print 'visiting assign!!!', node assign = self._assign_factory.from_ast(self.klass, node) # Does the assigment look like it *might* be a trait? (i.e., it is NOT # an expression or a constant etc.). if len(assign.source) > 0: assign.is_trait = self.klass.is_trait(assign.source) else: assign.is_trait = False for target in assign.targets: self.klass.locals[target] = assign self.klass._is_trait[target] = assign.is_trait if assign.is_trait: self.klass.traits[target] = assign else: self.klass.attributes[target] = assign return
def __init__(self, klass): """ Creates a new visitor. """ self.klass = klass # Factories used to create klasses, functions and assignments from # AST nodes. self._function_factory = FunctionFactory() self._assign_factory = AssignFactory() return