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(pyast.Assign(targets = [ pyast.Tuple([ pyast.E(name) for name in component_names ])], value = pyast.Name(tuple_info.data['local_variable'].name))) cur = None for component in tuple_info.components: if component.is_Value: n = pyast.Builder.If(test = pyast.Builder.Compare(left = pyast.E(component.data['local_variable'].name), ops = [ pyast.Eq() ], comparators = [ pyast.E(repr(component.raw)) ]), # TO DO unify value & pickle orelse = []) if cur == None: cur = n seq.append(n) else: cur.body = [n] cur = n if cur != None: cur.body = [ self.compile(node.body) ] else: seq.append(self.compile(node.body)) return seq
def compile_CheckType(self, node): type_info = node.type if type_info.is_AnyType: return self.compile(node.body) test = pyast.E("isinstance(" + node.variable.name + ", " + type2str(node.type) + ")") return pyast.If(test = test, body = self.compile(node.body))
def gen_tuple(self, tuple_info): elts = [] for info in tuple_info: if info.is_Value: elts.append(pyast.E(repr(info.raw))) elif info.is_Variable: elts.append(pyast.E(info.name)) elif info.is_Tuple: elts.append(self.gen_tuple(info)) elif info.is_Expression: elts.append(pyast.E(info.raw)) else: raise NotImplementedError, info.component.__class__ return pyast.Tuple(elts)
def compile_SuccP(self, node): stmts = [ self.compile(node.body), pyast.E('return ' + node.arg_marking_acc_var.name) ] return pyast.FunctionDef(name = node.function_name, args = pyast.arguments(args = [ pyast.Name(id = node.arg_marking_var.name), pyast.Name(id = node.arg_marking_acc_var.name), pyast.Name(id = node.arg_ctx_var.name)]), body = stmts)
def compile_TokenEnumeration(self, node): place_type = self.env.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)) else: return pyast.For(target = pyast.E(node.token_var.name), iter = place_type.iterable_expr(env = self.env, marking_var = node.marking_var), body = [ self.compile(node.body) ])
def gen_imports(self): nodes = [] for decl in self._declarations: stmt = pyast.E(decl) nodes.append(stmt) for module in self._imports: nodes.append( ast.Import(names=[ast.alias(name=module, asname=None)])) return nodes
def compile_SuccT(self, node): self.env.push_variable_provider(node.variable_provider) stmts = [ self.compile(node.body), pyast.E('return ' + node.arg_marking_acc_var.name) ] result = pyast.FunctionDef(name = node.function_name, args = pyast.arguments(args = [ pyast.Name(id = node.arg_marking_var.name), pyast.Name(id = node.arg_marking_acc_var.name), pyast.Name(id = node.arg_ctx_var.name) ]), body = stmts) self.env.pop_variable_provider() return result
def compile_NormalizeMarking(self, node): function = mrkpidmethods.select_normalization_function(self.config) pidfree_hash_set = "{}.pid_free_hash".format(node.arg_ctx_var.name) state_space = "{}.state_space".format(node.arg_ctx_var.name) remaining_set = "{}.remaining".format(node.arg_ctx_var.name) return pyast.E("{dst} = {fun}({mrk}, {hs}, {acc}, {todo}, {ss})".format(dst = node.normalized_marking_var.name, fun = function, mrk = node.marking_var.name, hs = pidfree_hash_set, acc = node.marking_acc_var.name, todo = remaining_set, ss = state_space))
def compile_MarkingCopy(self, node): nodes = [] nodes.append(pyast.E(node.dst.name + " = Marking()")) names = {} for info in node.mod: names[info.name] = info for (place, place_type) in self.env.marking_type.place_types.iteritems(): # dst_place_expr = place_type.place_expr(self.env, marking_var = node.dst) # src_place_expr = place_type.place_expr(self.env, marking_var = node.src) if names.has_key(place): nodes.append(place_type.copy_stmt(self.env, node.dst, node.src)) # nodes.append( pyast.Assign(targets=[dst_place_expr], # value=place_type.copy_expr(self.env, node.src) # ) # ) else: nodes.append(place_type.copy_stmt(self.env, node.dst, node.src)) # nodes.append( pyast.Assign(targets=[dst_place_expr], # value=src_place_expr # ) # ) return nodes
def new_marking_expr(self, env, *args): return pyast.E("Marking()")
def compile_Pickle(self, node): output = StringIO.StringIO() cPickle.dump(node.obj, output) pickle_str = output.getvalue() return pyast.E("cPickle.load(StringIO.StringIO(" + repr(pickle_str) + "))")
def compile_PyExpr(self, node): assert isinstance(node.expr, ExpressionInfo) return pyast.E(node.expr.raw)
def compile_CheckTuple(self, node): tuple_info = node.tuple_info test = pyast.E("isinstance(" + node.tuple_var.name + ", tuple) and len(" + node.tuple_var.name + ") == " + repr(len(tuple_info))) return pyast.If(test = test, body = self.compile(node.body))
def compile_UpdateHashSet(self, node): # return [] pidfree_hash_set = "{}.pid_free_hash".format(node.ctx_var.name) return pyast.stmt(pyast.E("{}.add({}.__pid_free_hash__())".format(pidfree_hash_set, node.marking_var.name)))
def compile_InitialPid(self, node): return pyast.E("Pid.from_str('1')")
def compile_InitGeneratorPlace(self, node): marking_type = self.env.marking_type generator_place = marking_type.get_place_type_by_name(GENERATOR_PLACE) return [ generator_place.add_token_stmt(self.env, pyast.E("( Pid.from_str('1'), 0 )"), node.marking_var) ]
def compile_Name(self, node): return pyast.E(node.name)
def add_marking_stmt(self, env, markingset, marking): return pyast.stmt( pyast.Call(func=pyast.Attribute( value=pyast.Name(id=markingset.name), attr='add'), args=[pyast.E(marking.name)]))
def compile_FunctionCall(self, node): return pyast.E(node.function_name).call([ self.compile(arg) for arg in node.arguments ])