Changeset 1751

Show
Ignore:
Timestamp:
11/18/07 21:17:43 (14 months ago)
Author:
nishio
Message:

lang/unlambda/impl/in_python: support [d], the delay special form

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/unlambda/impl/in_python/unlambda.py

    r1749 r1751  
    44>>> run("`.ai") 
    55a 
     6>>> run("``i.ai") 
     7a 
     8>>> run("`i`.ai") 
     9a 
     10>>> run("`d`.ai") 
     11>>> run("``d`.aii") 
     12a 
     13>>> run("`.b`i`.ai") 
     14ab 
     15>>> run("`.b`d`.ai") 
     16b 
     17>>> run("``.b`d`.aii") 
     18ba 
     19""" 
    620 
    7 """ 
    8 globals().clear() 
    921# Main loop 
    1022# 
     
    3042def EvalTask(node, cont): 
    3143    def run(): 
     44        #print node 
    3245        return node.eval(cont) 
    3346    return run 
     
    4053class I(Node): 
    4154    "identity" 
     55    def __str__(self): 
     56        return "I" 
    4257    def eval(self, cont): 
    4358        def I0(X, cont): 
    44             return cont.invoke(X) 
     59            return cont(X) 
    4560        return cont(I0) 
    4661 
     
    4964    def __init__(self, c): 
    5065        self.c = c 
     66    def __str__(self): 
     67        return "(print %r)" % self.c 
    5168    def eval(self, cont): 
    5269        def Dot0(X, cont): 
     
    6279        self.nX = X 
    6380        self.nY = Y 
     81    def __str__(self): 
     82        return "(apply %s %s)" % (self.nX, self.nY) 
    6483    def eval(self, cont): 
    6584        nY = self.nY 
     
    6887                "evalated X and not-evaluated Y" 
    6988 
    70 #                if X.isDelay: 
    71 #                     return self.cont.invoke(D1Function(self.nY)) 
     89                if X.__class__ == D: 
     90                    def Promise0(Y, cont): 
     91                        def next(X): 
     92                            def Promise1(X, cont): 
     93                                return X(Y, cont) 
     94                            return cont(Promise1) 
     95                        return nY.eval(next) 
     96                    return cont(Promise0) 
    7297 
    7398                def next(Y): 
     
    78103            return _App1 
    79104        return EvalTask(self.nX, next) 
    80      
     105 
     106class D(Node): 
     107    def __str__(self): 
     108         return "D" 
     109    def eval(self, cont): 
     110        return cont(self) 
     111    def __call__(self, Y, cont): 
     112        raise NotImplemented 
     113 
    81114def _parse(iter): 
    82115    c = iter.next() 
     
    85118        ".": lambda: Dot(iter.next()), 
    86119        "`": lambda: (lambda f=_parse(iter): Apply(f, _parse(iter)))(), 
     120        "r": lambda: Dor("\n"), 
     121        "d": lambda: D(), # special form 
    87122 
    88         "r": lambda: R(), 
    89123        "v": lambda: V(), 
    90         "d": lambda: "(delay)", # special form 
    91124        "c": lambda: C(), 
    92125        "s": lambda: S(), 
     
    103136    doctest.testmod() 
    104137 
    105 run("`.ai")