Example #1
0
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()))
Example #2
0
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
Example #3
0
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)
Example #4
0
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()}")
Example #5
0
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
Example #6
0
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)
Example #7
0
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
Example #8
0
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