-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_letdoutil.py
More file actions
522 lines (435 loc) · 21.6 KB
/
test_letdoutil.py
File metadata and controls
522 lines (435 loc) · 21.6 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# -*- coding: utf-8 -*-
"""Detect let and do forms, and destructure them writably."""
from ...syntax import macros, test, test_raises, warn, the # noqa: F401
from ...test.fixtures import session, testset
from mcpyrate.quotes import macros, q, n # noqa: F401, F811
from mcpyrate.metatools import macros, expandrq # noqa: F811
from ...syntax import (macros, let, letrec, dlet, dletrec, # noqa: F811, F401
do, local,
autocurry)
from ast import Tuple, Name, Constant, Lambda, BinOp, Attribute, Call
import sys
from mcpyrate import unparse
from ...syntax.astcompat import getconstant, Num
from ...syntax.letdoutil import (canonize_bindings,
isenvassign, islet, isdo,
UnexpandedEnvAssignView,
UnexpandedLetView, ExpandedLetView,
UnexpandedDoView, ExpandedDoView)
from ...syntax.nameutil import isx
def runtests():
# --------------------------------------------------------------------------------
# Internal-ish utilities
with testset("canonize_bindings"):
# canonize_bindings takes in a list of bindings, and outputs a list of bindings.
def validate(lst):
for b in lst:
if type(b) is not Tuple or len(b.elts) != 2:
return False # pragma: no cover, only reached if the test fails.
k, v = b.elts
if type(k) is not Name:
return False # pragma: no cover, only reached if the test fails.
return True
test[validate(the[canonize_bindings(q[k0, v0].elts)])] # noqa: F821, it's quoted.
test[validate(the[canonize_bindings(q[((k0, v0),)].elts)])] # noqa: F821
test[validate(the[canonize_bindings(q[(k0, v0), (k1, v1)].elts)])] # noqa: F821
# --------------------------------------------------------------------------------
# AST structure matching
# The let[] and do[] macros, used in the tests of islet() and isdo(),
# need this utility, so we must test it first.
with testset("isenvassign"):
test[not isenvassign(q[x])] # noqa: F821
test[isenvassign(q[x << 42])] # noqa: F821
with testset("islet"):
test[not islet(q[x])] # noqa: F821
test[not islet(q[f()])] # noqa: F821
test[islet(the[expandrq[let[(x, 21)][2 * x]]]) == ("expanded_expr", "let")] # noqa: F821, `let` defines `x`
test[islet(the[expandrq[let[(x, 21) in 2 * x]]]) == ("expanded_expr", "let")] # noqa: F821
test[islet(the[expandrq[let[2 * x, where(x, 21)]]]) == ("expanded_expr", "let")] # noqa: F821
with expandrq as testdata: # pragma: no cover
@dlet((x, 21)) # noqa: F821
def f1():
return 2 * x # noqa: F821
test[islet(the[testdata[0].decorator_list[0]]) == ("expanded_decorator", "let")]
testdata = q[let[(x, 21)][2 * x]] # noqa: F821
test[islet(the[testdata], expanded=False) == ("lispy_expr", "let")]
# one binding special case for haskelly let-in
testdata = q[let[(x, 21) in 2 * x]] # noqa: F821
test[islet(the[testdata], expanded=False) == ("in_expr", "let")]
testdata = q[let[((x, 21), (y, 2)) in y * x]] # noqa: F821
test[islet(the[testdata], expanded=False) == ("in_expr", "let")]
testdata = q[let[2 * x, where(x, 21)]] # noqa: F821
test[islet(the[testdata], expanded=False) == ("where_expr", "let")]
# some other macro invocation
test[not islet(the[q[someothermacro((x, 21))[2 * x]]], expanded=False)] # noqa: F821
test[not islet(the[q[someothermacro[(x, 21) in 2 * x]]], expanded=False)] # noqa: F821
# invalid syntax for haskelly let-in
testdata = q[let[a in b]] # noqa: F821
test[not islet(the[testdata], expanded=False)]
with q as testdata: # pragma: no cover
@dlet((x, 21)) # noqa: F821
def f2():
return 2 * x # noqa: F821
test[islet(the[testdata[0].decorator_list[0]], expanded=False) == ("decorator", "dlet")]
with testset("islet integration with autocurry"):
# NOTE: We have to be careful with how we set up the test data here.
#
# The quasiquote operator must be outside the `with autocurry` block,
# because otherwise `autocurry` will attempt to curry the AST-lifted
# representation, leading to arguably funny but nonsensical things like
# `ctx=currycall(ast.Load)`.
with expandrq as testdata:
with autocurry: # pragma: no cover
let((x, 21))[2 * x] # noqa: F821 # note this goes into an ast.Expr
thelet = testdata[0].value
test[islet(the[thelet]) == ("curried_expr", "let")]
with expandrq as testdata:
with autocurry: # pragma: no cover
let[(x, 21) in 2 * x] # noqa: F821
thelet = testdata[0].value
test[islet(the[thelet]) == ("curried_expr", "let")]
with expandrq as testdata:
with autocurry: # pragma: no cover
let[2 * x, where(x, 21)] # noqa: F821
thelet = testdata[0].value
test[islet(the[thelet]) == ("curried_expr", "let")]
with testset("isdo"):
test[not isdo(q[x])] # noqa: F821
test[not isdo(q[f()])] # noqa: F821
test[isdo(the[expandrq[do[x << 21, # noqa: F821
2 * x]]]) == "expanded"] # noqa: F821
with expandrq as testdata: # pragma: no cover
with autocurry:
do[x << 21, # noqa: F821
2 * x] # noqa: F821
thedo = testdata[0].value
test[isdo(the[thedo]) == "curried"]
testdata = q[do[x << 21, # noqa: F821
2 * x]] # noqa: F821
test[isdo(the[testdata], expanded=False) == "do"]
testdata = q[do0[23, # noqa: F821
x << 21, # noqa: F821
2 * x]] # noqa: F821
test[isdo(the[testdata], expanded=False) == "do0"]
testdata = q[someothermacro[x << 21, # noqa: F821
2 * x]] # noqa: F821
test[not isdo(the[testdata], expanded=False)]
# --------------------------------------------------------------------------------
# Destructuring - envassign
with testset("envassign destructuring"):
testdata = q[x << 42] # noqa: F821
view = UnexpandedEnvAssignView(testdata)
# read
test[view.name == "x"]
test[type(the[view.value]) in (Constant, Num) and getconstant(view.value) == 42] # Python 3.8: ast.Constant
# write
view.name = "y"
view.value = q[23]
test[view.name == "y"]
test[type(the[view.value]) in (Constant, Num) and getconstant(view.value) == 23] # Python 3.8: ast.Constant
# it's a live view
test[unparse(testdata) == "(y << 23)"]
# error cases
test_raises[TypeError,
UnexpandedEnvAssignView(q[x]), # noqa: F821
"not an env assignment"]
with test_raises(TypeError, "name must be str"):
view.name = 1234
# --------------------------------------------------------------------------------
# Destructuring - unexpanded let - the phase where all sensible people do their AST edits
with testset("let destructuring (unexpanded)"):
def testletdestructuring(testdata):
view = UnexpandedLetView(testdata)
# read
# In the unexpanded form, the outer container of bindings is a `list`.
test[len(view.bindings) == 2]
test[unparse(view.bindings[0]) == "(x, 21)"] # the variable names are identifiers
test[unparse(view.bindings[1]) == "(y, 2)"]
test[unparse(view.body) == "(y * x)"]
# write
#
# It's also legal to edit the AST nodes in view.bindings directly.
# But the job of the setter, which we want to test here,
# is to handle reassigning `view.bindings`.
newbindings = q[(z, 21), (t, 2)].elts # noqa: F821
view.bindings = newbindings # ...like this.
view.body = q[z * t] # noqa: F821
test[len(view.bindings) == 2]
test[unparse(view.bindings[0]) == "(z, 21)"]
test[unparse(view.bindings[1]) == "(t, 2)"]
test[unparse(view.body) == "(z * t)"]
# lispy expr
testdata = q[let[(x, 21), (y, 2)][y * x]] # noqa: F821
testletdestructuring(testdata)
# haskelly let-in
testdata = q[let[((x, 21), (y, 2)) in y * x]] # noqa: F821
testletdestructuring(testdata)
# haskelly let-where
testdata = q[let[y * x, where((x, 21), (y, 2))]] # noqa: F821
testletdestructuring(testdata)
# disembodied haskelly let-in (just the content, no macro invocation)
testdata = q[((x, 21), (y, 2)) in y * x] # noqa: F821
testletdestructuring(testdata)
# disembodied haskelly let-where (just the content, no macro invocation)
testdata = q[y * x, where((x, 21), (y, 2))] # noqa: F821
testletdestructuring(testdata)
# decorator
with q as testdata: # pragma: no cover
@dlet((x, 21), (y, 2)) # noqa: F821
def f3():
return 2 * x # noqa: F821
# read
view = UnexpandedLetView(testdata[0].decorator_list[0])
test[len(view.bindings) == 2]
test[unparse(view.bindings[0]) == "(x, 21)"]
test[unparse(view.bindings[1]) == "(y, 2)"]
test_raises[TypeError,
view.body,
"decorator let does not have an accessible body"]
# write
newbindings = q[(z, 21), (t, 2)].elts # noqa: F821
view.bindings = newbindings
test[len(view.bindings) == 2]
test[unparse(view.bindings[0]) == "(z, 21)"]
test[unparse(view.bindings[1]) == "(t, 2)"]
with test_raises(TypeError, "decorator let does not have an accessible body"):
view.body = q[x] # noqa: F821
test_raises[TypeError,
UnexpandedLetView(q[x]), # noqa: F821
"not a let form"]
# --------------------------------------------------------------------------------
# Destructuring - expanded let - for those unfortunate macros that expand after let[]
# (yet need to edit its AST for interop purposes)
def testexpandedletdestructuring(testdata):
view = ExpandedLetView(testdata)
# read
# In the expanded form, the outer container of bindings is an `ast.Tuple`.
test[len(view.bindings.elts) == 2]
test[unparse(view.bindings.elts[0]) == "('x', 21)"] # the variable names are strings
test[unparse(view.bindings.elts[1]) == "('y', 2)"]
# Reading an expanded let body is painful:
lam = view.body # lambda e: e.y * e.x
test[type(the[lam]) is Lambda]
lambody = lam.body
test[type(the[lambody]) is BinOp]
test[type(the[lambody.left]) is Attribute and lambody.left.attr == "y"]
test[type(the[lambody.right]) is Attribute and lambody.right.attr == "x"]
# write
newbindings = q[("z", 21), ("t", 2)] # noqa: F821
view.bindings = newbindings
test[len(view.bindings.elts) == 2]
test[unparse(view.bindings.elts[0]) == "('z', 21)"]
test[unparse(view.bindings.elts[1]) == "('t', 2)"]
# edit an expanded let body
envname = view.envname
newbody = q[lambda _: name[envname].z * name[envname].t] # noqa: F821
view.body = newbody # the body lambda gets the correct envname auto-injected as its arg
lam = view.body # lambda e: e.z * e.t
test[type(lam) is Lambda]
lambody = lam.body
test[type(lambody) is BinOp]
test[type(the[lambody.left]) is Attribute and lambody.left.attr == "z"]
test[type(the[lambody.right]) is Attribute and lambody.right.attr == "t"]
with testset("let destructuring (expanded let)"):
# lispy expr
testdata = expandrq[let[(x, 21), (y, 2)][y * x]] # noqa: F821
testexpandedletdestructuring(testdata)
# haskelly let-in
testdata = expandrq[let[((x, 21), (y, 2)) in y * x]] # noqa: F821
testexpandedletdestructuring(testdata)
# haskelly let-where
testdata = expandrq[let[y * x, where((x, 21), (y, 2))]] # noqa: F821
testexpandedletdestructuring(testdata)
# decorator
with expandrq as testdata: # pragma: no cover
@dlet((x, 21), (y, 2)) # noqa: F821
def f4():
return 2 * x # noqa: F821
view = ExpandedLetView(testdata[0].decorator_list[0])
test_raises[TypeError,
view.body,
"decorator let does not have an accessible body"]
with test_raises(TypeError, "decorator let does not have an accessible body"):
view.body = q[x] # noqa: F821
test[view.envname is None] # dlet decorator doesn't have an envname, either
# let with implicit do (extra bracket syntax)
#
# with step_expansion:
# let[((x, 21)) in [local[z << 2],
# z * x]]
#
# After macro expansion:
# letter((('x', 21),),
# namelambda('let_body')((lambda e1:
# dof(namelambda('do_line1')((lambda e: e._set('z', 2))),
# namelambda('do_line2')((lambda e: (e.z * e1.x)))))),
# mode='let')
#
testdata = expandrq[let[((x, 21)) in [local[z << 2], # noqa: F821
z * x]]] # noqa: F821
view = ExpandedLetView(testdata)
lam = view.body
test[isdo(the[lam.body])]
test_raises[TypeError,
ExpandedLetView(q[x]), # noqa: F821
"not an expanded let form"]
# --------------------------------------------------------------------------------
# Destructuring - expanded letrec - for the truly desperate
def testexpandedletrecdestructuring(testdata):
view = ExpandedLetView(testdata)
# With an expanded letrec, even reading the bindings gets painful:
def testbindings(*expected):
for b, (k, v) in zip(view.bindings.elts, expected):
test[len(b.elts) == 2]
bk, lam = b.elts
# outer quotes, source code; inner quotes, str within that source
test[the[unparse(bk)] == the[f"'{k}'"]]
test[type(the[lam]) is Lambda]
lambody = lam.body
test[type(the[lambody]) in (Constant, Num) and getconstant(lambody) == the[v]] # Python 3.8: ast.Constant
# read
test[len(view.bindings.elts) == 2]
testbindings(('x', 21), ('y', 2))
# Reading an expanded letrec body
lam = view.body # lambda e: e.y * e.x
test[type(the[lam]) is Lambda]
lambody = lam.body
test[type(the[lambody]) is BinOp]
test[type(the[lambody.left]) is Attribute and lambody.left.attr == "y"]
test[type(the[lambody.right]) is Attribute and lambody.right.attr == "x"]
# write
newbindings = q[("z", lambda _: 21), ("t", lambda _: 2)] # noqa: F821
view.bindings = newbindings # each binding lambda gets the correct envname auto-injected as its arg
test[len(view.bindings.elts) == 2]
testbindings(('z', 21), ('t', 2))
# Editing an expanded letrec body
envname = view.envname
newbody = q[lambda _: name[envname].z * name[envname].t] # noqa: F821
view.body = newbody # the body lambda gets the correct envname auto-injected as its arg
lam = view.body # lambda e: e.z * e.t
test[type(lam) is Lambda]
lambody = lam.body
test[type(lambody) is BinOp]
test[type(the[lambody.left]) is Attribute and lambody.left.attr == "z"]
test[type(the[lambody.right]) is Attribute and lambody.right.attr == "t"]
with testset("let destructuring (expanded letrec)"):
# lispy expr
testdata = expandrq[letrec[((x, 21), (y, 2)) in y * x]] # noqa: F821
testexpandedletrecdestructuring(testdata)
# haskelly let-in
testdata = expandrq[letrec[((x, 21), (y, 2)) in y * x]] # noqa: F821
testexpandedletrecdestructuring(testdata)
# haskelly let-where
testdata = expandrq[letrec[y * x, where((x, 21), (y, 2))]] # noqa: F821
testexpandedletrecdestructuring(testdata)
# decorator, letrec
with expandrq as testdata: # pragma: no cover
@dletrec((x, 21), (y, 2)) # noqa: F821
def f5():
return 2 * x # noqa: F821
view = ExpandedLetView(testdata[0].decorator_list[0])
test_raises[TypeError,
view.body,
"decorator let does not have an accessible body"]
with test_raises(TypeError, "decorator let does not have an accessible body"):
view.body = q[x] # noqa: F821
test[view.envname is not None] # dletrec decorator has envname in the bindings
# --------------------------------------------------------------------------------
# Destructuring - expanded let and letrec, integration with autocurry
with testset("let destructuring (expanded) integration with autocurry"):
with expandrq as testdata:
with autocurry: # pragma: no cover
let[((x, 21), (y, 2)) in y * x] # noqa: F821 # note this goes into an ast.Expr
thelet = testdata[0].value
testexpandedletdestructuring(thelet)
with expandrq as testdata:
with autocurry: # pragma: no cover
letrec[((x, 21), (y, 2)) in y * x] # noqa: F821
thelet = testdata[0].value
testexpandedletrecdestructuring(thelet)
# --------------------------------------------------------------------------------
# Destructuring - unexpanded do
with testset("do destructuring (unexpanded)"):
testdata = q[do[local[x << 21], # noqa: F821
2 * x]] # noqa: F821
view = UnexpandedDoView(testdata)
# read
thebody = view.body
if sys.version_info >= (3, 9, 0): # Python 3.9+: the Index wrapper is gone.
thing = thebody[0].slice
else:
thing = thebody[0].slice.value
test[isenvassign(the[thing])]
# write
# This mutates the original, but we have to assign `view.body` to trigger the setter.
thebody[0] = q[local[x << 9001]] # noqa: F821
view.body = thebody
# implicit do, a.k.a. extra bracket syntax
testdata = q[let[[local[x << 21], # noqa: F821
2 * x]]] # noqa: F821
if sys.version_info >= (3, 9, 0): # Python 3.9+: the Index wrapper is gone.
theimplicitdo = testdata.slice
else:
theimplicitdo = testdata.slice.value
view = UnexpandedDoView(theimplicitdo)
# read
thebody = view.body
if sys.version_info >= (3, 9, 0): # Python 3.9+: the Index wrapper is gone.
thing = thebody[0].slice
else:
thing = thebody[0].slice.value
test[isenvassign(the[thing])]
# write
thebody[0] = q[local[x << 9001]] # noqa: F821
view.body = thebody
test_raises[TypeError,
UnexpandedDoView(q[x]), # noqa: F821
"not a do form"]
# --------------------------------------------------------------------------------
# Destructuring - expanded do
with testset("do destructuring (expanded)"):
testdata = expandrq[do[local[x << 21], # noqa: F821
2 * x]] # noqa: F821
view = ExpandedDoView(testdata)
test[view.envname is not None]
# read
# e._set('x', 21)
thebody = view.body
lam = thebody[0]
test[type(the[lam.body]) is Call and
type(lam.body.func) is Attribute and
lam.body.func.attr == "_set" and
unparse(lam.body.args[0]) == "'x'"]
# write
# This mutates the original, but we have to assign `view.body` to trigger the setter.
envname = view.envname
thebody[0] = q[lambda _: name[envname]._set('x', 9001)] # noqa: F821
view.body = thebody # the body lambdas gets the correct envname auto-injected as their arg
test_raises[TypeError,
ExpandedDoView(q[x]), # noqa: F821
"not an expanded do form"]
with testset("do destructuring (expanded) integration with autocurry"):
with expandrq as testdata: # pragma: no cover
with autocurry:
do[local[x << 21], # noqa: F821
2 * x] # noqa: F821
thedo = testdata[0].value
view = ExpandedDoView(thedo)
test[view.envname is not None]
# read
# currycall(e._set, 'x', 21)
thebody = view.body
lam = thebody[0]
test[type(the[lam.body]) is Call and
isx(lam.body.func, "currycall") and
type(lam.body.args[0]) is Attribute and
lam.body.args[0].attr == "_set" and
unparse(lam.body.args[1]) == "'x'"]
# write
# This mutates the original, but we have to assign `view.body` to trigger the setter.
envname = view.envname
thebody[0] = q[lambda _: name[envname]._set('x', 9001)] # noqa: F821
view.body = thebody # the body lambdas gets the correct envname auto-injected as their arg
if __name__ == '__main__': # pragma: no cover
with session(__file__):
runtests()