client.nim 973 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. discard """
  2. output: "Done"
  3. cmd: "nim $target --debuginfo --hints:on --define:useNimRtl $options $file"
  4. """
  5. type
  6. TNodeKind = enum nkLit, nkSub, nkAdd, nkDiv, nkMul
  7. TNode = object
  8. case k: TNodeKind
  9. of nkLit: x: int
  10. else: a, b: ref TNode
  11. PNode = ref TNode
  12. when defined(windows):
  13. const dllname = "server.dll"
  14. elif defined(macosx):
  15. const dllname = "libserver.dylib"
  16. else:
  17. const dllname = "libserver.so"
  18. proc newLit(x: int): PNode {.importc: "newLit", dynlib: dllname.}
  19. proc newOp(k: TNodeKind, a, b: PNode): PNode {.
  20. importc: "newOp", dynlib: dllname.}
  21. proc buildTree(x: int): PNode {.importc: "buildTree", dynlib: dllname.}
  22. proc eval(n: PNode): int =
  23. case n.k
  24. of nkLit: result = n.x
  25. of nkSub: result = eval(n.a) - eval(n.b)
  26. of nkAdd: result = eval(n.a) + eval(n.b)
  27. of nkDiv: result = eval(n.a) div eval(n.b)
  28. of nkMul: result = eval(n.a) * eval(n.b)
  29. # Test the GC:
  30. for i in 0..100_000:
  31. discard eval(buildTree(2))
  32. echo "Done"