|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | """Local bindings (let), imperative code in expression position (do).""" |
3 | 3 |
|
4 | | -__all__ = ["let", "letseq", "letrec", |
| 4 | +__all__ = ["where", |
| 5 | + "let", "letseq", "letrec", |
5 | 6 | "dlet", "dletseq", "dletrec", |
6 | 7 | "blet", "bletseq", "bletrec", |
7 | 8 | "local", "delete", "do", "do0"] |
|
30 | 31 |
|
31 | 32 | from mcpyrate.quotes import macros, q, u, n, a, t, h # noqa: F401 |
32 | 33 |
|
33 | | -from mcpyrate import gensym, parametricmacro |
| 34 | +from mcpyrate import gensym, namemacro, parametricmacro |
34 | 35 | from mcpyrate.quotes import capture_as_macro, is_captured_value |
35 | 36 | from mcpyrate.walkers import ASTTransformer, ASTVisitor |
36 | 37 |
|
@@ -85,6 +86,22 @@ def _destructure_and_apply_let(tree, args, macro_expander, let_transformer, allo |
85 | 86 | # -------------------------------------------------------------------------------- |
86 | 87 | # Macro interface - expr macros |
87 | 88 |
|
| 89 | +@namemacro |
| 90 | +def where(tree, *, syntax, **kw): |
| 91 | + """[syntax, special] `where` operator for let. |
| 92 | +
|
| 93 | + Usage:: |
| 94 | +
|
| 95 | + let[body, where((k0, v0), ...)] |
| 96 | +
|
| 97 | + Only meaningful for declaring the bindings in a let-where, for all |
| 98 | + expression-form let constructs: `let`, `letseq`, `letrec`, `let_syntax`, |
| 99 | + `abbrev`. |
| 100 | + """ |
| 101 | + if syntax != "name": |
| 102 | + raise SyntaxError("where (unpythonic.syntax.letdo.where) is a name macro only") # pragma: no cover |
| 103 | + raise SyntaxError("where() is only meaningful in a let[body, where((k0, v0), ...)]") # pragma: no cover |
| 104 | + |
88 | 105 | @parametricmacro |
89 | 106 | def let(tree, *, args, syntax, expander, **kw): |
90 | 107 | """[syntax, expr] Introduce expression-local variables. |
|
0 commit comments