def Varlist(m): if (pop.EmptyP(m)): return while True: pio.WriteItem(pop.Head(m)) m = pop.Tail(m) if (pop.EmptyP(m)): return printf(",")
def Definitions(dl): while not pop.EmptyP(dl): Definition(pop.Head(dl)) printf(";") dl = pop.Tail(dl) if pop.EmptyP(dl): return NewLine()
def Termlist(m): if (pop.EmptyP(m)): return while True: Term(pop.Head(m)) m = pop.Tail(m) if pop.EmptyP(m): return printf(",")
def Lists(l): #pop list l as a python string assert pop.ListP(l), 'lists given a non list' m = l b = '[' while not pop.EmptyP(m): b = b+ Items(pop.Head(m)) m = pop.Tail(m) if not pop.EmptyP(m): b=b+' ' return b+']'
def Eprogram(pg): """ return pg with only where clauses for function defs left """ """ assume function defs at top level only """ subj, wk, body = WhereD(pg) b = body ebody = pop.Empty while not pop.EmptyP(b): #run through dfs in body df, b = pop.ConsD(b) #get current def and advance lhs, es, rhs = DefinitionD(df) #dismantle current def if not WhereP(rhs): # no where's ebody = pop.AddElement(ebody, df) #add current def unchanged #print('ebody is',pio.Items(ebody));exit() continue rsubj, rwk, rbody = WhereD(rhs) #dismantle rhs if VarDefinitionP(df): #a var defined by a where rhs1 = Eprogram(rhs) #flatten rhs r1subj, rwk1, r1body = WhereD( rhs1) #dismantle new flat whereclause ebody = pop.Append( ebody, r1body) #add the emptied defs to the program body df1 = DefinitionC(lhs, es, r1subj) #change df so var is equated to subject ebody = pop.AddElement(ebody, df1) #add simplified def continue #a function where definition rhsf = Eprogram(rhs) #flatten the rhs where clause df1 = DefinitionC(lhs, es, rhsf) #construct new definition with flattened rhs ebody = pop.AddElement(ebody, df1) return WhereC(subj, wk, ebody)
def ActualDefs(amap, formap): """ generate defs for the formals in terms of actuals""" """amap is the actuals map formap is the formals map """ funset = map.Domain(amap) #set of function variable deflist = pop.Empty 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
def CurrentItem(ig): """The current item of item generator ig - does not advance""" [m,ci,r] = ig if m == 'l': if ci == '': if pop.EmptyP(r): return '' #end of data ig[1] = pop.Head(r) return ig[1] return ci if m == 'c': if ci == '': skipwhites(r) d = ItemRead(r) if d == '' : return '' ig[1] = d return d return ci assert False, 'unknown item generator type '+m
def Reserve(l): s = set() while not pop.EmptyP(l): s.add(pop.Head(l)) l = pop.Tail(l) return s
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()