Skip to content

Conversation

@W95Psp
Copy link
Member

@W95Psp W95Psp commented Aug 13, 2025

This PR adds a improves the printing and backend infrastructure.

The PrettyAst trait

The main change of this PR in the introduction of the PrettyAst trait, that reduces boilerplate.
Instead of implementing pretty::Pretty for every type of the AST, one must only implement PrettyAst, which has default methods.
The default methods print debugging information that makes it easy to iterate when writting a printer.

Helper macros

The pretty library we are using for pretty printing uses an explicit allocator: whenever we must render a node, the allocator needs to be specified expclitely.
As it is visible in the existing lean printer, this is very verbose.

This PR introduces pretty_ast::install_pretty_helpers!, a macro that hides this allocator away. This macro basically creates partial applications of the allocator method using a local variable.

Instead of doing:

docs![allocator, "something", intersperse!(docs![allocator, node], docs![allocator, ",", allocator.space()])]

You can do:

install_pretty_helpers!(allocator);
docs!["something", intersperse!(docs![node], docs![",", space!()])]

We also introduce the macro prepend_associated_functions_with!(something) that prepends something to each body of associated function.

Combining the trait PrettyAst, the macro prepend_associated_functions_with and the macros given by install_pretty_helpers, we get implicit allocator across entire printers.
See backends::rust for an example.

Rust Printer

This PR introduces a dummy placeholder Rust printer.

@W95Psp W95Psp requested a review from clementblaudeau August 13, 2025 16:24
@W95Psp W95Psp force-pushed the rengine-print-traits branch from 46306f4 to af84fbc Compare August 14, 2025 07:01
@W95Psp W95Psp marked this pull request as ready for review August 14, 2025 08:07
@W95Psp W95Psp requested a review from a team as a code owner August 14, 2025 08:07
@W95Psp W95Psp requested review from a team and jschneider-bensch and removed request for a team August 14, 2025 08:07
Copy link
Contributor

@clementblaudeau clementblaudeau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR addresses a real issue of verbosity when writing backends. It will make the code more concise and provide better error messages when parts of the ast are unsupported.

@W95Psp W95Psp force-pushed the rengine-print-traits branch from 3d8ec14 to 0ae2e7a Compare August 14, 2025 13:18
@W95Psp W95Psp force-pushed the rengine-print-traits branch from 0ae2e7a to 643423e Compare August 14, 2025 13:20
Copy link
Contributor

@jschneider-bensch jschneider-bensch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clément already brought up a lot of things I wanted to ask about, and you already addressed those.
I have, as usual, some questions, but I think the code is good!

@W95Psp
Copy link
Member Author

W95Psp commented Aug 14, 2025

Thanks @clementblaudeau and @jschneider-bensch for your reviews! :)
Let's merge :shipit:

@W95Psp W95Psp added this pull request to the merge queue Aug 14, 2025
Merged via the queue into main with commit 8854417 Aug 14, 2025
17 checks passed
@W95Psp W95Psp deleted the rengine-print-traits branch August 14, 2025 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants