tstmtexprs.nim 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. discard """
  2. output: '''24
  3. (bar: "bar")
  4. 1244
  5. 6
  6. abcdefghijklmnopqrstuvwxyz
  7. 145 23
  8. 3
  9. 2'''
  10. """
  11. import strutils
  12. const fac4 = (var x = 1; for i in 1..4: x *= i; x)
  13. echo fac4
  14. when true:
  15. proc test(foo: proc (x, y: int): bool) =
  16. echo foo(5, 5)
  17. type Foo = object
  18. bar: string
  19. proc newfoo(): Foo =
  20. result.bar = "bar"
  21. echo($newfoo())
  22. proc retInt(x, y: int): int =
  23. if (var yy = 0; yy != 0):
  24. echo yy
  25. else:
  26. echo(try: parseInt("1244") except ValueError: -1)
  27. result = case x
  28. of 23: 3
  29. of 64:
  30. case y
  31. of 1: 2
  32. of 2: 3
  33. of 3: 6
  34. else: 8
  35. else: 1
  36. echo retInt(64, 3)
  37. proc buildString(): string =
  38. result = ""
  39. for x in 'a'..'z':
  40. result.add(x)
  41. echo buildString()
  42. #test(
  43. # proc (x, y: int): bool =
  44. # if x == 5: return true
  45. # if x == 2: return false
  46. # if y == 78: return true
  47. #)
  48. proc q(): int {.discardable.} = 145
  49. proc p(): int =
  50. q()
  51. proc p2(a: int): int =
  52. # result enforces a void context:
  53. if a == 2:
  54. result = 23
  55. q()
  56. echo p(), " ", p2(2)
  57. proc semiProblem() =
  58. if false: echo "aye"; echo "indeed"
  59. semiProblem()
  60. # bug #844
  61. import json
  62. proc parseResponse(): JsonNode =
  63. result = % { "key1": % { "key2": % "value" } }
  64. for key, val in result["key1"]:
  65. var excMsg = key & "("
  66. if (var n=result["key2"]; n != nil):
  67. excMsg &= n.str
  68. raise newException(CatchableError, excMsg)
  69. #bug #992
  70. var se = @[1,2]
  71. let b = (se[1] = 1; 1)
  72. # bug #1161
  73. type
  74. PFooBase = ref object of RootRef
  75. field: int
  76. PFoo[T] = ref object of PFooBase
  77. field2: T
  78. var testIf =
  79. if true:
  80. 2
  81. else:
  82. 3
  83. var testCase =
  84. case 8
  85. of 8: 9
  86. else: 10
  87. var testTry =
  88. try:
  89. PFoo[string](field: 3, field2: "asfasf")
  90. except:
  91. PFooBase(field: 5)
  92. echo(testTry.field)
  93. # bug #6166
  94. proc quo(op: proc (x: int): bool): int =
  95. result =
  96. if op(3):
  97. 2
  98. else:
  99. 0
  100. echo(
  101. if true:
  102. quo do (a: int) -> bool:
  103. a mod 2 != 0
  104. else:
  105. quo do (a: int) -> bool:
  106. a mod 3 != 0
  107. )
  108. # bug #6980
  109. proc fooBool: bool {.discardable.} =
  110. true
  111. if true:
  112. fooBool()
  113. else:
  114. raise newException(ValueError, "argh")
  115. # bug #5374
  116. proc test1(): int64 {.discardable.} = discard
  117. proc test2(): int {.discardable.} = discard
  118. if true:
  119. test1()
  120. else:
  121. test2()