コード例 #1
0
def eval_postfix(iter):

    s = LinkedList()  #s = stack
    t = VarTree()  #t = tree

    for token in iter:
        if (str(token)).isalnum():
            s.push(token)

        elif token == '=':
            val = s.pop()
            if not (str(val)
                    ).isdigit():  #allows assigning one variable to another
                val = t.lookup(
                    val
                )  #if it finds a variable, grabs its value from the tree
            var = s.pop()
            t.assign(str(var), val)
            s.push(var)

        else:
            right = s.pop()
            left = s.pop()
            s.push(eval(str(left) + token + str(right)))

    #the solution should be the only value in the stack
    sol = s.pop()
    if not (str(sol)).isdigit():
        sol = t.lookup(sol)
    return int(sol)
コード例 #2
0
 def evaluate(self, variables, functions):
     x = functions.lookup(self._name)
     y = VarTree()
     for i in range(len(x[0])):
         y.assign(x[0][i],
                  self._parameter[i].evaluate(variables, functions))
     return x[1].evaluate(y, functions)
コード例 #3
0
 def evaluate(self, variables, functions):
     temp = VarTree()
     [params, body] = functions.lookup(self._name)
     for i in range(len(params)):
         par = params[i]
         parVal = self._args[i].evaluate(variables, functions)
         temp.assign(par, parVal)
     return body.evaluate(temp, functions)
コード例 #4
0
	def evaluate(self, variables, functions):
		parms, body = functions.lookup(self._name)
		newTree = VarTree()
		for i in range(0, len(parms)):
			newTree.assign(parms[i], self._arg[i].evaluate(variables, functions))
		return body.evaluate(newTree, functions)