src/fusion/astdsl

Search:
Group by:

Macros

macro buildAst(children: untyped): NimNode
macro buildAst(node, children: untyped): NimNode

A DSL for convenient construction of Nim ASTs (of type NimNode). It composes with all of Nim's control flow constructs.

Note: Check The AST in Nim section of macros module on how to construct valid Nim ASTs.

Also see dumpTree, dumpAstGen and dumpLisp.

Example:

import macros

macro hello(): untyped =
  result = buildAst(stmtList):
    call(bindSym"echo", newLit"Hello world")

macro min(args: varargs[untyped]): untyped =
  result = buildAst(stmtListExpr):
    let tmp = genSym(nskVar, "minResult")
    expectMinLen(args, 1)
    newVarStmt(tmp, args[0])
    for i in 1..<args.len:
      ifStmt:
        elifBranch(infix(ident"<", args[i], tmp)):
          asgn(tmp, args[i])
    tmp

assert min("d", "c", "b", "a") == "a"