Beispiel #1
0
    def generate(self, env):
        marking_type = env.marking_type
        config = marking_type.config

        vp = VariableProvider()
        self_var = vp.new_variable(env.marking_type.type, 'self')
        other_var = vp.new_variable(env.marking_type.type, 'other')

        function = pyast.FunctionDef(name='__eq__',
                                     args=pyast.A(self_var.name).param(
                                         other_var.name).ast())
        return_str = "return ("
        for i, (name,
                place_type) in enumerate(marking_type.place_types.iteritems()):
            if name == GENERATOR_PLACE and config.normalize_pids:
                continue

            id_name = place_type.field
            if i > 0:
                return_str += " and "
            return_str += "(%s == %s)" % (id_name.access_from(self_var),
                                          id_name.access_from(other_var))
        return_str += ")"

        function.body = [pyast.E(return_str)]
        return function
Beispiel #2
0
    def generate(self, env):
        marking_type = env.marking_type

        vp = VariableProvider()
        self_var = vp.new_variable(marking_type.type, name='self')
        tree_var = vp.new_variable(TypeInfo.get('AnyType'), name='tree')
        token_var = vp.new_variable(TypeInfo.get('AnyType'), name='token')
        pid_dict_var = vp.new_variable(TypeInfo.get('Dict'), name='pid_dict')

        tmp_pid_var = vp.new_variable(TypeInfo.get('Pid'), name='tpid')
        tmp_marking_var = vp.new_variable(TypeInfo.get('Marking'), name='tmkr')

        function = pyast.FunctionDef(name='buildPidTree',
                                     args=pyast.A(self_var.name).ast())
        body = []
        body.append(
            pyast.E("{} = defaultdict(Marking)".format(pid_dict_var.name)))
        body.append(pyast.E("{} = PidTree(0)".format(tree_var.name)))
        # build the tree
        for name, place_type in marking_type.place_types.iteritems():
            if not place_type.allow_pids:
                continue

            if name == GENERATOR_PLACE:
                enum_body = [
                    pyast.If(test=pyast.E('not {}.has_key({}[0])'.format(
                        pid_dict_var.name, token_var.name)),
                             body=[
                                 pyast.E("{}[ {}[0] ] = Marking(True)".format(
                                     pid_dict_var.name, token_var.name))
                             ]),
                    pyast.
                    E("{}[ Pid.from_list({}[0].data + [{}[1] + 1]) ] = 'next_pid'"
                      .format(pid_dict_var.name, token_var.name,
                              token_var.name))
                ]

                body.append(
                    place_type.enumerate(env, self_var, token_var, enum_body))
            else:
                body.append(
                    place_type.extract_pids(env, self_var, pid_dict_var))

        # body.append(pyast.E("print {}".format(pid_dict_var.name)))
        body.append(
            pyast.For(target=pyast.E('{}, {}'.format(tmp_pid_var.name,
                                                     tmp_marking_var.name)),
                      iter=pyast.E(
                          ('{}.iteritems()'.format(pid_dict_var.name))),
                      body=[
                          pyast.stmt(
                              pyast.E('{}.add_marking({}, {})'.format(
                                  tree_var.name, tmp_pid_var.name,
                                  tmp_marking_var.name)))
                      ]))
        body.append(pyast.E("return {}".format(tree_var.name)))
        function.body = body
        return function
Beispiel #3
0
    def generate(self, env):
        marking_type = env.marking_type

        function = pyast.FunctionDef(name = '__init__',
                                     args = pyast.A('self').param('alloc', default = 'True').ast())

        vp = VariableProvider()
        self_var = vp.new_variable(marking_type.type, name = 'self')

        if_block = pyast.If(test = pyast.Name(id = 'alloc'))

        for place_type in marking_type.place_types.values():
            if_block.body.append(place_type.new_place_stmt(env, self_var))

        function.body = [ pyast.E('self.{} = None'.format(marking_type.get_field('_hash').name)), if_block ]
        return function
Beispiel #4
0
    def generate(self, env):
        marking_type = env.marking_type

        vp = VariableProvider()
        self_var = vp.new_variable(marking_type.type, name = 'self')
        marking_var = vp.new_variable(marking_type.type)

        function = pyast.FunctionDef(name = 'copy',
                                     args = pyast.A(self_var.name).param('alloc', default = 'True').ast())

        tmp = [ pyast.Assign(targets = [pyast.Name(id = marking_var.name)],
                             value = pyast.E('Marking(False)')) ]

        for place_type in marking_type.place_types.values():
            tmp.append(place_type.copy_stmt(env, marking_var, self_var))

        tmp.append(pyast.Return(pyast.Name(id = marking_var.name)))
        function.body = tmp
        return function
Beispiel #5
0
    def generate(self, env):
        marking_type = env.marking_type

        vp = VariableProvider()
        self_var = vp.new_variable(marking_type.type, 'self')
        other_var = vp.new_variable(marking_type.type, 'other')

        function = pyast.FunctionDef(name = '__eq__', args = pyast.A(self_var.name).param(other_var.name).ast())

        return_str = "return ("
        for i, place_type in enumerate(marking_type.place_types.values()):
            if i > 0:
                return_str += " and "
            field = place_type.field
            return_str += "({} == {})".format(field.access_from(self_var),
                                              field.access_from(other_var))
        return_str += ")"

        function.body = [ pyast.E(return_str) ]
        return function
Beispiel #6
0
    def generate(self, env):
        marking_type = env.marking_type

        vp = VariableProvider()
        self_var = vp.new_variable(marking_type.type, name='self')
        new_pid_dict_var = vp.new_variable(marking_type.type,
                                           name='new_pid_dict')

        function = pyast.FunctionDef(name='update_pids',
                                     args=pyast.A(self_var.name).param(
                                         new_pid_dict_var.name).ast())

        body = []
        for name, place_type in marking_type.place_types.iteritems():
            print place_type
            if not place_type.allow_pids:
                continue
            if name == GENERATOR_PLACE:
                body.append(
                    pyast.Assign(
                        targets=[place_type.place_expr(env, self_var)],
                        value=pyast.Call(func=pyast.Name(
                            stubs['generator_place_update_pids']),
                                         args=[
                                             place_type.place_expr(
                                                 env, self_var),
                                             pyast.Name(new_pid_dict_var.name)
                                         ])))
            else:
                body.append(
                    place_type.update_pids_stmt(env, self_var,
                                                new_pid_dict_var))

        if not body:
            body = [pyast.Pass()]

        function.body = body
        return function