various2.nim 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. discard """
  2. exitCode: 0
  3. outputsub: '''42 is greater than 0'''
  4. """
  5. if 42 >= 0:
  6. echo "42 is greater than 0"
  7. echo("Output: ",
  8. 5)
  9. echo(5 +
  10. 5)
  11. # --- Removed code that is supposed to fail here. Not going to test those. ---
  12. # Single-line comment
  13. #[
  14. Multiline comment
  15. ]#
  16. when false:
  17. echo("Commented-out code")
  18. let decimal = 42
  19. let hex = 0x42
  20. let octal = 0o42
  21. let binary = 0b101010
  22. let a: int16 = 42
  23. let b = 42'i8
  24. let c = 1'f32 # --- Changed names here to avoid clashes ---
  25. let d = 1.0e19
  26. let e = false
  27. let f = true
  28. let g = 'A'
  29. let h = '\109'
  30. let i = '\x79'
  31. let text = "The book title is \"Nim in Action\""
  32. let filepath = "C:\\Program Files\\Nim"
  33. # --- Changed name here to avoid clashes ---
  34. let filepath1 = r"C:\Program Files\Nim"
  35. let multiLine = """foo
  36. bar
  37. baz
  38. """
  39. echo multiLine
  40. import strutils
  41. # --- Changed name here to avoid clashes ---
  42. let multiLine1 = """foo
  43. bar
  44. baz
  45. """
  46. echo multiLine1.unindent
  47. doAssert multiLine1.unindent == "foo\nbar\nbaz\n"
  48. proc fillString(): string =
  49. result = ""
  50. echo("Generating string")
  51. for i in 0 .. 4:
  52. result.add($i) #<1>
  53. const count = fillString()
  54. var
  55. text1 = "hello"
  56. number: int = 10
  57. isTrue = false
  58. var 火 = "Fire"
  59. let ogień = true
  60. var `var` = "Hello"
  61. echo(`var`)
  62. proc myProc(name: string): string = "Hello " & name
  63. discard myProc("Dominik")
  64. proc bar(): int #<1>
  65. proc foo(): float = bar().float
  66. proc bar(): int = foo().int
  67. proc noReturn() = echo("Hello")
  68. proc noReturn2(): void = echo("Hello")
  69. proc noReturn3 = echo("Hello")
  70. proc message(recipient: string): auto =
  71. "Hello " & recipient
  72. doAssert message("Dom") == "Hello Dom"
  73. proc max(a: int, b: int): int =
  74. if a > b: a else: b
  75. doAssert max(5, 10) == 10
  76. proc max2(a, b: int): int =
  77. if a > b: a else: b
  78. proc genHello(name: string, surname = "Doe"): string =
  79. "Hello " & name & " " & surname
  80. # -- Leaving these as asserts as that is in the original code, just in case
  81. # -- somehow in the future `assert` is removed :)
  82. assert genHello("Peter") == "Hello Peter Doe"
  83. assert genHello("Peter", "Smith") == "Hello Peter Smith"
  84. proc genHello2(names: varargs[string]): string =
  85. result = ""
  86. for name in names:
  87. result.add("Hello " & name & "\n")
  88. doAssert genHello2("John", "Bob") == "Hello John\nHello Bob\n"
  89. proc getUserCity(firstName, lastName: string): string =
  90. case firstName
  91. of "Damien": return "Tokyo"
  92. of "Alex": return "New York"
  93. else: return "Unknown"
  94. proc getUserCity(userID: int): string =
  95. case userID
  96. of 1: return "Tokyo"
  97. of 2: return "New York"
  98. else: return "Unknown"
  99. doAssert getUserCity("Damien", "Lundi") == "Tokyo"
  100. doAssert getUserCity(2) == "New York" # -- Errata here: missing closing "
  101. import sequtils
  102. let numbers = @[1, 2, 3, 4, 5, 6]
  103. let odd = filter(numbers, proc (x: int): bool = x mod 2 != 0)
  104. doAssert odd == @[1, 3, 5]
  105. import sequtils, sugar
  106. let numbers1 = @[1, 2, 3, 4, 5, 6]
  107. let odd1 = filter(numbers1, (x: int) -> bool => x mod 2 != 0)
  108. assert odd1 == @[1, 3, 5]
  109. proc isValid(x: int, validator: proc (x: int): bool) =
  110. if validator(x): echo(x, " is valid")
  111. else: echo(x, " is NOT valid")
  112. import sugar
  113. proc isValid2(x: int, validator: (x: int) -> bool) =
  114. if validator(x): echo(x, " is valid")
  115. else: echo(x, " is NOT valid")
  116. var list: array[3, int]
  117. list[0] = 1
  118. list[1] = 42
  119. assert list[0] == 1
  120. assert list[1] == 42
  121. assert list[2] == 0 #<1>
  122. echo list.repr #<2>
  123. # echo list[500]
  124. var list2: array[-10 .. -9, int]
  125. list2[-10] = 1
  126. list2[-9] = 2
  127. var list3 = ["Hi", "There"]
  128. var list4 = ["My", "name", "is", "Dominik"]
  129. for item in list4:
  130. echo(item)
  131. for i in list4.low .. list4.high:
  132. echo(list4[i])
  133. var list5: seq[int] = @[]
  134. doAssertRaises(IndexDefect):
  135. list5[0] = 1
  136. list5.add(1)
  137. assert list5[0] == 1
  138. doAssertRaises(IndexDefect):
  139. echo list5[42]
  140. # -- Errata: var list: seq[int]; echo(list[0]). This now creates an exception,
  141. # -- not a SIGSEGV.
  142. block:
  143. var list = newSeq[string](3)
  144. assert list[0].len == 0
  145. list[0] = "Foo"
  146. list[1] = "Bar"
  147. list[2] = "Baz"
  148. list.add("Lorem")
  149. block:
  150. let list = @[4, 8, 15, 16, 23, 42]
  151. for i in 0 ..< list.len:
  152. stdout.write($list[i] & " ")
  153. var collection: set[int16]
  154. doAssert collection == {}
  155. block:
  156. let collection = {'a', 'x', 'r'}
  157. doAssert 'a' in collection
  158. block:
  159. let collection = {'a', 'T', 'z'}
  160. let isAllLowerCase = {'A' .. 'Z'} * collection == {}
  161. doAssert(not isAllLowerCase)
  162. let age = 10
  163. if age > 0 and age <= 10:
  164. echo("You're still a child")
  165. elif age > 10 and age < 18:
  166. echo("You're a teenager")
  167. else:
  168. echo("You're an adult")
  169. let variable = "Arthur"
  170. case variable
  171. of "Arthur", "Zaphod", "Ford":
  172. echo("Male")
  173. of "Marvin":
  174. echo("Robot")
  175. of "Trillian":
  176. echo("Female")
  177. else:
  178. echo("Unknown")
  179. let ageDesc = if age < 18: "Non-Adult" else: "Adult"
  180. block:
  181. var i = 0
  182. while i < 3:
  183. echo(i)
  184. i.inc
  185. block label:
  186. var i = 0
  187. while true:
  188. while i < 5:
  189. if i > 3: break label
  190. i.inc
  191. iterator values(): int =
  192. var i = 0
  193. while i < 5:
  194. yield i
  195. i.inc
  196. for value in values():
  197. echo(value)
  198. import os
  199. for filename in walkFiles("*.nim"):
  200. echo(filename)
  201. for item in @[1, 2, 3]:
  202. echo(item)
  203. for i, value in @[1, 2, 3]: echo("Value at ", i, ": ", value)
  204. doAssertRaises(IOError):
  205. proc second() =
  206. raise newException(IOError, "Somebody set us up the bomb")
  207. proc first() =
  208. second()
  209. first()
  210. block:
  211. proc second() =
  212. raise newException(IOError, "Somebody set us up the bomb")
  213. proc first() =
  214. try:
  215. second()
  216. except:
  217. echo("Cannot perform second action because: " &
  218. getCurrentExceptionMsg())
  219. first()
  220. block:
  221. type
  222. Person = object
  223. name: string
  224. age: int
  225. var person: Person
  226. var person1 = Person(name: "Neo", age: 28)
  227. block:
  228. type
  229. PersonObj = object
  230. name: string
  231. age: int
  232. PersonRef = ref PersonObj
  233. # proc setName(person: PersonObj) =
  234. # person.name = "George"
  235. proc setName(person: PersonRef) =
  236. person.name = "George"
  237. block:
  238. type
  239. Dog = object
  240. name: string
  241. Cat = object
  242. name: string
  243. let dog: Dog = Dog(name: "Fluffy")
  244. let cat: Cat = Cat(name: "Fluffy")
  245. block:
  246. type
  247. Dog = tuple
  248. name: string
  249. Cat = tuple
  250. name: string
  251. let dog: Dog = (name: "Fluffy")
  252. let cat: Cat = (name: "Fluffy")
  253. echo(dog == cat)
  254. block:
  255. type
  256. Point = tuple[x, y: int]
  257. Point2 = (int, int)
  258. let pos: Point = (x: 100, y: 50)
  259. doAssert pos == (100, 50)
  260. let pos1: Point = (x: 100, y: 50)
  261. let (x, y) = pos1 #<1>
  262. let (left, _) = pos1
  263. doAssert x == pos1[0]
  264. doAssert y == pos1[1]
  265. doAssert left == x
  266. block:
  267. type
  268. Color = enum
  269. colRed,
  270. colGreen,
  271. colBlue
  272. let color: Color = colRed
  273. block:
  274. type
  275. Color {.pure.} = enum
  276. red, green, blue
  277. let color = Color.red