def Assembly(aprog): """ convert atomic program aprog to assembly program asprog """ asprog = {} subj, wk, dl = exp.WhereD(aprog) if exp.VarP(subj): subj = exp.Operation1C(exp.IDWord, subj) odef = exp.DefinitionC(exp.VarC(exp.OUTPUTWord), pop.EQUALWord, subj) dl = pop.Cons(odef, dl) asprog = {} #initialize the assembly program, a dictionary #that assigns to every variable name a tuple consisting #of the operation symbol and the array of the names of the variables #all python strings while dl != pop.Empty: #loop through the definitions of the atomized program df, dl = pop.DeCons(dl) lhs, es, rhs = exp.DefinitionD(df) #dismantle current definition vname = pio.Words(exp.Var(lhs)) #get name of var being defined if exp.LiteralP( rhs): #if its a literal treat quote mark as a pseudo op asprog[vname] = ('"', exp.LiteralValue(rhs)) continue if exp.VarP(rhs): # rhs a single variable, add id operator rhs = exp.Operation1C(exp.IDWord, rhs) #its an operation applied to operands o, ods = exp.OperationD( rhs) #break rhs into operation symbol and operand list odsa = [] #initialize array of operands while ods != pop.Empty: #iterate through operand list vw, ods = pop.ConsD(ods) #get current operand, a var, and advance vws = pio.Words( exp.Var(vw)) #name of current operand, a var, as a string odsa.append(vws) #append to the array #finished looping throug operands, so odsa complete asprog[vname] = (pio.Words(o), odsa) return asprog
def Arglist(ig): """ get a comma separated list of expressions""" #doesn't consume terminating item f = Expr(ig) if f == exp.Unterm: return exp.Unterm if pio.CurrentItem(ig) == CommaWord: pio.NextItem(ig) return pop.Cons(f,Arglist(ig)) return pop.List1(f)
def ListBodyRead(g): skipwhites(g) assert CurrentChar(g) != '', 'incomplete list' if CurrentChar(g) == ']': NextChar(g) return pop.Empty i = ItemRead(g) m = ListBodyRead(g) return pop.Cons(i,m)
def Awhere(wc): """ atomize subject and rhs of where clause adding generated defs to body """ subject, wk,body = exp.WhereD(wc) atomicsubject, atomicdefs = Aexpr(subject) assert pop.ListP(atomicdefs), 'oops' while body != pop.Empty: mdef ,body = pop.DeCons(body) newdef, eqns = Adefinition(mdef); assert pop.ListP(eqns), 'arg' atomicdefs = pop.Append(atomicdefs,eqns) atomicdefs = pop.Cons(newdef,atomicdefs) return exp.WhereC(atomicsubject,wk,atomicdefs),pop.Empty
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
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 Definitions(ig): """pick up a list of definitions""" d = Definition(ig) if d=='': return pop.Empty el = Definitions(ig) return pop.Cons(d,el)