def Operation(t): s = exp.OperationSymbol(t) al = exp.OperationOperandL(t) n = pop.Length(al) if (n == 0): printf(" ") pio.WriteItem(exp.OperationSymbol(t)) printf(" ") return if (exp.PrefixP(s)): Prefix(t) return if (exp.InfixP(s)): Infix(t) return if (s == exp.LLISTPARENWord): printf("[% ") Termlist(al) printf(" %]") return if s == exp.LSETPARENWord: printf("{% ") Termlist(al) printf(" %}") return pio.WriteItem(exp.OperationSymbol(t)) printf("(") Termlist(al) printf(")")
def TermLprecedence(t): # the force with which term t binds from the left (to its right operands) */ if (exp.VarP(t) or exp.LiteralP(t)): return 100 if (exp.OperationP(t)): s = exp.OperationSymbol(t) if (exp.InfixP(s) or exp.PrefixP(s)): return exp.Lprecedence(exp.OperationSymbol(t)) return 100 return 100
def Prefix(t): s = exp.OperationSymbol(t) a = exp.OperationOperand1(t) pio.WriteItem(s) printf(" ") if (exp.Rprecedence(s) > TermLprecedence(a)): bprp(a) else: Term(a)
def Infix(t): """print a term with an infix operator""" s = exp.OperationSymbol(t) #print('Infix called with ',s) #debug a1 = exp.OperationOperand1(t) a2 = exp.OperationOperand2(t) if (exp.Lprecedence(s) > TermRprecedence(a1)): bprp(a1) else: Term(a1) printf(" ") pio.WriteItem(s) printf(" ") if (exp.Rprecedence(s) > TermLprecedence(a2)): bprp(a2) else: Term(a2)
def Aoperation(e): """ returns e1 dl where dl is the list of atomic definitions generated and e1 is the new atomic expression """ o = exp.OperationSymbol(e) #operator l = exp.OperationOperandL(e) #operands vl = pop.Empty #new variables introduced aeqs = pop.Empty #list of equations generated while l != pop.Empty: #iterate down operand list opd = pop.Head(l);l = pop.Tail(l) #get next operand and advance if exp.VarP(opd): #if it's a var nothing to do vl = pop.Cons(opd,vl) continue ae, dl = Aexpr(opd) # process the operand aeqs = pop.Append(dl,aeqs) # collect equations generated if not exp.VarP(ae): nv = VarGen() # generate a new variable vl = pop.Cons(nv,vl) # save it d = exp.DefinitionC(nv,pop.EQUALWord,ae) # generate new atomic definition aeqs = pop.Cons(d,aeqs) else: vl = pop.Cons(ae,vl) vl = pop.Reverse(vl) # variables accumulated in reverse order e1 = exp.OperationC(o,vl) # new atomic expression return e1, aeqs # return the atomic expression and equations generated