topt_refcursors.nim 900 B

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