Example #1
0
def take_index(stack):
    num, l = popn(stack, 2)
    if type(num) == list:
        stack.append(l[num[0]:num[1]])
    else:
        stack.append(l[num])
    return stack
Example #2
0
def filter_(stack):
    cond, l = popn(stack, 2)
    L = []
    for x in l:
        if cond([x])[0]:
            L.append(x)
    stack.append(L)
    return stack
Example #3
0
def map_(stack):
    f, l = popn(stack, 2)
    L = []
    for x in l:
        y = f([x])
        L.append(y[0])
    stack.append(L)

    return stack
def bi(stack):
    f, g, arg = popn(stack, 3)
    # f is a function of a stack and return stack
    # in this way I access the real result
    res1 = g([arg])[0]
    res2 = f([arg])[0]
    stack.append(res1)
    stack.append(res2)
    return stack
def bif(stack):
    # for function which return one output
    stmt, cond = popn(stack, 2)
    stack_ = stack.copy()
    stack = cond(stack)
    if stack[-1]:
        stack = stmt(stack_)
        return stack
        # print ('stack_', stack_)
    else:
        return stack
def bii(stack):
    # for function which return one output
    f, g = popn(stack, 2)
    # aggiornare con deepcopy ovunque
    stack_ = deepcopy(stack)
    stack = f(stack)
    stack_ = g(stack_)
    # print ('ss', stack_)

    stack.append(stack_[-1])
    return stack
Example #7
0
def reduce_(stack):
    f, l = popn(stack, 2)
    if len(l) == 1:
        stack.append(l[0])
        return stack
    while l:
        l = f(l)
        if len(l) == 1:
            break

    stack.append(l[0])
    return stack
Example #8
0
def dountil(stack):
    f, l = popn(stack, 2)
    L = []
    for x in l:
        y = f([x])
        # y è uno stack (una lista), il cui ultimo elemento deve essere un bool
        boolean = y.pop()
        L.append(boolean)
        if boolean:
            break
    stack.append(L)

    return stack
def all(stack):
    # for function which take n input and return one output
    funcList = popn(stack, 1)

    # la prima funzione la faccio fuori dal ciclo
    f = funcList[0]
    stack_ = deepcopy(stack)  # copy of the original stack
    stack = f(stack)
    results = []
    for f in funcList[1:]:
        stack__ = deepcopy(stack_)
        stack__ = f(stack__)
        results.append(stack__[-1])

    stack.extend(results)
    return stack
Example #10
0
def execute(tokens, stack=None, currentEnv=env, path=None, parser=None):
    if stack is None: stack = []
    # if currentEnv is None: currentEnv = env
    for token in tokens:
        if token.head == 'import_stmt':
            name = "".join(token.tail[0].tail)
            loadModule(name, path, parser=parser)

        if token.head == 'assign_stmt':
            type_ = token.tail[0].head
            names = [x.tail[0] for x in token.tail[0].tail]
            # print (names, type_, token.tail[0].tail)
            if type_ == 'assign_normal':
                values = stack[-len(names):]
                # print (values)
                for name, value in zip(names, values):
                    env[name] = value
            if type_ == 'assign_drop':

                for name in reversed(names):
                    value = stack.pop()
                    env[name] = value

            if type_ == 'assign_acc':
                # per il momento gli acc non possono avere liste come target
                value = stack[-1]
                try:
                    env[names[0]].append(value)
                except:
                    env[names[0]] = [value]

            if type_ == 'assign_acc_drop':
                # per il momento gli acc non possono avere liste come target
                value = stack.pop()
                try:
                    env[names[0]].append(value)
                except:
                    env[names[0]] = [value]

        if token.head == 'definition':
            name = token.tail[0].tail[0]

            def _(stack_, token=token):
                if type(token.tail[1]) != list:
                    params = token.tail[1]
                    for par in reversed([par.tail[0] for par in params.tail]):
                        value = stack_.pop()
                        env[par] = value
                    return execute(token.tail[2], stack_)
                else:
                    return execute(token.tail[1], stack_)

            _.__name__ = name
            env[name] = _

        if token.head == 'int':
            stack.append(int(token.tail[0]))

        if token.head == 'float':
            stack.append(float(token.tail[0]))

        if token.head == 'bool':
            if token.tail[0] == 'True':
                stack.append(True)
            else:
                stack.append(False)

        if token.head == 'name':
            try:
                value = env[token.tail[0]]
            except KeyError as error:
                print('Error:', token.tail[0], 'is undefined')
                sys.exit(1)

            try:
                # va sistemato un po sto punto
                stack = value(stack)
            except TypeError as error:
                stack.append(value)
            except UnboundLocalError as error:
                pass

        if token.head == 'string':
            stack.append(token.tail[0][1:-1])

        if token.head == 'list':
            # def _(stack_, token=token):
            #     return execute(token.tail, stack_)
            stack.append(parse_list(token))

        if token.head == 'array':
            stack.append(execute(token.tail, []))

        if token.head == 'concat':
            res = []
            for func in token.tail:
                l_old = len(stack)
                stack = parse_list(func)(stack)
                l_new = len(stack)
                # non posso poppare un solo elemento, devo poppare tutti quelli nuovo calcolati
                # può capitare anche che lo stack sia più corto di prima!
                # devo trovare un modo per far capire quanti sono i valori tornati da func
                arity = ...
                if arity == 1:
                    res.append(stack.pop())
                else:
                    res.extend(popn(stack, arity))
            stack.extend(res)

    return stack
Example #11
0
def interval(stack):
    x, y = popn(stack, 2)
    x = int(math.ceil(x))
    y = int(math.ceil(y))
    stack.append(list(range(y, x + 1)))
    return stack
Example #12
0
def pair(stack):
    a, b = popn(stack, 2)
    stack.append([b, a])
    return stack
Example #13
0
def remove(stack):
    n, l = popn(stack, 2)
    value = l.pop(n)
    stack.append(l)
    stack.append(value)
    return stack
Example #14
0
def z(stack):
    l1, l2 = popn(stack, 2)
    stack.append(list(zip(l2, l1)))
    return stack
Example #15
0
def group(stack):
    n = stack.pop()
    values = popn(stack, n)
    stack.append(list(reversed(values)))
    return stack
Example #16
0
def in_(stack):
    l, num = popn(stack, 2)
    stack.append(num in l)
    return stack
Example #17
0
def append(stack):
    num, l = popn(stack, 2)
    l.append(num)
    stack.append(l)
    return stack
Example #18
0
def switch(stack):
    a, b = popn(stack, 2)
    stack.append(a)
    stack.append(b)
    return stack