예제 #1
0
    def compile_Succs(self, node):
        body = []
        body.extend(self.compile(node.body))
        body.append(cyast.E("return " + node.arg_marking_acc_var.name))
        f1 = cyast.Builder.FunctionCpDef(
            name=node.function_name,
            args=self.main_succ_function_args(node),
            body=body,
            lang=cyast.CpDef(public=True),
            returns=cyast.Name("set"),
            decl=[
                cyast.CVar(name=node.arg_marking_acc_var.name,
                           type=self.env.type2str(
                               node.arg_marking_acc_var.type),
                           init=self.env.marking_set_type.new_marking_set_expr(
                               self.env))
            ])

        body = [cyast.E("l = ctypes_ext.neco_list_new()")]

        body.append(
            cyast.For(
                target=cyast.to_ast(cyast.E("e")),
                iter=cyast.to_ast(cyast.E("succs(m, ctx)")),
                body=[
                    cyast.to_ast(
                        cyast.stmt(cyast.E("ctypes_ext.__Pyx_INCREF(e)"))),
                    cyast.Expr(
                        cyast.Call(
                            func=cyast.to_ast(
                                cyast.E("ctypes_ext.neco_list_push_front")),
                            args=[cyast.to_ast(cyast.E("l")),
                                  cyast.Name("e")],
                            keywords=[],
                            starargs=None,
                            kwargs=None))
                ]))

        body.append(cyast.E("return l"))
        f2 = cyast.Builder.FunctionCDef(
            name="neco_succs",
            args=(cyast.A(
                "m", type=self.env.type2str(node.arg_marking_var.type)).param(
                    "ctx", type=self.env.type2str(node.arg_ctx_var.type))),
            body=body,
            returns=cyast.Name("ctypes_ext.neco_list_t*"),
            decl=[
                cyast.CVar(name="l", type="ctypes_ext.neco_list_t*"),
                cyast.CVar(name="e", type="Marking")
            ])

        return [f1]
예제 #2
0
    def compile_IsFireable(self, node):
        self.env.push_cvar_env()
        self.env.push_variable_provider(node.variable_provider)

        self.var_helper = node.transition_info.variable_helper

        stmts = [self.compile(node.body)]

        decl = netir.CVarSet()
        input_arcs = node.transition_info.input_arcs
        for input_arc in input_arcs:
            decl.extend(self.try_gen_type_decl(input_arc))

        inter_vars = node.transition_info.intermediary_variables
        for var in inter_vars:
            if (not var.type.is_UserType) or self.env.is_cython_type(var.type):
                decl.add(
                    cyast.CVar(name=var.name,
                               type=self.env.type2str(var.type)))

        additionnal_decls = self.env.pop_cvar_env()
        for var in additionnal_decls:
            decl.add(var)

        result = cyast.to_ast(
            cyast.Builder.FunctionDef(name=node.function_name,
                                      args=(cyast.A(
                                          node.arg_marking_var.name,
                                          type=self.env.type2str(
                                              node.arg_marking_var.type))),
                                      body=stmts,
                                      lang=cyast.CDef(public=False),
                                      returns=cyast.Name("int"),
                                      decl=decl))
        return result
예제 #3
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]
예제 #4
0
    def try_gen_type_decl(self, input_arc):
        if input_arc.is_Variable:
            variable = input_arc.variable
            place_info = input_arc.place_info
            pi_type = self.env.marking_type.get_place_type_by_name(
                place_info.name).token_type

            if (not pi_type.is_UserType) or (self.env.is_cython_type(pi_type)):
                return CVarSet([
                    cyast.CVar(name=variable.name,
                               type=self.env.type2str(pi_type))
                ])

        elif input_arc.is_Test:
            # TO DO declare variables appearing in tests
            return CVarSet()
            # inner = input_arc.inner
            # return CVarSet( [ self.try_gen_type_decl(input_arc.inner) ] )

        elif input_arc.is_MultiArc:
            varset = CVarSet()
            for arc in input_arc.sub_arcs:
                varset.extend(self.try_gen_type_decl(arc))
            return varset

        return CVarSet()
예제 #5
0
    def generate_pxd(self, env):
        cls = cyast.Builder.PublicClassCDef(name="Marking",
                                            bases=[cyast.E("object")],
                                            spec=cyast.type_name_spec(
                                                o="Marking", t="MarkingType"))

        ################################################################################
        # attributes
        ################################################################################

        if self.chunk_manager.packed_bits() > 0:
            (attr_name, attr_type,
             count) = self.chunk_manager.packed_attribute()
            cls.add_decl(
                cyast.CVar(attr_name + '[' + str(count) + ']',
                           type=env.type2str(attr_type)))

        for chunk in self.chunk_manager.normal_chunks:
            attr_name = chunk.get_attribute_name()
            attr_type = chunk.get_cython_type()

            cls.add_decl(
                cyast.CVar(name=attr_name, type=env.type2str(attr_type)))
            #   place = chunk_place_map[attr_name]
            cls.add_decl(cyast.Comment("{}".format(chunk.hint)))

        cls.add_method(
            cyast.FunctionDecl(name='copy',
                               args=cyast.to_ast(
                                   cyast.A("self",
                                           cyast.Name(env.type2str(
                                               self.type)))),
                               returns=cyast.Name(env.type2str(self.type)),
                               lang=cyast.CDef()))

        return cyast.to_ast(cls)