Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Mar 22, 2021
1 parent 4970051 commit a038d95
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/core/macros.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1460,8 +1460,9 @@ macro genAstOpt*(options: static set[GenAstOpt], args: varargs[untyped]): untype
else:
newEmptyNode()

proc newLitMaybe[T](a: T): auto =
when compiles(newLit(a)): newLit(a)
template newLitMaybe(a): untyped =
when type(a) is NimNode: a
elif compiles(newLit(a)): newLit(a)
else: a

# using `_` as workaround, see https://github.com/nim-lang/Nim/issues/2465#issuecomment-511076669
Expand Down
19 changes: 19 additions & 0 deletions tests/macros/tgenast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,22 @@ block: # sanity check: check passing `{}` also works
macro bar(): untyped =
result = genAstOpt({}, s1=true): s1
doAssert bar() == true

block: # test passing function and type symbols
proc z1(): auto = 41
type Z4 = type(1'i8)
macro bar(Z1: typedesc): untyped =
proc z2(): auto = 42
proc z3[T](a: T): auto = 43
let Z2 = genAst():
type(true)
let z4 = genAst():
proc myfun(): auto = 44
myfun
type Z3 = type(1'u8)
result = genAst(z4, Z1, Z2):
# z1, z2, z3, Z3, Z4 are captured automatically
# z1, z2, z3 can optionally be specified in capture list
(z1(), z2(), z3('a'), z4(), $Z1, $Z2, $Z3, $Z4)
type Z1 = type('c')
doAssert bar(Z1) == (41, 42, 43, 44, "char", "bool", "uint8", "int8")

0 comments on commit a038d95

Please sign in to comment.