from collections import deque
import re


class Formula(object):

    """
        Formula allows translation from a prefix-notation expression in a string to a complex number.
            This is eventually to be replaced with a cython, c++, or openCL implementation as I'm sure
            the performance of this class is pretty horrible.

    """
    def __init__(self, formulaString='+ ** z 2 c'):
        self.formulaString = formulaString
        self.functions = None

    """
        Compile:
            This method takes in a the prefix statement and evaluates it for given values z and c.
    """
    def compile(self, c, z):
        form = deque(self.formulaString.split(' '))
        return self.parse(form, c, z)
            return self.parse(queuestring, c, z) + self.parse(queuestring, c, z)
        elif (value == '-'):
            return self.parse(queuestring, c, z) - self.parse(queuestring, c, z)
        elif (value == '*'):
            return self.parse(queuestring, c, z) * self.parse(queuestring, c, z)
        elif (value == '/'):
            return self.parse(queuestring, c, z) / self.parse(queuestring, c, z)
        elif (value == '^' or value == '**'):
            return self.parse(queuestring, c, z) ** self.parse(queuestring, c, z)
        elif (value == 'mod' or value == '%'):
            return self.parse(queuestring, c, z) % self.parse(queuestring, c, z)
        elif (value == 'rpart'):
            return complex(self.parse(queuestring, c, z)).real
        elif (value == 'ipart'):
            return complex(self.parse(queuestring, c, z)).imag
        elif (value == 'z'):
            return z
        elif (value == 'c'):
            return c
        elif (re.compile('^[\d\.]+').match(value)):
            return float(value)
        elif (re.compile('^[\d\.]+[\+\-][\d\.]+i$').match(value)):
            nums = re.split('[\+\-]', value)
            return complex(float(nums[0], nums[1][:-1]))
        else:
            return EOFError

    def parse(self, queuestring, c, z=0.0):
        value = queuestring.popleft()
        if (value == '+'):