thallo.nim 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Hallo
  2. import
  3. os, strutils, macros
  4. type
  5. TMyEnum = enum
  6. meA, meB, meC, meD
  7. when isMainModule:
  8. {.hint: "this is the main file".}
  9. proc fac[T](x: T): T =
  10. # test recursive generic procs
  11. if x <= 1: return 1
  12. else: return x.`*`(fac(x-1))
  13. macro macrotest(n: varargs[untyped]): untyped =
  14. let n = callsite()
  15. expectKind(n, nnkCall)
  16. expectMinLen(n, 2)
  17. result = newNimNode(nnkStmtList, n)
  18. for i in 2..n.len-1:
  19. result.add(newCall("write", n[1], n[i]))
  20. result.add(newCall("writeLine", n[1], newStrLitNode("")))
  21. macro debug(n: untyped): untyped {.immediate.} =
  22. let n = callsite()
  23. result = newNimNode(nnkStmtList, n)
  24. for i in 1..n.len-1:
  25. result.add(newCall("write", newIdentNode("stdout"), toStrLit(n[i])))
  26. result.add(newCall("write", newIdentNode("stdout"), newStrLitNode(": ")))
  27. result.add(newCall("writeLine", newIdentNode("stdout"), n[i]))
  28. macrotest(stdout, "finally", 4, 5, "variable", "argument lists")
  29. macrotest(stdout)
  30. #GC_disable()
  31. echo("This was compiled by Nim version " & system.NimVersion)
  32. writeLine(stdout, "Hello", " World", "!")
  33. echo(["a", "b", "c", "d"].len)
  34. for x in items(["What's", "your", "name", "?", ]):
  35. echo(x)
  36. var `name` = readLine(stdin)
  37. {.breakpoint.}
  38. echo("Hi " & thallo.name & "!\n")
  39. debug(name)
  40. var testseq: seq[string] = @[
  41. "a", "b", "c", "d", "e"
  42. ]
  43. echo(repr(testseq))
  44. var dummy = "hello"
  45. echo(substr(dummy, 2, 3))
  46. echo($meC)
  47. # test tuples:
  48. for x, y in items([(1, 2), (3, 4), (6, 1), (5, 2)]):
  49. echo x
  50. echo y
  51. proc simpleConst(): int = return 34
  52. # test constant evaluation:
  53. const
  54. constEval3 = simpleConst()
  55. constEval = "abc".contains('b')
  56. constEval2 = fac(7)
  57. echo(constEval3)
  58. echo(constEval)
  59. echo(constEval2)
  60. echo(1.`+`(2))
  61. for i in 2..6:
  62. for j in countdown(i+4, 2):
  63. echo(fac(i * j))
  64. when isMainModule:
  65. {.hint: "this is the main file".}