def NoEmbeddeds(e): #print('--> ');prp.Term(e);print() if LiteralP(e): return True if VarP(e): return True if CallP(e): return True if OperationP(e): pop.ForAll(OperationOperandL(e), NoEmbeddeds) return True if WhereP(e): print('Embedded where clause:') prp.Term(e) print() exit() if DefinitionP(e): lhs, es, rhs = DefinitionD(e) if WhereP(rhs): subj, wk, body = WhereD(rhs) NoEmbeddeds(subj) pop.ForAll(body, NoEmbeddeds) return True else: NoEmbeddeds(rhs) return True assert False, 'weird term for NoEmbeddeds ' + str(e)
def CheckLocals(e): """ check that locals are variables """ #prp.printf('chl -> ');prp.Term(e);print() if LiteralP(e): return True if VarP(e): return True if OperationP(e): pop.ForAll(OperationOperandL(e), CheckLocals) return True if CallP(e): args = CallActualsL(e) pop.ForAll(args, CheckLocals) return True if WhereP(e): subj, wk, body = WhereD(e) CheckLocals(subj) pop.ForAll(body, CheckLocals) ll = WhereLhssL(e) while ll != pop.Empty: lhs, ll = pop.DeCons(ll) if VarP(lhs): continue if CallP(lhs) and VarP(CallFun(lhs)): continue prp.printf('locals of a where clause must be variables not ') prp.Term(lhs) print() exit() return True if DefinitionP(e): lhs, es, rhs = DefinitionD(e) if CallP(lhs): ll = CallActualsL(lhs) while ll != pop.Empty: v, ll = pop.DeCons(ll) if VarP(v): continue print( 'Formals of a function definition must be variables not ') prp.Term(v) print() exit() CheckLocals(rhs) return True assert False, 'weird term for checklocals ' + pio.Items(e)
""" globalize definition d """ if VarDefinitionP(d): #simple var definition return d #nothing to do fun, formals,es, rhs = FunDefinitionD(d) grhs = Gterm(rhs,formals) return FunDefinitionC(fun,formals,es,grhs) def Gterm(t,formals): """ apply globals to all nonformals in t""" if LiteralP(t): return t if VarP(t): if pop.Occurs(Var(t),formals): return t return Operation1C(GLOBALWord,t) if OperationP(t): o,opds = OperationD(t) gopds = pop.ConsAll2(opds,Gterm,formals) return OperationC(o,gopds) if CallP(t): fun, actuals = CallD(t) gactuals = pop.ConsAll2(actuals,Gterm,formals) return CallC(fun,gactuals) print('Cannot globalize ') pio.WriteItemln(t) exit() if __name__ == "__main__": pg = prs.ParseFile("testprog.lu") gpg = Gprogram(pg) prp.Term(gpg) print()
while not pop.EmptyP(funset): #run through the functions fun, funset = pop.AddD(funset) ok, actsl = map.Apply(amap, fun) if not ok: print('No actuals recorded for ' + str(fun)) exit() ok, formals = map.Apply(formap, fun) fms = formals al = actsl while not pop.EmptyP(fms): acts = pop.ConsAll(al, pop.Head) al = pop.ConsAll(al, pop.Tail) fm, fms = pop.ConsD(fms) actexpr = OperationC(ACTUALWord, acts) df = DefinitionC(VarC(fm), EQUALWord, actexpr) deflist = pop.Cons(df, deflist) return deflist if __name__ == "__main__": pg = prs.ParseFile("testprog.lu") atab, ftab, ypg = ActualTable(pg, map.EmptyMap, map.EmptyMap) prp.Term(ypg) print() pio.WriteItemln(atab) pio.WriteItemln(ftab) defs = ActualDefs(atab, ftab) while not pop.EmptyP(defs): df, defs = pop.ConsD(defs) prp.Term(df) print()
rvname = pop.WordName(v) return pop.WordC(rvname) def Rdefinition(d, m, m1): #rename a definition m being the outer map lhs, es, rhs = DefinitionD(d) if es == EQUALWord: #recursive definition if VarP(lhs): return DefinitionC(Rename(lhs, m1), EQUALWord, Rename(rhs, m1)) l = CallActualsL(lhs) lv = pop.ConsAll(l, Var) n = NewMap(lv) n1 = map.Update(m1, n) return DefinitionC(Rename(lhs, n1), EQUALWord, Rename(rhs, n1)) #nonrecursive definition if VarP(lhs): return DefinitionC(Rename(lhs, m1), EQUALWord, Rename(rhs, m)) #rhs renamed with outer map l = CallActualsL(lhs) lv = pop.ConsAll(l, Var) n = NewMap(lv) n1 = map.Update(m1, n) m2 = map.Update(m, n) return DefinitionC(Rename(lhs, n1), EQUALWord, Rename(rhs, m2)) if __name__ == "__main__": pg = prs.ParseFile("testprog.lu") rpg = Rename(pg, pop.EmptySet) prp.Term(rpg) print()
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 varcount = 0 def VarGen(): global varcount s = str(varcount+100) varcount = varcount+1 var = 'V'+s[1:] return exp.VarC(pop.WordC(var)) if __name__ == "__main__": prog = '' while True: ln = input() if ln==';': break prog = prog + ' ' + ln cg = gen.CharStringC(prog) ig = pio.ItemGenChargenC(cg) e = prs.Expr(ig) pio.WriteItemln(e) prp.Term(e);print() e1,dl = Aexpr(e) prp.Term(e1);print(' where') while dl != pop.Empty: prp.Definition(pop.Head(dl));print() dl = pop.Tail(dl)