Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEP 638: Typographic fixes #2368

Merged
merged 4 commits into from
Apr 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions pep-0638.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
PEP: 638
Title: Syntactic Macros
Author: Mark Shannon <[email protected]>
Discussions-To: https://mail.python.org/archives/list/[email protected]/thread/U4C4XHNRC4SHS3TPZWCTY4SN4QU3TT6V/
Status: Draft
Type: Standards Track
Content-Type: text/x-rst
Created: 24-Sep-2020
Post-History: 26-Sep-2020

Abstract
========
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down