op.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 gtr(_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 leq(_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 neq(_a interface{}, _b interface{}) bool {
  83. assert.For(_a != nil, 20)
  84. assert.For(_b != nil, 21)
  85. var a int32 = int32Of(_a)
  86. var b int32 = int32Of(_b)
  87. return a != b
  88. }
  89. func not(_a interface{}) bool {
  90. assert.For(_a != nil, 20)
  91. var a bool = boolOf(_a)
  92. return !a
  93. }
  94. func is(p object.Object, typ object.ComplexType) bool {
  95. var compare func(x, a object.RecordType) bool
  96. compare = func(x, a object.RecordType) bool {
  97. switch {
  98. case x.Name() == a.Name():
  99. // fmt.Println("eq")
  100. return true //опасно сравнивать имена конеш
  101. case x.BaseType() != nil:
  102. // fmt.Println("go base")
  103. return compare(x.BaseType(), a)
  104. default:
  105. return false
  106. }
  107. }
  108. x, a := p.Complex().(object.RecordType)
  109. y, b := typ.(object.RecordType)
  110. fmt.Println("compare", p.Complex(), typ, a, b, a && b && compare(x, y))
  111. return a && b && compare(x, y)
  112. }
  113. func length(a object.Object, _a, _b interface{}) (ret int64) {
  114. //assert.For(a != nil, 20)
  115. assert.For(_b != nil, 21)
  116. var b int32 = int32Of(_b)
  117. assert.For(b == 0, 22)
  118. if a != nil {
  119. assert.For(a.Type() == object.COMPLEX, 23)
  120. switch typ := a.Complex().(type) {
  121. case object.ArrayType:
  122. ret = typ.Len()
  123. case object.DynArrayType:
  124. switch _a.(type) {
  125. case string:
  126. ret = int64(utf8.RuneCountInString(_a.(string)))
  127. default:
  128. ret = 0
  129. fmt.Sprintln("unsupported", reflect.TypeOf(_a))
  130. }
  131. default:
  132. panic(fmt.Sprintln("unsupported", reflect.TypeOf(a.Complex())))
  133. }
  134. } else {
  135. switch _a.(type) {
  136. case string:
  137. ret = int64(utf8.RuneCountInString(_a.(string)))
  138. case []interface{}:
  139. ret = int64(len(_a.([]interface{})))
  140. default:
  141. panic(fmt.Sprintln("unsupported", reflect.TypeOf(_a)))
  142. }
  143. }
  144. return ret
  145. }
  146. func mopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  147. var fu nodeframe.FrameUtils
  148. sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
  149. n := fu.NodeOf(f).(node.MonadicNode)
  150. op := func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  151. n := fu.NodeOf(f)
  152. switch n.(node.OperationNode).Operation() {
  153. case operation.NOT:
  154. fu.DataOf(f.Parent())[n] = not(fu.DataOf(f)[n.Left()])
  155. return frame.End()
  156. case operation.IS:
  157. x := sc.Select(scope.Designator(n.Left())).(object.Object)
  158. fu.DataOf(f.Parent())[n] = is(x, n.Object())
  159. return frame.End()
  160. default:
  161. panic("no such op")
  162. }
  163. }
  164. switch n.Operation() {
  165. case operation.CONVERT:
  166. switch n.Left().(type) {
  167. case node.VariableNode, node.ParameterNode:
  168. x := sc.Select(scope.Designator(n.Left()))
  169. assert.For(x != nil, 40)
  170. switch n.Type() {
  171. case object.INTEGER:
  172. switch x.(type) {
  173. case int8:
  174. fu.DataOf(f.Parent())[n] = int32(x.(int8))
  175. default:
  176. panic(fmt.Sprintln("ooops", reflect.TypeOf(x)))
  177. }
  178. default:
  179. panic("wrong type")
  180. }
  181. return frame.End()
  182. default:
  183. panic(fmt.Sprintln("unsupported left", reflect.TypeOf(n.Left())))
  184. }
  185. case operation.NOT:
  186. switch n.Left().(type) {
  187. case node.ConstantNode:
  188. fu.DataOf(f)[n.Left()] = n.Left().(node.ConstantNode).Data()
  189. return op, frame.NOW
  190. case node.VariableNode, node.ParameterNode:
  191. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  192. sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
  193. fu.DataOf(f)[n.Left()] = sc.Select(scope.Designator(n.Left()))
  194. return op, frame.NOW
  195. }
  196. ret = frame.NOW
  197. return seq, ret
  198. case node.OperationNode, node.DerefNode:
  199. fu.Push(fu.New(n.Left()), f)
  200. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  201. return op, frame.NOW
  202. }
  203. ret = frame.LATER
  204. return seq, ret
  205. case node.FieldNode:
  206. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  207. sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
  208. fu.DataOf(f)[n.Left()] = sc.Select(scope.Designator(n.Left()))
  209. return op, frame.NOW
  210. }
  211. ret = frame.NOW
  212. return seq, ret
  213. default:
  214. fmt.Println(reflect.TypeOf(n.Left()))
  215. panic("wrong left")
  216. }
  217. case operation.IS:
  218. switch n.Left().(type) {
  219. case node.VariableNode, node.ParameterNode:
  220. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  221. return op, frame.NOW
  222. }
  223. ret = frame.NOW
  224. return seq, ret
  225. default:
  226. fmt.Println(reflect.TypeOf(n.Left()))
  227. panic("wrong left")
  228. }
  229. default:
  230. panic(fmt.Sprintln("no such operation", n.(node.MonadicNode).Operation()))
  231. }
  232. }
  233. func dopSeq(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  234. var fu nodeframe.FrameUtils
  235. op := func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  236. n := fu.NodeOf(f)
  237. switch n.(node.OperationNode).Operation() {
  238. case operation.PLUS:
  239. fu.DataOf(f.Parent())[n] = sum(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  240. return frame.End()
  241. case operation.MINUS:
  242. fu.DataOf(f.Parent())[n] = sub(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  243. return frame.End()
  244. case operation.EQUAL:
  245. fu.DataOf(f.Parent())[n] = eq(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  246. return frame.End()
  247. case operation.LESSER:
  248. fu.DataOf(f.Parent())[n] = lss(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  249. return frame.End()
  250. case operation.LESS_EQUAL:
  251. fu.DataOf(f.Parent())[n] = leq(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  252. return frame.End()
  253. case operation.LEN:
  254. fu.DataOf(f.Parent())[n] = length(n.Left().Object(), fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  255. return frame.End()
  256. case operation.NOT_EQUAL:
  257. fu.DataOf(f.Parent())[n] = neq(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  258. return frame.End()
  259. case operation.GREATER:
  260. fu.DataOf(f.Parent())[n] = gtr(fu.DataOf(f)[n.Left()], fu.DataOf(f)[n.Right()])
  261. return frame.End()
  262. default:
  263. panic("unknown operation")
  264. }
  265. }
  266. right := func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  267. n := fu.NodeOf(f)
  268. switch n.Right().(type) {
  269. case node.ConstantNode:
  270. fu.DataOf(f)[n.Right()] = n.Right().(node.ConstantNode).Data()
  271. return op, frame.NOW
  272. case node.VariableNode, node.ParameterNode:
  273. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  274. sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
  275. fu.DataOf(f)[n.Right()] = sc.Select(scope.Designator(n.Right()))
  276. //fmt.Println(n.Right().Object(), reflect.TypeOf(n.Right().Object()))
  277. assert.For(fu.DataOf(f)[n.Right()] != nil, 60)
  278. return op, frame.NOW
  279. }
  280. ret = frame.NOW
  281. return seq, ret
  282. case node.OperationNode, node.DerefNode:
  283. fu.Push(fu.New(n.Right()), f)
  284. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  285. return op, frame.NOW
  286. }
  287. ret = frame.LATER
  288. return seq, ret
  289. case node.FieldNode:
  290. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  291. sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
  292. fu.DataOf(f)[n.Right()] = sc.Select(scope.Designator(n.Right()))
  293. return op, frame.NOW
  294. }
  295. ret = frame.NOW
  296. return seq, ret
  297. default:
  298. fmt.Println(reflect.TypeOf(n.Right()))
  299. panic("wrong right")
  300. }
  301. }
  302. left := func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  303. n := fu.NodeOf(f)
  304. switch n.Left().(type) {
  305. case node.ConstantNode:
  306. fu.DataOf(f)[n.Left()] = n.Left().(node.ConstantNode).Data()
  307. return right, frame.NOW
  308. case node.VariableNode, node.ParameterNode:
  309. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  310. sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
  311. fu.DataOf(f)[n.Left()] = sc.Select(scope.Designator(n.Left()))
  312. return right, frame.NOW
  313. }
  314. ret = frame.NOW
  315. return seq, ret
  316. case node.OperationNode, node.DerefNode:
  317. fu.Push(fu.New(n.Left()), f)
  318. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  319. return right, frame.NOW
  320. }
  321. ret = frame.LATER
  322. return seq, ret
  323. case node.FieldNode:
  324. seq = func(f frame.Frame) (seq frame.Sequence, ret frame.WAIT) {
  325. sc := f.Domain().Discover(context.SCOPE).(scope.Manager)
  326. fu.DataOf(f)[n.Left()] = sc.Select(scope.Designator(n.Left()))
  327. return right, frame.NOW
  328. }
  329. ret = frame.NOW
  330. return seq, ret
  331. default:
  332. fmt.Println(reflect.TypeOf(n.Left()))
  333. panic("wrong left")
  334. }
  335. }
  336. return left, frame.NOW
  337. }