def slice_list(args: List, node): """slice of list""" if len(args) < 2: evaluator.error( "(lists.lists.slice) at least 2 parameters expected, {} given.". format(len(args)), node) param = evaluator.evaluate_node(args[0]) if not isinstance(param, list) and not isinstance(param, str): evaluator.error( "(lists.lists.slice) 1st parameter must be of type list or str", node) params = evaluator.evaluate_parallel_args(args[1:]) if not isinstance(params[0], int): evaluator.error( "(lists.lists.slice) 1st parameter must be of type int", node) if len(args) == 2: return param[params[0]:] if not isinstance(params[1], int): evaluator.error( "(lists.lists.slice) 2st parameter must be of type int", node) return param[params[0]:params[1]]
def minus(args: List, _): """Evaluates expression (- a b c d ...) as {a - (b + c + d + ...)}""" params = evaluator.evaluate_parallel_args(args) if len(params) == 1: return -1 * params[0] return params[0] - functools.reduce(lambda x, y: x + y, params[1:])
def modulo(args: List, node): """Evaluates expression (sqrt a) as {sqrt(a)}""" if len(args) != 2: evaluator.error( "(arithmetic.basic.modulo) wrong number of arguments, got {}, 2 expected" .format(str(len(args))), node) params = evaluator.evaluate_parallel_args(args) return params[0] % params[1]
def assert_equal(args: List, node): """First argument must equals to the second one""" if len(args) != 2: evaluator.error("(special_forms.assert_value.assert-equal) 2 parameter expected, {} given".format(len(args)), node) values = evaluator.evaluate_parallel_args(args) if values[0] != values[1]: evaluator.error("assertion failed: {} != {}".format(values[0], values[1]), node) return None
def substr(args: List, _): """python str[x:y] equiv""" if not args: evaluator.error("max 3 arguments expected, {} given".format(len(args))) params = evaluator.evaluate_parallel_args(args) if not isinstance(params[0], str): evaluator.error("first param must be of type str, {} given".format( type(params[0]))) if len(params) == 1: return params[0] if len(params) == 2: if not isinstance(params[1], int): evaluator.error("second param must be of type int") return params[0][params[1]:] elif len(params) == 3: if not isinstance(params[2], int): evaluator.error("third param must be of type int") return params[0][params[1]:params[2]]
def python_getattr(args: List, node): """Call python function from mplisp""" if len(args) != 2: evaluator.error( "(special_forms.module_import.get_attribute) 2 parameters expected, {} given" .format(len(args)), node) params = evaluator.evaluate_parallel_args(args) if not isinstance(params[0], ModuleType): params[0] = import_module(args[0:1], node) if not isinstance(params[1], str): evaluator.error( "(special_forms.module_import.get_attribute) 2st param must be of type str, {} given" .format(type(params[1])), node) attr = getattr(params[0], params[1]) if callable(attr): return function_caller(attr) return attr
def list_ref(args: List, _): """Check whether the first argument is list and the second one is int. Return element on that position""" if len(args) != 2: evaluator.error( "(lists.lists.list-ref) 2 parameters expected, {} given.".format( len(args)), node) params = evaluator.evaluate_parallel_args(args) if not isinstance(params[0], list): evaluator.error( "(lists.lists.list-ref) 1st parameter must be of type list", node) if not isinstance(params[1], int): evaluator.error( "(lists.lists.list-ref) 2st parameter must be of type list", node) if params[1] < 0 or params[1] >= len(params[0]): evaluator.error( "(lists.lists.list-ref) index {} is out of range".format( params[1]), node) return params[0][params[1]]
def func(local_args: List, _): """Callable object""" params = evaluator.evaluate_parallel_args(local_args) return func_obj(*params)
def divide(args: List, _): """Evaluates expression (/ a b c d ...) as {a / (b / (c / (d / ...)..)}""" params = evaluator.evaluate_parallel_args(args) return functools.reduce(lambda x, y: x / y, params)
def multiply(args: List, _): """Evaluates expression (* a b c d ...) as {a * b * c * d * ...)}""" params = evaluator.evaluate_parallel_args(args) return functools.reduce(lambda x, y: x * y, params)
def plus(args: List, _): """Evaluates expression (+ a b c d ...) as {a + b + c + d + ...)}""" params = evaluator.evaluate_parallel_args(args) return functools.reduce(lambda x, y: x + y, params)
def gen_list(args: List, _): """get range(*params)""" params = evaluator.evaluate_parallel_args(args) return list(range(*params))
def create_list(args: List, _): """Create list""" return evaluator.evaluate_parallel_args(args)