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 Adefinition(df): """atomize a definition, returning atomic def and deflist""" rhs = exp.Rhs(df); lhs = exp.Lhs(df) assert exp.VarP(lhs), 'cannot handle compund definitions' if exp.LiteralP(rhs): #equated to a literal, no action return df,pop.Empty arhs, defs = Aexpr(rhs) return exp.DefinitionC(lhs,pop.EQUALWord, arhs), defs
def TermLprecedence(t): # the force with which term t binds from the left (to its right operands) */ if (exp.VarP(t) or exp.LiteralP(t)): return 100 if (exp.OperationP(t)): s = exp.OperationSymbol(t) if (exp.InfixP(s) or exp.PrefixP(s)): return exp.Lprecedence(exp.OperationSymbol(t)) return 100 return 100
def Aexpr(e): """ returns e1 dl where dl is the list of atomic definitions generated and e1 is the new atomic expression """ if exp.OperationP(e): return Aoperation(e) if exp.VarP(e): return e, pop.Empty if exp.WhereP(e): return Awhere(e) assert not exp.CallP(e), ' cannot do function calls ' assert exp.LiteralP(e), ' bad arg to Aexpr' v = VarGen() d = exp.DefinitionC(v,pop.EQUALWord,e) return v, pop.List1(d)
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)