add string comparison
Some checks failed
Build / build (push) Failing after 1m41s
Test / build (push) Failing after 11m44s

This commit is contained in:
Chuck Smith
2024-03-15 14:20:04 -04:00
parent d7938e59e4
commit c818591f79
4 changed files with 40 additions and 5 deletions

View File

@@ -455,6 +455,10 @@ func (vm *VM) executeComparison(op code.Opcode) error {
return vm.executeIntegerComparison(op, left, right)
}
if left.Type() == object.STRING_OBJ && right.Type() == object.STRING_OBJ {
return vm.executeStringComparison(op, left, right)
}
switch op {
case code.OpEqual:
return vm.push(nativeBoolToBooleanObject(right == left))
@@ -581,6 +585,22 @@ func (vm *VM) executeStringIndex(str, index object.Object) error {
return vm.push(&object.String{Value: string(stringObject.Value[i])})
}
func (vm *VM) executeStringComparison(op code.Opcode, left, right object.Object) error {
leftValue := left.(*object.String).Value
rightValue := right.(*object.String).Value
switch op {
case code.OpEqual:
return vm.push(nativeBoolToBooleanObject(rightValue == leftValue))
case code.OpNotEqual:
return vm.push(nativeBoolToBooleanObject(rightValue != leftValue))
case code.OpGreaterThan:
return vm.push(nativeBoolToBooleanObject(leftValue > rightValue))
default:
return fmt.Errorf("unknown operator: %d", op)
}
}
func nativeBoolToBooleanObject(input bool) *object.Boolean {
if input {
return True