File: tmacro2.nim

package info (click to toggle)
nim 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,911,644 kB
  • sloc: sh: 24,603; ansic: 1,761; python: 1,492; makefile: 1,013; sql: 298; asm: 141; xml: 13
file content (36 lines) | stat: -rw-r--r-- 735 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
discard """
  output: "ta-da Your value sir: 'HE!!!!o Wor!!d'"
"""

import macros, strutils

proc testBlock(): string {.compileTime.} =
  block myBlock:
    while true:
      echo "inner block"
      break myBlock
    echo "outer block"
  result = "ta-da"

macro mac(n: typed): string =
  let n = callsite()
  expectKind(n, nnkCall)
  expectLen(n, 2)
  expectKind(n[1], nnkStrLit)
  var s: string = n[1].strVal
  s = s.replace("l", "!!")
  result = newStrLitNode("Your value sir: '$#'" % [s])

const s = testBlock()
const t = mac("HEllo World")
echo s, " ", t


#-----------------------------------------------------------------------------
# issue #15326
macro m(n:typed):auto =
  result = n

proc f[T](x:T): T {.m.} = x

discard f(3)