Strings
This commit is contained in:
20
vm/vm.go
20
vm/vm.go
@@ -177,11 +177,14 @@ func (vm *VM) executeBinaryOperation(op code.Opcode) error {
|
||||
leftType := left.Type()
|
||||
rightRight := right.Type()
|
||||
|
||||
if leftType == object.INTEGER_OBJ && rightRight == object.INTEGER_OBJ {
|
||||
switch {
|
||||
case leftType == object.INTEGER_OBJ && rightRight == object.INTEGER_OBJ:
|
||||
return vm.executeBinaryIntegerOperation(op, left, right)
|
||||
case leftType == object.STRING_OBJ && rightRight == object.STRING_OBJ:
|
||||
return vm.executeBinaryStringOperation(op, left, right)
|
||||
default:
|
||||
return fmt.Errorf("unsupported types for binary operation: %s %s", leftType, rightRight)
|
||||
}
|
||||
|
||||
return fmt.Errorf("unsupported types for binary operation: %s %s", leftType, rightRight)
|
||||
}
|
||||
|
||||
func (vm *VM) executeBinaryIntegerOperation(op code.Opcode, left, right object.Object) error {
|
||||
@@ -206,6 +209,17 @@ func (vm *VM) executeBinaryIntegerOperation(op code.Opcode, left, right object.O
|
||||
return vm.push(&object.Integer{Value: result})
|
||||
}
|
||||
|
||||
func (vm *VM) executeBinaryStringOperation(op code.Opcode, left, right object.Object) error {
|
||||
if op != code.OpAdd {
|
||||
return fmt.Errorf("unknown string operator: %d", op)
|
||||
}
|
||||
|
||||
leftValue := left.(*object.String).Value
|
||||
rightValue := right.(*object.String).Value
|
||||
|
||||
return vm.push(&object.String{Value: leftValue + rightValue})
|
||||
}
|
||||
|
||||
func (vm *VM) executeComparison(op code.Opcode) error {
|
||||
right := vm.pop()
|
||||
left := vm.pop()
|
||||
|
||||
Reference in New Issue
Block a user