PageRenderTime 11ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/source/core/io/OcWriter.ooc

http://github.com/nddrylliog/oc
Unknown | 87 lines | 70 code | 17 blank | 0 comment | 0 complexity | ace705a08970fbad4b3824641997f1be MD5 | raw file
  1. import io/Writer, structs/List
  2. OcWriter: class {
  3. stream: Writer
  4. tabLevel := 0
  5. tabWidth := 2
  6. init: func (=stream) { }
  7. close: func {
  8. app('\n')
  9. stream close()
  10. }
  11. app: func ~chr (c: Char) {
  12. stream write(c)
  13. }
  14. app: func ~str (s: String) {
  15. stream write(s)
  16. }
  17. /**
  18. * <left> elem0 <delim> elem1 <delim> elem2 <delim> elem3 <right>
  19. * f is passed every elem of list and is responsible of writing them
  20. */
  21. writeEach: func <T> (list: List<T>, left, delim, right: String, f: Func(T)) {
  22. app(left)
  23. first := true
  24. list each(|e|
  25. if(!first) app(delim)
  26. f(e)
  27. first = false
  28. )
  29. app(right)
  30. }
  31. /**
  32. * {
  33. * elem0 <delim>
  34. * elem1 <delim>
  35. * elem2 <delim>
  36. * }
  37. * f is passed every elem of list and is responsible of writing them
  38. */
  39. writeBlock: func <T> (list: List<T>, delim: String, f: Func(T)) {
  40. openBlock()
  41. list each(|e|
  42. nl()
  43. f(e)
  44. app(delim)
  45. )
  46. closeBlock()
  47. }
  48. writeTabs: func {
  49. stream write(" " times (tabLevel * tabWidth), tabLevel * tabWidth)
  50. }
  51. newUntabbedLine: func {
  52. stream write('\n')
  53. }
  54. nl: func {
  55. newUntabbedLine()
  56. writeTabs()
  57. }
  58. tab: func {
  59. tabLevel += 1
  60. }
  61. untab: func {
  62. tabLevel -= 1
  63. }
  64. openBlock: func {
  65. this app("{"). tab()
  66. }
  67. closeBlock: func {
  68. this untab(). nl(). app("}")
  69. }
  70. }