No description
Find a file
2021-10-26 16:47:10 +08:00
src remove echo 2021-10-26 16:47:10 +08:00
tests add support for match node directly 2021-10-26 15:18:21 +08:00
dslutils.nimble bump version 2021-10-26 15:20:06 +08:00
LICENSE add license 2021-10-22 14:37:59 +08:00
README.md add metadata 2021-10-23 12:53:21 +08:00

A macro collection for creating DSL in nim

Current modules: astpat, identsubs, metadata

ASTPAT: Do pattern match on nim ast

WARNING: require experimental option: caseStmtMacros, you can enable it by pragma {.experimental: "caseStmtMacros".}

usage:

case line:
of (`key` = `value*`):
  # key match only "simple" node like ident, sym, string, int
  # value* match all node
  echo repr key, " = ", repr value
of (a.`key` = func(`value*`)):
  echo "a.", repr key, " = func(", repr value, ")"

IDENTSUBS: Replace left most identifier in expression

usage:


type MyObj = object
  vint: int
  varr: array[2, int]

proc fun(self: MyObj) = echo repr self

var a = MyObj(vint: 1)
var b = MyObj(vint: 2, varr: [5, 6])

macro withObj(body: untyped{nkStmtList}) =
  result = newStmtList()
  for item in body.items:
    result.add: item.identsubs: {
      "vint": newDotExpr(bindSym "a", ident "vint"),
      "varr": newDotExpr(bindSym "a", ident "varr"),
      "primary": bindSym "a",
      "secondary": bindSym "b",
    }

withObj:
  vint = 4
  varr[0] = 1
  primary.fun()
  fun(secondary)

result:

a.vint = 4
a.varr[0] = 1
a.fun()
fun(b)

METADATA: Attach metadata to any value

Usage:

let value = 5 ~~ { meta: 1 }

macro dumpMetadata(target: typed) =
  let meta = target.metadata
  for kv in meta:
    echo treerepr kv

dumpMetadata value

got:

ExprColonExpr
  Ident "meta"
  IntLit 1