Exemplo n.º 1
0
 def reduce(self,gomer,isStmt,stmtCollector,inClass):
     assert len(gomer)>=2
     if isStmt:
         scratch=None
     else:
         scratch=mkScratch('while')
         stmtCollector([S(':='),scratch,None])
     ReduceWhile.scratchStack.append(scratch)
     condBody=[]
     condExpr=reduce(gomer[1],False,condBody.append)
     for cb in condBody:
         stmtCollector(cb)
     body=[]
     bodyExpr=None
     for (i,b) in enumerate(gomer[2:]):
         bodyExpr=reduce(b,
                         isStmt or ((i+2)<(len(gomer)-1)),
                         body.append)
     if body and not isStmt:
         body.append([S(':='),scratch,bodyExpr])
     for cb in condBody:
         body.append(cb)
     stmtCollector([S('while'),condExpr,maybeBegin(body)])
     ReduceWhile.scratchStack.pop()
     return scratch
Exemplo n.º 2
0
 def reduce(self,gomer,isStmt,stmtCollector,inClass):
     assert len(gomer) in [3,4]
     condExpr=reduce(gomer[1],False,stmtCollector)
     thenBody=[]
     thenExpr=reduce(gomer[2],isStmt,thenBody.append)
     if isStmt:
         scratch=None
     else:
         scratch=mkScratch('if')
         thenBody.append([S(':='),scratch,thenExpr])
     if len(gomer)==4:
         elseBody=[]
         elseExpr=reduce(gomer[3],isStmt,elseBody.append)
         if not isStmt:
             elseBody.append([S(':='),scratch,elseExpr])
         if not elseBody:
             stmtCollector([S('if'),condExpr,
                            maybeBegin(thenBody)])
         else:
             stmtCollector([S('if'),condExpr,
                            maybeBegin(thenBody),
                            maybeBegin(elseBody)])
     else:
         pyle=[S('if'),condExpr,maybeBegin(thenBody)]
         if not isStmt:
             pyle.append([S(':='),scratch,None])
         stmtCollector(pyle)
     return scratch
Exemplo n.º 3
0
 def reduce(self,gomer,isStmt,stmtCollector,inClass):
     assert len(gomer)==2
     if isStmt:
         return reduce([[S('.'),gomer[1],S('reverse')]],
                       isStmt,stmtCollector)
     else:
         scratch=mkScratch()
         return reduce([S('begin'),
                        [S(':='),scratch,[S('to-list'),gomer[1]]],
                        [[S('.'),scratch,S('reverse')]],
                        scratch],
                       isStmt,stmtCollector)
Exemplo n.º 4
0
 def expr():
     if len(gomer)==1:
         return self.identity
     if len(gomer)==2 and isinstance(gomer[1],int) and self.unop:
             return self.unop(gomer[1])
     op1=reduce(gomer[1],False,stmtCollector)
     if len(gomer)==2:
         return [S('binop'),self.op,self.identity,op1]
     op2=reduce(gomer[2],False,stmtCollector)
     if len(gomer)==3:
         return [S('binop'),self.op,op1,op2]
     scratch=mkScratch()
     stmtCollector([S(':='),scratch,[S('binop'),self.op,op1,op2]])
     return reduce([S('binop'),self.op,
                    scratch
                    ]+gomer[3:],
                    isStmt,stmtCollector)
Exemplo n.º 5
0
    def reduce(self,gomer,isStmt,stmtCollector,inClass):
        def clauseStmt(clauseStmts):
            clauseBody=[]
            expr=reduce([S('begin')]+clauseStmts,
                        isStmt,
                        clauseBody.append)
            return (maybeBegin(clauseBody),expr)

        scratch=mkScratch() if not isStmt else None
        last=None
        gomerBody=[]
        exnClauses=[]
        finallyClause=None
        for g in gomer[1:]:
            assert not finallyClause
            if isinstance(g,list) and isinstance(g[0],S) and g[0].isKeyword():
                if g[0] is S(':finally'):
                    (finallyStmt,_)=clauseStmt(g[1:])
                    finallyClause=[S(':finally'),finallyStmt]
                else:
                    (exnStmt,exnScratch)=clauseStmt(g[2:])
                    if scratch:
                        assignment=[S(':='),scratch,exnScratch]
                        if (isinstance(exnStmt,list)
                            and isinstance(exnStmt[0],S)
                            and exnStmt[0] is S('begin')):
                            exnStmt.append(assignment)
                        else:
                            exnStmt=[S('begin'),exnStmt,assignment]
                    exnClauses.append([g[0],g[1],exnStmt])
            else:
                assert not exnClauses
                gomerBody.append(g)
        pyleBody=[]
        for g in gomerBody[:-1]:
            reduce(g,True,pyleBody.append)
        if gomerBody:
            last=reduce(gomerBody[-1],isStmt,pyleBody.append)
        if scratch:
            pyleBody.append([S(':='),scratch,last])
        res=[S('try'),maybeBegin(pyleBody)]+exnClauses
        if finallyClause:
            res.append(finallyClause)
        stmtCollector(res)
        return scratch
Exemplo n.º 6
0
def reduce(gomer,isStmt,stmtCollector,*,inAssignment=False,inClass=False):
    if isinstance(gomer,list):
        assert gomer
        reducer=getReducer(gomer[0])
        gomer=reducer.reduce(gomer,isStmt,stmtCollector,inClass)
    else:
        if isinstance(gomer,S) and gomer.isKeyword():
            if isStmt:
                return
            else:
                return [S('quote'),gomer]
    if isStmt:
        if isinstance(gomer,list):
            stmtCollector(gomer)
    else:
        if ((not inAssignment)
            and isinstance(gomer,list)
            and gomer[0] is not S(':=')
            ):
            scratch=mkScratch()
            stmtCollector([S(':='),scratch,gomer])
            gomer=scratch
        return gomer
Exemplo n.º 7
0
 def testScratch(self):
     gensym.nextId=1
     assert mkScratch('foo').isScratch