def gen_different(self, indices): base = None current = None first_index = indices.pop() for index in indices: check = pyast.If(test = pyast.Compare(left = pyast.Name(first_index), ops = [ pyast.NotEq() ], comparators = [ pyast.Name(index) ]), body = [], orelse = []) if not base: base = check current = check else: current.body.append(check) current = check inner = current if len(indices) > 1: _, inner = self.gen_different(indices) current.body.append(base) return base, inner
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_Succs(self, node): body = [ pyast.Assign(targets = [pyast.Name(id = node.arg_marking_acc_var.name)], value = self.env.marking_set_type.new_marking_set_expr(self.env)) ] body.extend(self.compile(node.body)) body.append(pyast.Return(pyast.Name(id = 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_ctx_var.name) ]), body = body)
def compile_Init(self, node): new_marking = pyast.Assign(targets = [ pyast.Name(id = node.marking_var.name) ], value = self.env.marking_type.new_marking_expr(self.env)) return_stmt = pyast.Return(pyast.Name(id = node.marking_var.name)) stmts = [new_marking] stmts.extend(self.compile(node.body)) stmts.append(return_stmt) return pyast.FunctionDef(name = node.function_name, body = stmts)
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 generate_api(self, env): cls = pyast.ClassDef('Marking', bases=[pyast.Name(id='object')]) elts = [] names = [] for field in self.fields: # self.place_types.iteritems(): name = field.name elts.append(pyast.Str(name)) names.append(name) slots = pyast.Assign(targets=[pyast.Name('__slots__')], value=pyast.Tuple(elts)) cls.body = [slots] + self.generate_methods(env) return cls
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_MultiTokenEnumeration(self, node): place_type = self.env.marking_type.get_place_type_by_name(node.place_name) base = None current = None if place_type.provides_by_index_access: for sub_arc in node.multiarc.sub_arcs: variable = sub_arc.data['local_variable'] index = sub_arc.data['index'] assign = pyast.Assign(targets = [pyast.Name(variable)], value = place_type.get_token_expr(self.env, node.marking_var, pyast.Name(index))) enumeration = pyast.For(target = pyast.Name(index), iter = pyast.Call(func = pyast.Name('range'), args = [pyast.Num(0), place_type.get_size_expr(self.env, node.marking_var)]), body = [ assign ]) if base == None: current = enumeration base = enumeration else: current.body.append(enumeration) current = enumeration else: # no index access for sub_arc in node.multiarc.sub_arcs: variable = sub_arc.data['local_variable'] index = sub_arc.data['index'] init = pyast.Assign(targets = [pyast.Name(index.name)], value = pyast.Num(0)) enumeration = pyast.For(target = pyast.Name(variable.name), iter = place_type.iterable_expr(env = self.env, marking_var = node.marking_var), body = [ pyast.AugAssign(target = pyast.Name(index.name), op = pyast.Add(), value = pyast.Num(1)) ]) if base == None: current = [ init, enumeration ] base = [ init, enumeration ] else: current[1].body.append([init, enumeration]) current = [init, enumeration] indices = [ sub_arc.data['index'].name for sub_arc in node.multiarc.sub_arcs ] inner_base, inner = self.gen_different(indices) if isinstance(current, list): current[1].body.append(inner_base) else: current.body.append(inner_base) current = inner current.body.extend([ self.compile(node.body) ]) return base
def compile_FlowCheck(self, node): return self.env.marking_type.gen_check_flow(env = self.env, marking_var = node.marking_var, place_info = node.place_info, current_flow = pyast.Name(node.current_flow.name))
def compile_ProcedureCall(self, node): return pyast.stmt(pyast.Call(func = pyast.Name(id = node.function_name), args = [ self.compile(arg) for arg in node.arguments ]))
def compile_Assign(self, node): return pyast.Assign(targets = [pyast.Name(id = node.variable.name)], value = self.compile(node.expr))
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 new_marking_set_expr(self, env): return pyast.Call(func=pyast.Name(id='set'))
def copy_marking_expr(self, env, marking_var, *args): return pyast.Call(func=pyast.Attribute( value=pyast.Name(id=marking_var.name), attr='copy'))