Changeset 1764 for lang/unlambda/impl/in_python/unlambda_lambda.py
 Timestamp:
 11/19/07 01:38:11 (8 years ago)
 Files:

 1 modified
Legend:
 Unmodified
 Added
 Removed

lang/unlambda/impl/in_python/unlambda_lambda.py
r1763 r1764 22 22 a 23 23 """ 24 import sys 25 24 26 # Main loop 25 27 # … … 33 35 34 36 # Continuation 35 # __call__(Func) returns Task 36 def FinalCont(f): 37 return FinalTask 37 FinalCont = lambda f:FinalTask 38 38 39 39 # Task … … 43 43 raise StopIteration() 44 44 45 def EvalTask(node, cont): 46 def run(): 47 if Debug.show_node: print node 48 return node(cont) 49 return run 50 51 def ApplyTask(operator, operand, cont): 52 def run(): 53 return operator(operand, cont) 54 return run 45 EvalTask = lambda node,c:lambda:node(c) 46 ApplyTask = lambda X,Y,c:lambda:X(Y,c) 55 47 56 48 # Node 57 49 # 58 class Node(object): 59 "parsed element" 60 61 I = lambda: lambda c: c(lambda X, c: c(X)) 62 63 import sys 50 I = lambda: lambda c:c(lambda X,c:c(X)) 64 51 Dot = lambda char: (lambda c: c(lambda X, c: sys.stdout.write(char) or c(X))) 65 66 Apply = lambda nX,nY:\ 67 lambda c:\ 68 EvalTask(nX, 69 lambda X:\ 70 lambda:\ 71 (X == D_)and( 72 c(lambda Y,c:nY(lambda X:c(lambda X,c:X(Y,c)))) 73 )or( 74 nY(lambda Y: lambda :X(Y, c)) 75 ) 76 77 ) 78 52 Apply = lambda nX,nY:lambda c:\ 53 EvalTask(nX, lambda X:\ 54 lambda:\ 55 (X == D_)and( 56 c(lambda Y,c:nY(lambda X:c(lambda X,c:X(Y,c)))) 57 )or( 58 nY(lambda Y: lambda :X(Y, c)))) 79 59 D_ = lambda c:c(D_) 80 60 D = lambda :D_ 81 82 61 C = lambda :lambda c:c(lambda Y,c: lambda :Y(lambda F,c_:c(F),c)) 83 84 62 V0 = lambda X,c:c(V0) 85 63 V = lambda:lambda c:c(V0) 86 87 64 K = lambda:lambda c:c(lambda X,c:c(lambda Y,c:c(X))) 88 89 65 S = lambda:lambda c:c( 90 66 lambda X,c:c( … … 99 75 Func2Node(Z))), c)))) 100 76 101 class Func2Node(Node): 102 def __str__(self): return "%s" % self.f.func_name 103 def __init__(self, f): 104 self.f = f 105 def __call__(self, cont): 106 return cont(self.f) 77 Func2Node = lambda f: lambda c:c(f) 107 78 108 class At(Node):109 def __str__(self): return "At"110 def __call__(self, cont):111 def At0(X, cont):112 import sys113 try:114 #At.c = sys.stdin.read(1)115 At.c = raw_input(">>> ")[0]116 switch = I()117 except:118 switch = V()119 120 return EvalTask(121 Apply(122 Func2Node(X),123 switch),124 cont)125 return cont(At0)126 127 class Qu(Node):128 def __str__(self): return "(? %r)" % self.c129 def __init__(self, c):130 self.c = c131 132 def __call__(self, cont):133 def Qu0(X, cont):134 if At.c == self.c:135 switch = I()136 else:137 switch = V()138 139 return EvalTask(Apply(Func2Node(X), switch), cont)140 return cont(Qu0)141 142 class Pipe(Node):143 def __call__(self, cont):144 def Pipe0(X, cont):145 if At.c != 1:146 switch = Dot(At.c)147 else:148 switch = V()149 150 return ApplyTask(X, switch, cont)151 return Pipe0152 153 class E(Node):154 def __call__(self, cont):155 def E0(X, cont):156 return FinalTask(X)157 return E0158 159 79 def _parse(iter): 160 c = iter.next() 161 result = { 80 return { 162 81 "i": lambda: I(), 163 82 ".": lambda: Dot(iter.next()), … … 169 88 "s": lambda: S(), 170 89 "k": lambda: K(), 171 "@": lambda: At(), 172 "?": lambda: Qu(iter.next()), 173 "": lambda: Pipe(), 174 "e": lambda: E(), 175 }.get(c, lambda: _parse(iter))() 176 return result 90 }.get(iter.next(), lambda: _parse(iter))() 177 91 178 92 def parse(s): 179 93 return _parse(list(s).__iter__()) 180 181 class Debug():182 show_node = False183 94 184 95 def _test(): … … 196 107 """) 197 108 198 #Debug.show_node = True199 #run("`````@?a.oi.kv")200