Repository navigation
Expand file tree
/
Copy pathletdoutil.py
More file actions
559 lines (470 loc) · 23.8 KB
/
Copy pathletdoutil.py
File metadata and controls
559 lines (470 loc) · 23.8 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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# -*- coding: utf-8 -*-
"""Detect let and do forms, and destructure them writably.
Separate from letdo.py for dependency reasons.
Separate from util.py due to the length.
"""
from ast import (Call, Name, Subscript, Index, Compare, In,
Tuple, List, Str, BinOp, LShift)
import re
from macropy.core import Captured
def where(*bindings):
"""[syntax] Only meaningful in a let[body, where((k0, v0), ...)]."""
raise RuntimeError("where() is only meaningful in a let[body, where((k0, v0), ...)]")
# avoid circular dependency; can't import from .util, so implement a minimal isx() for what we need
def _isx(tree, x):
ismatch = x if callable(x) else lambda s: s == x
return ((type(tree) is Name and ismatch(tree.id)) or
(type(tree) is Captured and ismatch(tree.name)))
def _pred(x):
rematch = re.match
pat = re.compile(r"^{}\d*$".format(x)) # numbering caused by MacroPy hq[], hygienic quote
return lambda s: rematch(pat, s)
_isletf = _pred("letter") # name must match what ``unpythonic.syntax.letdo._letimpl`` uses in its output.
_isdof = _pred("dof") # name must match what ``unpythonic.syntax.letdo.do`` uses in its output.
_iscurrycall = _pred("currycall") # output of ``unpythonic.syntax.curry``
def _canonize_bindings(elts, locref, allow_call_in_name_position=False):
"""Wrap a single binding without container into a length-1 list.
Pass through multiple bindings as-is.
Yell if the input format is invalid.
elts: list of bindings, either::
[(k0, v0), ...] # multiple bindings
[(k, v)] # single binding also ok
[k, v] # special single binding format, missing container
locref: AST node to copy location information from, in case we need to
make a wrapper for a single binding.
allow_call_in_name_position: used by let_syntax to allow template definitions.
"""
def iskey(x):
return ((type(x) is Name) or
(allow_call_in_name_position and type(x) is Call and type(x.func) is Name))
if len(elts) == 2 and iskey(elts[0]):
return [Tuple(elts=elts, lineno=locref.lineno, col_offset=locref.col_offset)]
if all((type(b) is Tuple and len(b.elts) == 2 and iskey(b.elts[0])) for b in elts):
return elts
assert False, "expected bindings to be ((k0, v0), ...) or a single (k, v)"
def islet(tree, expanded=True):
"""Test whether tree is a ``let[]``, ``letseq[]``, ``letrec[]``,
``let_syntax[]``, or ``abbrev[]``.
expanded: if ``True``, test for the already expanded form.
If ``False``, test for the form that exists prior to macro expansion.
(Which you need depends on when your macro runs.)
Note ``let_syntax[]`` and ``abbrev[]`` are completely eliminated by
macro expansion, so they are seen only if ``expanded=False``.
Return a truthy value if ``tree`` is a let form, ``False`` if not.
The truthy return value is a tuple of two strings, ``(kind, mode)``.
**If expanded=False**:
Then ``kind`` is one of ``"decorator"``, ``"lispy_expr"``, ``"in_expr"``
or ``"where_expr"``.
If ``kind="decorator"``, then ``mode`` is one of ``"dlet"``, ``"dletseq"``,
``"dletrec"``, ``"blet"``, ``"bletseq"`` or ``"bletrec"``, identifying
which decorator it is.
Otherwise ``mode`` is one of ``"let"``, ``"letseq"``, ``"letrec"``,
``"let_syntax"`` or ``"abbrev"``, identifying which let form it is.
**If expanded=True**:
Then ``kind`` is one of ``"expanded_decorator"``, ``"expanded_expr"``,
``"curried_decorator"`` or ``"curried_expr"``, and ``mode`` is
``"let"`` or ``"letrec"``.
Keep in mind that ``letseq[]`` expands into a sequence of nested ``let[]``.
(This is a lot of cases for the caller to handle, but that's because
there are many different AST structures that correspond to ``let`` forms
in unpythonic.)
"""
if expanded:
if type(tree) is not Call:
return False
kind = "expanded"
if _isx(tree.func, _iscurrycall) and _isx(tree.args[0], _isletf):
kind = "curried"
elif not _isx(tree.func, _isletf):
return False
mode = [kw.value for kw in tree.keywords if kw.arg == "mode"]
assert len(mode) == 1 and type(mode[0]) is Str # TODO: Python 3.8+: ast.Constant, no ast.Str
mode = mode[0].s
kwnames = [kw.arg for kw in tree.keywords]
if "_envname" in kwnames:
return ("{}_decorator".format(kind), mode) # this call was generated by _dletimpl
else:
return ("{}_expr".format(kind), mode) # this call was generated by _letimpl
return False
# dlet((k0, v0), ...) (call, usually in a decorator list)
deconames = ("dlet", "dletseq", "dletrec",
"blet", "bletseq", "bletrec")
if type(tree) is Call and type(tree.func) is Name:
s = tree.func.id
if any(s == x for x in deconames):
return ("decorator", s)
# otherwise we should have an expr macro invocation
if not (type(tree) is Subscript and type(tree.slice) is Index):
return False
macro = tree.value
expr = tree.slice.value
exprnames = ("let", "letseq", "letrec", "let_syntax", "abbrev")
# let((k0, v0), ...)[body]
if type(macro) is Call and type(macro.func) is Name:
s = macro.func.id
if any(s == x for x in exprnames):
return ("lispy_expr", s)
# The haskelly syntaxes are only available as a let expression (no decorator form).
elif type(macro) is Name:
s = macro.id
if not any(s == x for x in exprnames):
return False
h = _ishaskellylet(expr)
if h:
return (h, s)
return False
def _ishaskellylet(tree):
"""Test whether tree is the content of a haskelly let.
Return a truthy value if it is, ``False`` if not.
In other words, detect the part inside the brackets in::
let[((k0, v0), ...) in body]
let[body, where((k0, v0), ...)]
To detect the full expression including the ``let[]``, use ``islet`` instead.
"""
# let[((k0, v0), ...) in body]
def maybeiscontentofletin(tree):
return (type(tree) is Compare and
len(tree.ops) == 1 and type(tree.ops[0]) is In and
type(tree.left) is Tuple)
# let[body, where((k0, v0), ...)]
def maybeiscontentofletwhere(tree):
return type(tree) is Tuple and len(tree.elts) == 2 and type(tree.elts[1]) is Call
if maybeiscontentofletin(tree):
bindings = tree.left
if all((type(b) is Tuple and len(b.elts) == 2 and type(b.elts[0]) is Name)
for b in bindings.elts):
return "in_expr"
# Single binding special case: let's not require a trailing comma.
# In this case, the wrapper tuple containing the bindings is missing.
# (For consistency of surface syntax with the other variants that don't
# require it, because they look like function calls in the AST.)
if len(bindings.elts) == 2 and type(bindings.elts[0]) is Name:
return "in_expr"
elif maybeiscontentofletwhere(tree):
thecall = tree.elts[1]
if type(thecall.func) is Name and thecall.func.id == "where":
return "where_expr"
return False
def isdo(tree, expanded=True):
"""Detect whether tree is a ``do[]`` or ``do0[]``.
expanded: if ``True``, test for the already expanded form.
If ``False``, test for the form that exists prior to macro expansion.
Return a truthy value if ``tree`` is a do form, ``False`` if not.
**If expanded=False**:
Then the truthy return value is just ``True``.
**If expanded=True**:
Then the truthy return value is one of the strings ``"expanded"`` or
``"curried"``.
"""
if expanded:
if type(tree) is not Call:
return False
kind = "expanded"
if _isx(tree.func, _iscurrycall) and _isx(tree.args[0], _isdof):
kind = "curried"
elif not _isx(tree.func, _isdof):
return False
return kind
# TODO: detect also do[] with a single expression inside? (now requires a comma)
return (type(tree) is Subscript and
type(tree.value) is Name and any(tree.value.id == x for x in ("do", "do0")) and
type(tree.slice) is Index and type(tree.slice.value) is Tuple)
def isenvassign(tree):
"""Detect whether tree is an unpythonic ``env`` assignment, ``name << value``.
The only way this differs from a general left-shift is that the LHS must be
an ``ast.Name``.
"""
return type(tree) is BinOp and type(tree.op) is LShift and type(tree.left) is Name
# -----------------------------------------------------------------------------
# TODO: kwargs support for let(x=42)[...] if implemented later
class UnexpandedLetView:
"""Destructure a let form, writably.
If ``tree`` cannot be interpreted as a ``let`` form, then ``TypeError``
is raised.
For in-place modification of ``bindings`` or ``body``. Use before the ``let``
form is expanded away.
**Supported formats**::
dlet((k0, v0), ...) # decorator
let((k0, v0), ...)[body] # lispy expression
let[((k0, v0), ...) in body] # haskelly expression
let[body, where((k0, v0), ...)] # haskelly expression, inverted
In addition, we also support *just the bracketed part* of the haskelly
formats. This is to make it easier for the macro interface to destructure
these forms (for sending into the ``let`` syntax transformer). So these
forms are supported, too::
((k0, v0), ...) in body
(body, where((k0, v0), ...))
This is a data abstraction that hides the detailed structure of the AST,
since there are three alternate syntaxes that can be used for a ``let``
expression.
For the decorator forms, ``tree`` should be the decorator call. In this case
only ``bindings`` is available (the body is then the body of the function
being decorated).
**Attributes**:
``bindings`` is a ``list`` of ``ast.Tuple``, where each item is of the form
``(k, v)``, where ``k`` is an ``ast.Name``. Writing to ``bindings`` updates
the original.
``body`` (when available) is an AST representing a single expression.
If it is an ``ast.List``, it means an implicit ``do[]`` (handled by the
``let`` expander), allowing a multiple-expression body.
Writing to ``body`` updates the original.
When not available, ``body is None``.
``mode`` is one of ``let``, ``letseq``, ``letrec``; for information only
(this essentially says what the ``bindings`` mean).
If ``tree`` is just the bracketed part of a haskelly let, then ``mode`` is
``None``, because the mode information is contained in the surrounding
subscript form (expr macro invocation) and hence not accessible from here.
"""
def __init__(self, tree):
data = islet(tree, expanded=False)
self._has_subscript_container = True
if not data:
# The macro interface only gets the bracketed part as "tree",
# so we jump through hoops to make this usable both from
# syntax transformers (which have access to the full AST)
# and the macro interface (which needs to destructure bindings and body
# from the given tree, to send them to the let transformer).
h = _ishaskellylet(tree)
if not h:
raise TypeError("expected a tree representing an unexpanded let, got {}".format(tree))
data = (h, None) # cannot detect mode, because no access to the surrounding Subscript AST node
self._has_subscript_container = False
self._tree = tree
self._type, self.mode = data
if self._type not in ("decorator", "lispy_expr", "in_expr", "where_expr"):
raise NotImplementedError("unknown unexpanded let form type '{}'".format(self._type))
if self._type == "decorator":
self.body = None
def _getbindings(self):
t = self._type
if t == "decorator": # bare Call, dlet(...), blet(...)
return _canonize_bindings(self._tree.args, self._tree)
elif t == "lispy_expr": # Call inside a Subscript, (let(...))[...]
return _canonize_bindings(self._tree.value.args, self._tree.value)
else: # haskelly let, let[(...) in ...], let[..., where(...)]
theexpr = self._tree.slice.value if self._has_subscript_container else self._tree
if t == "in_expr":
return _canonize_bindings(theexpr.left.elts, theexpr.left)
elif t == "where_expr":
return _canonize_bindings(theexpr.elts[1].args, theexpr.elts[1])
def _setbindings(self, newbindings):
t = self._type
if t == "decorator":
self._tree.args = newbindings
elif t == "lispy_expr":
self._tree.value.args = newbindings
else:
theexpr = self._tree.slice.value if self._has_subscript_container else self._tree
if t == "in_expr":
theexpr.left.elts = newbindings
elif t == "where_expr":
theexpr.elts[1].args = newbindings
bindings = property(fget=_getbindings, fset=_setbindings, doc="The bindings subform of the let. Writable.")
def _getbody(self):
t = self._type
if t == "decorator":
# not reached, but let's leave this here for documentation.
raise TypeError("the body of a decorator let form is the body of decorated function, not a subform of the let.")
elif t == "lispy_expr":
return self._tree.slice.value
else:
theexpr = self._tree.slice.value if self._has_subscript_container else self._tree
if t == "in_expr":
return theexpr.comparators[0]
elif t == "where_expr":
return theexpr.elts[0]
def _setbody(self, newbody):
t = self._type
if t == "decorator":
# not reached, but let's leave this here for documentation.
raise TypeError("the body of a decorator let form is the body of decorated function, not a subform of the let.")
elif t == "lispy_expr":
self._tree.slice.value = newbody
else:
theexpr = self._tree.slice.value if self._has_subscript_container else self._tree
if t == "in_expr":
theexpr.comparators[0] = newbody
elif t == "where_expr":
theexpr.elts[0] = newbody
body = property(fget=_getbody, fset=_setbody, doc="The body subform of the let (only for expr forms). Writable.")
class UnexpandedDoView:
"""Destructure a do form, writably.
If ``tree`` cannot be interpreted as a ``do`` form, then ``TypeError``
is raised.
For easy in-place modification of ``body``. Use before the ``do`` form
is expanded away.
**Supported formats**:
do[body0, ...]
do0[body0, ...]
[...]
The list format is for convenience, for viewing an implicit ``do[]`` in the
body of a ``let`` form.
**Attributes**:
``body`` is a ``list`` of the expressions in the body of the ``do[]``.
Writing to it updates the original.
"""
def __init__(self, tree):
self._implicit = False
if not isdo(tree, expanded=False):
if type(tree) is not List: # for implicit do[]
raise TypeError("expected a tree representing an unexpanded do, got {}".format(tree))
self._implicit = True
self._tree = tree
def _getbody(self):
return self._tree.slice.value.elts if not self._implicit else self._tree.elts
def _setbody(self, newbody):
if not self._implicit:
self._tree.slice.value.elts = newbody
else:
self._tree.elts = newbody
body = property(fget=_getbody, fset=_setbody, doc="The body of the do. Writable.")
class UnexpandedEnvAssignView:
"""Destructure an env-assignment, writably.
If ``tree`` cannot be interpreted as an unpythonic ``env`` assignment
of the form ``name << value``, then ``TypeError`` is raised.
For easy in-place modification of both ``name`` and ``value``. Use before
the env-assignment is expanded away (so, before the ``let[]`` or ``do[]``
containing it is expanded away).
**Attributes**:
``name``: the name of the variable, as a str.
``value``: the thing being assigned, as an AST.
Writing to either attribute updates the original.
"""
def __init__(self, tree):
if not isenvassign(tree):
raise TypeError("expected a tree representing an unexpanded env-assignment, got {}".format(tree))
self._tree = tree
def _getname(self):
return self._tree.left.id
def _setname(self, newname):
if not isinstance(newname, str):
raise TypeError("expected str for new name, got '{}' with value '{}'".format(type(newname), newname))
self._tree.left.id = newname
name = property(fget=_getname, fset=_setname, doc="The name of the assigned var, as an str. Writable.")
def _getvalue(self):
return self._tree.right
def _setvalue(self, newvalue):
self._tree.right = newvalue
value = property(fget=_getvalue, fset=_setvalue, doc="The value of the assigned var, as an AST. Writable.")
# -----------------------------------------------------------------------------
class ExpandedLetView:
"""Like UnexpandedLetView, but for already expanded let constructs.
We support both "with curry" and bare formats.
This is for simple in-place modifications; changing the number of bindings
is currently not supported. Prefer doing any extensive modifications in the
first pass, before the ``let[]`` expands.
Depending on whether let mode is "let" or "letrec", each binding is a
bare value or ``lambda e: ...``, respectively.
Usually ``body``, when available, is a single expression. In the case of a
multiple-expression body, ``body`` is an expanded ``do``, which can be
destructured using ``ExpandedDoView``.
"""
def __init__(self, tree):
data = islet(tree, expanded=True)
if not data:
raise TypeError("expected a tree representing an expanded let, got {}".format(tree))
self._tree = tree
self._type, self.mode = data
if self._type not in ("expanded_decorator", "expanded_expr", "curried_decorator", "curried_expr"):
raise NotImplementedError("unknown expanded let form type '{}'".format(self._type))
if self._type.endswith("decorator"):
self.body = None
self.curried = self._type.startswith("curried")
def _getbindings(self):
# Abstract away the namelambda(...). We support both "with curry" and bare formats:
# currycall(letter, bindings, currycall(currycall(namelambda, "let_body"), curryf(lambda e: ...)))
# letter(bindings, namelambda("let_body")(lambda e: ...))
thebindings = self._tree.args[1] if self.curried else self._tree.args[0]
if self.mode == "letrec":
myelts = []
if self.curried:
# "((k, currycall(currycall(namelambda, "letrec_binding_YYY"), curryf(lambda e: ...))), ...)"
for b in thebindings.elts:
k, v = b.elts
myelts.append(Tuple(elts=[k, v.args[1].args[0]]))
else:
# "((k, namelambda("letrec_binding_YYY")(lambda e: ...)), ...)"
for b in thebindings.elts:
k, v = b.elts
myelts.append(Tuple(elts=[k, v.args[0]]))
return Tuple(elts=myelts)
else: # "((k, v), ...)"
return thebindings
def _setbindings(self, newbindings):
if type(newbindings) is not Tuple:
raise TypeError("Expected ast.Tuple as the new bindings of the let, got {}".format(type(newbindings)))
if not all(type(elt) is Tuple for elt in newbindings.elts):
raise TypeError("Expected ast.Tuple of ast.Tuple as the new bindings of the let")
if not all(len(binding.elts) == 2 for binding in newbindings.elts):
raise TypeError("Expected ast.Tuple of length-2 ast.Tuple as the new bindings of the let")
if len(newbindings.elts) != len(self.bindings.elts):
assert False, "changing the number of items currently not supported by this view (do that before the let[] expands)"
thebindings = self._tree.args[1] if self.curried else self._tree.args[0]
if self.mode == "letrec":
newelts = []
curried = self.curried
for oldb, newb in zip(thebindings.elts, newbindings.elts):
oldk, oldv = oldb.elts
newk, newv = newb.elts
if curried:
oldv.args[1].args[0] = newv
oldv.args[0].args[1].s = "letrec_binding_{}".format(newk.id)
else:
oldv.args[0] = newv
oldv.func.args[0].s = "letrec_binding_{}".format(newk.id) # update name in the namelambda(...)
newelts.append(Tuple(elts=[newk, oldv], lineno=oldb.lineno, col_offset=oldb.col_offset))
thebindings.elts = newelts
else:
thebindings.elts = newbindings.elts
bindings = property(fget=_getbindings, fset=_setbindings, doc="The bindings subform of the let, as an ast.Tuple. Writable.")
def _getbody(self):
# currycall(letter, bindings, currycall(currycall(namelambda, "let_body"), curryf(lambda e: ...)))
# letter(bindings, namelambda("let_body")(lambda e: ...))
if self.curried:
return self._tree.args[2].args[1].args[0]
else:
return self._tree.args[1].args[0]
def _setbody(self, newbody):
if self.curried:
self._tree.args[2].args[1].args[0] = newbody
else:
self._tree.args[1].args[0] = newbody
body = property(fget=_getbody, fset=_setbody, doc="The body subform of the let (only for expr forms). Writable.")
class ExpandedDoView:
"""Like UnexpandedDoView, but for already expanded do forms.
We support both "with curry" and bare formats.
This is for simple in-place modifications; changing the number of do-items
is currently not supported. Prefer doing any extensive modifications in the
first pass, before the ``do[]`` expands.
Each item in the ``body`` list is of the form ``lambda e: ...``.
"""
def __init__(self, tree):
t = isdo(tree, expanded=True)
if not t:
raise TypeError("expected a tree representing an expanded do, got {}".format(tree))
self.curried = t.startswith("curried")
self._tree = tree
def _getbody(self):
# "currycall(dof, currycall(currycall(namelambda, "do_lineXXX"), curryf(lambda e: ...)), ...)"
# "dof(namelambda("do_lineXXX")(lambda e: ...), ...)"
if self.curried:
theitems = self._tree.args[1:]
return [item.args[1].args[0] for item in theitems]
else:
theitems = self._tree.args
return [item.args[0] for item in theitems]
def _setbody(self, newbody):
if not isinstance(newbody, list): # yes, a runtime list!
raise TypeError("Expected list as the new body of the do, got {}".format(type(newbody)))
if len(newbody) != len(self.body):
assert False, "changing the number of items currently not supported by this view (do that before the do[] expands)"
if self.curried:
theitems = self._tree.args[1:]
for old, new in zip(theitems, newbody):
old.args[1].args[0] = new
else:
theitems = self._tree.args
for old, new in zip(theitems, newbody):
old.args[0] = new
body = property(fget=_getbody, fset=_setbody, doc="The body of the do. Writable.")
# -----------------------------------------------------------------------------