Strings
Some checks failed
Build / build (push) Failing after 1m35s
Test / build (push) Failing after 1m31s

This commit is contained in:
Chuck Smith
2024-02-21 16:29:53 -05:00
parent 8caeaca559
commit e4bca02235
5 changed files with 189 additions and 5 deletions

View File

@@ -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()