Sha256: 78e95c9eea9950cc006f311d332c30d935796588fa3a9a481587726ebd686192

Contents?: true

Size: 1.8 KB

Versions: 78

Compression:

Stored size: 1.8 KB

Contents

class StackUnderflowError(Exception):
    pass


def is_integer(string):
    try:
        int(string)
        return True
    except ValueError:
        return False


def evaluate(input_data):
    if not input_data:
        return []
    defines = {}
    while input_data[0][:1] == ':':
        values = input_data.pop(0).split()
        values.pop()
        values.pop(0)
        key = values.pop(0).lower()
        if is_integer(key):
            raise ValueError("Integers cannot be redefined")
        defines[key] = values
    stack = []
    input_data = input_data[-1].split()
    while any(input_data):
        word = input_data.pop(0).lower()
        try:
            if is_integer(word):
                stack.append(int(word))
            elif word in defines:
                input_data = defines[word] + input_data
            elif word == '+':
                stack.append(stack.pop() + stack.pop())
            elif word == '-':
                stack.append(-stack.pop() + stack.pop())
            elif word == '*':
                stack.append(stack.pop() * stack.pop())
            elif word == '/':
                divisor = stack.pop()
                if divisor == 0:
                    raise ZeroDivisionError("Attempted to divide by zero")
                stack.append(int(stack.pop() / divisor))
            elif word == 'dup':
                stack.append(stack[-1])
            elif word == 'drop':
                stack.pop()
            elif word == 'swap':
                stack.append(stack[-2])
                del stack[-3]
            elif word == 'over':
                stack.append(stack[-2])
            else:
                raise ValueError("{} has not been defined".format(word))
        except IndexError:
            raise StackUnderflowError("Insufficient number of items in stack")
    return stack

Version data entries

78 entries across 78 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/python/exercises/forth/example.py
trackler-2.2.1.179 tracks/python/exercises/forth/example.py
trackler-2.2.1.178 tracks/python/exercises/forth/example.py
trackler-2.2.1.177 tracks/python/exercises/forth/example.py
trackler-2.2.1.176 tracks/python/exercises/forth/example.py
trackler-2.2.1.175 tracks/python/exercises/forth/example.py
trackler-2.2.1.174 tracks/python/exercises/forth/example.py
trackler-2.2.1.173 tracks/python/exercises/forth/example.py
trackler-2.2.1.172 tracks/python/exercises/forth/example.py
trackler-2.2.1.171 tracks/python/exercises/forth/example.py
trackler-2.2.1.170 tracks/python/exercises/forth/example.py
trackler-2.2.1.169 tracks/python/exercises/forth/example.py
trackler-2.2.1.167 tracks/python/exercises/forth/example.py
trackler-2.2.1.166 tracks/python/exercises/forth/example.py
trackler-2.2.1.165 tracks/python/exercises/forth/example.py
trackler-2.2.1.164 tracks/python/exercises/forth/example.py
trackler-2.2.1.163 tracks/python/exercises/forth/example.py
trackler-2.2.1.162 tracks/python/exercises/forth/example.py
trackler-2.2.1.161 tracks/python/exercises/forth/example.py
trackler-2.2.1.160 tracks/python/exercises/forth/example.py