def veval(op: 'nodes.BinOpType', left: 'values.Value', right: 'values.Value', lineprop : 'utils.LineProperty'): if isinstance(left, values.ListValue): return functions.generate_value_with_same_type(left) if isinstance(left, values.TupleValue): return functions.generate_value_with_same_type(left) return functions.generate_value_with_same_type(left)
def veval(op: 'nodes.BinOpType', left: 'values.Value', right: 'values.Value', lineprop: 'utils.LineProperty'): initialize_lazy() if isinstance(left, values.ListValue): return functions.generate_value_with_same_type(left) if isinstance(left, values.TupleValue): return functions.generate_value_with_same_type(left) if isinstance(right, values.NumberValue) or isinstance( right, values.TensorValue): if not isinstance(left, values.NumberValue) and not isinstance( left, values.TensorValue): utils.print_warning('Unestimated type is on left', lineprop) left = values.NumberValue(0.0) left_type = left.dtype right_type = right.dtype if not config.float_restrict: if left_type == utils.dtype_float64: left_type = utils.dtype_float32 if right_type == utils.dtype_float64: right_type = utils.dtype_float32 if left_type is None: left_type = np.array(0.0, np.float32).dtype if right_type is None: right_type = np.array(0.0, np.float32).dtype result_type = None for v in binop_type_table: if v[0] == op and v[1] == left_type and v[2] == right_type: result_type = v[3] break assert (result_type is not None) if isinstance(right, values.TensorValue): return functions.generate_value_with_type(right, type_=result_type) else: return functions.generate_value_with_type(left, type_=result_type) return values.Value()
def veval_ast_aug_assign(astc: 'AstContext', local_field: 'values.Field', graph: 'Graph'): assert (isinstance(astc.nast, gast.gast.AugAssign)) lineprop = utils.LineProperty(astc.lineno, astc.filename) target = veval_ast(astc.c(astc.nast.target), local_field, graph) value = veval_ast(astc.c(astc.nast.value), local_field, graph) target_value = try_get_value(target, 'aug_assign', lineprop) value_value = try_get_value(value, 'aug_assign', lineprop) binop = nodes.BinOpType.Unknown if isinstance(astc.nast.op, gast.Add): binop = nodes.BinOpType.Add if isinstance(astc.nast.op, gast.Sub): binop = nodes.BinOpType.Sub node_aug_assign = nodes.NodeAugAssign(target_value, value_value, binop, astc.lineno) graph.add_node(node_aug_assign) # TODO : estimate type new_value = functions.generate_value_with_same_type(target_value) node_aug_assign.set_outputs([new_value]) target.get_ref().revise(new_value)
def revise(self, value: 'Value', update_parent=True): super().revise(value) if not update_parent: return assert isinstance(self.parent_object, Object) target_value = utils.try_get_value(self.parent_object, 'subscript_object', -1) revision_value = utils.try_get_value(self.value, 'subscript_object', -1) subscript_value = utils.try_get_value(self.subscript, 'subscript_object', -1) if isinstance(subscript_value, TupleValue): # ex. x[1,2] if subscript_value.has_constant_value(): subscripts = [ utils.try_get_value(x, 'subscript', -1) for x in subscript_value.get_constant_value() ] else: if config.show_warnings: assert False, "This subscript is not supported." else: # ex. x[1] subscripts = [subscript_value] node = nodes.NodeSetItem(target_value, subscripts, revision_value) ret_value = functions.generate_value_with_same_type(target_value) node.set_outputs([ret_value]) self.graph.add_node(node) self.parent_object.revise(ret_value)
def vcall(self, module: 'values.Field', graph: 'graphs.Graph', inst: 'values.Object', args: 'functions.FunctionArgInput', option: 'vevaluator.VEvalContext' = None, line=-1): funcArgs = self.args.merge_inputs(inst, args) node = nodes.NodeCall(self, funcArgs, line) if inst.in_container: raise Exception('Invalid operation') old_v = inst.get_value() new_v = functions.generate_value_with_same_type(old_v) # estimate a type contained if old_v.has_constant_value(): new_v.internal_value = list(old_v.internal_value) for v in funcArgs.inputs[1:]: new_v.append(v) # update value inst.revise(new_v) new_v.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([new_v]) graph.add_node(node) return values.NoneValue()
def veval(op: 'nodes.UnaryOpType', value: 'values.Value'): if isinstance(value, values.NumberValue): if value.internal_value is not None: if op == nodes.UnaryOpType.UAdd: return values.NumberValue(value.internal_value) if op == nodes.UnaryOpType.USub: return values.NumberValue(-value.internal_value) else: return functions.generate_value_with_same_type(value) else: return functions.generate_value_with_same_type(value) if isinstance(value, values.BoolValue): return functions.generate_value_with_same_type(value) if op == nodes.UnaryOpType.Not and isinstance(value, values.ListValue): return values.BoolValue(None) return values.Value()
def vcall(self, module: 'values.Field', graph: 'graphs.Graph', inst: 'values.Object', args: 'functions.FunctionArgInput', context: 'functions.VEvalContext' = None, line=-1): funcArgs = self.args.merge_inputs(inst, args) vargs = funcArgs.get_value().inputs node = nodes.NodeCall(self, funcArgs, line) graph.add_node(node) value = functions.generate_value_with_same_type(vargs[0]) value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) return values.Object(value)
def vcall(self, module: 'Field', graph: 'Graph', inst: 'values.ValueRef', args: 'functions.FunctionArgInput', line=-1): assert (inst is None) funcArgs = self.args.merge_inputs(inst, args) vargs = funcArgs.get_value().inputs node = nodes.NodeCall(self, funcArgs, line) graph.add_node(node) value = functions.generate_value_with_same_type(vargs[0]) value.name = '@F.{}.{}'.format(line, self.name) node.set_outputs([value]) return values.ValueRef(value)
def veval_ast_listcomp(astc: 'AstContext', local_field: 'values.Field', graph: 'Graph'): ''' Ex. [x for x in xx] [elt for target in iter] ''' assert (isinstance(astc.nast, gast.gast.ListComp)) lineprop = utils.LineProperty(astc.lineno, astc.filename) listcomp_guid = str(utils.get_guid()) listcomp_id = 'listcomp_' + listcomp_guid body_id = 'listcomp_body_' + listcomp_guid internal_counter_id = '@internal/listcomp_counter_' + listcomp_guid internal_list_id = '@internal/listcomp_list_' + listcomp_guid internal_cond_id = '@internal/listcomp_cond_' + listcomp_guid generator = astc.nast.generators[0] iter_value = try_get_value( veval_ast(astc.c(generator.iter), local_field, graph), 'generator', lineprop) list_value = values.ListValue() list_obj = values.ValueRef(list_value) node_generate_list = nodes.NodeGenerate('List', [], lineprop) node_generate_list.set_outputs([list_value]) graph.add_node(node_generate_list) # body target_name = '' if isinstance(generator.target, gast.gast.Name): target_name = generator.target.id else: if config.show_warnings: print('This for is not supported. in L.{}'.format(astc.lineno)) return None counter_value = values.NumberValue(None) counter_value.dtype = np.array(0).dtype counter_value.name = internal_counter_id cond_value = values.BoolValue(None) cond_value.name = internal_cond_id # set values with internal name local_field.get_attribute(internal_list_id).revise(list_obj) values.push_history(listcomp_id) body_graph = Graph() body_graph.root_graph = graph.root_graph body_graph.name = 'Body_' + listcomp_guid node_forgen = nodes.NodeForGenerator(counter_value, iter_value) target_ref = iter_value.get_iterator() if target_ref is None: target_ref = values.ValueRef(values.UnknownValue()) if config.show_warnings: print('unknown iteratable type in L.{}'.format(lineprop)) target_value = target_ref.get_value() node_forgen.set_outputs([target_ref.get_value()]) local_field.get_attribute(target_name).revise(target_ref) body_graph.add_node(node_forgen) elt = veval_ast(astc.c(astc.nast.elt), local_field, body_graph) elt_obj = try_get_ref(elt, 'listcomp', lineprop) finput = functions.FunctionArgInput() finput.inputs.append(elt_obj) append_value = local_field.get_attribute(internal_list_id).get_ref( ).get_field().get_attribute('append').get_ref().get_value() append_value.func.vcall( None, body_graph, local_field.get_attribute(internal_list_id).get_ref(), finput, lineprop) value_inputs = values.get_inputs() value_outputs = values.get_outputs() values.pop_history() inputs = [] outputs = [] # default input for subgraph's input body_graph.add_input_value(counter_value) body_graph.add_input_value(cond_value) body_graph.add_input_value(iter_value) # default output for subgraph's output body_graph.add_output_value(cond_value) body_graph.add_output_value(iter_value) # default output outputs.append(functions.generate_value_with_same_type(iter_value)) # generate pairs value_pairs = {} for v in value_inputs: key = str(v.field.id) + '_' + v.name if not (key in value_pairs.keys()): value_pairs[key] = {} value_pairs[key]['field'] = v.field value_pairs[key]['name'] = v.name value_pairs[key]['input_value'] = v.input_value value_pairs[key]['input_body_value'] = v.value for v in value_outputs: key = str(v.field.id) + '_' + v.name if not (key in value_pairs.keys()): value_pairs[key] = {} value_pairs[key]['field'] = v.field value_pairs[key]['name'] = v.name value_pairs[key]['output_body_value'] = v.value value_pairs[key]['output_obj'] = v.obj # remove iterator removed_name = str(local_field.id) + '_' + target_value.name del value_pairs[removed_name] for k, v in value_pairs.items(): name = v['name'] field = v['field'] if 'input_body_value' in v: inputs.append(v['input_value']) body_graph.add_input_value(v['input_body_value']) else: temp_value1 = functions.generate_value_with_same_type( v['output_body_value']) temp_value2 = functions.generate_value_with_same_type( v['output_body_value']) inputs.append(temp_value1) body_graph.add_input_value(temp_value2) if 'output_body_value' in v: body_graph.add_output_value(v['output_body_value']) output_value = functions.generate_value_with_same_type( v['output_body_value']) outputs.append(output_value) if 'output_obj' in v: obj = v['output_obj'] obj.revise(output_value) field.get_attribute(name).revise(obj) elif field.get_attribute(name).has_obj(): field.get_attribute(name).get_ref().revise(output_value) else: field.get_attribute(name).revise(values.ValueRef(output_value)) else: temp_value1 = v['input_body_value'] temp_value2 = functions.generate_value_with_same_type( v['input_body_value']) body_graph.add_output_value(temp_value1) outputs.append(temp_value2) node = nodes.NodeListcomp(iter_value, inputs, body_graph, astc.lineno) node.set_outputs(outputs) graph.add_node(node) return local_field.get_attribute(internal_list_id).get_ref()
def veval_ast_subscript(astc: 'AstContext', local_field: 'values.Field', graph: 'Graph'): ''' Ex. x[1], x[y,z] ''' assert (isinstance(astc.nast, gast.gast.Subscript)) lineprop = utils.LineProperty(astc.lineno, astc.filename) def veval_with_default(nast, default_value): if nast is None: ret = values.NumberValue(default_value) ret.name = '@SliceDefault' return ret obj = veval_ast(astc.c(nast), local_field, graph) return try_get_value(obj, 'subscript', lineprop) def get_slice_indices(slice): if slice.lower is None and slice.upper is None and slice.step is None: return [] indices = [ veval_with_default(slice.lower, 0), veval_with_default(slice.upper, utils.slice_int_max) ] if slice.step is not None: indices.append(veval_with_default(slice.step, 1)) return indices value = veval_ast(astc.c(astc.nast.value), local_field, graph) value_value = try_get_value(value, 'subscript', lineprop) if isinstance(astc.nast.slice, gast.gast.Index): slice_ = veval_ast(astc.c(astc.nast.slice.value), local_field, graph) slice_value = try_get_value(slice_, 'subscript', lineprop) if isinstance(slice_value, values.TupleValue): # ex. x[1,2] if slice_value.has_constant_value(): values_ = [ try_get_value(x, 'subscript', lineprop) for x in slice_value.get_constant_value() ] node = nodes.NodeGetItem(value_value, values_, line=lineprop) else: if config.show_warnings: print('This subscript is not supported. in L.{}'.format( astc.lineno)) node = nodes.NodeInvalid(line=lineprop) else: # ex. x[1] node = nodes.NodeGetItem(value_value, [slice_value]) if isinstance(value_value, values.ListValue) and value_value.vtype != None: ret_value = value_value.vtype() ret_value.dtype = value_value.dtype elif isinstance(value_value, values.TupleValue) and value_value.vtype != None: ret_value = value_value.vtype() ret_value.dtype = value_value.dtype else: ret_value = values.UnknownValue() node.set_outputs([ret_value]) graph.add_node(node) return values.ValueRef(ret_value) elif isinstance(astc.nast.slice, gast.gast.Slice): indices = get_slice_indices(astc.nast.slice) node = nodes.NodeSlice(value_value, indices, [len(indices)]) ret_value = functions.generate_value_with_same_type(value_value) node.set_outputs([ret_value]) graph.add_node(node) return values.ValueRef(ret_value) elif isinstance(astc.nast.slice, gast.gast.ExtSlice): indices = [] slice_specs = [] for dim in astc.nast.slice.dims: if isinstance(dim, gast.gast.Index): indices.append( try_get_value( veval_ast(astc.c(dim.value), local_field, graph), 'subscript', lineprop)) slice_specs.append(1) elif isinstance(dim, gast.gast.Slice): ni = get_slice_indices(dim) indices.extend(ni) slice_specs.append(len(ni)) else: assert False, 'Unknown slice: %s in %s' % (dim, nast.slice) node = nodes.NodeSlice(value_value, indices, slice_specs) ret_value = functions.generate_value_with_same_type(value_value) node.set_outputs([ret_value]) graph.add_node(node) return values.ValueRef(ret_value) return None
def veval_ast_if(astc: 'AstContext', local_field: 'values.Field', graph: 'Graph'): assert (isinstance(astc.nast, gast.gast.If)) lineprop = utils.LineProperty(astc.lineno, astc.filename) # if condition test = veval_ast(astc.c(astc.nast.test), local_field, graph) test_value = try_get_value(test, 'if', lineprop) id_str = str(utils.get_guid()) if_id = 'if_' + id_str true_id = 'true_' + id_str false_id = 'false_' + id_str # True values.push_history(true_id) true_graph = Graph() true_graph.root_graph = graph.root_graph true_graph.name = 'True' true_body = veval_ast(astc.c(astc.nast.body), local_field, true_graph) true_value_inputs = values.get_inputs() true_value_outputs = values.get_outputs() values.pop_history() # False values.push_history(false_id) false_graph = Graph() false_graph.root_graph = graph.root_graph false_graph.name = 'False' false_body = veval_ast(astc.c(astc.nast.orelse), local_field, false_graph) false_value_inputs = values.get_inputs() false_value_outputs = values.get_outputs() values.pop_history() # generate pairs value_pairs = {} for v in true_value_inputs: key = str(v.field.id) + '_' + v.name if not (key in value_pairs.keys()): value_pairs[key] = {} value_pairs[key]['field'] = v.field value_pairs[key]['name'] = v.name value_pairs[key]['true_input_value'] = v.input_value value_pairs[key]['true_input_body_value'] = v.value value_pairs[key]['true_input_obj'] = v.obj for v in true_value_outputs: key = str(v.field.id) + '_' + v.name if not (key in value_pairs.keys()): value_pairs[key] = {} value_pairs[key]['field'] = v.field value_pairs[key]['name'] = v.name value_pairs[key]['true_output_body_value'] = v.value value_pairs[key]['true_output_obj'] = v.obj for v in false_value_inputs: key = str(v.field.id) + '_' + v.name if not (key in value_pairs.keys()): value_pairs[key] = {} value_pairs[key]['field'] = v.field value_pairs[key]['name'] = v.name value_pairs[key]['false_input_value'] = v.input_value value_pairs[key]['false_input_body_value'] = v.value value_pairs[key]['false_input_obj'] = v.obj for v in false_value_outputs: key = str(v.field.id) + '_' + v.name if not (key in value_pairs.keys()): value_pairs[key] = {} value_pairs[key]['field'] = v.field value_pairs[key]['name'] = v.name value_pairs[key]['false_output_body_value'] = v.value value_pairs[key]['false_output_obj'] = v.obj inputs = [] outputs = [] for k, v in value_pairs.items(): name = v['name'] field = v['field'] input_value = None true_input_body_value = None false_input_body_value = None # search input value if 'true_input_value' in v: input_value = v['true_input_value'] elif 'false_input_value' in v: input_value = v['false_input_value'] if input_value is not None: if 'true_input_body_value' in v: true_input_body_value = v['true_input_body_value'] else: true_input_body_value = functions.generate_value_with_same_type( input_value) if 'false_input_body_value' in v: false_input_body_value = v['false_input_body_value'] else: false_input_body_value = functions.generate_value_with_same_type( input_value) true_output_body_value = None false_output_body_value = None output_value = None # search output value if 'true_output_body_value' in v: true_output_body_value = v['true_output_body_value'] if 'false_output_body_value' in v: false_output_body_value = v['false_output_body_value'] if true_output_body_value is not None or false_output_body_value is not None: if true_output_body_value is None: if true_input_body_value is not None: # e.x. not changed true_output_body_value = true_input_body_value else: # e.x. make a value in false statement true_output_body_value = functions.generate_value_with_same_type( false_output_body_value, is_dummy_value=True) if false_output_body_value is None: if false_input_body_value is not None: # e.x. not changed false_output_body_value = false_input_body_value else: # e.x. make a value in true statement false_output_body_value = functions.generate_value_with_same_type( true_output_body_value, is_dummy_value=True) # check types between true and false true_output_body_value_type = None false_output_body_value_type = None if true_output_body_value is not None and true_output_body_value.is_not_none_or_any_value( ): true_output_body_value_type = true_output_body_value if false_output_body_value is not None and false_output_body_value.is_not_none_or_any_value( ): false_output_body_value_type = false_output_body_value if true_output_body_value_type is not None and false_output_body_value_type is not None and type( true_output_body_value_type) != type( false_output_body_value_type): utils.print_warning( 'Values with differenet type were generated {} between true ande false' .format(k), lineprop) if true_output_body_value_type != None: output_value = functions.generate_value_with_same_type( true_output_body_value_type) elif false_output_body_value_type != None: output_value = functions.generate_value_with_same_type( false_output_body_value_type) elif true_output_body_value is not None: output_value = functions.generate_value_with_same_type( true_output_body_value) elif false_output_body_value is not None: output_value = functions.generate_value_with_same_type( false_output_body_value) if input_value is not None: inputs.append(input_value) true_graph.add_input_value(true_input_body_value) false_graph.add_input_value(false_input_body_value) if output_value is not None: outputs.append(output_value) true_graph.add_output_value(true_output_body_value) false_graph.add_output_value(false_output_body_value) if 'true_output_obj' in v and not 'false_output_obj' in v: obj = v['true_output_obj'] elif not 'true_output_obj' in v and 'false_output_obj' in v: obj = v['false_output_obj'] elif 'true_output_obj' in v and 'false_output_obj' in v: obj = None else: assert (False) if obj is not None: obj.revise(output_value) field.get_attribute(name).revise(obj) elif field.get_attribute(name).has_obj(): field.get_attribute(name).get_ref().revise(output_value) else: field.get_attribute(name).revise(values.ValueRef(output_value)) node = nodes.NodeIf(test_value, inputs, true_graph, false_graph, astc.lineno) node.set_outputs(outputs) graph.add_node(node) return None
def veval_ast_for(astc: 'AstContext', local_field: 'values.Field', graph: 'Graph'): ''' for target in iter: ... ''' assert (isinstance(astc.nast, gast.gast.For)) lineprop = utils.LineProperty(astc.lineno, astc.filename) # for target in iter: iter_ = veval_ast(astc.c(astc.nast.iter), local_field, graph) input_iter_value = try_get_value(iter_, 'for', lineprop) body_iter_value = functions.generate_value_with_same_type( input_iter_value, suffix_type=functions.SuffixType.Input) # get target name target_name = '' if isinstance(astc.nast.target, gast.gast.Name): target_name = astc.nast.target.id else: if config.show_warnings: print('This for is not supported. in L.{}'.format(astc.lineno)) return None # unroll? if isinstance(input_iter_value, values.ListValue) and input_iter_value.has_constant_value( ) and input_iter_value.dtype is None: return veval_ast_for_unroll(astc, target_name, input_iter_value, local_field, graph) for_guid = utils.get_guid() for_id = 'for_' + str(for_guid) body_id = 'body_' + str(for_guid) values.push_history(for_id) # body body_graph = Graph() body_graph.root_graph = graph.root_graph body_graph.name = 'Body_' + str(for_guid) # generate a node for input node_input = nodes.NodeInput('input') body_graph.add_node(node_input) body_counter_value = values.NumberValue(None) body_counter_value.dtype = np.array(0).dtype body_counter_value.name = 'for_counter_' + str(for_guid) body_cond_value = values.BoolValue(None) body_cond_value.name = 'for_cond_' + str(for_guid) # create a node to lookup a value from sequence node_forgen = nodes.NodeForGenerator(body_counter_value, body_iter_value) # generate iterator target_ref = input_iter_value.get_iterator() if target_ref is None: target_ref = values.ValueRef(values.UnknownValue()) if config.show_warnings: print('unknown iteratable type in L.{}'.format(astc.lineno)) target_value = target_ref.get_value() node_forgen.set_outputs([target_ref.get_value()]) target_attribute = local_field.get_attribute(target_name) target_attribute.revise(target_ref) body_graph.add_node(node_forgen) # veval body body = veval_ast(astc.c(astc.nast.body), local_field, body_graph) value_inputs = values.get_inputs() value_outputs = values.get_outputs() break_attribute = local_field.get_attribute('#keepgoing') if break_attribute.has_obj(): break_attribute_ref = break_attribute.get_ref() break_attribute_value = break_attribute_ref.get_value() else: break_attribute_value = body_cond_value values.pop_history() inputs = [] outputs = [] node_input_outputs = [] # default input for subgraph's input body_graph.add_input_value(body_counter_value) body_graph.add_input_value(body_cond_value) body_graph.add_input_value(body_iter_value) # default output for subgraph's output body_graph.add_output_value(break_attribute_value) body_graph.add_output_value(body_iter_value) # default output outputs.append(functions.generate_value_with_same_type(input_iter_value)) # generate pairs value_pairs = {} for v in value_inputs: key = str(v.field.id) + '_' + v.name if not (key in value_pairs.keys()): value_pairs[key] = {} value_pairs[key]['field'] = v.field value_pairs[key]['name'] = v.name value_pairs[key]['input_value'] = v.input_value value_pairs[key]['input_body_value'] = v.value for v in value_outputs: key = str(v.field.id) + '_' + v.name if not (key in value_pairs.keys()): value_pairs[key] = {} value_pairs[key]['field'] = v.field value_pairs[key]['name'] = v.name value_pairs[key]['output_body_value'] = v.value value_pairs[key]['output_obj'] = v.obj for k, v in value_pairs.items(): name = v['name'] field = v['field'] if 'input_body_value' in v: inputs.append(v['input_value']) body_graph.add_input_value(v['input_body_value']) else: temp_value1 = functions.generate_value_with_same_type( v['output_body_value'], is_dummy_value=True, suffix_type=functions.SuffixType.Dummy) temp_value2 = functions.generate_value_with_same_type( v['output_body_value'], suffix_type=functions.SuffixType.Dummy) inputs.append(temp_value1) body_graph.add_input_value(temp_value2) node_input_outputs.append(temp_value2) if 'output_body_value' in v: body_graph.add_output_value(v['output_body_value']) output_value = functions.generate_value_with_same_type( v['output_body_value']) outputs.append(output_value) if 'output_obj' in v: obj = v['output_obj'] obj.revise(output_value) field.get_attribute(name).revise(obj) elif field.get_attribute(name).has_obj(): field.get_attribute(name).get_ref().revise(output_value) else: field.get_attribute(name).revise(values.ValueRef(output_value)) else: temp_value1 = v['input_body_value'] temp_value2 = functions.generate_value_with_same_type( v['input_body_value']) body_graph.add_output_value(temp_value1) outputs.append(temp_value2) node = nodes.NodeFor(input_iter_value, inputs, body_graph, body_cond_value, astc.lineno) node.set_outputs(outputs) node_input.set_outputs(node_input_outputs) graph.add_node(node) return None
def ret_same(funcArgs): return functions.generate_value_with_same_type(funcArgs.keywords['x'].get_value())
def veval(op: 'nodes.BinOpType', left: 'values.Value', right: 'values.Value', lineprop: 'utils.LineProperty'): initialize_lazy() if isinstance(left, values.ListValue): return functions.generate_value_with_same_type(left) if isinstance(left, values.TupleValue): return functions.generate_value_with_same_type(left) if isinstance(left, values.StrValue): if not (isinstance(right, values.NumberValue) or isinstance( right, values.StrValue) or isinstance(right, values.BoolValue) or isinstance(right, values.TupleValue)): assert False if not (left.has_constant_value() or right.has_constant_value()): assert False if op == nodes.BinOpType.Add: if not isinstance(right, values.StrValue): assert False return values.StrValue(left.internal_value + right.internal_value) elif op == nodes.BinOpType.Mod: right_internal_value = right.internal_value if isinstance(right, values.TupleValue): values_ = [] for ref in right_internal_value: if not (isinstance(ref.get_value(), values.NumberValue) or isinstance(ref.get_value(), values.StrValue) or isinstance(ref.get_value(), values.BoolValue)): assert False if not ref.get_value().has_constant_value(): assert False values_.append(ref.get_value()) right_internal_value = tuple(value.internal_value for value in values_) return values.StrValue(left.internal_value % right_internal_value) return values.StrValue("") if isinstance(right, values.NumberValue) or isinstance( right, values.TensorValue): if not isinstance(left, values.NumberValue) and not isinstance( left, values.TensorValue): utils.print_warning('Unestimated type is on left', lineprop) left = values.NumberValue(0.0) left_type = left.dtype right_type = right.dtype if not config.float_restrict: if left_type == utils.dtype_float64: left_type = utils.dtype_float32 if right_type == utils.dtype_float64: right_type = utils.dtype_float32 if left_type is None: left_type = np.array(0.0, np.float32).dtype if right_type is None: right_type = np.array(0.0, np.float32).dtype result_type = None for v in binop_type_table: if v[0] == op and v[1] == left_type and v[2] == right_type: result_type = v[3] break assert (result_type is not None) if isinstance(right, values.TensorValue): return functions.generate_value_with_type(right, type_=result_type) else: return functions.generate_value_with_type(left, type_=result_type) return values.Value()