expression.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package transpiler
  2. import "fmt"
  3. import "strings"
  4. import "../parser/syntax"
  5. func TranspileOperationSequence (tree Tree, ptr int) [][]string {
  6. if tree.Nodes[ptr].Part.Id != syntax.Name2Id["operand_tail"] {
  7. panic("invalid usage of TranspileOperationSequence()")
  8. }
  9. var file = GetFileName(tree)
  10. var operations = make([][]string, 0, 20)
  11. for NotEmpty(tree, ptr) {
  12. // operand_tail? = get operand_tail | call operand_tail
  13. var operation_ptr = tree.Nodes[ptr].Children[0]
  14. var next_ptr = tree.Nodes[ptr].Children[1]
  15. var op_node = &tree.Nodes[operation_ptr]
  16. var row, col = GetRowColInfo(tree, operation_ptr)
  17. switch syntax.Id2Name[op_node.Part.Id] {
  18. case "slice":
  19. // slice = Get [ low_bound : high_bound ]!
  20. var children = Children(tree, operation_ptr)
  21. var lo_ptr = children["low_bound"]
  22. var hi_ptr = children["high_bound"]
  23. var lo string
  24. var hi string
  25. if NotEmpty(tree, lo_ptr) {
  26. lo = TranspileFirstChild(tree, lo_ptr)
  27. } else {
  28. lo = G(T_SLICE_INDEX_DEF)
  29. }
  30. if NotEmpty(tree, hi_ptr) {
  31. hi = TranspileFirstChild(tree, hi_ptr)
  32. } else {
  33. hi = G(T_SLICE_INDEX_DEF)
  34. }
  35. operations = append(operations, [] string {
  36. G(SLICE), lo, hi,
  37. file, row, col,
  38. })
  39. case "get":
  40. // get_expr = Get [ expr! ]! nil_flag
  41. // get_name = Get . name! nil_flag
  42. var key, nil_flag = GetKey(tree, operation_ptr)
  43. operations = append(operations, []string {
  44. G(GET), key, nil_flag,
  45. file, row, col,
  46. })
  47. case "call":
  48. // call = call_self | call_method
  49. var child_ptr = op_node.Children[0]
  50. var params = Children(tree, child_ptr)
  51. // call_self = Call args
  52. // call_method = -> name method_args
  53. var args = TranspileLastChild(tree, child_ptr)
  54. var name_ptr, is_method_call = params["name"]
  55. if is_method_call {
  56. operations = append(operations, []string {
  57. L_METHOD_CALL, Transpile(tree, name_ptr), args,
  58. file, row, col,
  59. })
  60. } else {
  61. operations = append(operations, []string {
  62. G(CALL), args, file, row, col,
  63. })
  64. }
  65. }
  66. ptr = next_ptr
  67. }
  68. return operations
  69. }
  70. var ExpressionMap = map[string]TransFunction {
  71. // expr = lower_unary operand expr_tail | operand expr_tail
  72. "expr": func (tree Tree, ptr int) string {
  73. var file = GetFileName(tree)
  74. var children = Children(tree, ptr)
  75. var operand_ptrs = FlatSubTree(tree, ptr, "operand", "expr_tail")
  76. var tail = children["expr_tail"]
  77. var operator_ptrs = FlatSubTree(tree, tail, "operator", "expr_tail")
  78. var operators = make([]syntax.Operator, len(operator_ptrs))
  79. for i, operator_ptr := range operator_ptrs {
  80. operators[i] = GetOperatorInfo(tree, operator_ptr)
  81. }
  82. var reduced = ReduceExpression(operators)
  83. var do_transpile func(int) string
  84. do_transpile = func (operand_index int) string {
  85. if operand_index >= 0 {
  86. return Transpile(tree, operand_ptrs[operand_index])
  87. }
  88. var reduced_index = -(operand_index) - 1
  89. var sub_expr [3]int = reduced[reduced_index]
  90. var operand1 = do_transpile(sub_expr[0])
  91. var operand2 = do_transpile(sub_expr[1])
  92. var operator_ptr = operator_ptrs[sub_expr[2]]
  93. var operator = Transpile(tree, operator_ptr)
  94. var operator_info = operators[sub_expr[2]]
  95. var lazy_eval = operator_info.Lazy
  96. var row, col = GetRowColInfo(tree, operator_ptr)
  97. var real_operand2 string
  98. if lazy_eval {
  99. real_operand2 = LazyValueWrapper(operand2)
  100. } else {
  101. real_operand2 = operand2
  102. }
  103. return fmt.Sprintf(
  104. "%v(%v, [%v, %v], %v, %v, %v)",
  105. G(CALL), operator, operand1, real_operand2,
  106. file, row, col,
  107. )
  108. }
  109. var main_part = do_transpile(-len(reduced))
  110. var low_ptr, has_lower_unary = children["lower_unary"]
  111. if has_lower_unary {
  112. var low = Transpile(tree, low_ptr)
  113. var row, col = GetRowColInfo(tree, low_ptr)
  114. return fmt.Sprintf (
  115. "%v(%v, [%v], %v, %v, %v)",
  116. G(CALL), low, main_part, file, row, col,
  117. )
  118. } else {
  119. return main_part
  120. }
  121. },
  122. // lower_unary = @mount | @push
  123. "lower_unary": func (tree Tree, ptr int) string {
  124. var child = tree.Nodes[ptr].Children[0]
  125. var name = syntax.Id2Name[tree.Nodes[child].Part.Id]
  126. var file = GetFileName(tree)
  127. var row, col = GetRowColInfo(tree, ptr)
  128. if name == "@mount" {
  129. return fmt.Sprintf (
  130. "%v(%v, [], %v, %v, %v)",
  131. G(CALL), L_OP_MOUNT, file, row, col,
  132. )
  133. } else if name == "@push" {
  134. return fmt.Sprintf (
  135. "%v(%v, [], %v, %v, %v)",
  136. G(CALL), L_OP_PUSH, file, row, col,
  137. )
  138. } else {
  139. panic("unknown lower-unary operator " + name)
  140. }
  141. },
  142. // operand = unary operand_base operand_tail
  143. "operand": func (tree Tree, ptr int) string {
  144. var children = Children(tree, ptr)
  145. var unary_ptr = children["unary"]
  146. var base_ptr = children["operand_base"]
  147. var tail_ptr = children["operand_tail"]
  148. var base = Transpile(tree, base_ptr)
  149. var operations = TranspileOperationSequence(tree, tail_ptr)
  150. var buf strings.Builder
  151. var reduce func (int)
  152. reduce = func (i int) {
  153. if i == -1 {
  154. buf.WriteString(base)
  155. return
  156. }
  157. var op = operations[i]
  158. buf.WriteString(op[0])
  159. buf.WriteRune('(')
  160. reduce(i-1)
  161. for j := 1; j < len(op); j++ {
  162. buf.WriteString(", ")
  163. buf.WriteString(op[j])
  164. }
  165. buf.WriteRune(')')
  166. }
  167. var has_unary = NotEmpty(tree, unary_ptr)
  168. if has_unary {
  169. var unary = Transpile(tree, unary_ptr)
  170. buf.WriteString(G(CALL))
  171. buf.WriteRune('(')
  172. buf.WriteString(unary)
  173. buf.WriteString(", ")
  174. buf.WriteRune('[')
  175. }
  176. reduce(len(operations)-1)
  177. if has_unary {
  178. buf.WriteRune(']')
  179. var file = GetFileName(tree)
  180. var row, col = GetRowColInfo(tree, unary_ptr)
  181. buf.WriteString(", ")
  182. WriteList(&buf, []string {
  183. file, row, col,
  184. })
  185. buf.WriteRune(')')
  186. }
  187. return buf.String()
  188. },
  189. // unary? = unary_group1 | unary_group2 | unary_group3
  190. "unary": func (tree Tree, ptr int) string {
  191. var group_ptr = tree.Nodes[ptr].Children[0]
  192. var real_child_ptr = tree.Nodes[group_ptr].Children[0]
  193. var child_node = &tree.Nodes[real_child_ptr]
  194. var name = syntax.Id2Name[child_node.Part.Id]
  195. if name == "-" {
  196. return fmt.Sprintf (
  197. `%v("negate")`,
  198. G(OPERATOR),
  199. )
  200. } else {
  201. var t = strings.TrimPrefix(name, "@")
  202. return fmt.Sprintf (
  203. "%v(%v)",
  204. G(OPERATOR), EscapeRawString([]rune(t)),
  205. )
  206. }
  207. },
  208. // operand_base = wrapped | lambda | literal | dot_para | identifier
  209. "operand_base": TranspileFirstChild,
  210. // wrapped = ( expr! )!
  211. "wrapped": TranspileChild("expr"),
  212. // dot_para = . Name
  213. "dot_para": func (tree Tree, ptr int) string {
  214. var last_ptr = tree.Nodes[ptr].Children[tree.Nodes[ptr].Length-1]
  215. return VarLookup(tree, last_ptr)
  216. },
  217. // identifier = Name
  218. "identifier": func (tree Tree, ptr int) string {
  219. var content = GetTokenContent(tree, ptr)
  220. var content_string = string(content)
  221. if strings.HasPrefix(content_string, "__") && content_string != "__" {
  222. // ECMAScript Identifier
  223. var trimed = strings.TrimPrefix(content_string, "__")
  224. return fmt.Sprintf("(%v)", trimed)
  225. } else {
  226. return VarLookup(tree, ptr)
  227. }
  228. },
  229. // operator = op_group1 | op_compare | op_logic | op_arith
  230. "operator": func (tree Tree, ptr int) string {
  231. // depended by CommandsMap["set_op"]
  232. var info = GetOperatorInfo(tree, ptr)
  233. var buf strings.Builder
  234. buf.WriteString(G(OPERATOR))
  235. buf.WriteRune('(')
  236. var name = strings.TrimPrefix(info.Match, "@")
  237. buf.WriteString(EscapeRawString([]rune(name)))
  238. buf.WriteRune(')')
  239. return buf.String()
  240. },
  241. // nil_flag? = ?
  242. "nil_flag": func (tree Tree, ptr int) string {
  243. if NotEmpty(tree, ptr) {
  244. return "true"
  245. } else {
  246. return "false"
  247. }
  248. },
  249. // method_args = Call args | extra_arg
  250. "method_args": func (tree Tree, ptr int) string {
  251. var children = Children(tree, ptr)
  252. var extra_ptr, is_only_extra = children["extra_arg"]
  253. if is_only_extra {
  254. var buf strings.Builder
  255. buf.WriteRune('[')
  256. if tree.Nodes[extra_ptr].Length > 0 {
  257. buf.WriteString(Transpile(tree, extra_ptr))
  258. }
  259. buf.WriteRune(']')
  260. return buf.String()
  261. } else {
  262. return Transpile(tree, children["args"])
  263. }
  264. },
  265. // args = ( arglist )! extra_arg | < type_arglist >
  266. "args": func (tree Tree, ptr int) string {
  267. var children = Children(tree, ptr)
  268. var type_arglist, exists = children["type_arglist"]
  269. if exists { return Transpile(tree, type_arglist) }
  270. var buf strings.Builder
  271. buf.WriteRune('[')
  272. var arglist_ptr = children["arglist"]
  273. var has_arglist = NotEmpty(tree, arglist_ptr)
  274. if has_arglist {
  275. buf.WriteString(Transpile(tree, arglist_ptr))
  276. }
  277. var extra_ptr = children["extra_arg"]
  278. var has_extra = NotEmpty(tree, extra_ptr)
  279. if has_extra {
  280. if has_arglist {
  281. buf.WriteString(", ")
  282. }
  283. buf.WriteString(Transpile(tree, extra_ptr))
  284. }
  285. buf.WriteRune(']')
  286. return buf.String()
  287. },
  288. // extra_arg? = -> lambda
  289. "extra_arg": TranspileLastChild,
  290. // arglist? = exprlist
  291. "arglist": TranspileFirstChild,
  292. // exprlist = expr exprlist_tail
  293. "exprlist": func (tree Tree, ptr int) string {
  294. var expr_ptrs = FlatSubTree(tree, ptr, "expr", "exprlist_tail")
  295. var buf strings.Builder
  296. for i, expr_ptr := range expr_ptrs {
  297. buf.WriteString(Transpile(tree, expr_ptr))
  298. if i != len(expr_ptrs)-1 {
  299. buf.WriteString(", ")
  300. }
  301. }
  302. return buf.String()
  303. },
  304. }