tmacrobust1512.nim 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. discard """
  2. output: ""
  3. """
  4. import macros, strutils
  5. # https://github.com/nim-lang/Nim/issues/1512
  6. proc macrobust0(input: string): string =
  7. var output = ""
  8. proc p1(a:string) =
  9. output.add(a)
  10. proc p2(a:string) = p1(a)
  11. proc p3(a:string) = p2(a)
  12. proc p4(a:string) = p3(a)
  13. proc p5(a:string) = p4(a)
  14. proc p6(a:string) = p5(a)
  15. proc p7(a:string) = p6(a)
  16. proc p8(a:string) = p7(a)
  17. proc p9(a:string) = p8(a)
  18. proc p10(a:string) = p9(a)
  19. proc p11(a:string) = p10(a)
  20. proc p12(a:string) = p11(a)
  21. proc p13(a:string) = p12(a)
  22. proc p14(a:string) = p13(a)
  23. proc p15(a:string) = p14(a)
  24. proc p16(a:string) = p15(a)
  25. proc p17(a:string) = p16(a)
  26. proc p18(a:string) = p17(a)
  27. proc p19(a:string) = p18(a)
  28. proc p20(a:string) = p19(a)
  29. for a in input.split():
  30. p20(a)
  31. p19(a)
  32. p18(a)
  33. p17(a)
  34. p16(a)
  35. p15(a)
  36. p14(a)
  37. p13(a)
  38. p12(a)
  39. p11(a)
  40. p10(a)
  41. p9(a)
  42. p8(a)
  43. p7(a)
  44. p6(a)
  45. p5(a)
  46. p4(a)
  47. p3(a)
  48. p2(a)
  49. p1(a)
  50. result = output
  51. macro macrobust(input: static[string]): untyped =
  52. var output = ""
  53. proc p1(a:string) =
  54. output.add(a)
  55. proc p2(a:string) = p1(a)
  56. proc p3(a:string) = p2(a)
  57. proc p4(a:string) = p3(a)
  58. proc p5(a:string) = p4(a)
  59. proc p6(a:string) = p5(a)
  60. proc p7(a:string) = p6(a)
  61. proc p8(a:string) = p7(a)
  62. proc p9(a:string) = p8(a)
  63. proc p10(a:string) = p9(a)
  64. proc p11(a:string) = p10(a)
  65. proc p12(a:string) = p11(a)
  66. proc p13(a:string) = p12(a)
  67. proc p14(a:string) = p13(a)
  68. proc p15(a:string) = p14(a)
  69. proc p16(a:string) = p15(a)
  70. proc p17(a:string) = p16(a)
  71. proc p18(a:string) = p17(a)
  72. proc p19(a:string) = p18(a)
  73. proc p20(a:string) = p19(a)
  74. for a in input.split():
  75. p20(a)
  76. p19(a)
  77. p18(a)
  78. p17(a)
  79. p16(a)
  80. p15(a)
  81. p14(a)
  82. p13(a)
  83. p12(a)
  84. p11(a)
  85. p10(a)
  86. p9(a)
  87. p8(a)
  88. p7(a)
  89. p6(a)
  90. p5(a)
  91. p4(a)
  92. p3(a)
  93. p2(a)
  94. p1(a)
  95. result = newLit(output)
  96. const input = """
  97. fdsasadfsdfa sadfsdafsdaf
  98. dsfsdafdsfadsfa fsdaasdfasdf
  99. fsdafsadfsad asdfasdfasdf
  100. fdsasdfasdfa sadfsadfsadf
  101. sadfasdfsdaf sadfsdafsdaf dsfasdaf
  102. sadfsdafsadf fdsasdafsadf fdsasadfsdaf
  103. sdfasadfsdafdfsa sadfsadfsdaf
  104. sdafsdaffsda sdfasadfsadf
  105. fsdasdafsdfa sdfasdfafsda
  106. sdfasdafsadf sdfasdafsdaf sdfasdafsdaf
  107. """
  108. let str1 = macrobust(input)
  109. let str2 = macrobust0(input)
  110. doAssert str1 == str2