tmacrostmt.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import macros
  2. macro case_token(n: varargs[untyped]): untyped =
  3. # creates a lexical analyzer from regular expressions
  4. # ... (implementation is an exercise for the reader :-)
  5. nil
  6. case_token: # this colon tells the parser it is a macro statement
  7. of r"[A-Za-z_]+[A-Za-z_0-9]*":
  8. return tkIdentifier
  9. of r"0-9+":
  10. return tkInteger
  11. of r"[\+\-\*\?]+":
  12. return tkOperator
  13. else:
  14. return tkUnknown
  15. case_token: inc i
  16. #bug #488
  17. macro foo() =
  18. var exp = newCall("whatwhat", newIntLitNode(1))
  19. if compiles(getAst(exp)):
  20. return exp
  21. else: echo "Does not compute! (test OK)"
  22. foo()
  23. #------------------------------------
  24. # bug #8287
  25. type MyString = distinct string
  26. proc `$` (c: MyString): string {.borrow.}
  27. proc `!!` (c: cstring): int =
  28. c.len
  29. proc f(name: MyString): int =
  30. !! $ name
  31. macro repr_and_parse(fn: typed) =
  32. let fn_impl = fn.getImpl
  33. fn_impl.name = genSym(nskProc, $fn_impl.name)
  34. echo fn_impl.repr
  35. result = parseStmt(fn_impl.repr)
  36. macro repr_to_string(fn: typed): string =
  37. let fn_impl = fn.getImpl
  38. result = newStrLitNode(fn_impl.repr)
  39. repr_and_parse(f)
  40. #------------------------------------
  41. # bugs #8343 and #8344
  42. proc one_if_proc(x, y : int): int =
  43. if x < y: result = x
  44. else: result = y
  45. proc test_block(x, y : int): int =
  46. block label:
  47. result = x
  48. result = y
  49. #------------------------------------
  50. # bugs #8348
  51. template `>`(x, y: untyped): untyped =
  52. ## "is greater" operator. This is the same as ``y < x``.
  53. y < x
  54. proc test_cond_stmtlist(x, y: int): int =
  55. result = x
  56. if x > y:
  57. result = x
  58. #------------------------------------
  59. # bug #8762
  60. proc t2(a, b: int): int =
  61. `+`(a, b)
  62. #------------------------------------
  63. # bug #8761
  64. proc fn1(x, y: int):int =
  65. 2 * (x + y)
  66. proc fn2(x, y: float): float =
  67. (y + 2 * x) / (x - y)
  68. proc fn3(x, y: int): bool =
  69. (((x and 3) div 4) or (x mod (y xor -1))) == 0 or y notin [1,2]
  70. proc fn4(x: int): int =
  71. if x mod 2 == 0: return x + 2
  72. else: return 0
  73. proc fn5(a, b: float): float =
  74. result = - a * a / (b * b)
  75. proc `{}`(x: seq[float], i: int, j: int): float =
  76. x[i + 0 * j]
  77. proc `{}=`(x: var seq[float], i: int, val: float) =
  78. x[i] = val
  79. proc fn6() =
  80. var a = @[1.0, 2.0]
  81. let z = a{0, 1}
  82. a{2} = 5.0
  83. #------------------------------------
  84. # bug #10807
  85. proc fn_unsafeaddr(x: int): int =
  86. cast[int](unsafeAddr(x))
  87. static:
  88. let fn1s = "proc fn1(x, y: int): int =\n result = 2 * (x + y)\n"
  89. let fn2s = "proc fn2(x, y: float): float =\n result = (y + 2 * x) / (x - y)\n"
  90. let fn3s = "proc fn3(x, y: int): bool =\n result = ((x and 3) div 4 or x mod (y xor -1)) == 0 or not contains([1, 2], y)\n"
  91. let fn4s = "proc fn4(x: int): int =\n if x mod 2 == 0:\n return x + 2\n else:\n return 0\n"
  92. let fn5s = "proc fn5(a, b: float): float =\n result = -a * a / (b * b)\n"
  93. let fn6s = "proc fn6() =\n var a = @[1.0, 2.0]\n let z = a{0, 1}\n a{2} = 5.0\n"
  94. let fnAddr = "proc fn_unsafeaddr(x: int): int =\n result = cast[int](unsafeAddr(x))\n"
  95. doAssert fn1.repr_to_string == fn1s
  96. doAssert fn2.repr_to_string == fn2s
  97. doAssert fn3.repr_to_string == fn3s
  98. doAssert fn4.repr_to_string == fn4s
  99. doAssert fn5.repr_to_string == fn5s
  100. doAssert fn6.repr_to_string == fn6s
  101. doAssert fn_unsafeaddr.repr_to_string == fnAddr
  102. #------------------------------------
  103. # bug #8763
  104. type
  105. A {.pure.} = enum
  106. X, Y
  107. B {.pure.} = enum
  108. X, Y
  109. proc test_pure_enums(a: B) =
  110. case a
  111. of B.X: echo B.X
  112. of B.Y: echo B.Y
  113. repr_and_parse(one_if_proc)
  114. repr_and_parse(test_block)
  115. repr_and_parse(test_cond_stmtlist)
  116. repr_and_parse(t2)
  117. repr_and_parse(test_pure_enums)