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})
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func runVmTests(t *testing.T, tests []vmTestCase) {
|
||||
t.Fatalf("vm error: %s", err)
|
||||
}
|
||||
|
||||
stackElem := vm.StackTop()
|
||||
stackElem := vm.LastPoppedStackElem()
|
||||
|
||||
testExpectedObject(t, tt.expected, stackElem)
|
||||
}
|
||||
@@ -75,6 +75,14 @@ func TestIntegerArithmetic(t *testing.T) {
|
||||
{"1", 1},
|
||||
{"2", 2},
|
||||
{"1 + 2", 3},
|
||||
{"1 * 2", 2},
|
||||
{"4 / 2", 2},
|
||||
{"50 / 2 * 2 + 10 - 5", 55},
|
||||
{"5 + 5 + 5 + 5 - 10", 10},
|
||||
{"2 * 2 * 2 * 2 * 2", 32},
|
||||
{"5 * 2 + 10", 20},
|
||||
{"5 + 2 * 10", 25},
|
||||
{"5 * (2 + 10)", 60},
|
||||
}
|
||||
|
||||
runVmTests(t, tests)
|
||||
|
||||
Reference in New Issue
Block a user