op.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. package rules
  2. import (
  3. "fmt"
  4. "fw/cp/constant/operation"
  5. "fw/cp/node"
  6. "fw/cp/object"
  7. "fw/rt2/context"
  8. "fw/rt2/frame"
  9. "fw/rt2/nodeframe"
  10. "fw/rt2/scope"
  11. "reflect"
  12. "unicode/utf8"
  13. "ypk/assert"
  14. )
  15. func boolOf(x interface{}) (a bool) {
  16. switch x.(type) {
  17. case *bool:
  18. z := *x.(*bool)
  19. a = z
  20. case bool:
  21. a = x.(bool)
  22. default:
  23. panic(fmt.Sprintln("unsupported type", reflect.TypeOf(x)))
  24. }
  25. return a
  26. }
  27. func int32Of(x interface{}) (a int32) {
  28. //fmt.Println(reflect.TypeOf(x))
  29. switch x.(type) {
  30. case *int32:
  31. z := *x.(*int32)
  32. a = z
  33. case int32:
  34. a = x.(int32)
  35. default:
  36. panic(fmt.Sprintln("unsupported type", reflect.TypeOf(x)))
  37. }
  38. return a
  39. }
  40. func sum(_a interface{}, _b interface{}) interface{} {
  41. assert.For(_a != nil, 20)
  42. assert.For(_b != nil, 21)
  43. var a int32 = int32Of(_a)
  44. var b int32 = int32Of(_b)
  45. return a + b
  46. }
  47. func sub(_a interface{}, _b interface{}) interface{} {
  48. assert.For(_a != nil, 20)
  49. assert.For(_b != nil, 21)
  50. var a int32 = int32Of(_a)
  51. var b int32 = int32Of(_b)
  52. return a - b
  53. }
  54. func eq(_a interface{}, _b interface{}) bool {
  55. assert.For(_a != nil, 20)
  56. assert.For(_b != nil, 21)
  57. var a int32 = int32Of(_a)
  58. var b int32 = int32Of(_b)
  59. return a == b
  60. }
  61. func lss(_a interface{}, _b interface{}) bool {
  62. assert.For(_a != nil, 20)
  63. assert.For(_b != nil, 21)
  64. var a int32 = int32Of(_a)
  65. var b int32 = int32Of(_b)
  66. return a < b
  67. }
  68. func leq(_a interface{}, _b interface{}) bool {
  69. assert.For(_a != nil, 20)
  70. assert.For(_b != nil, 21)
  71. var a int32 = int32Of(_a)
  72. var b int32 = int32Of(_b)
  73. return a <= b
  74. }
  75. func neq(_a interface{}, _b interface{}) bool {
  76. assert.For(_a != nil, 20)
  77. assert.For(_b != nil, 21)
  78. var a int32 = int32Of(_a)
  79. var b int32 = int32Of(_b)
  80. return a != b
  81. }
  82. func not(_a interface{}) bool {
  83. assert.For(_a != nil, 20)
  84. var a bool = boolOf(_a)
  85. return !a
  86. }
  87. func is(p, typ object.Object) bool {
  88. var compare func(x, a object.RecordType) bool
  89. compare = func(x, a object.RecordType) bool {
  90. switch {
  91. case x.Name() == a.Name():
  92. // fmt.Println("eq")
  93. return true //опасно сравнивать имена конеш
  94. case x.BaseType() != nil:
  95. // fmt.Println("go base")
  96. return compare(x.BaseType(), a)
  97. default:
  98. return false
  99. }
  100. }
  101. x, a := p.Complex().(object.RecordType)
  102. y, b := typ.Complex().(object.RecordType)
  103. //fmt.Println("compare", p.Complex(), typ.Complex(), a, b, compare(x, y))
  104. return a && b && compare(x, y)
  105. }
  106. func length(a object.Object, _a, _b interface{}) (ret int64) {
  107. //assert.For(a != nil, 20)
  108. assert.For(_b != nil, 21)
  109. var b int32 = int32Of(_b)
  110. assert.For(b == 0, 22)
  111. if a != nil {
  112. assert.For(a.Type() == object.COMPLEX, 23)
  113. switch typ := a.Complex().(type) {
  114. case object.ArrayType:
  115. ret = typ.Len()
  116. case object.DynArrayType:
  117. switch _a.(type) {
  118. case string:
  119. ret = int64(utf8.RuneCountInString(_a.(string)))
  120. default:
  121. ret = 0
  122. fmt.Sprintln("unsupported", reflect.TypeOf(_a))
  123. }
  124. default:
  125. panic(fmt.Sprintln("unsupported", reflect.TypeOf(a.Complex())))
  126. }
  127. } else {
  128. switch _a.(type) {
  129. case string:
  130. ret = int64(utf8.RuneCountInString(_a.(string)))
  131. case []interface{}:
  132. ret = int64(len(_a.([]interface{})))
  133. default:
  134. panic(fmt.Sprintln("unsupported", reflect.TypeOf(_a)))
  135. }
  136. }
  137. return ret
  138. }
  139. func mopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  140. var fu nodeframe.FrameUtils
  141. sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
  142. n := fu.NodeOf(f).(node.MonadicNode)
  143. op := func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  144. n := fu.NodeOf(f)
  145. switch n.(node.OperationNode).Operation() {
  146. case operation.NOT:
  147. fu.DataOf(f.Parent())[n] = not(fu.DataOf(f)[n.Left()])
  148. return frame.End()
  149. case operation.IS:
  150. x := sc.Select(scope.Designator(n.Left())).(object.Object)
  151. fu.DataOf(f.Parent())[n] = is(x, n.Object())
  152. return frame.End()
  153. default:
  154. panic("no such op")
  155. }
  156. }
  157. switch n.Operation() {
  158. case operation.CONVERT:
  159. switch n.Left().(type) {
  160. case node.VariableNode, node.ParameterNode:
  161. x := sc.Select(scope.Designator(n.Left()))
  162. assert.For(x != nil, 40)
  163. switch n.Type() {
  164. case object.INTEGER:
  165. switch x.(type) {
  166. case int8:
  167. fu.DataOf(f.Parent())[n] = int32(x.(int8))
  168. default:
  169. panic(fmt.Sprintln("ooops", reflect.TypeOf(x)))
  170. }
  171. default:
  172. panic("wrong type")
  173. }
  174. return frame.End()
  175. default:
  176. panic(fmt.Sprintln("unsupported left", reflect.TypeOf(n.Left())))
  177. }
  178. case operation.NOT:
  179. switch n.Left().(type) {
  180. case node.ConstantNode:
  181. fu.DataOf(f)[n.Left()] = n.Left().(node.ConstantNode).Data()
  182. return op, frame.NOW
  183. case node.VariableNode, node.ParameterNode:
  184. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  185. sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
  186. fu.DataOf(f)[n.Left()] = sc.Select(scope.Designator(n.Left()))
  187. return op, frame.NOW
  188. }
  189. ret = frame.NOW
  190. return seq, ret
  191. case node.OperationNode, node.DerefNode:
  192. fu.Push(fu.New(n.Left()), f)
  193. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  194. return op, frame.NOW
  195. }
  196. ret = frame.LATER
  197. return seq, ret
  198. case node.FieldNode:
  199. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  200. sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
  201. fu.DataOf(f)[n.Left()] = sc.Select(scope.Designator(n.Left()))
  202. return op, frame.NOW
  203. }
  204. ret = frame.NOW
  205. return seq, ret
  206. default:
  207. fmt.Println(reflect.TypeOf(n.Left()))
  208. panic("wrong left")
  209. }
  210. case operation.IS:
  211. switch n.Left().(type) {
  212. case node.VariableNode, node.ParameterNode:
  213. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  214. return op, frame.NOW
  215. }
  216. ret = frame.NOW
  217. return seq, ret
  218. default:
  219. fmt.Println(reflect.TypeOf(n.Left()))
  220. panic("wrong left")
  221. }
  222. default:
  223. panic(fmt.Sprintln("no such operation", n.(node.MonadicNode).Operation()))
  224. }
  225. }
  226. func dopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  227. var fu nodeframe.FrameUtils
  228. op := func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  229. n := fu.NodeOf(f)
  230. switch n.(node.OperationNode).Operation() {
  231. case operation.PLUS:
  232. fu.DataOf(f.Parent())[n] = sum(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  233. return frame.End()
  234. case operation.MINUS:
  235. fu.DataOf(f.Parent())[n] = sub(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  236. return frame.End()
  237. case operation.EQUAL:
  238. fu.DataOf(f.Parent())[n] = eq(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  239. return frame.End()
  240. case operation.LESSER:
  241. fu.DataOf(f.Parent())[n] = lss(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  242. return frame.End()
  243. case operation.LESS_EQUAL:
  244. fu.DataOf(f.Parent())[n] = leq(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  245. return frame.End()
  246. case operation.LEN:
  247. fu.DataOf(f.Parent())[n] = length(n.Left().Object(), fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  248. return frame.End()
  249. case operation.NOT_EQUAL:
  250. fu.DataOf(f.Parent())[n] = neq(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  251. return frame.End()
  252. default:
  253. panic("unknown operation")
  254. }
  255. }
  256. right := func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  257. n := fu.NodeOf(f)
  258. switch n.Right().(type) {
  259. case node.ConstantNode:
  260. fu.DataOf(f)[n.Right()] = n.Right().(node.ConstantNode).Data()
  261. return op, frame.NOW
  262. case node.VariableNode, node.ParameterNode:
  263. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  264. sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
  265. fu.DataOf(f)[n.Right()] = sc.Select(scope.Designator(n.Right()))
  266. //fmt.Println(n.Right().Object(), reflect.TypeOf(n.Right().Object()))
  267. assert.For(fu.DataOf(f)[n.Right()] != nil, 60)
  268. return op, frame.NOW
  269. }
  270. ret = frame.NOW
  271. return seq, ret
  272. case node.OperationNode, node.DerefNode:
  273. fu.Push(fu.New(n.Right()), f)
  274. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  275. return op, frame.NOW
  276. }
  277. ret = frame.LATER
  278. return seq, ret
  279. case node.FieldNode:
  280. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  281. sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
  282. fu.DataOf(f)[n.Right()] = sc.Select(scope.Designator(n.Right()))
  283. return op, frame.NOW
  284. }
  285. ret = frame.NOW
  286. return seq, ret
  287. default:
  288. fmt.Println(reflect.TypeOf(n.Right()))
  289. panic("wrong right")
  290. }
  291. }
  292. left := func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  293. n := fu.NodeOf(f)
  294. switch n.Left().(type) {
  295. case node.ConstantNode:
  296. fu.DataOf(f)[n.Left()] = n.Left().(node.ConstantNode).Data()
  297. return right, frame.NOW
  298. case node.VariableNode, node.ParameterNode:
  299. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  300. sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
  301. fu.DataOf(f)[n.Left()] = sc.Select(scope.Designator(n.Left()))
  302. return right, frame.NOW
  303. }
  304. ret = frame.NOW
  305. return seq, ret
  306. case node.OperationNode, node.DerefNode:
  307. fu.Push(fu.New(n.Left()), f)
  308. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  309. return right, frame.NOW
  310. }
  311. ret = frame.LATER
  312. return seq, ret
  313. case node.FieldNode:
  314. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  315. sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
  316. fu.DataOf(f)[n.Left()] = sc.Select(scope.Designator(n.Left()))
  317. return right, frame.NOW
  318. }
  319. ret = frame.NOW
  320. return seq, ret
  321. default:
  322. fmt.Println(reflect.TypeOf(n.Left()))
  323. panic("wrong left")
  324. }
  325. }
  326. return left, frame.NOW
  327. }