do.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. package eval
  2. import (
  3. "fw/cp"
  4. "fw/cp/constant/enter"
  5. "fw/cp/node"
  6. "fw/rt2"
  7. "fw/rt2/context"
  8. "fw/rt2/frame"
  9. "fw/rt2/scope"
  10. "reflect"
  11. "ypk/assert"
  12. )
  13. import (
  14. "ypk/halt"
  15. )
  16. func BeginExpression(in IN) (out OUT) {
  17. switch e := in.IR.(type) {
  18. case node.ConstantNode:
  19. out = Now(getConst)
  20. case node.DyadicNode:
  21. out = Now(getDop)
  22. case node.MonadicNode:
  23. out = Now(getMop)
  24. case node.RangeNode:
  25. out = Now(getRange)
  26. case node.Designator:
  27. out = Now(BeginDesignator)
  28. default:
  29. halt.As(100, reflect.TypeOf(e))
  30. }
  31. return
  32. }
  33. func BeginDesignator(in IN) (out OUT) {
  34. switch e := in.IR.(type) {
  35. case node.VariableNode:
  36. out = Now(getVar)
  37. case node.ParameterNode:
  38. out = Now(getVarPar)
  39. case node.ProcedureNode:
  40. out = Now(getProc)
  41. case node.DerefNode:
  42. out = Now(getDeref)
  43. case node.FieldNode:
  44. out = Now(getField)
  45. case node.IndexNode:
  46. out = Now(getIndex)
  47. case node.GuardNode:
  48. out = Now(getGuard)
  49. default:
  50. halt.As(100, reflect.TypeOf(e))
  51. }
  52. return
  53. }
  54. func GetStrange(in IN, key interface{}, ss node.Node, next Do) (out OUT) {
  55. assert.For(ss != nil, 20)
  56. assert.For(key != nil, 21)
  57. switch ss.(type) {
  58. case node.IfNode:
  59. nf := rt2.New(ss)
  60. rt2.Push(nf, in.Frame)
  61. rt2.RegOf(in.Frame)[context.KEY] = key
  62. rt2.Assert(in.Frame, func(f frame.Frame, do frame.Condition) {
  63. v := rt2.RegOf(f)[key]
  64. do(v != nil, 1980)
  65. })
  66. return Later(func(IN) OUT {
  67. return Now(next)
  68. })
  69. default:
  70. halt.As(100, reflect.TypeOf(ss))
  71. }
  72. return
  73. }
  74. func GetExpression(in IN, key interface{}, expr node.Node, next Do) OUT {
  75. assert.For(expr != nil, 20)
  76. _, e_ok := expr.(node.Expression)
  77. _, d_ok := expr.(node.Designator)
  78. assert.For(e_ok || d_ok, 21, reflect.TypeOf(expr))
  79. assert.For(key != nil, 22)
  80. nf := rt2.New(expr)
  81. rt2.Push(nf, in.Frame)
  82. rt2.RegOf(in.Frame)[context.KEY] = key
  83. rt2.RegOf(in.Frame)[key] = nil
  84. rt2.Assert(in.Frame, func(f frame.Frame, do frame.Condition) {
  85. if _, call := expr.(node.CallNode); call {
  86. tmp := rt2.RegOf(in.Frame)[context.RETURN]
  87. if tmp != nil {
  88. tmp_id := cp.ID(cp.Some())
  89. rt2.RegOf(in.Frame)[key] = tmp_id
  90. rt2.ValueOf(in.Frame)[tmp_id] = tmp.(scope.Value)
  91. }
  92. }
  93. v := rt2.RegOf(f)[key]
  94. do(v != nil, 1961, " ", key)
  95. })
  96. return Later(func(IN) OUT {
  97. return Now(next)
  98. })
  99. }
  100. func GetDesignator(in IN, key interface{}, design node.Node, next Do) OUT {
  101. assert.For(design != nil, 20)
  102. _, ok := design.(node.Designator)
  103. _, call := design.(node.CallNode)
  104. assert.For(ok || call, 21, reflect.TypeOf(design))
  105. assert.For(key != nil, 22)
  106. nf := rt2.New(design)
  107. rt2.Push(nf, in.Frame)
  108. rt2.RegOf(in.Frame)[context.KEY] = key
  109. rt2.RegOf(in.Frame)[key] = nil
  110. rt2.RegOf(in.Frame)[context.META] = nil
  111. rt2.Assert(in.Frame, func(f frame.Frame, do frame.Condition) {
  112. //do(m != nil, 1480, " no meta")
  113. if m := rt2.RegOf(f)[context.META]; m != nil {
  114. meta := m.(*Meta)
  115. if call {
  116. tmp := rt2.RegOf(in.Frame)[context.RETURN]
  117. assert.For(tmp != nil, 40)
  118. tmp_id := cp.ID(cp.Some())
  119. rt2.RegOf(in.Frame)[key] = tmp_id
  120. var vr scope.Value
  121. switch t := tmp.(type) {
  122. case scope.Pointer:
  123. vr = t.Copy()
  124. case scope.Index:
  125. vr = t
  126. }
  127. assert.For(vr != nil, 50)
  128. rt2.ValueOf(in.Frame)[tmp_id] = vr
  129. //meta.Scope = rt2.ScopeFor(in.Frame, vr.Id())
  130. //meta.Id = vr.Id()
  131. }
  132. do(meta.Scope != nil || meta.Rec != nil || meta.Arr != nil, 1380, " wrong meta")
  133. }
  134. v := rt2.RegOf(f)[key]
  135. do(v != nil, 1957, " no data for ", key)
  136. })
  137. return Later(func(IN) OUT {
  138. return Now(next)
  139. })
  140. }
  141. func BeginStrange(in IN) OUT {
  142. switch s := in.IR.(type) {
  143. case node.IfNode:
  144. return Now(doIf)
  145. default:
  146. halt.As(100, reflect.TypeOf(s))
  147. }
  148. panic(0)
  149. }
  150. func BeginStatement(in IN) (out OUT) {
  151. switch n := in.IR.(type) {
  152. case node.EnterNode:
  153. var tail Do
  154. tail = func(in IN) OUT {
  155. q := in.Frame.Root().Queue()
  156. if q != nil {
  157. in.Frame.Root().PushFor(q, nil)
  158. return Later(tail)
  159. } else {
  160. return Now(doEnter)
  161. }
  162. }
  163. out = Now(tail)
  164. case node.CompNode:
  165. next := n
  166. return Now(func(IN) OUT {
  167. right := func(in IN) OUT {
  168. if next.Right() != nil {
  169. rt2.Push(rt2.New(next.Right()), in.Frame)
  170. return Later(Tail(STOP))
  171. }
  172. return End()
  173. }
  174. left := func(in IN) OUT {
  175. if next.Left() != nil {
  176. rt2.Push(rt2.New(next.Left()), in.Frame)
  177. return Later(right)
  178. }
  179. return Now(right)
  180. }
  181. return Now(left)
  182. })
  183. case node.AssignNode:
  184. out = Now(doAssign)
  185. case node.CallNode:
  186. out = Now(doCall)
  187. case node.ReturnNode:
  188. out = Now(doReturn)
  189. case node.ConditionalNode:
  190. out = Now(doCondition)
  191. case node.WhileNode:
  192. out = Now(doWhile)
  193. case node.RepeatNode:
  194. out = Now(doRepeat)
  195. case node.LoopNode:
  196. out = Now(doLoop)
  197. case node.ExitNode:
  198. out = Now(doExit)
  199. case node.InitNode:
  200. out = Later(Tail(STOP))
  201. case node.TrapNode:
  202. out = Now(doTrap)
  203. case node.WithNode:
  204. out = Now(doWith)
  205. case node.CaseNode:
  206. out = Now(doCase)
  207. default:
  208. halt.As(100, reflect.TypeOf(n))
  209. }
  210. return
  211. }
  212. func EndStatement(in IN) (out OUT) {
  213. switch n := in.IR.(type) {
  214. case node.EnterNode:
  215. out = Now(func(in IN) OUT {
  216. if n.Enter() == enter.PROCEDURE {
  217. rt2.CallScope(in.Frame).Target().(scope.ScopeAllocator).Dispose(n)
  218. }
  219. if in.Parent != nil {
  220. par := rt2.RegOf(in.Parent)
  221. for k, v := range rt2.RegOf(in.Frame) {
  222. par[k] = v
  223. }
  224. val := rt2.ValueOf(in.Parent)
  225. for k, v := range rt2.ValueOf(in.Frame) {
  226. val[k] = v
  227. }
  228. }
  229. return End()
  230. })
  231. case node.CallNode:
  232. out = Now(func(in IN) OUT {
  233. next := n.Link()
  234. if next != nil {
  235. nf := rt2.New(next)
  236. if nf != nil {
  237. in.Frame.Root().PushFor(nf, in.Parent)
  238. }
  239. }
  240. if _, ok := n.(node.CallNode); ok {
  241. if in.Parent != nil {
  242. par := rt2.RegOf(in.Parent)
  243. for k, v := range rt2.RegOf(in.Frame) {
  244. par[k] = v
  245. }
  246. val := rt2.ValueOf(in.Parent)
  247. for k, v := range rt2.ValueOf(in.Frame) {
  248. val[k] = v
  249. }
  250. }
  251. }
  252. return End()
  253. })
  254. case node.AssignNode, node.ConditionalNode, node.WhileNode, node.RepeatNode, node.ExitNode, node.InitNode, node.WithNode, node.CaseNode, node.CompNode:
  255. out = Now(func(in IN) OUT {
  256. next := n.Link()
  257. if next != nil {
  258. nf := rt2.New(next)
  259. if nf != nil {
  260. in.Frame.Root().PushFor(nf, in.Parent)
  261. }
  262. }
  263. return End()
  264. })
  265. case node.ReturnNode, node.LoopNode: //do nothing
  266. default:
  267. halt.As(100, "statement with no end", reflect.TypeOf(n))
  268. }
  269. if out.Next != WRONG {
  270. return out.Do(in)
  271. }
  272. return End()
  273. }