def AssemblyDisplay(asprog): for vname in asprog: o, odsa = asprog[vname] prp.printf(vname + ': ') if o == '"': prp.printf('" ') pio.WriteItemln(odsa) continue prp.printf(o + ' ') for od in odsa: prp.printf(od) prp.printf(' ') print()
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)
def Compile(f): """ compile the program in file f to atomic equations """ pio.parsing = True pg = prs.ParseFile(f) print('Program is ') #pio.WriteItemln(pg) prp.Termln(pg) print('Defs to import ', prs.defined) defs = pop.Empty for w in prs.defined: #read parse and add defs of defined operators like wvr wname = WordName(w) f = open("defs/" + wname + ".lu", "r") sourcedef = f.read() f.close() ig = pio.ItemGenStringC(sourcedef) df = prs.Definition(ig) #print('Adding df') #prp.Termln(df);exit() defs = pop.AddElement(defs, df) subj, wk, body = exp.WhereD(pg) body = pop.Append(body, defs) #add imported definitions pg = exp.WhereC(subj, wk, body) print('Expanded program') prp.Termln(pg) #print("check locals") pre.CheckLocals(pg) print("check dups") pre.CheckDups(pg) print("check embeddings") pre.ProgramEmbedded(pg) print('renamed program ') rpg = ren.Rename(pg, pop.EmptySet) prp.Termln(rpg) m = ari.Aritytab(rpg, map.EmptyMap) ok, var, arity = ari.ArityCheck(rpg, m) if ok: k = 1 print('arities check out ') else: prp.printf("Variable ") pio.WriteItem(var) prp.printf(" should have arity ") print(arity) exit() lpg = elp.Eloop(rpg, map.EmptyMap) print('Delooped program ') prp.Termln(lpg) fpg = ewh.Eprogram(lpg) print('flattened program') pio.WriteItemln(fpg) prp.Termln(fpg) atab, ftab, ypg = ali.ActualTable(fpg, map.EmptyMap, map.EmptyMap) print('alified program') prp.Termln(ypg) defs = ali.ActualDefs(atab, ftab) subj, wk, body = exp.WhereD(ypg) body = pop.Append(body, defs) #add ali defs ypg = exp.WhereC(subj, wk, body) #print('globalized program') ypg = glb.Gprogram(ypg) #prp.Termln(ypg) print('reflattend program') ypg = ewh.Eprogram(ypg) prp.Termln(ypg) print('atomized program') apg, dfs = atz.Aexpr(ypg) prp.Termln(apg) return apg
def Expect(w,ig): if pio.CurrentItem(ig) == w: pio.NextItem(ig); return True prp.printf('Expected ');pio.WriteItemln(w) print('Found '); pio.Dump5(ig) print() exit()
def ArityCheckList(l, m): while l != pop.Empty: e, l = pop.ConsD(l) ok, v, n = ArityCheck(e, m) if not ok: return False, v, n return True, None, None def AritytabList(l, m): while l != pop.Empty: e, l = pop.ConsD(l) m = Aritytab(e, m) return m if __name__ == "__main__": pg = prs.ParseFile("testprog.lu") print('renamed program ') rpg = ren.Rename(pg, pop.EmptySet) prp.Termln(rpg) m = Aritytab(rpg, map.EmptyMap) pio.WriteItemln(m) ok, var, arity = ArityCheck(rpg, m) if ok: print('arities check out ') else: prp.printf("Variable ") pio.WriteItem(var) prp.printf(" should have arity ") print(arity) print(ArityCheck(rpg, m))