def visit_For(self, pyc_for: For): # convert pyc_for to while # self.while_converter(pyc_for) for_node = ForNode() self.set_coordinate(for_node, pyc_for.coord) init = self.visit(pyc_for.init) if type(init) is list: for_node.init.extend(init) else: for_node.init.append(init) for_node.term = self.visit(pyc_for.cond) update = self.visit(pyc_for.next) if type(update) is list: for_node.update.extend(update) else: for_node.update.append(update) if type(pyc_for.stmt) is not list: pyc_for.stmt = [pyc_for.stmt] for child in pyc_for.stmt: child_node = self.visit(child) for_node.add_children(child_node) return for_node
def visit_EnhancedForControl( self, java_enhanced_for_control: EnhancedForControl): for_node = ForNode() self.set_coordinate(for_node, java_enhanced_for_control) #init assign_node = AssignNode() variable_node = self.visit(java_enhanced_for_control.var)[0].target constant_node = ConstantNode() assign_node.target = variable_node assign_node.value = constant_node for_node.init.append(assign_node) #condiction operator_node = Operator() operator_node.op = '<' operator_node.left = variable_node operator_node.right = self.visit(java_enhanced_for_control.iterable) operator_node.add_children(operator_node.left) operator_node.add_children(operator_node.right) for_node.term = operator_node #update update_operator_node = Operator() update_const_node = ConstantNode() update_const_node.value = 1 update_operator_node.right = update_const_node update_operator_node.left = variable_node update_operator_node.op = '+' update_assign_node = AssignNode() update_assign_node.target = variable_node update_assign_node.value = update_operator_node for_node.update.append(update_assign_node) return for_node
def visit_ForControl(self, java_for_control: ForControl): for_node = ForNode() self.set_coordinate(for_node, java_for_control) if type(java_for_control.init) is not list: java_for_control.init = [java_for_control.init] for child in java_for_control.init: init = self.visit(child) if type(init) is list: for_node.init.extend(init) else: for_node.init.append(init) for_node.term = self.visit(java_for_control.condition) if type(java_for_control.update) is not list: java_for_control.update = [java_for_control.update] for child in java_for_control.update: update = self.visit(child) if type(update) is list: for_node.update.extend(update) else: for_node.update.append(update) return for_node
def visit_ForNode(self, for_node: ForNode): if len(for_node.init) != 1: raise NotImplementedError("len(for_node.init) != 1") if len(for_node.update) != 1: raise NotImplementedError("len(for_node.update)") # init variable = self.visit(for_node.init[0].target) a_1 = self.visit(for_node.init[0].value) # term term = for_node.term t_left = self.visit(term.left) t_right = self.visit(term.right) if variable == t_left: a_n = t_right if term.op == '<': a_n = a_n - 1 elif term.op == '>': a_n = a_n + 1 elif variable == t_right: a_n = t_left if term.op == '<': a_n = a_n + 1 elif term.op == '>': a_n = a_n - 1 else: raise NotImplementedError("unknown condition: ", t_left, t_right) # update update = for_node.update[0] op = self.visit(update.value) step = 0 if op.is_Add: d = op - variable step = (a_n - a_1) / d + 1 elif op.is_Mul: q = op / variable step = sympy.log(a_n / a_1, q) + 1 else: raise NotImplementedError('can not handle loop update, op=', op) if step.expand().is_negative: raise NotImplementedError('this loop can not analyze.\n', ) tc = 0 for child in for_node.children: self.visit(child) tc += child.time_complexity if tc == 0: tc = 1 for_node.time_complexity = step * tc pass
def visit_ForNode(self, for_node: ForNode): if len(for_node.init) != 1: raise NotImplementedError("len(for_node.init) != 1") if len(for_node.update) != 1: raise NotImplementedError("len(for_node.update)") self.backward_table_manager.push_table() # init variable = self.visit(for_node.init[0].target) a_1 = self.visit(for_node.init[0].value) # term term = for_node.term t_left = self.visit(term.left) t_right = self.visit(term.right) # update tc = 0 for child in for_node.children: self.visit(child) tc += child.time_complexity if tc == 0: tc = 1 if type(term.right) == VariableNode: right_rate = self.backward_table_manager.get_symbol_rate( term.right.name) if type(term.left) == VariableNode: left_rate = self.backward_table_manager.get_symbol_rate( term.left.name) update = for_node.update[0] op = self.visit(update.value) step = 0 if op.is_Add: a_n = t_right d = op - variable if '*' == left_rate or '/' == right_rate: q = 2 step = sympy.log(a_n, q) + 1 else: step = (a_n - a_1) / d + 1 elif op.is_Mul: a_n = t_right q = op / variable step = sympy.log(a_n, q) + 1 else: raise NotImplementedError('can not handle loop update, op=', op) if step.expand().is_negative: raise NotImplementedError('this loop can not analyze.\n') self.backward_table_manager.pop_table() tc *= step for child in for_node.init: tc += child.time_complexity self.visit(for_node.term) tc += for_node.term.time_complexity for child in for_node.update: tc += child.time_complexity for_node.time_complexity = tc pass