Changeset 1754 for lang/unlambda
 Timestamp:
 11/18/07 22:12:57 (7 years ago)
 Files:

 1 modified
Legend:
 Unmodified
 Added
 Removed

lang/unlambda/impl/in_python/unlambda.py
r1751 r1754 17 17 >>> run("``.b`d`.aii") 18 18 ba 19 >>> run("``.b`.a`ci.c") 20 ababc 21 >>> Debug.show_node = True 22 >>> run("``v.a.a") 23 (apply (apply V (print 'a')) (print 'a')) 24 (apply V (print 'a')) 25 V 26 >>> Debug.show_node = False 27 >>> run("````skk.av") 28 a 19 29 """ 20 21 30 # Main loop 22 31 # … … 42 51 def EvalTask(node, cont): 43 52 def run(): 44 #print node53 if Debug.show_node: print node 45 54 return node.eval(cont) 46 55 return run … … 105 114 106 115 class D(Node): 116 "delay (special form)" 107 117 def __str__(self): 108 118 return "D" … … 112 122 raise NotImplemented 113 123 124 class C(Node): 125 "call/cc" 126 def __str__(self): 127 return "C" 128 def eval(self, cont): 129 def C0(Y, cont): 130 def C1(func, new_cont): 131 return cont(func) # use old cont 132 operator = Y 133 operand = C1 134 def Apply(): # task 135 return operator(operand, cont) 136 return Apply 137 return cont(C0) 138 139 class V(Node): 140 "void" 141 def __str__(self): 142 return "V" 143 def eval(self, cont): 144 def V0(X, cont): 145 return cont(V0) 146 return cont(V0) 147 148 class K(Node): 149 "constant: ``kXY = X" 150 def __str__(self): 151 return "K" 152 def eval(self, cont): 153 def K0(X, cont): 154 def K1(Y, cont): 155 return cont(X) 156 return cont(K1) 157 return cont(K0) 158 159 class S(Node): 160 "substitution: ```sXYZ = ``XZ`YZ" 161 def __str__(self): 162 return "S" 163 def eval(self, cont): 164 def S0(X, cont): 165 def S1(Y, cont): 166 def S2(Z, cont): 167 return EvalTask( 168 Apply( 169 Apply(Func2Node(X), Func2Node(Z)), 170 Apply(Func2Node(Y), Func2Node(Z))), cont) 171 return cont(S2) 172 return cont(S1) 173 return cont(S0) 174 175 class Func2Node(Node): 176 def __init__(self, f): 177 self.f = f 178 def eval(self, cont): 179 return cont(self.f) 180 114 181 def _parse(iter): 115 182 c = iter.next() … … 117 184 "i": lambda: I(), 118 185 ".": lambda: Dot(iter.next()), 119 "`": lambda: (lambda f=_parse(iter): Apply(f, _parse(iter)))(),120 "r": lambda: Do r("\n"),186 "`": lambda: Apply(_parse(iter), _parse(iter)), 187 "r": lambda: Dot("\n"), 121 188 "d": lambda: D(), # special form 122 189 "c": lambda: C(), 123 190 "v": lambda: V(), 124 "c": lambda: C(),125 191 "s": lambda: S(), 126 192 "k": lambda: K(), … … 131 197 return _parse(list(s).__iter__()) 132 198 199 class Debug(): 200 show_node = False 201 133 202 def _test(): 134 203 import doctest … … 136 205 doctest.testmod() 137 206 207 def fib(): 208 "show Fibonacci numbers" 209 run(""" 210 ```s``s``sii`ki 211 `k.*``s``s`ks 212 ``s`k`s`ks``s``s`ks``s`k`s`kr``s`k`sikk 213 `k``s`ksk 214 """)