def generate(self, env): marking_type = env.marking_type items = list(marking_type.place_types.iteritems()) items.sort(lambda (n1, t1), (n2, t2) : cmp(n1, n2)) builder = pyast.Builder() builder.begin_FunctionDef(name = "__repr__", args = pyast.A("self").ast()) builder.emit(pyast.E('s = "hdict({"')) for (i, (place_name, place_type)) in enumerate(items): tmp = ',\n ' if i > 0 else '' builder.emit(pyast.AugAssign(target = pyast.Name(id = 's'), op = pyast.Add(), value = pyast.BinOp(left = pyast.Str(s = tmp + "'" + place_name + "' : "), op = pyast.Add(), right = pyast.E('repr(self.' + place_type.field.name + ')') ) ) ) builder.emit(pyast.E('s += "})"')) builder.emit_Return(pyast.E('s')) builder.end_FunctionDef() return builder.ast()
def generate(self, env): marking_type = env.marking_type items = list(marking_type.place_types.iteritems()) items.sort(lambda (n1, t1), (n2, t2) : cmp(n1, n2)) vp = VariableProvider() self_var = vp.new_variable(marking_type.type, name = 'self') list_var = vp.new_variable() builder = pyast.Builder() builder.begin_FunctionDef(name = "__dump__", args = pyast.A(self_var.name).ast()) builder.emit(pyast.E('%s = ["{"]' % list_var.name)) for (place_name, place_type) in items: if place_type.is_ProcessPlace: builder.emit(place_type.dump_expr(env, self_var, list_var)) else: builder.emit(pyast.stmt(pyast.Call(func = pyast.E('{}.append'.format(list_var.name)), args = [ pyast.BinOp(left = pyast.Str(s = repr(place_name) + " : "), op = pyast.Add(), right = pyast.BinOp(left = place_type.dump_expr(env, self_var), op = pyast.Add(), right = pyast.Str(', ')) ) ] ) ) ) builder.emit(pyast.stmt(pyast.E('%s.append("}")' % list_var.name))) builder.emit_Return(pyast.E('"\\n".join({})'.format(list_var.name))) builder.end_FunctionDef() return builder.ast()
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') cmp_var = vp.new_variable(marking_type.type, 'tmp') builder = pyast.Builder() builder.begin_FunctionDef(name='pid_free_compare', args=pyast.A(self_var.name).param( other_var.name).ast()) for place_type in marking_type.place_types.values(): type_info = place_type.token_type print type_info, place_type if type_info.is_Pid: builder.emit( pyast.Assign(targets=[pyast.E(cmp_var.name)], value=place_type.pid_free_compare_expr( env, self_var, other_var, [0]))) builder.begin_If(test=pyast.E('{} != 0'.format(cmp_var.name))) builder.emit_Return(pyast.E(cmp_var.name)) builder.end_If() elif type_info.has_pids: # must be tuple assert (type_info.is_TupleType) ignore = [ i for i, subtype in enumerate(type_info) if subtype.is_Pid ] # builder.emit("{!s} = {!r}".format(ig_var.name, ig)) builder.emit( pyast.Assign(targets=[pyast.E(cmp_var.name)], value=place_type.pid_free_compare_expr( env, self_var, other_var, ignore))) builder.begin_If(test=pyast.E('{} != 0'.format(cmp_var.name))) builder.emit_Return(pyast.E(cmp_var.name)) builder.end_If() else: builder.emit( pyast.Assign(targets=[pyast.E(cmp_var.name)], value=place_type.compare_expr( env, self_var, other_var))) builder.begin_If(test=pyast.E('{} != 0'.format(cmp_var.name))) builder.emit_Return(pyast.E(cmp_var.name)) builder.end_If() builder.emit_Return(pyast.Num(0)) builder.end_FunctionDef() return builder.ast()
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
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
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') builder = pyast.Builder() builder.begin_FunctionDef(name='__pid_free_hash__', args=pyast.A(self_var.name).ast()) builder.emit(pyast.E('h = 0')) for (name, place_type) in marking_type.place_types.iteritems(): magic = hash(name) type_info = place_type.token_type if type_info.is_Pid: # builder.emit( pyast.E('h ^= hash(' + place_type.field.access_from(self_var) + ') * ' + str(magic)) ) right_operand = pyast.BinOp(left=place_type.pid_free_hash_expr( env, self_var, [0]), op=pyast.Mult(), right=pyast.E(str(magic))) builder.emit( pyast.AugAssign(target=[pyast.E("h")], op=pyast.BitXor(), value=right_operand)) elif type_info.has_pids: # must be tuple assert (type_info.is_TupleType) ignore = [ i for i, subtype in enumerate(type_info) if subtype.is_Pid ] # builder.emit("{!s} = {!r}".format(ig_var.name, ig)) right_operand = pyast.BinOp(left=place_type.pid_free_hash_expr( env, self_var, ignore), op=pyast.Mult(), right=pyast.E(str(magic))) builder.emit( pyast.AugAssign(target=[pyast.E("h")], op=pyast.BitXor(), value=right_operand)) else: builder.emit( pyast.E('h ^= hash(' + place_type.field.access_from(self_var) + ') * ' + str(magic))) builder.emit_Return(pyast.E("h")) builder.end_FunctionDef() return builder.ast()
def generate(self, env): marking_type = env.marking_type vp = VariableProvider() self_var = vp.new_variable(marking_type.type, 'self') builder = pyast.Builder() builder.begin_FunctionDef(name = '__hash__', args = pyast.A(self_var.name).ast()) builder.begin_If(test = pyast.E('self.{} != None'.format(marking_type.get_field('_hash').name))) builder.emit_Return(pyast.E('self.{}'.format(marking_type.get_field('_hash').name))) builder.end_If() builder.emit(pyast.E('h = 0')) for (name, place_type) in marking_type.place_types.iteritems(): magic = hash(name) builder.emit(pyast.E('h ^= hash(' + place_type.field.access_from(self_var) + ') * ' + str(magic))) # builder.emit(pyast.E("print h")) builder.emit_Return(pyast.E("h")) builder.end_FunctionDef() return builder.ast()
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
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
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