tvarargsuntyped.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. discard """
  2. output: '''Let's go!
  3. (left: 2, r: 7, x: 8, height: 4, s: test, width: 3, y: 9, top: 1, g: 7, b: 8)
  4. (left: 2, r: 7, x: 8, height: 4, s: text, width: 3, y: 9, top: 1, g: 7, b: 8)
  5. (left: 2, r: 7, x: 8, height: 4, s: text, width: 3, y: 9, top: 4, g: 7, b: 8)
  6. (left: 2, r: 7, x: 8, height: 4, s: test, width: 3, y: 9, top: 1, g: 7, b: 8)
  7. 10
  8. hello 18.0'''
  9. """
  10. import macros
  11. proc internalBar(top, left, width, height: cint, s: string, x, y: int, r,g,b: int) =
  12. echo "(left: ", left, ", r: ", r, ", x: ", x, ", height: ", height, ", s: ", s,
  13. ", width: ", width, ", y: ", y, ", top: ", top, ", g: ", g, ", b: ", b, ")"
  14. # we need these dummy constructors due to the wrong implementation
  15. # of 'varargs[untyped]' in the compiler:
  16. proc point(x, y: int): int = discard
  17. proc color(r, g, b: int): int = discard
  18. proc rect(a, b, c, d: int): int = discard
  19. template declareUnpackingMacro(nimname,extname) =
  20. macro nimname(n: varargs[untyped]): untyped =
  21. var s: string = astToStr(extname) & "("
  22. var first = true
  23. echo repr n
  24. for x in n.children:
  25. var unpack = false
  26. if x.kind in nnkCallKinds:
  27. case $x[0]
  28. of "point":
  29. expectLen(x, 3)
  30. unpack = true
  31. of "rect":
  32. expectLen(x, 5)
  33. unpack = true
  34. of "color":
  35. expectLen(x, 4)
  36. unpack = true
  37. else: discard
  38. if unpack:
  39. for i in 1..<x.len:
  40. if first:
  41. first = false
  42. else:
  43. add(s, ", ")
  44. add(s, repr(x[i]))
  45. else:
  46. if first:
  47. first = false
  48. else:
  49. add(s, ", ")
  50. add(s, repr(x))
  51. add(s, ")")
  52. echo s
  53. result = parseStmt(s)
  54. declareUnpackingMacro(bar,internalBar)
  55. type MyInt = distinct int
  56. proc myInt(i: int): MyInt = cast[MyInt](i)
  57. converter toCInt(mi: MyInt): cint = cast[cint](mi)
  58. echo "Let's go!"
  59. bar(rect(1, 2, 3, 4), "test", point(8, 9), color(7,7,8))
  60. bar(1,2,3,4,"text",8,9,7,7,8)
  61. bar(myInt(4),2,3,4,"text",8,9,7,7,8)
  62. let top: cint = 1
  63. let left: cint = 2
  64. let width: cint = 3
  65. let height: cint = 4
  66. bar(rect(top, left, width, height), "test", point(8, 9), color(7,7,8))
  67. # bug #10075
  68. import macros
  69. proc convert_hidden_stdconv(args: NimNode): NimNode =
  70. var n = args
  71. while n.len == 1 and n[0].kind == nnkHiddenStdConv:
  72. n = n[0][1]
  73. return n
  74. macro t2(s: int, v: varargs[untyped]): untyped =
  75. let v = convert_hidden_stdconv(v)
  76. echo v.treeRepr
  77. let (v1, v2) = (v[0], v[1])
  78. quote do:
  79. echo `v1`, " ", `v2`
  80. template t1(s: int, v: varargs[typed]) =
  81. #static:
  82. # dumpTree v
  83. echo s
  84. t2(s, v)
  85. t1(10, "hello", 18.0)