do.go 6.3 KB

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