PEP 638: Typographic fixes (#2368)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
parent
ebff37d5e7
commit
26d7a56338
23
pep-0638.rst
23
pep-0638.rst
|
@ -1,10 +1,12 @@
|
|||
PEP: 638
|
||||
Title: Syntactic Macros
|
||||
Author: Mark Shannon <mark@hotpy.org>
|
||||
Discussions-To: https://mail.python.org/archives/list/python-dev@python.org/thread/U4C4XHNRC4SHS3TPZWCTY4SN4QU3TT6V/
|
||||
Status: Draft
|
||||
Type: Standards Track
|
||||
Content-Type: text/x-rst
|
||||
Created: 24-Sep-2020
|
||||
Post-History: 26-Sep-2020
|
||||
|
||||
Abstract
|
||||
========
|
||||
|
@ -169,7 +171,7 @@ Compilation
|
|||
|
||||
Upon encountering a ``macro`` during translation to bytecode,
|
||||
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.
|
||||
|
||||
For macros with multiple names,
|
||||
|
@ -219,14 +221,14 @@ Defining macro processors
|
|||
~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
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.
|
||||
* ``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.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.EXPR_MACRO`` An expression macro.
|
||||
* ``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 in the same block. The following statement is moved into the macro as its body.
|
||||
* ``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.
|
||||
* ``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):
|
||||
|
||||
_fields = "name", "args", "importname", "asname", "body"
|
||||
|
||||
class macro_expr(_ast.expr):
|
||||
|
||||
_fields = "name", "args"
|
||||
|
||||
In addition, macro processors will needs 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.
|
||||
In addition, macro processors will need a means to express control flow or side-effecting code, that produces a value.
|
||||
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.
|
||||
It will be compiled to bytecode by compiling the statement, then compiling the value.
|
||||
|
||||
::
|
||||
|
||||
class stmt_expr(_ast.expr):
|
||||
|
||||
_fields = "stmt", "value"
|
||||
|
||||
Hygiene and debugging
|
||||
|
@ -450,8 +449,8 @@ can be replaced with the zero-cost macro:
|
|||
def foo(...):
|
||||
...
|
||||
|
||||
Protyping language extensions
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Prototyping language extensions
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
Although macros would be most valuable for domain-specific extensions, it is possible to
|
||||
demonstrate possible language extensions using macros.
|
||||
|
|
Loading…
Reference in New Issue