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 IfExpr(ig): """parse if expression""" pio.NextItem(ig) condition = Expr(ig) if not Expect(exp.THENWord,ig): return '' alt1 = Expr(ig) if not Expect(exp.ELSEWord,ig): return '' alt2 = Expr(ig) if not Expect(exp.FIWord,ig): return '' return exp.Operation3C(exp.IFWord,condition,alt1,alt2)
def Factor(g): """ return next factor - smallest initial sub expression """ lex = pio.CurrentItem(g) if lex == '': return '' if lex == pop.QUOTEWord: return Word(g) if lex == pop.IFWord: return IfExpr(g) if lex == pop.VALOFWord: return Valof(g) if lex == pop.LLISTPARENWord: return Listexpression(g) if lex == pop.LSETPARENWord: return Setexpression(g) if(exp.PrefixP(lex)): return Complete('',g); # prefix operator if exp.NullfixP(lex): pio.NextItem(g);return exp.Operation0C(lex) if(pop.NumP(lex) or pop.StringP(lex) or pop.ListP(lex)): pio.NextItem(g);return exp.LiteralC(lex) #a literal if lex in reservedwords: return '' if(pio.IdentP(lex)): pio.NextItem(g); if pio.CurrentItem(g) == LparenWord: #function call return Call(exp.VarC(lex),g) return exp.VarC(lex) if exp.NullfixP(lex): pio.NextItem(g);return Operation0C(lex) if lex==LparenWord : return ParenExpr(g) #parenthesized expression print('Expected factor, found') pio.Dump5(g);print() exit()
def Definition(ig): """ pick up a definition """ lhs = Expr(ig) if lhs == '': return '' lex = pio.NextItem(ig) if lex != exp.EQUALWord and lex != exp.ISWord: pro.printf('Expected = or is') print('Found '); pio.Dump5(ig) print() exit() rhs = Expr(ig) df = exp.DefinitionC(lhs,lex,rhs) if not Expect(exp.SEMICOLONWord,ig): return Interm(df) return df
def Complete(e,ig): # current item is an infix operator # return longest initial subexpression q = pio.NextItem(ig) # q is the infix operator f = Factor(ig) if f=='': if e == '': return Operation1C(q,unterm) return exp.Operation2C(q,e,exp.Unterm) while pio.CurrentItem(ig) != '' and exp.InfixP(pio.CurrentItem(ig)) and YieldsP(q,pio.CurrentItem(ig)): f = Complete(f,ig) if e == '': return exp.Operation1C(q,f) #if q == WVRWord or q==WHILEWord or q==SWVRWord: return exp.CallC(exp.VarC(q),exp.List2(e,f)) if q in definables: defined.add(q) return exp.CallC(exp.VarC(q),exp.List2(e,f)) return exp.Operation2C(q,e,f)
def Input(opds): global warehouse, nextindex, endoffile, treg, sreg, ig if treg < nextindex: #already read it in, should be in warehouse tag = ('input', treg, (), sreg, 0) if tag in warehouse: age, val = warehouse[tag] return val assert False, 'lost input' if endoffile: # reached the end of file, result is eod return EODWord tsave = treg ssave = sreg treg = nextindex #read in whole lines sreg = 0 while True: try: buff = input() except: endoffile = True nextindex = treg treg = tsave sreg = ssave return EODWord ig = pio.ItemGenStringC(buff) while pio.CurrentItem(ig) != '': tag = ('input', treg, (), sreg, 0) warehouse[tag] = (0, pio.NextItem(ig)) sreg += 1 warehouse[('input', treg, (), sreg, 0)] = (0, EOSWord) sreg = 0 treg += 1 if treg > tsave: break treg = tsave sreg = ssave nextindex = treg + 1 tag = ('input', treg, (), sreg, 0) age, val = warehouse[tag] return val
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 Word(g): pio.NextItem(g) w = pio.NextItem(g) Expect(pop.QUOTEWord,g) return exp.LiteralC(w)
def Supers(s): """ register a bunch of supers""" ig = pio.ItemGenStringC(s) sup = pop.WordName(pio.NextItem(ig)) while pio.CurrentItem(ig) != '': super[pop.WordName(pio.NextItem(ig))] = sup