seq.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package rt
  2. import (
  3. "cp/constant/operation"
  4. "cp/node"
  5. "cp/object"
  6. "cp/statement"
  7. )
  8. type assignSeq struct {
  9. step func() Wait
  10. }
  11. func (s *assignSeq) Do(f *frame) (ret Wait) {
  12. a := f.ir.(node.AssignNode)
  13. assign := func() Wait {
  14. switch f.ir.Left().(type) {
  15. case node.VariableNode:
  16. f.ret[f.ir.Left()] = f.ir.Left().Object()
  17. s.step = func() Wait {
  18. switch f.ir.Right().(type) {
  19. case node.ConstantNode:
  20. f.ret[f.ir.Right()] = f.ir.Right().(node.ConstantNode).Data()
  21. s.step = func() Wait {
  22. a := f.p.heap.This(f.ret[f.ir.Left()].(object.Object))
  23. a.Set(f.ret[f.ir.Right()])
  24. return STOP
  25. }
  26. return DO
  27. case node.OperationNode:
  28. nf := NewFrame(f.p, f.ir.Right()).(*frame)
  29. f.push(nf)
  30. s.step = func() Wait {
  31. a := f.p.heap.This(f.ret[f.ir.Left()].(object.Object))
  32. a.Set(f.ret[f.ir.Right()])
  33. return STOP
  34. }
  35. return SKIP
  36. case node.VariableNode:
  37. a := f.p.heap.This(f.ir.Right().Object())
  38. b := f.p.heap.This(f.ret[f.ir.Left()].(object.Object))
  39. b.Set(a)
  40. return STOP
  41. default:
  42. panic("wrong right")
  43. }
  44. }
  45. return DO
  46. default:
  47. panic("wrong left")
  48. }
  49. }
  50. if s.step == nil {
  51. ret = DO
  52. switch a.Statement() {
  53. case statement.ASSIGN:
  54. s.step = assign
  55. default:
  56. panic("unknown assign subclass")
  57. }
  58. } else {
  59. ret = s.step()
  60. }
  61. return ret
  62. }
  63. type opSeq struct {
  64. step func() Wait
  65. }
  66. func (s *opSeq) Do(f *frame) (ret Wait) {
  67. _ = f.ir.(node.OperationNode)
  68. op := func() Wait {
  69. switch f.ir.(node.OperationNode).Operation() {
  70. case operation.PLUS:
  71. a := f.ret[f.ir.Left()]
  72. b := f.ret[f.ir.Right()]
  73. f.ret[f.ir] = Sum(a, b)
  74. return STOP
  75. default:
  76. panic("unknown operation")
  77. }
  78. }
  79. right := func() Wait {
  80. switch f.ir.Right().(type) {
  81. case node.ConstantNode:
  82. f.ret[f.ir.Right()] = f.ir.Right().(node.ConstantNode).Data()
  83. s.step = op
  84. return DO
  85. case node.VariableNode:
  86. s.step = func() Wait {
  87. f.ret[f.ir.Right()] = f.p.heap.This(f.ir.Right().Object())
  88. s.step = op
  89. return DO
  90. }
  91. return SKIP
  92. default:
  93. panic("wrong right")
  94. }
  95. }
  96. left := func() Wait {
  97. switch f.ir.Left().(type) {
  98. case node.ConstantNode:
  99. f.ret[f.ir.Left()] = f.ir.Left().(node.ConstantNode).Data()
  100. s.step = right
  101. return DO
  102. case node.VariableNode:
  103. s.step = func() Wait {
  104. f.ret[f.ir.Left()] = f.p.heap.This(f.ir.Left().Object())
  105. s.step = right
  106. return DO
  107. }
  108. return SKIP
  109. default:
  110. panic("wrong left")
  111. }
  112. }
  113. if s.step == nil {
  114. s.step = left
  115. ret = DO
  116. } else {
  117. ret = s.step()
  118. }
  119. return ret
  120. }
  121. type callSeq struct {
  122. step func() Wait
  123. }
  124. func (s *callSeq) Do(f *frame) Wait {
  125. if s.step == nil {
  126. switch f.ir.Left().(type) {
  127. case node.ProcedureNode:
  128. proc := f.p.thisMod.NodeByObject(f.ir.Left().Object())
  129. f.push(NewFrame(f.p, proc).(*frame))
  130. s.step = func() Wait {
  131. return STOP
  132. }
  133. return SKIP
  134. default:
  135. panic("unknown call left")
  136. }
  137. } else {
  138. return s.step()
  139. }
  140. }
  141. type enterSeq struct {
  142. step func() Wait
  143. }
  144. func (e *enterSeq) Do(f *frame) Wait {
  145. if e.step == nil {
  146. //for f.ir == EnterNode entering to .Right()
  147. f.push(NewFrame(f.p, f.ir.Right()).(*frame))
  148. e.step = func() Wait {
  149. return STOP
  150. }
  151. return SKIP
  152. } else {
  153. return e.step()
  154. }
  155. }