-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimitives.py
264 lines (227 loc) · 6.42 KB
/
primitives.py
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
from tokens import *
from cells import *
bfltrue = ID('#t')
bflfalse = ID('#f')
### All methods that return a list had to be modified with the inclusion of TCO
### if you want (x1 ... xn) to be returned then you have to return something like
### (quote (x1 ... nx)), when the evaluator gets it, it will return (x1 ... xn).
def car(cell):
return Cell(ID('quote'), Cell(cell.car(), Nil()))
#return cell.car()
def cdr(cell):
return Cell(ID('quote'), Cell(cell.cdr(), Nil()))
#return cell.cdr()
def cons(item1, item2):
return Cell(ID('quote'), Cell(Cell(item1, item2), Nil()))
#return Cell(item1, item2)
def listp(x):
if type(x) == Nil or (type(x) == Cell and x.islist()):
return bfltrue
else:
return bflfalse
def buildlist(*items):
if not items:
return Nil()
res = Cell(ID('quote'), Cell(Cell(items[0], Nil()), Nil()))
nextcell = res.rest.first
for x in items[1:]:
nextcell.rest = Cell(x, Nil())
nextcell = nextcell.rest
return res
def setcar(x, value):
if type(x) != Cell:
return String('Not a cons cell.')
else:
x.first = value
return value
def setcdr(x, value):
if type(x) != Cell:
return String('Not a cons cell.')
else:
x.rest = value
return value
def nullp(x):
return bfltrue if type(x) == Nil else bflfalse
def pairp(x):
return bfltrue if type(x) == Cell else bflfalse
def append(x, *y):
if listp(x) == bflfalse:
raise AttributeError('First argument is not a list.')
if nullp(x) == bflfalse:
## make a res of x.
trial = Cell(ID('quote'), Cell(Cell(None, None), Nil()))
res = trial.rest.first
#res = Cell(None, None)
forward = res
currentxcell = x
while True:
forward.first = currentxcell.first
currentxcell = currentxcell.rest
if type(currentxcell) == Nil:
forward.rest = Nil() #forward is how we will get to the end of the res later.
break
forward.rest = Cell(None, None)
forward = forward.rest
else:
trial = Cell(ID('quote'), Cell(Cell(None, None), Nil()))
res = trial.rest.first
#res = Cell(None, None)
forward = res
for item in y:
if forward.first is not None:
forward.rest = Cell(None, None)
forward = forward.rest
forward.first = item
forward.rest = Nil()
return trial
return res
def basicplus(x, y):
if x.toknum == 3 and y.toknum == 3:
return Int(x.lexval + y.lexval)
elif x.toknum == 19 or y.toknum == 19:
return Float(x.lexval + y.lexval)
else:
raise TypeError('Unsupported type(s) for +: {} and {}'.format(type(x).__name__,
type(y).__name__))
def basicmult(x, y):
if x.toknum == 3 and y.toknum == 3:
return Int(x.lexval * y.lexval)
else:
return Float(x.lexval + y.lexval)
def basicminus(x, y):
if x.toknum == 3 and y.toknum == 3:
return Int(x.lexval - y.lexval)
else:
return Float(x.lexval - y.lexval)
def negative(x):
if x.toknum == 3:
return Int(-x.lexval)
else:
return Float(-x.lexval)
def plus(*x):
res = Int(0)
for tok in x:
res = basicplus(res, tok)
return res
def mult(*x):
res = Int(1)
for tok in x:
res = basicmult(res, tok)
return res
def minus(*x):
l = len(x)
if l == 1:
val = negative(x[0])
return val
else:
return basicminus(x[0], plus(*x[1:]))
def div(x, y):
if x.toknum == 3 and y.toknum == 3:
return Int(x.lexval / y.lexval)
else:
return Float(x.lexval / y.lexval)
def lt(x, y):
if x.lexval < y.lexval:
return bfltrue
else:
return bflfalse
def lte(x, y):
if x.lexval <= y.lexval:
return bfltrue
#return ID('#t')
else:
return bflfalse
#return ID('#f')
def gt(x, y):
if y.lexval < x.lexval:
return bfltrue
else:
return bflfalse
def gte(x, y):
if y.lexval <= x.lexval:
return bfltrue
else:
return bflfalse
def neq(x, y):
if x.lexval != y.lexval:
return bfltrue
else:
return bflfalse
def eq(x, y):
if x.lexval == y.lexval:
return bfltrue
else:
return bflfalse
def zerop(x):
if (type(x) == Int or type(x) == Float) and x.lexval == 0: # in python 0 == 0.0
return bfltrue
else:
return bflfalse
def stringp(x):
return bfltrue if type(x) == String else bflfalse
def stringlength(s):
return Int(len(s.lexval)-2) #remove the DQuotes
def bflor(*items):
for x in items:
if x.lexval != bflfalse.lexval:
return bfltrue
return bflfalse
def bfland(*items):
for x in items:
if x.lexval == bflfalse.lexval:
return bflfalse
return bfltrue
def bflnot(x):
if x.lexval == bflfalse.lexval:
return bfltrue
else:
return bfltrue
def booleanp(x):
if x.lexval == bfltrue or x.lexval == bflfalse:
return bfltrue
else:
return bflfalse
def display(x):
if type(x) == String:
print(x.lexval.replace('"', ''), end='')
elif type(x) == Cell:
print(x, end='')
else:
print(x.lexval, end='')
return bfltrue
def newline():
print('\n', end='')
return bfltrue
## You can completely bork functionality by redefining these in the interpreter.
## I should consider shielding them from the user, but it's more fun to let
## them be accessible.
primitives = {'car': car,
'cdr': cdr,
'cons': cons,
'list?': listp,
'list': buildlist,
'set-car!': setcar,
'set-cdr!': setcdr,
'null?': nullp,
'pair?': pairp,
'append': append,
'+': plus,
'*': mult,
'-': minus,
'/': div,
'<': lt,
'<=': lte,
'>': gt,
'>=': gte,
'=': eq,
'zero?': zerop,
'string?': stringp,
'string-length': stringlength,
'or': bflor,
'and': bfland,
'not': bflnot,
'boolean?': booleanp,
'#t': True,
'#f': False,
'display': display,
'newline': newline}