topt_refcursors.nim 905 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. discard """
  2. output: ''''''
  3. cmd: '''nim c --gc:arc --expandArc:traverse --hint:Performance:off $file'''
  4. nimout: '''
  5. --expandArc: traverse
  6. var
  7. it_cursor
  8. jt
  9. try:
  10. it_cursor = root
  11. block :tmp:
  12. while (
  13. not (it_cursor == nil)):
  14. echo [it_cursor.s]
  15. it_cursor = it_cursor.ri
  16. `=copy`(jt, root)
  17. block :tmp_1:
  18. while (
  19. not (jt == nil)):
  20. var ri_1
  21. try:
  22. `=copy`(ri_1, jt.ri)
  23. echo [jt.s]
  24. `=sink`(jt, ri_1)
  25. `=wasMoved`(ri_1)
  26. finally:
  27. `=destroy`(ri_1)
  28. finally:
  29. `=destroy`(jt)
  30. -- end of expandArc ------------------------
  31. '''
  32. """
  33. type
  34. Node = ref object
  35. le, ri: Node
  36. s: string
  37. proc traverse(root: Node) =
  38. var it = root
  39. while it != nil:
  40. echo it.s
  41. it = it.ri
  42. var jt = root
  43. while jt != nil:
  44. let ri = jt.ri
  45. echo jt.s
  46. jt = ri
  47. traverse(nil)
  48. # XXX: This optimization is not sound