oo.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package transpiler
  2. import "fmt"
  3. import "strings"
  4. var OO_Map = map[string]TransFunction {
  5. // class = @class name generic_params supers { init methods class_opt }
  6. "class": func (tree Tree, ptr int) string {
  7. var file = GetFileName(tree)
  8. var row, col = GetRowColInfo(tree, ptr)
  9. var children = Children(tree, ptr)
  10. var name_ptr = children["name"]
  11. var gp_ptr = children["generic_params"]
  12. var name = Transpile(tree, name_ptr)
  13. var impls = Transpile(tree, children["supers"])
  14. var init = Transpile(tree, children["init"])
  15. var pfs = Transpile(tree, children["pfs"])
  16. var methods = Transpile(tree, children["methods"])
  17. var options = Transpile(tree, children["class_opt"])
  18. var def_point = fmt.Sprintf (
  19. "{ file: %v, row: %v, col: %v }",
  20. file, row, col,
  21. )
  22. var class = fmt.Sprintf (
  23. "%v(%v, [%v, %v, %v, %v, %v, %v, %v], %v, %v, %v)",
  24. G(CALL), G(C_CLASS),
  25. name, impls, init, pfs, methods, options, def_point,
  26. file, row, col,
  27. )
  28. var value string
  29. if NotEmpty(tree, gp_ptr) {
  30. value = TypeTemplate(tree, gp_ptr, name_ptr, class)
  31. } else {
  32. value = class
  33. }
  34. return fmt.Sprintf (
  35. "%v(%v, [%v, %v, true, %v], %v, %v, %v)",
  36. G(CALL), L_VAR_DECL, name, value, G(T_TYPE),
  37. file, row, col,
  38. )
  39. },
  40. // supers? = @is typelist
  41. "supers": func (tree Tree, ptr int) string {
  42. if Empty(tree, ptr) {
  43. return "[]"
  44. } else {
  45. return TranspileLastChild(tree, ptr)
  46. }
  47. },
  48. // init = @init Call paralist_strict! body! creators
  49. "init": func (tree Tree, ptr int) string {
  50. var children = Children(tree, ptr)
  51. var class_ptr = tree.Nodes[ptr].Parent // note: access of parent node
  52. var class_children = Children(tree, class_ptr)
  53. var name = GetTokenContent(tree, class_children["name"])
  54. var main = InitFunction(tree, ptr, name)
  55. var alternatives = Transpile(tree, children["creators"])
  56. return fmt.Sprintf("[%v, %v]", main, alternatives)
  57. },
  58. // creators? = creator creators
  59. "creators": func (tree Tree, ptr int) string {
  60. var init_ptr = tree.Nodes[ptr].Parent // note: access of parent node
  61. var class_ptr = tree.Nodes[init_ptr].Parent
  62. var class_children = Children(tree, class_ptr)
  63. var name = GetTokenContent(tree, class_children["name"])
  64. var creator_ptrs = FlatSubTree(tree, ptr, "creator", "creators")
  65. var buf strings.Builder
  66. buf.WriteRune('[')
  67. for i, creator_ptr := range creator_ptrs {
  68. // creator = @create Call paralist_strict! body!
  69. buf.WriteString(InitFunction(tree, creator_ptr, name))
  70. if i != len(creator_ptrs)-1 {
  71. buf.WriteString(", ")
  72. }
  73. }
  74. buf.WriteRune(']')
  75. return buf.String()
  76. },
  77. // pfs? = pf pfs
  78. "pfs": func (tree Tree, ptr int) string {
  79. return MethodTable(tree, ptr, "pf", "pfs")
  80. },
  81. // methods? = method methods
  82. "methods": func (tree Tree, ptr int) string {
  83. return MethodTable(tree, ptr, "method", "methods")
  84. },
  85. // class_opt = operator_defs data
  86. "class_opt": func (tree Tree, ptr int) string {
  87. // rule name depended by FunctionMap["operator_defs"]
  88. var children = Children(tree, ptr)
  89. var ops = Transpile(tree, children["operator_defs"])
  90. var data = Transpile(tree, children["data"])
  91. return fmt.Sprintf("{ ops: %v, data: %v }", ops, data)
  92. },
  93. // data? = @data hash
  94. "data": func (tree Tree, ptr int) string {
  95. if NotEmpty(tree, ptr) {
  96. return TranspileLastChild(tree, ptr)
  97. } else {
  98. return "{}"
  99. }
  100. },
  101. // interface = @interface name generic_params { members }
  102. "interface": func (tree Tree, ptr int) string {
  103. var file = GetFileName(tree)
  104. var row, col = GetRowColInfo(tree, ptr)
  105. var children = Children(tree, ptr)
  106. var name_ptr = children["name"]
  107. var gp_ptr = children["generic_params"]
  108. var name = Transpile(tree, name_ptr)
  109. var members = Transpile(tree, children["members"])
  110. var def_point = fmt.Sprintf (
  111. "{ file: %v, row: %v, col: %v }",
  112. file, row, col,
  113. )
  114. var interface_ = fmt.Sprintf (
  115. "%v(%v, [%v, %v, %v], %v, %v, %v)",
  116. G(CALL), G(C_INTERFACE), name, members, def_point,
  117. file, row, col,
  118. )
  119. var value string
  120. if NotEmpty(tree, gp_ptr) {
  121. value = TypeTemplate(tree, gp_ptr, name_ptr, interface_)
  122. } else {
  123. value = interface_
  124. }
  125. return fmt.Sprintf (
  126. "%v(%v, [%v, %v, true, %v], %v, %v, %v)",
  127. G(CALL), L_VAR_DECL, name, value, G(T_TYPE), file, row, col,
  128. )
  129. },
  130. // members? = member members
  131. "members": func (tree Tree, ptr int) string {
  132. if Empty(tree, ptr) { return "[]" }
  133. return TranspileSubTree(tree, ptr, "member", "members")
  134. },
  135. // member = method_implemented | method_blank
  136. "member": TranspileFirstChild,
  137. // method_implemented = name Call paralist_strict! ->! type! body
  138. "method_implemented": func (tree Tree, ptr int) string {
  139. // note: the rule name "method_implemented" is depended by Function()
  140. var children = Children(tree, ptr)
  141. var name = Transpile(tree, children["name"])
  142. var method = TransMapByName["f_sync"](tree, ptr)
  143. return fmt.Sprintf (
  144. "{ name: %v, f: %v }",
  145. name, method,
  146. )
  147. },
  148. // method_blank = name Call paralist_strict! -> type
  149. "method_blank": func (tree Tree, ptr int) string {
  150. var children = Children(tree, ptr)
  151. var name = Transpile(tree, children["name"])
  152. var parameters = Transpile(tree, children["paralist_strict"])
  153. var value_type = Transpile(tree, children["type"])
  154. var proto = fmt.Sprintf (
  155. "{ parameters: %v, value_type: %v }",
  156. parameters, value_type,
  157. )
  158. return fmt.Sprintf (
  159. "{ name: %v, f: %v }",
  160. name, proto,
  161. )
  162. },
  163. }