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 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 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 Term(t): #print('Prping ',t) if (t == exp.Unterm): printf("?") return if (exp.VarP(t)): pio.WriteItem(exp.Var(t)) return if (exp.LiteralP(t)): lv = exp.LiteralValue(t) if pop.WordP(lv): printf(' "' + pio.Words(lv) + '" ') return pio.WriteItem(lv) return if exp.IfP(t): If(t) return if (exp.OperationP(t)): Operation(t) return if (exp.CallP(t)): Call(t) return if (exp.WhereP(t)): Where(t) return if (exp.DefinitionP(t)): Definition(t) return if exp.ListexpressionP(t): Listexpression(t) return printf('Huh? ') pio.WriteItem(t) print() assert False, 'strange term for prp.Term: ' + str(t)
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 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
AssemblyDisplay(asprog) #sys.setrecursionlimit(10000) # evaluate pronouns columns = 1 rows = 3 numformat = '' if "rows" in asprog: rows = NumVal(Eval(asprog, "rows")) if "columns" in asprog: columns = NumVal(Eval(asprog, "columns")) if "numformat" in asprog: pio.numformat = StringVal(Eval(asprog, "numformat")) #def runprog(): for t in range(rows): treg = t for s in range(columns): sreg = s val = Eval(asprog, "output") if EosP(val) or EodP(val): break pio.WriteItem(val) pio.printf(' ') print() if pop.EodP(val): break #runprog() print('Number of evals: ' + str(evalcount)) print('warehouse saves ' + str(wsaves) + ' warehouse finds ' + str(wfinds)) print('Number of place codes generated ' + str(newcode)) print('Number of calculations ', clc.calccount) print("Vars fetched ", fetchedvars)
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))