- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrunlambda.py
More file actions
Latest commit
200 lines (184 loc) · 5.01 KB
/
Copy pathrunlambda.py
File metadata and controls
200 lines (184 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
importsys
importos
classCurrentchar:
def__init__(self):
self.c=''
curr=Currentchar()
classOp(object):
def__init__(self):
pass
classTick(Op):
'''
A graceless hack so that the stack in bt() is homogenous.
'''
pass
classI(Op):
defapp(self, a, c):
return (c,a)
classK(Op):
defapp(self, a, c):
return (c, K2(a))
classK2(Op):
def__init__(self, r):
self.r=r
defapp(self, _, c):
return (c, self.r)
classS(Op):
defapp(self, a, c):
return (c, S2(a))
classS2(Op):
def__init__(self, first):
self.first=first
defapp(self, a, c):
return (c, S3(self.first, a))
classS3(Op):
def__init__(self, first, second):
self.f=first
self.s=second
defapp(self, a, c):
returndescend(c, App(App(self.f,a),App(self.s,a)))
classV(Op):
defapp(self, _, c):
return (c, self)
classE(Op):
defapp(self, a, c):
raiseSystemExit(notisinstance(a, I))
# return (c, a)
classD(Op):
defapp(self, a, c):
assertFalse, "This never happens"
classD2(Op):
def__init__(self, right):
self.right=right
defapp(self, rv, k2):
returndescend(D_delayed(rv, k2), self.right)
classReadchar(Op):
defapp(self, a, c):
curr.c=os.read(0,1)
ifnotcurr.c: returndescend(c, App(a,v))
returndescend(c,App(a,i))
classPrintchar(I):
def__init__(self, char):
self.char=charor''# convince pypy translation that this is never None
defapp(self, a, c):
os.write(1, self.char)
return (c,a)
classR(Printchar):
def__init__(self):
Printchar.__init__(self,'\n')
classCompchar(Op):
def__init__(self, char):
self.char=char
defapp(self, a, c):
ifself.char==curr.c:
returndescend(c, App(a,i))
returndescend(c, App(a,v))
classReprint(Op):
defapp(self, a, c):
ifnotcurr.c: returndescend(c,App(a,v))
returndescend(c,App(a, Printchar(curr.c)))
classC(Op):
defapp(self, a, c):
returndescend(c,App(a, Callcc(c)))
classCallcc(Op):
def__init__(self, cont):
self.cont=cont
defapp(self, a, _):
return (self.cont, a)
tick=Tick()
i=I()
v=V()
defget_pchar(c):
ifcnotinpcharmap:
pcharmap[c] =Printchar(c)
returnpcharmap[c]
pcharmap= {}
defget_cchar(c):
ifcnotinccharmap:
ccharmap[c] =Compchar(c)
returnccharmap[c]
ccharmap= {}
classApp(Op):
def__init__(self, left, right):
self.left=left
self.right=right
noarg= {'i':i, 'v':v, 'e':E(), 'd':D(), 's':S(), 'k':K(),
'|':Reprint(), 'r':R(), '@':Readchar(), 'c':C()}
takesarg= {'.':get_pchar, '?':get_cchar}
defbt(fileno):
tree, stack, ticks, tot= [], [], 0, 0
whiletot!=2*ticks+1:
cur=os.read(fileno,1).lower()
ifnotcur: raiseEOFError
tot+=1
ifcur=='#':
whilecurandcur!='\n': cur=os.read(fileno, 1)
ifnotcur: raiseEOFError
ifcurinnoarg:
stack.append(noarg[cur])
elifcurintakesarg:
arg=os.read(fileno, 1)
ifnotarg: raiseEOFError
stack.append(takesarg[cur](arg))
elifcur=='`':
stack.append(tick)
ticks+=1
else:
tot-=1
whilestack:
item=stack.pop()
ifitemistick:
left=tree.pop()
right=tree.pop()
tree.append(App(left, right))
else:
tree.append(item)
returntree[0]
defdescend(cont, tree):
ifisinstance(tree, App):
dchecker=Dchecker(tree.right,cont)
returndescend(dchecker, tree.left)
returncont.evaluate(tree)
classEval(object):
def__init__(self):
pass
classExiter(Eval):
defevaluate(self, o):
raiseSystemExit(notisinstance(o, I))
classDchecker(Eval):
def__init__(self, right, cont):
self.right=right
self.cont=cont
defevaluate(self, lval):
ifisinstance(lval, D):
returnself.cont.evaluate(D2(self.right))
else:
returndescend(D_undelayed(lval, self.cont), self.right)
classD_delayed(Eval):
def__init__(self, rv, k2):
self.rv=rv
self.k2=k2
defevaluate(self, lv):
returnlv.app(self.rv, self.k2)
classD_undelayed(Eval):
def__init__(self, lval, cont):
self.lval=lval
self.cont=cont
defevaluate(self, rval):
returnself.lval.app(rval, self.cont)
defrun(argv):
iflen(argv) ==1: fileno=0
else: fileno=os.open(argv[1], os.O_RDONLY, 0777)
try:
tree=bt(fileno)
exceptEOFError:
os.write(2, "Input too short!\n")
return1
k, a=descend(Exiter(), tree)
whilenotisinstance(k, Exiter):
k, a=k.evaluate(a)
returnint(a!=i)
deftarget(*a):
return (run, None)
if__name__=='__main__':
run(sys.argv)