Esempio n. 1
0
 def takeFactor(self, tokens):
     now = tokens.front()
     if now == '(':
         try:
             tokens.popFront()
             exp = self.takeExpression(tokens)
             tokens.popFront()
         except IndexError:
             raise InvalidFormula()
         return Paranthesis(exp)
     elif Function.isSingleParamFunction(now) == True:
         try:
             self.functions.add(now)
             tokens.popFront()
             lparan = tokens.popFront()
             exp = self.takeExpression(tokens)
             rparan = tokens.popFront()
             if (lparan == '(' and rparan == ')') == False:
                 raise InvalidFormula()
         except IndexError:
             raise InvalidFormula()
         return Function.determine(now, param=exp)
     elif Function.isDoubleParamFunction(now) == True:
         try:
             self.functions.add(now)
             tokens.popFront()
             lparan = tokens.popFront()
             exp1 = self.takeExpression(tokens)
             tokens.popFront()
             exp2 = self.takeExpression(tokens)
             rparan = tokens.popFront()
             if (lparan == '(' and rparan == ')') == False:
                 raise InvalidFormula()
         except IndexError:
             raise InvalidFormula()
         return Function.determine(now, base=exp1, exponential=exp2)
     elif Number.isNumber(now) == True:
         tokens.popFront()
         return Number.determine(now)
     elif now == '-':
         try:
             tokens.popFront()
             fac = self.takeFactor(tokens)
             term = Term([Number(-1), fac], ['*'])
             expr = Expression([term], [])
         except IndexError:
             raise InvalidFormula()
         return Paranthesis(expr)
     elif Variable.isVariable(now) == True:
         tokens.popFront()
         return Variable(now)
     else:
         raise InvalidFormula()
Esempio n. 2
0
	def takeFactor(self, tokens):
		now = tokens.front()
		if now == '(':
			try:
				tokens.popFront()
				exp = self.takeExpression(tokens)
				tokens.popFront()
			except IndexError:
				raise InvalidFormula()
			return Paranthesis(exp)
		elif Function.isSingleParamFunction(now)==True:
			try:
				self.functions.add(now)
				tokens.popFront()
				lparan=tokens.popFront()
				exp = self.takeExpression(tokens)
				rparan=tokens.popFront()
				if (lparan=='(' and rparan==')')==False:
					raise InvalidFormula()
			except IndexError:
				raise InvalidFormula()
			return Function.determine(now, param=exp)
		elif Function.isDoubleParamFunction(now)==True:
			try:
				self.functions.add(now)
				tokens.popFront()
				lparan=tokens.popFront()	
				exp1 = self.takeExpression(tokens)
				tokens.popFront()
				exp2 = self.takeExpression(tokens)
				rparan=tokens.popFront()
				if (lparan=='(' and rparan==')')==False:
					raise InvalidFormula()
			except IndexError:
				raise InvalidFormula()
			return Function.determine(now, base=exp1, exponential=exp2)
		elif Number.isNumber(now)==True:
			tokens.popFront()
			return Number.determine(now)
		elif now == '-':
			try:
				tokens.popFront()
				fac = self.takeFactor(tokens)
				term=Term([Number(-1),fac],['*'])
				expr=Expression([term],[])
			except IndexError:
				raise InvalidFormula()
			return Paranthesis(expr)
		elif Variable.isVariable(now)==True:
			tokens.popFront()
			return Variable(now)
		else:
			raise InvalidFormula()