PEP 638: Typographic fixes (#2368)

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
David Foster 2022-04-29 06:51:43 -07:00 committed by GitHub
parent ebff37d5e7
commit 26d7a56338
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 12 deletions

View File

@ -1,10 +1,12 @@
PEP: 638 PEP: 638
Title: Syntactic Macros Title: Syntactic Macros
Author: Mark Shannon <mark@hotpy.org> Author: Mark Shannon <mark@hotpy.org>
Discussions-To: https://mail.python.org/archives/list/python-dev@python.org/thread/U4C4XHNRC4SHS3TPZWCTY4SN4QU3TT6V/
Status: Draft Status: Draft
Type: Standards Track Type: Standards Track
Content-Type: text/x-rst Content-Type: text/x-rst
Created: 24-Sep-2020 Created: 24-Sep-2020
Post-History: 26-Sep-2020
Abstract Abstract
======== ========
@ -169,7 +171,7 @@ Compilation
Upon encountering a ``macro`` during translation to bytecode, Upon encountering a ``macro`` during translation to bytecode,
the code generator will look up the macro processor registered for the macro, the code generator will look up the macro processor registered for the macro,
and pass the AST, rooted at the macro to the processor function. and pass the AST rooted at the macro to the processor function.
The returned AST will then be substituted for the original tree. The returned AST will then be substituted for the original tree.
For macros with multiple names, For macros with multiple names,
@ -219,14 +221,14 @@ Defining macro processors
~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~
A macro processor is defined by a four-tuple, consisting of A macro processor is defined by a four-tuple, consisting of
``(func, kind, version, additional_names)`` ``(func, kind, version, additional_names)``:
* ``func`` must be a callable that takes ``len(additional_names)+1`` arguments, all of which are abstract syntax trees, and returns a single abstract syntax tree. * ``func`` must be a callable that takes ``len(additional_names)+1`` arguments, all of which are abstract syntax trees, and returns a single abstract syntax tree.
* ``kind`` must be one of the following: * ``kind`` must be one of the following:
* ``macros.STMT_MACRO`` A statement macro where the body of the macro is indented. This is the only form allowed to have additional names. * ``macros.STMT_MACRO``: A statement macro where the body of the macro is indented. This is the only form allowed to have additional names.
* ``macros.SIBLING_MACRO`` A statement macro where the body of the macro is the next statement is the same block. The following statement is moved into the macro as its body. * ``macros.SIBLING_MACRO``: A statement macro where the body of the macro is the next statement in the same block. The following statement is moved into the macro as its body.
* ``macros.EXPR_MACRO`` An expression macro. * ``macros.EXPR_MACRO``: An expression macro.
* ``version`` is used to track versions of macros, so that generated bytecodes can be correctly cached. It must be an integer. * ``version`` is used to track versions of macros, so that generated bytecodes can be correctly cached. It must be an integer.
* ``additional_names`` are the names of the additional parts of the macro, and must be a tuple of strings. * ``additional_names`` are the names of the additional parts of the macro, and must be a tuple of strings.
@ -278,22 +280,19 @@ Two new AST nodes will be needed to express macros, ``macro_stmt`` and ``macro_e
:: ::
class macro_stmt(_ast.stmt): class macro_stmt(_ast.stmt):
_fields = "name", "args", "importname", "asname", "body" _fields = "name", "args", "importname", "asname", "body"
class macro_expr(_ast.expr): class macro_expr(_ast.expr):
_fields = "name", "args" _fields = "name", "args"
In addition, macro processors will needs a means to express control flow or side-effecting code, that produces a value. In addition, macro processors will need a means to express control flow or side-effecting code, that produces a value.
To support this, a new ast node, called ``stmt_expr``, that combines a statement and expression will be added. A new AST node called ``stmt_expr`` will be added, combining a statement and an expression.
This new ast node will be a subtype of ``expr``, but include a statement to allow side effects. This new ast node will be a subtype of ``expr``, but include a statement to allow side effects.
It will be compiled to bytecode by compiling the statement, then compiling the value. It will be compiled to bytecode by compiling the statement, then compiling the value.
:: ::
class stmt_expr(_ast.expr): class stmt_expr(_ast.expr):
_fields = "stmt", "value" _fields = "stmt", "value"
Hygiene and debugging Hygiene and debugging
@ -450,8 +449,8 @@ can be replaced with the zero-cost macro:
def foo(...): def foo(...):
... ...
Protyping language extensions Prototyping language extensions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Although macros would be most valuable for domain-specific extensions, it is possible to Although macros would be most valuable for domain-specific extensions, it is possible to
demonstrate possible language extensions using macros. demonstrate possible language extensions using macros.