Beispiel #1
0
def gen_check_function(checker_env, identifier, atom):

    marking_type = checker_env.marking_type

    variable_provider = VariableProvider()
    checker_env.push_cvar_env()
    checker_env.push_variable_provider(variable_provider)

    builder = cyast.Builder()
    marking_var = variable_provider.new_variable(
        variable_type=marking_type.type)

    function_name = "check_{}".format(identifier)
    builder.begin_FunctionCDef(name=function_name,
                               args=cyast.A(marking_var.name,
                                            type=checker_env.type2str(
                                                marking_var.type)),
                               returns=cyast.Name("int"),
                               decl=[],
                               public=False,
                               api=False)

    formula = atom.formula
    builder.emit(
        cyast.Return(gen_check_expression(checker_env, marking_var, formula)))

    builder.end_FunctionDef()
    tree = cyast.to_ast(builder)
    tree = flatten_ast(tree)

    checker_env.register_check_function(function_name,
                                        FunctionWrapper(function_name, tree))
    return tree
Beispiel #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
 def succ_function_args(self, node):
     return (cyast.A(
         node.arg_marking_var.name,
         type=self.env.type2str(node.arg_marking_var.type)).param(
             node.arg_marking_acc_var.name,
             type=self.env.type2str(node.arg_marking_acc_var.type)).param(
                 node.arg_ctx_var.name,
                 type=self.env.type2str(node.arg_ctx_var.type)))
Beispiel #4
0
def gen_main_check_function(checker_env, id_prop_map):

    function_name = "neco_check"
    builder = cyast.Builder()
    variable_provider = VariableProvider()

    checker_env.push_cvar_env()
    checker_env.push_variable_provider(variable_provider)

    marking_var = variable_provider.new_variable(
        variable_type=checker_env.marking_type.type)
    atom_var = variable_provider.new_variable(
        variable_type=TypeInfo.get('Int'))

    builder.begin_FunctionCDef(
        name=function_name,
        args=(cyast.A(marking_var.name,
                      type=checker_env.type2str(marking_var.type)).param(
                          atom_var.name,
                          type=checker_env.type2str(TypeInfo.get('Int')))),
        returns=cyast.Name("int"),
        decl=[],
        public=True,
        api=True)

    for (i, (ident, _)) in enumerate(id_prop_map.iteritems()):
        if i == 0:
            builder.begin_If(
                test=cyast.Compare(left=cyast.Name(atom_var.name),
                                   ops=[cyast.Eq()],
                                   comparators=[cyast.Num(ident)]))
        else:
            builder.begin_Elif(
                test=cyast.Compare(left=cyast.Name(atom_var.name),
                                   ops=[cyast.Eq()],
                                   comparators=[cyast.Num(ident)]))

        builder.emit_Return(
            checker_env.get_check_function("check_{}".format(ident)).call(
                [cyast.Name(marking_var.name)]))

    for _ in id_prop_map:
        builder.end_If()

    builder.emit(
        cyast.Print(dest=cyast.E('sys.stderr'),
                    values=[
                        cyast.Str(s='!W! invalid proposition identifier'),
                        cyast.Name(atom_var.name)
                    ],
                    nl=True))
    builder.emit_Return(cyast.Num(n=0))

    builder.end_FunctionDef()
    tree = cyast.to_ast(builder)
    checker_env.register_check_function(function_name,
                                        FunctionWrapper(function_name, tree))
    return tree
    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]
Beispiel #6
0
def gen_All_function(checker_env, function_name, place_name,
                     arg_function_name):
    marking_type = checker_env.marking_type
    variable_provider = VariableProvider()

    checker_env.push_cvar_env()
    checker_env.push_variable_provider(variable_provider)

    builder = cyast.Builder()
    marking_var = variable_provider.new_variable(
        variable_type=marking_type.type)

    place_type = marking_type.get_place_type_by_name(place_name)
    token_var = variable_provider.new_variable(
        variable_type=place_type.token_type)
    # check_var = variable_provider.new_variable(variable_type=TypeInfo.Int)

    builder.begin_FunctionCDef(name=function_name,
                               args=(cyast.A(marking_var.name,
                                             type=checker_env.type2str(
                                                 marking_var.type))),
                               returns=cyast.Name("int"),
                               decl=[],
                               public=False,
                               api=False)

    main_body = []

    loop_var = variable_provider.new_variable(
        variable_type=place_type.token_type)
    inner_body = cyast.If(
        cyast.UnaryOp(
            cyast.Not(),
            cyast.Name(arg_function_name + '(' + loop_var.name + ')')),
        [cyast.Return(cyast.Num(0))])
    node = place_type.enumerate_tokens(checker_env,
                                       loop_var,
                                       marking_var,
                                       body=inner_body)

    main_body.append(node)
    main_body.append(cyast.Return(value=cyast.Num(1)))

    for stmt in main_body:
        builder.emit(stmt)

    builder.end_FunctionDef()
    return cyast.to_ast(builder)
    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)