|
15 | 15 | BinOp, LShift, \ |
16 | 16 | FunctionDef, Return, \ |
17 | 17 | arguments, arg, \ |
18 | | - Load, \ |
19 | | - copy_location |
| 18 | + Load |
20 | 19 | from .astcompat import AsyncFunctionDef |
21 | 20 |
|
22 | 21 | from macropy.core.quotes import macros, q, u, ast_literal, name |
23 | 22 | from macropy.core.hquotes import macros, hq |
24 | 23 | from macropy.core.walkers import Walker |
25 | 24 |
|
26 | 25 | from ..lispylet import let as letf, letrec as letrecf, _dlet as dletf, _blet as bletf |
27 | | -from ..seq import begin as beginf, do as dof |
| 26 | +from ..seq import do as dof |
28 | 27 | from ..dynassign import dyn |
29 | 28 |
|
30 | 29 | from .scoping import scoped_walker |
@@ -243,29 +242,15 @@ def _dletseqimpl(bindings, fdef, kind): |
243 | 242 | return _dletseqimpl(rest, outer, kind) |
244 | 243 |
|
245 | 244 | # ----------------------------------------------------------------------------- |
246 | | -# Imperative code in expresssion position. Uses the "let" machinery. |
| 245 | +# Imperative code in expression position. Uses the "let" machinery. |
247 | 246 |
|
248 | 247 | def do(tree): |
249 | 248 | if type(tree) not in (Tuple, List): |
250 | 249 | assert False, "do body: expected a sequence of comma-separated expressions" |
251 | 250 |
|
252 | 251 | gen_sym = dyn.gen_sym |
253 | 252 | e = gen_sym("e") |
254 | | - # We must use env.__setattr__ to allow defining new names; env.set only rebinds. |
255 | | - # But to keep assignments chainable, the assignment must return the value. |
256 | | - # Use a let[] to avoid recomputing it (could be expensive and/or have side effects). |
257 | | - # So we need: |
258 | | - # lambda k, expr: let((v, expr))[begin(e.__setattr__(k, v), v)] |
259 | | - # ...but with gensym'd arg names to avoid spurious shadowing inside expr. |
260 | | - # TODO: cache the setter in e? Or even provide a new method in env that does this? |
261 | | - sa = Attribute(value=q[name[e]], attr="__setattr__", ctx=Load()) |
262 | | - k = gen_sym("k") |
263 | | - expr = gen_sym("expr") |
264 | | - envset = q[lambda: None] |
265 | | - envset.args.args = [arg(arg=k), arg(arg=expr)] |
266 | | - letbody = hq[beginf(ast_literal[sa](name[k], name["v"]), name["v"])] |
267 | | - letbody = copy_location(letbody, tree) |
268 | | - envset.body = let([q[(name["v"], name[expr])]], letbody) |
| 253 | + envset = Attribute(value=q[name[e]], attr="_set", ctx=Load()) # use internal _set to allow new definitions |
269 | 254 |
|
270 | 255 | def islocaldef(tree): |
271 | 256 | return type(tree) is Call and type(tree.func) is Name and tree.func.id == "local" |
|
0 commit comments