def tree_sum(iterator): a = tree_product(iterator) while peek(iterator) == '+' or peek(iterator) == '-': operation = next(iterator) b = tree_product(iterator) a = Oper(a, operation, b) return a
def next_op(i): #finds the next operator while peek(i) is not None: if peek(i) in [")", "("]: return None elif peek(i) in ["*", "/", "^", "&", "+", "-"]: return peek(i) next(i)
def tree_product(iterator): a = tree_factor(iterator) while peek(iterator) == '*' or peek(iterator) == '/' or peek(iterator) == '%': operation = next(iterator) b = tree_factor(iterator) a = Oper(a, operation, b) return a
def tree_factor(iter): """Handles parentheses""" next = str(next(iter)) if next == '(': next(iter) ans = tree_assign( iter ) #recursively makes a new expr subtree for the contents of the parens next(iter) #passes over ) return ans elif next.isdigit(): return Value(next(iter)) elif peek(iter) == '(': args = [] while peek(iter) != ')': next(iter) args.append(tree_assign(iter)) next(iter) return Funct(nxt, args) elif next.isalpha(): return Var(next(iter))
def tree_sum(iterator): left = tree_product(iterator) while (peek(iterator) == "+" or peek(iterator) == "-"): oper = next(iterator) right = tree_product(iterator) left = Oper(left, oper, right) return left
def tree_factor(iterator): peeking = peek(iterator) #check negative if (peeking == '-'): next(iterator) temp = tree_factor(iterator) return Value(int('-' + temp.__str__())) #check open parathesis if (peeking == '('): next(iterator) temp = tree_assign(iterator) next(iterator) return temp else: temp = next(iterator) if (temp[0].isalpha() and peek(iterator) == '('): parms = [] next(iterator) while (peek(iterator) not in operators): if (peek(iterator) != ','): parms.append(tree_assign(iterator)) else: next(iterator) next(iterator) return Func(temp, parms) elif (temp[0].isalpha()): return Var(temp) else: return Value(temp)
def postfix_sum(iterator): left_child = postfix_product(iterator) while (peek(iterator) == "+" or peek(iterator) == "-"): join = peek(iterator) next(iterator) right_child = postfix_product(iterator) left_child = Oper(left_child, join, right_child) return left_child
def tree_relational(iterator): temp = tree_sum(iterator) peeking = peek(iterator) if (peeking in relaOpers): next(iterator) temp = Oper(temp, peeking, tree_sum(iterator)) peeking = peek(iterator) return temp
def tree_assign(iterator): temp = tree_conditional(iterator) peeking = peek(iterator) if (peeking == '='): next(iterator) temp = Oper(temp, peeking, tree_assign(iterator)) peeking = peek(iterator) return temp
def postfix_sum (iterator): yield from postfix_product(iterator) while peek(iterator) == '+' or peek(iterator) == '-': sign = next(iterator) #should be an operator sign yield from postfix_product(iterator) yield sign
def postfix_product (iterator): yield from postfix_factor(iterator) while peek(iterator) == '*' or peek(iterator) == '/' or peek(iterator) =='%': sign = next(iterator) #should be an operator sign yield from postfix_factor(iterator) yield sign
def tree_product(iterator): left = tree_factor(iterator) while (peek(iterator) == "*" or peek(iterator) == "/" or peek(iterator) == "%"): oper = next(iterator) right = tree_factor(iterator) left = Oper(left, oper, right) return left
def tree_relation(iterator): a = tree_sum(iterator) if peek(iterator) == "<" or peek(iterator) == ">" or peek(iterator) == "<=" or peek(iterator) == ">=" or peek(iterator) == "==" or peek(iterator) == "!=": operation = next(iterator) b = tree_sum(iterator) return Oper(a, operation, b) else: return a
def postfix_product(iterator): left_child = postfix_factor(iterator) while (peek(iterator) == "*" or peek(iterator) == "/" or peek(iterator) == "%" or peek(iterator) == "//"): join = peek(iterator) next(iterator) right_child = postfix_factor(iterator) left_child = Oper(left_child, join, right_child) return left_child
def postfix_keyword(iterator): left_child = postfix_relation(iterator) while (peek(iterator) == "and" or peek(iterator) == "or" or peek(iterator) == "not"): join = peek(iterator) next(iterator) right_child = postfix_relation(iterator) left_child = Oper(left_child, join, right_child) return left_child
def tree_sum( iter ): left = tree_product( iter ) oper = peek(iter) while oper == '+' or oper == '-': next(iter) right = tree_product( iter ) left = Oper( left, oper, right ) oper = peek(iter) return left
def tree_product( iter ): left = tree_factor( iter ) oper = peek(iter) while oper == '*' or oper == '/' or oper == '%': next(iter) right = tree_factor( iter ) left = Oper( left, oper, right ) oper = peek(iter) return left
def tree_rel( iter ): left = tree_sum( iter ) oper = peek(iter) while oper in ['<','<=','>','>=','!=','==']: next(iter) right = tree_sum( iter ) left = Oper( left, oper, right ) oper = peek(iter) return left
def tree_sum(iter): """Handles addition""" left = tree_prod(iter) while peek(iter) == '+' or peek(iter) == '-': sign = next(iter) right = tree_prod(iter) left = Oper(left, sign, right) return left
def postfix_sum(iterator): op1 = postfix_product(iterator) oper = peek(iterator) while (oper == "+" or oper == "-"): next(iterator) op2 = postfix_product(iterator) Node = Oper(op1, oper, op2) op1 = Node oper = peek(iterator) return op1
def postfix_assign(iterator): op1 = postfix_conditional(iterator) oper = peek(iterator) while (oper == "="): next(iterator) op2 = postfix_assign(iterator) Node = Oper(op1, oper, op2) op1 = Node oper = peek(iterator) return op1
def postfix_product(iterator): op1 = postfix_factor(iterator) oper = peek(iterator) while (oper == "*" or oper == "/" or oper == "%"): next(iterator) op2 = postfix_factor(iterator) Node = Oper(op1, oper, op2) op1 = Node oper = peek(iterator) return op1
def postfix_conditional(iterator): t = postfix_keyword(iterator) if (peek(iterator) == "?"): next(iterator) true = postfix_keyword(iterator) if (peek(iterator) == ":"): next(iterator) false = postfix_keyword(iterator) t = Cond(t, true, false) return t
def postfix_relational(iterator): op1 = postfix_sum(iterator) oper = peek(iterator) while (oper == "<=" or oper == ">=" or oper == "==" or oper == "!=" or oper == ">" or oper == "<" or oper == "and" or oper == "or"): next(iterator) op2 = postfix_sum(iterator) Node = Oper(op1, oper, op2) op1 = Node oper = peek(iterator) return op1
def tree_product(iterator): #call factor to evaluate order of operation temp = tree_factor(iterator) peeking = peek(iterator) #this while loop evaluates product and multiplication #yield data back up from factor then yield the sign like postfix while (peeking == '*' or peeking == '/' or peeking == '%' or peeking == '//'): next(iterator) temp = Oper(temp, peeking, tree_factor(iterator)) peeking = peek(iterator) return temp
def define_func(iterator): next(iterator) name = next(iterator) next(iterator) parms = [] while peek(iterator) is not ')': parms.append(next(iterator)) if peek(iterator) == ',': next(iterator) next(iterator) next(iterator) body = tree_assign(iterator) return name, parms, body
def postfix_conditional(iterator): op1 = postfix_relational(iterator) oper = peek(iterator) while (oper == "?"): next(iterator) op2 = postfix_conditional(iterator) oper = peek(iterator) while (oper == ":"): next(iterator) op3 = postfix_conditional(iterator) oper = peek(iterator) Node = Cond(op1, op2, op3) op1 = Node oper = peek(iterator) return op1
def postfix_factor(iterator): if peek(iterator) == "(": next(iterator) dub = postfix_assign(iterator) next(iterator) return dub else: if (peek(iterator).isdigit()): join = peek(iterator) next(iterator) return Value(join) elif (peek(iterator).isalpha()): join = peek(iterator) next(iterator) return Var(join)
def tree_sum(iterator): # evaluate the first term while checking for the next term for multiplication temp = tree_product(iterator) #store the operator #this while loop evaluates sum and subtraction. #yield data back up from product then yield the sign like postfix peeking = peek(iterator) while (peeking == '+' or peeking == '-'): next(iterator) temp = Oper(temp, peeking, tree_product(iterator)) peeking = peek(iterator) # skip the ')' and continue changing the expression #if(peeking == ')'): # next(iterator) return temp
def postfix_assign(iterator): left_child = postfix_conditional(iterator) if peek(iterator) == "=": next(iterator) right_child = postfix_assign(iterator) left_child = Oper(left_child, "=", right_child) return left_child