예제 #1
0
파일: generator.py 프로젝트: hdeweirdt/imp
    def evaluate(self, state, local_scope):
        """
            Evaluate this statement. 
        """
        var = state.get_ref("variable").value
        
        for loop_var in var:
            # generate a subscope/namespace for each loop
            object_id = Scope.object_to_name(loop_var)
            namespace = Namespace(object_id, state.namespace)
            
            sub_scope = Scope.get_or_create_scope(state.graph,
                                                  namespace.to_path())

            # add the loop variable to the scope
            if not isinstance(loop_var, Variable):
                loop_var = Variable(loop_var)
            
            sub_scope.add_variable(self.loop_var, loop_var)
            
            # generate the import statement
            import_stmt = Import(self.module_name)
            import_stmt.namespace = namespace
            import_stmt.child_namespace = False
            
            child_state = DynamicState(state.compiler, namespace, import_stmt)
            child_state.add_to_graph(state.graph)
            state._child_statements[import_stmt] = child_state
예제 #2
0
파일: generator.py 프로젝트: hdeweirdt/imp
 def evaluate(self, state, local_scope):
     """ 
         Evaluate this statement.
     """
     ctor_id = Scope.object_to_name(state)
     
     # the type to construct
     type_class = state.get_type("classtype")
     if isinstance(type_class, Default):
         type_class = type_class.get_entity()
     
     object_instance = type_class.get_instance(ctor_id, local_scope)
     object_instance.__statement__ = state
     
     try:
         local_scope.get_variable("self").value.add_child(object_instance)
     except NotFoundException:
         pass
     
     if self.implemented:
         # generate an import for the module
         name = Reference(hex(id(self)))
         stmt = Import(name)
         stmt.namespace = self.namespace
         self.copy_location(stmt)
         stmt.child_namespace = False
             
         state.add_statement(stmt, child_ns = True)
             
     else:
         # generate an implementation
         stmt = Implement(type_class, object_instance)
         self.copy_location(stmt)
         stmt.namespace = self.namespace
         state.add_statement(stmt, child_ns = True)
     
         
     if self.register:
         object_name = str(hex(id(object_instance)))
         local_scope.add_variable(object_name, Variable(object_instance))
     
     return object_instance
예제 #3
0
파일: generator.py 프로젝트: hdeweirdt/imp
 def evaluate(self, state, local_scope):
     """
         Evaluate this statement
     """
     implement_list = self.get_implementation(state, local_scope)
     
     if len(implement_list) == 0:
         raise Exception("Unable to select implementation for entity %s" % 
                         self.instance_type.name)
     
     implementations = []
     for impl in implement_list:
         implementations.extend(impl.implementations)
     
     for impl in implementations:
         # generate a subscope/namespace for each loop
         object_id = Scope.object_to_name(impl)
         namespace = Namespace(object_id, state.namespace)
         
         for stmt in impl.statements:
             child_state = DynamicState(state.compiler, namespace, stmt)
             child_state.add_to_graph(state.graph)
             state._child_statements[stmt] = child_state    
예제 #4
0
파일: state.py 프로젝트: hdeweirdt/imp
 def add_statement(self, statement, child_ns = False):
     """
         Add a new statement that is generated or included by the statement
         linked to this state. The new statement is defined in the 
         same namespace as this statement or in a child namespace.
         
         @param statement: The new statement
         @param child_ns: Optional can be set to true to define it in a child
             namespace.
     """
     if statement in self._child_statements:
         raise Exception("Statement %s already added by %s" % 
                         (statement, self))
     
     namespace = self.namespace
     if child_ns:
         object_id = Scope.object_to_name(self)
         namespace = Namespace(object_id, self.namespace)
         
     child_state = DynamicState(self.compiler, namespace, statement)
     child_state.add_to_graph(self.graph)
     self._child_statements[statement] = child_state
     
     return child_state
예제 #5
0
파일: generator.py 프로젝트: hdeweirdt/imp
    def new_statements(self, state):
        """
            Add any arguments that need to be validated to the graph
        """
        attributes = set()
        
        # Set the value from the constructor
        object_ref = state.get_result_reference()
        for name, value in self.__attributes.items():
            # set the attributes passed with the constructor
            stmt = SetAttribute(object_ref, name, value)
            self.copy_location(stmt)
            stmt.namespace = self.namespace
            state.add_statement(stmt)
            
            attributes.add(name)
            
        # Set values defined in default constructors
        type_class = state.get_type("classtype")
        if isinstance(type_class, Default):
            default = type_class
            type_class = type_class.get_entity()
            
            # set default values
            for attribute_name in type_class.get_all_attribute_names():
                attribute = type_class.get_attribute(attribute_name)
                
                if attribute.name not in attributes:
                    try:
                        value = default.get_default(attribute.name)
                        stmt = SetAttribute(object_ref, attribute.name, value)
                        self.copy_location(stmt)
                        stmt.namespace = self.namespace
                        state.add_statement(stmt)
                        
                        attributes.add(attribute.name)
                    except AttributeError:
                        pass
                    
        # Set default values if they have not been set yet
        for name, value in type_class.get_default_values().items():
            if name not in attributes and value is not None:
                stmt = SetAttribute(object_ref, name, value)
                self.copy_location(stmt)
                stmt.namespace = self.namespace
                state.add_statement(stmt)
                
        # Make values of attributes available in subscopes by defining
        # variables with matching names in the subscope
        object_id = Scope.object_to_name(state)
        namespace = Namespace(object_id, state.namespace)
        scope = Scope.get_or_create_scope(state.graph, namespace.to_path())

        added_variables = set()
        for attribute in type_class.get_all_attribute_names():
            if attribute in added_variables:
                continue
            
            var = AttributeVariable.create(object_ref, attribute)
            self.copy_location(var)
            added_variables.add(attribute)
            scope.add_variable(attribute, var)
            
            # Set a attributes with low multiplicity == 0 -> set []
            attribute_obj = type_class.get_attribute(attribute)
            if hasattr(attribute_obj, "low") and attribute_obj.low == 0:
                value = Variable(QList())
                stmt = SetAttribute(object_ref, attribute_obj.name, value)
                self.copy_location(stmt)
                stmt.namespace = self.namespace
                state.add_statement(stmt)
            
        # set the self variable
        scope.add_variable("self", object_ref)
        
        # now check that all variables that have indexes on them, are already
        # defined and add the instance to the index
        for index in type_class._index_def:
            for attr in index:
                if attr not in attributes:
                    raise Exception("%s is part of an index and should be set in the constructor." % attr)