def _reconstruct_args(f, args, new_param): real_func = getattr(f, rt_seq_mode_id, f) new_args = list(args) arg_spec = inspect.signature(real_func).parameters if new_param is not None: if new_param.param_name in arg_spec.keys(): idx = list(arg_spec.keys()).index(new_param.param_name) datatype_name = new_param.default_elem.__class__.__name__ datatype = rtprimitives.get_class_by_name(datatype_name) if new_param.by_value: if isinstance(args[idx], DataType): value = args[idx].value else: value = args[idx] new_args[idx] = datatype(value) else: if not isinstance(args[idx], DataType): value = args[idx] new_args[idx] = datatype(value) else: raise errors.VeristandError( _errormessages.param_description_no_param) return tuple(new_args)
def _transform_data_type_scalar(node): # In case of data type creation or declaration, create the appropriate value (for example Double(3) # needs to return "3.0", not "3".) data_type = rtprimitives.get_class_by_name(node.func.id) data_value_str = str(data_type(utils.get_element_value(node.args[0])).value) # we need to check the symbols dictionary because of named constants. Using Boolean(True) # needs to return "true" not "True" if data_value_str in symbols._symbols: data_value_str = symbols._symbols[data_value_str] return data_value_str
def get_value_from_node(node, resources): if isinstance(node, ast.Call): call = generic_ast_node_transform(node.func, resources) node_id = call.split('.')[-1] if rtprimitives.is_supported_data_type(node_id): datatype = rtprimitives.get_class_by_name(node.func.id) if rtprimitives.is_channel_ref_type(datatype.__name__): if rtprimitives.is_array_type(datatype.__name__): datavalue = [0.0] else: datavalue = 0.0 elif type(node.args[0]) is ast.UnaryOp or is_node_ast_num( node.args[0]): datavalue = get_element_value(node.args[0]) elif type(node.args[0]) is ast.Name: if node.args[0].id in symbols._symbols: datavalue = symbols._symbols[node.args[0].id] else: raise TranslateError(_errormessages.init_var_invalid_type) elif is_node_ast_nameconstant(node.args[0]): node_value = get_value_from_nameconstant_node(node.args[0]) if node_value is True or node_value is False: datavalue = node_value else: raise TranslateError(_errormessages.init_var_invalid_type) elif type(node.args[0]) is ast.List: datavalue = [ get_element_value(element) for element in node.args[0].elts ] else: raise TranslateError(_errormessages.init_var_invalid_type) return datatype(datavalue) elif isinstance(node, ast.Name): if node.id in ['True', 'False']: return _datatypes.BooleanValue(node.id) elif is_node_ast_num(node): node_value = get_value_from_num_node(node) if isinstance(node_value, int): try: return_obj = _datatypes.I32Value(node_value) except OverflowError: return_obj = _datatypes.I64Value(node_value) return return_obj elif isinstance(node_value, float): return _datatypes.DoubleValue(node_value) elif is_node_ast_nameconstant(node): node_value = get_value_from_nameconstant_node(node) if node_value is None: raise TranslateError(_errormessages.init_var_invalid_type) return _datatypes.BooleanValue(node_value) raise TranslateError(_errormessages.init_var_invalid_type)
def get_value_from_node(node, resources): if isinstance(node, ast.Call): call = generic_ast_node_transform(node.func, resources) node_id = call.split('.')[-1] if rtprimitives.is_supported_data_type(node_id): datatype = rtprimitives.get_class_by_name(node.func.id) if rtprimitives.is_channel_ref_type(datatype.__name__): if rtprimitives.is_array_type(datatype.__name__): datavalue = [0.0] else: datavalue = 0.0 elif type(node.args[0]) in (ast.Num, ast.UnaryOp): datavalue = get_element_value(node.args[0]) elif type(node.args[0]) is ast.Name: if node.args[0].id in symbols._symbols: datavalue = symbols._symbols[node.args[0].id] else: raise TranslateError(_errormessages.init_var_invalid_type) # this is ugly, but NameConstant does not exist in 2.7 and we need to evaluate the system version # otherwise the 2.7 version will error out by not recognizing the NameConstant attribute elif sys.version_info >= (3, 5) and type( node.args[0]) is ast.NameConstant: if node.args[0].value is True or node.args[0].value is False: datavalue = node.args[0].value else: raise TranslateError(_errormessages.init_var_invalid_type) elif type(node.args[0]) is ast.List: datavalue = [ get_element_value(element) for element in node.args[0].elts ] else: raise TranslateError(_errormessages.init_var_invalid_type) return datatype(datavalue) elif isinstance(node, ast.Num): if isinstance(node.n, int): try: return_obj = _datatypes.I32Value(node.n) except OverflowError: return_obj = _datatypes.I64Value(node.n) return return_obj elif isinstance(node.n, float): return _datatypes.DoubleValue(node.n) elif sys.version_info >= (3, 5) and isinstance(node, ast.NameConstant): if node.value is None: raise TranslateError(_errormessages.init_var_invalid_type) return _datatypes.BooleanValue(node.value) elif isinstance(node, ast.Name): if node.id in ['True', 'False']: return _datatypes.BooleanValue(node.id) raise TranslateError(_errormessages.init_var_invalid_type)