Ejemplo n.º 1
0
    def compile_TokenEnumeration(self, node):
        marking_type = self.env.marking_type
        place_type = marking_type.get_place_type_by_name(node.place_name)

        if hasattr(place_type, 'enumerate'):
            return place_type.enumerate(self.env,
                                        node.marking_var, node.token_var,
                                        self.compile(node.body))

        arc = node.arc

        if place_type.provides_by_index_access:
            index_var = arc.data['index']
            size_var = self.env.variable_provider.new_variable()

            self.env.try_declare_cvar(index_var.name, TypeInfo.get('Int'))
            self.env.try_declare_cvar(node.token_var.name, node.token_var.type)
            self.env.try_declare_cvar(size_var.name, TypeInfo.get('Int'))

            place_size = place_type.get_size_expr(env=self.env,
                                                  marking_var=node.marking_var)

            get_token = place_type.get_token_expr(env=self.env,
                                                  index_expr=index_var,
                                                  marking_var=node.marking_var,
                                                  compiled_index=cyast.Name(
                                                      index_var.name))

            return [
                cyast.Assign(targets=[cyast.Name(size_var.name)],
                             value=place_size),
                cyast.Builder.CFor(
                    start=cyast.Num(0),
                    start_op=cyast.LtE(),
                    target=cyast.Name(index_var.name),
                    stop_op=cyast.Lt(),
                    stop=cyast.Name(size_var.name),
                    body=[
                        cyast.Assign(targets=[cyast.Name(node.token_var.name)],
                                     value=get_token),
                        self.compile(node.body)
                    ],
                    orelse=[])
            ]
        else:
            self.env.try_declare_cvar(node.token_var.name, node.token_var.type)
            place_type = marking_type.get_place_type_by_name(node.place_name)
            return cyast.Builder.For(target=cyast.Name(node.token_var.name),
                                     iter=place_type.iterable_expr(
                                         env=self.env,
                                         marking_var=node.marking_var),
                                     body=[self.compile(node.body)])
Ejemplo n.º 2
0
    def compile_Init(self, node):
        env = self.env
        env.push_cvar_env()

        new_marking = cyast.Assign(
            targets=[cyast.Name(node.marking_var.name)],
            value=self.env.marking_type.new_marking_expr(self.env))
        return_stmt = cyast.E("return {}".format(node.marking_var.name))

        stmts = [new_marking]
        stmts.extend(self.compile(node.body))
        stmts.append(return_stmt)

        decl = CVarSet()
        decl.extend([
            cyast.CVar(node.marking_var.name,
                       self.env.type2str(node.marking_var.type))
        ])

        additionnal_decls = self.env.pop_cvar_env()
        decl.extend(additionnal_decls)

        f1 = cyast.Builder.FunctionDef(name=node.function_name,
                                       body=stmts,
                                       returns=cyast.Name("Marking"),
                                       decl=decl)

        f2 = cyast.Builder.FunctionCDef(name="neco_init",
                                        body=stmts,
                                        returns=cyast.Name("Marking"),
                                        decl=decl)

        return [f1, f2]
Ejemplo n.º 3
0
 def compile_FlushIn(self, node):
     destination_place = self.env.marking_type.get_place_type_by_name(
         node.place_name)
     return [
         cyast.Assign(targets=[cyast.Name(node.token_var.name)],
                      value=destination_place.attribute_expr(
                          self.env, node.marking_var))
     ]
Ejemplo n.º 4
0
    def gen_copy(self, env, src_marking, dst_marking, modified_places):
        """

        @param modified_places:
        @type modified_places: C{}
        """
        nodes = []
        nodes.append(cyast.E(dst_marking.name + " = Marking()"))

        copy_attributes = set()
        assign_attributes = set()

        for place_type in self.place_types.itervalues():
            if place_type.info in modified_places:
                copy_attributes.add(place_type.get_attribute_name())
            else:
                assign_attributes.add(place_type.get_attribute_name())

        # a place in copy from a pack forces the copy of the whole pack
        assign_attributes = assign_attributes - copy_attributes

        copied = set()
        # copy packed
        if self.chunk_manager.packed_bits() > 0:
            attr_name, _, count = self.chunk_manager.packed_attribute()
            copied.add(attr_name)
            for i in range(count):
                target_expr = cyast.E('{object}.{attribute}[{index!s}]'.format(
                    object=dst_marking.name, attribute=attr_name, index=i))
                value_expr = cyast.E('{object}.{attribute}[{index!s}]'.format(
                    object=src_marking.name, attribute=attr_name, index=i))
                nodes.append(
                    cyast.Assign(targets=[target_expr], value=value_expr))

        # copy modified attributes
        for place_type in self.place_types.itervalues():
            attr_name = place_type.get_attribute_name()
            if attr_name in copied:
                continue

            if attr_name in copy_attributes:
                nodes.append(
                    place_type.copy_stmt(env, dst_marking, src_marking))
                nodes.append(
                    cyast.Comment('copy: {} {!s}'.format(
                        place_type.info.name, place_type.info.type)))

            elif attr_name in assign_attributes:
                nodes.append(
                    place_type.light_copy_stmt(env, dst_marking, src_marking))
                nodes.append(
                    cyast.Comment('assign: {} {!s}'.format(
                        place_type.info.name, place_type.info.type)))
            copied.add(attr_name)

        return cyast.to_ast(nodes)
Ejemplo n.º 5
0
 def compile_InitGeneratorPlace(self, node):
     marking_type = self.env.marking_type
     generator_place = marking_type.get_place_type_by_name(GENERATOR_PLACE)
     initial_pid_var = self.env.variable_provider.new_variable(
         variable_type=TypeInfo.get('Pid'))
     self.env.try_declare_cvar(initial_pid_var.name, initial_pid_var.type)
     assign = cyast.Assign(targets=[cyast.E(initial_pid_var.name)],
                           value=self.compile_InitialPid(None))
     return [
         assign,
         generator_place.add_pid_stmt(self.env, initial_pid_var,
                                      node.marking_var)
     ]
Ejemplo n.º 6
0
    def compile_Match(self, node):
        tuple_info = node.tuple_info
        seq = []

        component_names = [
            token_info.data['local_variable'].name for token_info in tuple_info
        ]
        seq.append(
            cyast.Assign(targets=[
                cyast.Tuple([cyast.E(name) for name in component_names])
            ],
                         value=cyast.Name(
                             tuple_info.data['local_variable'].name)))
        cur = None
        for component in tuple_info.components:
            if component.is_Value:
                # self.try_declare_cvar(component.data['local_variable'].name, component.type)
                n = cyast.Builder.If(
                    test=cyast.Builder.Compare(
                        left=cyast.E(component.data['local_variable'].name),
                        ops=[cyast.Eq()],
                        comparators=[cyast.E(repr(component.raw))
                                     ]),  # TO DO unify value & pickle
                    orelse=[])
                if cur == None:
                    cur = n
                    seq.append(n)
                else:
                    cur.body = [n]
                    cur = n
            elif component.is_Variable:
                self.env.try_declare_cvar(
                    component.data['local_variable'].name, component.type)

        if cur != None:
            cur.body = [self.compile(node.body)]
        else:
            seq.append(self.compile(node.body))

        return seq
Ejemplo n.º 7
0
    def compile_MultiTokenEnumeration(self, node):
        place_type = self.env.marking_type.get_place_type_by_name(
            node.place_name)

        base = None
        current = None
        if place_type.provides_by_index_access:
            for sub_arc in node.multiarc.sub_arcs:
                variable = sub_arc.data['local_variable']
                index_var = sub_arc.data['index']

                self.env.try_declare_cvar(index_var.name, TypeInfo.get('Int'))
                self.env.try_declare_cvar(variable.name, place_type.token_type)

                assign = cyast.Assign(targets=[cyast.Name(variable.name)],
                                      value=place_type.get_token_expr(
                                          self.env,
                                          index_expr=index_var,
                                          marking_var=node.marking_var,
                                          compiled_index=cyast.Name(
                                              index_var.name)))
                enumeration = cyast.For(target=cyast.Name(index_var.name),
                                        iter=cyast.Call(
                                            func=cyast.Name('range'),
                                            args=[
                                                cyast.Num(0),
                                                place_type.get_size_expr(
                                                    self.env, node.marking_var)
                                            ]),
                                        body=[assign])
                if base == None:
                    current = enumeration
                    base = enumeration
                else:
                    current.body.append(enumeration)
                    current = enumeration

        else:  # no index access
            for sub_arc in node.multiarc.sub_arcs:
                variable = sub_arc.data['local_variable']
                index_var = sub_arc.data['index']
                init = cyast.Assign(targets=[cyast.Name(index_var.name)],
                                    value=cyast.Num(0))

                self.env.try_declare_cvar(index_var.name, TypeInfo.get('Int'))
                self.env.try_declare_cvar(variable.name, place_type.token_type)

                enumeration = cyast.For(target=cyast.Name(variable.name),
                                        iter=place_type.iterable_expr(
                                            env=self.env,
                                            marking_var=node.marking_var),
                                        body=[
                                            cyast.AugAssign(target=cyast.Name(
                                                index_var.name),
                                                            op=cyast.Add(),
                                                            value=cyast.Num(1))
                                        ])
                if base == None:
                    current = [init, enumeration]
                    base = [init, enumeration]
                else:
                    current[1].body.append([init, enumeration])
                    current = [init, enumeration]

        indices = [sub_arc.data['index'] for sub_arc in node.multiarc.sub_arcs]
        inner_base, inner = self.gen_different(indices)
        if isinstance(current, list):
            current[1].body.append(inner_base)
        else:
            current.body.append(inner_base)
        current = inner

        current.body.extend([self.compile(node.body)])

        return base
Ejemplo n.º 8
0
    def compile_Assign(self, node):
        self.env.try_declare_cvar(node.variable.name, node.variable.type)

        return cyast.Assign(targets=[cyast.Name(node.variable.name)],
                            value=self.compile(node.expr))