Arithmetic
This commit is contained in:
57
vm/vm.go
57
vm/vm.go
@@ -27,11 +27,8 @@ func New(bytecode *compiler.Bytecode) *VM {
|
||||
}
|
||||
}
|
||||
|
||||
func (vm *VM) StackTop() object.Object {
|
||||
if vm.sp == 0 {
|
||||
return nil
|
||||
}
|
||||
return vm.stack[vm.sp-1]
|
||||
func (vm *VM) LastPoppedStackElem() object.Object {
|
||||
return vm.stack[vm.sp]
|
||||
}
|
||||
|
||||
func (vm *VM) Run() error {
|
||||
@@ -48,14 +45,14 @@ func (vm *VM) Run() error {
|
||||
return err
|
||||
}
|
||||
|
||||
case code.OpAdd:
|
||||
right := vm.pop()
|
||||
left := vm.pop()
|
||||
leftValue := left.(*object.Integer).Value
|
||||
rightValue := right.(*object.Integer).Value
|
||||
case code.OpAdd, code.OpSub, code.OpMul, code.OpDiv:
|
||||
err := vm.executeBinaryOperation(op)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result := leftValue + rightValue
|
||||
vm.push(&object.Integer{Value: result})
|
||||
case code.OpPop:
|
||||
vm.pop()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,3 +75,39 @@ func (vm *VM) pop() object.Object {
|
||||
vm.sp--
|
||||
return o
|
||||
}
|
||||
|
||||
func (vm *VM) executeBinaryOperation(op code.Opcode) error {
|
||||
right := vm.pop()
|
||||
left := vm.pop()
|
||||
|
||||
leftType := left.Type()
|
||||
rightRight := right.Type()
|
||||
|
||||
if leftType == object.INTEGER_OBJ && rightRight == object.INTEGER_OBJ {
|
||||
return vm.executeBinaryIntegerOperation(op, left, right)
|
||||
}
|
||||
|
||||
return fmt.Errorf("unsupported types for binary operation: %s %s", leftType, rightRight)
|
||||
}
|
||||
|
||||
func (vm *VM) executeBinaryIntegerOperation(op code.Opcode, left, right object.Object) error {
|
||||
leftValue := left.(*object.Integer).Value
|
||||
rightValue := right.(*object.Integer).Value
|
||||
|
||||
var result int64
|
||||
|
||||
switch op {
|
||||
case code.OpAdd:
|
||||
result = leftValue + rightValue
|
||||
case code.OpSub:
|
||||
result = leftValue - rightValue
|
||||
case code.OpMul:
|
||||
result = leftValue * rightValue
|
||||
case code.OpDiv:
|
||||
result = leftValue / rightValue
|
||||
default:
|
||||
return fmt.Errorf("unknown integer operator: %d", op)
|
||||
}
|
||||
|
||||
return vm.push(&object.Integer{Value: result})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user