Skip to content

Instantly share code, notes, and snippets.

@andreaferretti
Created July 12, 2016 15:23
Show Gist options
  • Save andreaferretti/d55f290d32bad11eaba5afd2e476ac5e to your computer and use it in GitHub Desktop.
Save andreaferretti/d55f290d32bad11eaba5afd2e476ac5e to your computer and use it in GitHub Desktop.
Nim macro expansion
import macros
macro expandMacros(body: stmt): stmt =
template inner(x: stmt): stmt = x
result = getAst(inner(body))
echo result.toStrLit
@autf
Copy link

autf commented Apr 4, 2023

https://nim-lang.org/docs/macros.html#expandMacros.m%2Ctyped
https://github.com/nim-lang/Nim/blob/version-1-6/lib/core/macros.nim#L1502

macro expandMacros*(body: typed): untyped =
  ## Expands one level of macro - useful for debugging.
  ## Can be used to inspect what happens when a macro call is expanded,
  ## without altering its result.
  ##
  ## For instance,
  ##
  ## .. code-block:: nim
  ##   import std/[sugar, macros]
  ##
  ##   let
  ##     x = 10
  ##     y = 20
  ##   expandMacros:
  ##     dump(x + y)
  ##
  ## will actually dump `x + y`, but at the same time will print at
  ## compile time the expansion of the `dump` macro, which in this
  ## case is `debugEcho ["x + y", " = ", x + y]`.
  echo body.toStrLit
  result = body

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment