nim-lang / RFCs

A repository for your Nim proposals.
135 stars 26 forks source link

"Strict" mode for renderer that outputs more parseable code #482

Open metagn opened 1 year ago

metagn commented 1 year ago

Abstract

The Nim AST renderer should have a "strict" mode on top of the current, default behavior, to output code that is easier to parse.

Motivation

There are issues like https://github.com/nim-lang/Nim/issues/19735 which will impact performance of the renderer if fixed exhaustively. Making it opt-in makes it easier to justify.

Description

Obviously it might still be that a strict mode will not be able to output correct code in some cases (for example some identifiers are not possible without the double-backtick syntax proposed here and here). But it will still account for more cases than normal rendering would.

Code Examples

From https://github.com/nim-lang/Nim/issues/19735:

import macros

macro dumpStrictRepr(u: typed) =
  echo repr(u, rmStrict)

template decl(a: untyped) =
  proc `a @ procName`(`a @ param` {.inject.}: int) = discard

dumpStrictRepr:
  decl(hi)

Output:

proc `hi@procName`(`a@param`: int) =
  discard

Backwards Compatibility

No response