vm.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Author: simon
  2. // Author: ynwdlxm@163.com
  3. // Date: 2022/10/22 13:55
  4. // Desc: Stack VM implement
  5. package vm
  6. import (
  7. "fmt"
  8. "github/runnignwater/monkey/code"
  9. "github/runnignwater/monkey/compiler"
  10. "github/runnignwater/monkey/object"
  11. )
  12. const StackSize = 2048
  13. var True = &object.Boolean{Value: true}
  14. var False = &object.Boolean{Value: false}
  15. type VM struct {
  16. constants []object.Object
  17. instructions code.Instructions
  18. stack []object.Object
  19. sp int // Always points to the next value. Top of stack is stack[sp-1]
  20. }
  21. func New(byteCode *compiler.ByteCode) *VM {
  22. return &VM{
  23. instructions: byteCode.Instructions,
  24. constants: byteCode.Constants,
  25. stack: make([]object.Object, StackSize),
  26. sp: 0,
  27. }
  28. }
  29. // func (vm *VM) StackTop() object.Object {
  30. // if vm.sp == 0 {
  31. // return nil
  32. // }
  33. // return vm.stack[vm.sp-1]
  34. // }
  35. func (vm *VM) Run() error {
  36. for ip := 0; ip < len(vm.instructions); ip++ {
  37. op := code.Opcode(vm.instructions[ip])
  38. switch op {
  39. case code.OpConstant:
  40. constIndex := code.ReadUint16(vm.instructions[ip+1:])
  41. ip += 2
  42. // 执行
  43. err := vm.push(vm.constants[constIndex])
  44. if err != nil {
  45. return err
  46. }
  47. case code.OpTrue:
  48. err := vm.push(True)
  49. if err != nil {
  50. return err
  51. }
  52. case code.OpFalse:
  53. err := vm.push(False)
  54. if err != nil {
  55. return err
  56. }
  57. case code.OpAdd, code.OpSub, code.OpMul, code.OpDiv:
  58. err := vm.executeBinaryOperation(op)
  59. if err != nil {
  60. return err
  61. }
  62. case code.OpEqual, code.OpNotEqual, code.OpGreaterThan:
  63. err := vm.executeComparison(op)
  64. if err != nil {
  65. return err
  66. }
  67. case code.OpMinus:
  68. err := vm.executeMinusOperator()
  69. if err != nil {
  70. return err
  71. }
  72. case code.OpBang:
  73. err := vm.executeBangOperator()
  74. if err != nil {
  75. return err
  76. }
  77. case code.OpPop:
  78. vm.pop()
  79. }
  80. }
  81. return nil
  82. }
  83. func (vm *VM) push(o object.Object) error {
  84. if vm.sp >= StackSize {
  85. return fmt.Errorf("stack overflow")
  86. }
  87. vm.stack[vm.sp] = o
  88. vm.sp++
  89. return nil
  90. }
  91. func (vm *VM) pop() object.Object {
  92. o := vm.stack[vm.sp-1]
  93. vm.sp--
  94. return o
  95. }
  96. func (vm *VM) executeBinaryOperation(op code.Opcode) error {
  97. right := vm.pop()
  98. left := vm.pop()
  99. leftType := left.Type()
  100. rightType := right.Type()
  101. if leftType == object.IntegerObj && rightType == object.IntegerObj {
  102. return vm.executeBinaryIntegerOperation(op, left, right)
  103. }
  104. return fmt.Errorf("unsupported types of binary operation: %s %s", leftType, rightType)
  105. }
  106. func (vm *VM) executeComparison(op code.Opcode) error {
  107. right := vm.pop()
  108. left := vm.pop()
  109. if left.Type() == object.IntegerObj || right.Type() == object.IntegerObj {
  110. return vm.executeIntegerComparison(op, left, right)
  111. }
  112. switch op {
  113. case code.OpEqual:
  114. return vm.push(nativeBoolToBooleanObject(right == left))
  115. case code.OpNotEqual:
  116. return vm.push(nativeBoolToBooleanObject(right != left))
  117. default:
  118. return fmt.Errorf("unknown operator: %d (%s %s)", op, left.Type(), right.Type())
  119. }
  120. }
  121. func (vm *VM) LastPopStackElem() object.Object {
  122. return vm.stack[vm.sp]
  123. }
  124. func (vm *VM) executeBinaryIntegerOperation(
  125. op code.Opcode,
  126. left, right object.Object,
  127. ) error {
  128. leftValue := left.(*object.Integer).Value
  129. rightValue := right.(*object.Integer).Value
  130. var result int64
  131. switch op {
  132. case code.OpAdd:
  133. result = leftValue + rightValue
  134. case code.OpSub:
  135. result = leftValue - rightValue
  136. case code.OpMul:
  137. result = leftValue * rightValue
  138. case code.OpDiv:
  139. result = leftValue / rightValue
  140. default:
  141. return fmt.Errorf("unknown integer operator: %d", op)
  142. }
  143. return vm.push(&object.Integer{Value: result})
  144. }
  145. func (vm *VM) executeIntegerComparison(
  146. op code.Opcode,
  147. left, right object.Object,
  148. ) error {
  149. leftValue := left.(*object.Integer).Value
  150. rightValue := right.(*object.Integer).Value
  151. switch op {
  152. case code.OpEqual:
  153. return vm.push(nativeBoolToBooleanObject(rightValue == leftValue))
  154. case code.OpNotEqual:
  155. return vm.push(nativeBoolToBooleanObject(rightValue != leftValue))
  156. case code.OpGreaterThan:
  157. return vm.push(nativeBoolToBooleanObject(leftValue > rightValue))
  158. default:
  159. return fmt.Errorf("unknown operator: %d", op)
  160. }
  161. }
  162. func (vm *VM) executeMinusOperator() error {
  163. operand := vm.pop()
  164. if operand.Type() != object.IntegerObj {
  165. return fmt.Errorf("unsupported type for nagation: %s", operand.Type())
  166. }
  167. value := operand.(*object.Integer).Value
  168. return vm.push(&object.Integer{Value: -value})
  169. }
  170. func (vm *VM) executeBangOperator() error {
  171. operand := vm.pop()
  172. switch operand {
  173. case True:
  174. return vm.push(False)
  175. case False:
  176. return vm.push(True)
  177. default:
  178. return vm.push(False)
  179. }
  180. }
  181. func nativeBoolToBooleanObject(input bool) *object.Boolean {
  182. if input {
  183. return True
  184. }
  185. return False
  186. }