do.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. vr := tmp.(scope.Pointer).Copy()
  121. assert.For(vr != nil, 50)
  122. rt2.ValueOf(in.Frame)[tmp_id] = vr
  123. //meta.Scope = rt2.ScopeFor(in.Frame, vr.Id())
  124. //meta.Id = vr.Id()
  125. }
  126. do(meta.Scope != nil || meta.Rec != nil || meta.Arr != nil, 1380, " wrong meta")
  127. }
  128. v := rt2.RegOf(f)[key]
  129. do(v != nil, 1957, " no data for ", key)
  130. })
  131. return Later(func(IN) OUT {
  132. return Now(next)
  133. })
  134. }
  135. func BeginStrange(in IN) OUT {
  136. switch s := in.IR.(type) {
  137. case node.IfNode:
  138. return Now(doIf)
  139. default:
  140. halt.As(100, reflect.TypeOf(s))
  141. }
  142. panic(0)
  143. }
  144. func BeginStatement(in IN) (out OUT) {
  145. switch n := in.IR.(type) {
  146. case node.EnterNode:
  147. var tail Do
  148. tail = func(in IN) OUT {
  149. q := in.Frame.Root().Queue()
  150. if q != nil {
  151. in.Frame.Root().PushFor(q, nil)
  152. return Later(tail)
  153. } else {
  154. return Now(doEnter)
  155. }
  156. }
  157. out = Now(tail)
  158. case node.CompNode:
  159. next := n
  160. return Now(func(IN) OUT {
  161. right := func(in IN) OUT {
  162. if next.Right() != nil {
  163. rt2.Push(rt2.New(next.Right()), in.Frame)
  164. return Later(Tail(STOP))
  165. }
  166. return End()
  167. }
  168. left := func(in IN) OUT {
  169. if next.Left() != nil {
  170. rt2.Push(rt2.New(next.Left()), in.Frame)
  171. return Later(right)
  172. }
  173. return Now(right)
  174. }
  175. return Now(left)
  176. })
  177. case node.AssignNode:
  178. out = Now(doAssign)
  179. case node.CallNode:
  180. out = Now(doCall)
  181. case node.ReturnNode:
  182. out = Now(doReturn)
  183. case node.ConditionalNode:
  184. out = Now(doCondition)
  185. case node.WhileNode:
  186. out = Now(doWhile)
  187. case node.RepeatNode:
  188. out = Now(doRepeat)
  189. case node.LoopNode:
  190. out = Now(doLoop)
  191. case node.ExitNode:
  192. out = Now(doExit)
  193. case node.InitNode:
  194. out = Later(Tail(STOP))
  195. case node.TrapNode:
  196. out = Now(doTrap)
  197. case node.WithNode:
  198. out = Now(doWith)
  199. case node.CaseNode:
  200. out = Now(doCase)
  201. default:
  202. halt.As(100, reflect.TypeOf(n))
  203. }
  204. return
  205. }
  206. func EndStatement(in IN) (out OUT) {
  207. switch n := in.IR.(type) {
  208. case node.EnterNode:
  209. out = Now(func(in IN) OUT {
  210. if n.Enter() == enter.PROCEDURE {
  211. rt2.CallScope(in.Frame).Target().(scope.ScopeAllocator).Dispose(n)
  212. }
  213. if in.Parent != nil {
  214. par := rt2.RegOf(in.Parent)
  215. for k, v := range rt2.RegOf(in.Frame) {
  216. par[k] = v
  217. }
  218. val := rt2.ValueOf(in.Parent)
  219. for k, v := range rt2.ValueOf(in.Frame) {
  220. val[k] = v
  221. }
  222. }
  223. return End()
  224. })
  225. case node.CallNode:
  226. out = Now(func(in IN) OUT {
  227. next := n.Link()
  228. if next != nil {
  229. nf := rt2.New(next)
  230. if nf != nil {
  231. in.Frame.Root().PushFor(nf, in.Parent)
  232. }
  233. }
  234. if _, ok := n.(node.CallNode); ok {
  235. if in.Parent != nil {
  236. par := rt2.RegOf(in.Parent)
  237. for k, v := range rt2.RegOf(in.Frame) {
  238. par[k] = v
  239. }
  240. val := rt2.ValueOf(in.Parent)
  241. for k, v := range rt2.ValueOf(in.Frame) {
  242. val[k] = v
  243. }
  244. }
  245. }
  246. return End()
  247. })
  248. case node.AssignNode, node.ConditionalNode, node.WhileNode, node.RepeatNode, node.ExitNode, node.InitNode, node.WithNode, node.CaseNode, node.CompNode:
  249. out = Now(func(in IN) OUT {
  250. next := n.Link()
  251. if next != nil {
  252. nf := rt2.New(next)
  253. if nf != nil {
  254. in.Frame.Root().PushFor(nf, in.Parent)
  255. }
  256. }
  257. return End()
  258. })
  259. case node.ReturnNode, node.LoopNode: //do nothing
  260. default:
  261. halt.As(100, "statement with no end", reflect.TypeOf(n))
  262. }
  263. if out.Next != WRONG {
  264. return out.Do(in)
  265. }
  266. return End()
  267. }