def compute(self, env: Environment): ln: VectorObj = env.find_object('length') if ln.get_type().name not in ['double', 'integer']: raise errors.InvalidArg('length') if len(ln.items) != 1: raise errors.InvalidArg('length') val = ln.items[0][1] if val.is_inf: raise errors.R_RuntimeError('vector size cannot be infinite') elif val.is_na: raise errors.InvalidArg('length') elif val.is_nan: raise errors.R_RuntimeError('vector size cannot be NA/NaN') count = val.value items = [ VectorItem(None, Atomic(False, types.LogicalType())) for _ in range(int(count) * (-1 if val.is_neg else 1)) ] ret = VectorObj(items, types.LogicalType()) return ret
def compute(self, env: Environment): e1 = env.find_object('e1') e2 = env.find_object('e2') if e1.get_type().name not in ['double', 'logical', 'integer']: raise errors.InvalidArgTypeInArgs('x', 'x ** y') if e2.get_type().name not in ['double', 'logical', 'integer']: raise errors.InvalidArgTypeInArgs('y', 'x ** y') as_integer = False if e1.get_type().name == 'integer' and e2.get_type().name == 'integer': as_integer = True elif e1.get_type().name == 'integer' and e2.get_type( ).name == 'logical': as_integer = True elif e1.get_type().name == 'logical' and e2.get_type( ).name == 'integer': as_integer = True res = perform_op_on_vector_or_atomic( e1, e2, lambda l, r: cast_atomic(atomic_mod(l, r), 'integer' if as_integer else 'double')) res = [VectorItem(None, el) for el in res] if len(res) == 0: t = get_more_important_vector_type(e1.get_type(), e2.get_type()) return VectorObj([], t) ret = VectorObj.create(res) return ret
def compute(self, env: Environment): dots: DotsObj = env.find_object('...') items = dots.items list_items = [] for item in items: itm = ListItem(item.name, item.value) list_items.append(itm) return ListObj(list_items)
def compute(self, env: Environment): e1 = env.find_object('x') e2 = env.find_object('y') if e1.get_type().name not in ['double', 'logical', 'integer']: raise errors.InvalidArgTypeInArgs('x', 'x | y') if e2.get_type().name not in ['double', 'logical', 'integer']: raise errors.InvalidArgTypeInArgs('y', 'x | y') res = VectorItem(None, atomic_or(e1.items[0].value, e2.items[0].value)) ret = VectorObj.create([res]) return ret
def compute(self, env: Environment): e: DotsObj = env.find_object('...') for index, param in enumerate(e.items): if param[1].get_type().name not in [ 'character', 'double', 'integer', 'logical' ]: raise errors.ArgumentCannotBeHandledByFun( index + 1, param[1].get_type().name, 'cat') for atom in param[1].items: rep = atom.show_self() print(rep) return NULLObj()
def compute(self, env: Environment): e1 = env.find_object('x') e2 = env.find_object('y') if e1.get_type().name not in ['double', 'logical', 'integer']: raise errors.InvalidArgTypeInArgs('x', 'x || y') if e2.get_type().name not in ['double', 'logical', 'integer']: raise errors.InvalidArgTypeInArgs('y', 'x || y') res = perform_op_on_vector_or_atomic( e1, e2, lambda l, r: cast_atomic(atomic_power(l, r))) res = [VectorItem(None, el) for el in res] ret = VectorObj.create(res) return ret
def compute(self, env: Environment): e1 = env.find_object('e1') e2 = env.find_object('e2') if isinstance(e2, EmptyParamObj): if e1.get_type().name not in ['double', 'logical', 'integer']: raise errors.InvalidArgTypeInArgs('x', '- x') res = [] for item in e1.items: res.append( atomic_subtract(Atomic(False, types.LogicalType()), item.value)) res = [VectorItem(None, el) for el in res] ret = VectorObj(res) return ret if e1.get_type().name not in ['double', 'logical', 'integer']: raise errors.InvalidArgTypeInArgs('x', 'x - y') if e2.get_type().name not in ['double', 'logical', 'integer']: raise errors.InvalidArgTypeInArgs('y', 'x - y') as_integer = False if e1.get_type().name == 'integer' and e2.get_type().name == 'integer': as_integer = True elif e1.get_type().name == 'integer' and e2.get_type( ).name == 'logical': as_integer = True elif e1.get_type().name == 'logical' and e2.get_type( ).name == 'integer': as_integer = True res = perform_op_on_vector_or_atomic( e1, e2, lambda l, r: cast_atomic(atomic_subtract(l, r), 'integer' if as_integer else 'double')) res = [VectorItem(None, el) for el in res] if len(res) == 0: t = get_more_important_vector_type(e1.get_type(), e2.get_type()) return VectorObj([], t) ret = VectorObj.create(res) return ret
def compute(self, env: Environment): dots: DotsObj = env.find_object('...') if dots.get_type().name == 'NULL': return NULLObj() items = dots.items if len(items) == 0: return NULLObj() items_types = set(map(lambda i: i[1].get_type().name, items)) casts = ['list', 'character', 'double', 'integer', 'logical'] if set(casts).issubset(items_types): list_fun: FunctionObj = built_in_env.find_function('list') ret = list_fun.compute(env) return ret flat_items = [] for item in items: if isinstance(item[1], Atomic): flat_items.append(VectorItem(item[0], item[1])) elif isinstance(item[1], VectorObj): flat_items.extend(item[1].items) else: raise Exception('Invalid item in c function - {}'.format(item)) if 'character' in items_types: if len(items_types) > 1: flat_items = cast_vector_items(flat_items, str, types.CharacterType) elif 'double' in items_types: if len(items_types) > 1: flat_items = cast_vector_items(flat_items, float, types.DoubleType) elif 'integer' in items_types: if len(items_types) > 1: flat_items = cast_vector_items(flat_items, int, types.IntegerType) elif 'logical' in items_types: if len(items_types) > 1: flat_items = cast_vector_items(flat_items, bool, types.LogicalType) else: raise Exception( "unknown vector type in types - {}".format(items_types)) return VectorObj.create(flat_items)
def compute(self, env: Environment): e1 = env.find_object('x') e2 = env.find_object('y') if e1.get_type().name not in ['double', 'logical', 'integer']: raise errors.InvalidArgTypeInArgs('x', 'x && y') if e2.get_type().name not in ['double', 'logical', 'integer']: raise errors.InvalidArgTypeInArgs('y', 'x && y') try: res = perform_op_on_vector_or_atomic(e1, e2, atomic_and) except UnsupportedVectorType as e: raise errors.InvalidArgTypeInArgs( 'x' if e.operand_index == 0 else 'y', 'x && y') res = [VectorItem(None, el) for el in res] ret = VectorObj.create(res) return ret
def compute(self, env: Environment): e1 = env.find_object('x') if e1.get_type().name not in ['double', 'logical', 'integer']: raise errors.InvalidArgType() res = [] if isinstance(e1, VectorObj): if len(e1.items) == 0: return VectorObj([], type(e1.get_type())()) for item in e1.items: res.append(atomic_not(item[1])) else: res.append(atomic_not(e1)) res = [VectorItem(None, el) for el in res] ret = VectorObj.create(res) return ret
def evaluate(self, env: Environment): ret = env.find_object(self.name) return ret