def _len(*args) -> object.Object: if len(args) != 1: return evaluator.new_error('wrong number of arguments. got={}, want=1'.format(len(args))) if type(args[0]) == object.Array: return object.Integer(len(args[0].elements)) elif type(args[0]) == object.String: return object.Integer(len(args[0].value)) else: return evaluator.new_error('argument to `len` not supported, got {}'.format(args[0].type()))
def first(*args: List[object.Object]) -> object.Object: if len(args) != 1: return evaluator.new_error('wrong number of arguments. got={}, want=1', len(args)) if args[0].type() != object.ARRAY_OBJ: return evaluator.new_error('argument to `first` must be ARRAY, got {}'.format(args[0].type())) arr = args[0] if len(arr.elements) > 0: return arr.elements[0] return evaluator.NULL
def _push(*args): arguments = args[0] arg = arguments[0] if len(arguments) != 2: return evaluator.new_error( f"wrong number of arguments. got={len(arguments)}, want=2") elif not isinstance(arg, Array): return evaluator.new_error( f"argument to `push` must be ARRAY, got {arg.object_type()}") new_array = arg.elements[:] new_array.append(arguments[1]) return Array(new_array)
def _len(*args): arguments = args[0] arg = arguments[0] if len(arguments) != 1: return evaluator.new_error( f"wrong number of arguments. got={len(arguments)}, want=1") elif isinstance(arg, Array): return Integer(len(arg.elements)) elif isinstance(arg, String): return Integer(len(arg.value)) return evaluator.new_error( f"argument to `len` not supported, got {arg.object_type()}")
def _first(*args): arguments = args[0] arg = arguments[0] if len(arguments) != 1: return evaluator.new_error( f"wrong number of arguments. got={len(arguments)}, want=1") elif not isinstance(arg, Array): return evaluator.new_error( f"argument to `first` must be ARRAY, got {arg.object_type()}") if len(arg.elements) > 0: return arg.elements[0] return NULL
def push(*args: List[object.Object]) -> object.Object: if len(args) != 2: return evaluator.new_error('wrong number of arguments. got={}, want=2', len(args)) if args[0].type() != object.ARRAY_OBJ: return evaluator.new_error('argument to `push` must be ARRAY, got {}'.format(args[0].type())) arr = args[0] new_elements = copy.deepcopy(arr.elements) new_elements.append(args[1]) return object.Array(new_elements)
def rest(*args: List[object.Object]) -> object.Object: if len(args) != 1: return evaluator.new_error('wrong number of arguments. got={}, want=1', len(args)) if args[0].type() != object.ARRAY_OBJ: return evaluator.new_error('argument to `rest` must be ARRAY, got {}'.format(args[0].type())) arr = args[0] length = len(arr.elements) if length > 0: new_elements = copy.deepcopy(arr.elements[1:length]) return object.Array(new_elements) return evaluator.NULL
def _rest(*args): arguments = args[0] arg = arguments[0] if len(arguments) != 1: return evaluator.new_error( f"wrong number of arguments. got={len(arguments)}, want=1") elif not isinstance(arg, Array): return evaluator.new_error( f"argument to `rest` must be ARRAY, got {arg.object_type()}") length = len(arg.elements) if length > 0: new_array = arg.elements[1:] return Array(new_array) return NULL