Skip to content

codehz/dslutils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages