Skip to content

Commit

Permalink
Merge pull request #270 from koto-lang/null-comparisons
Browse files Browse the repository at this point in the history
Check for null on the LHS of equality comparisons
  • Loading branch information
irh authored Dec 28, 2023
2 parents 1b6b925 + bce5336 commit f25e624
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ The Koto project adheres to
- `0x`, `0o`, and `0b` prefixes are understood for parsing hex, octal, or
binary numbers respectively.
- An overload has been added that accepts a number base between 2 and 36.
- If the string doesn't contain a number null is now returned instead of an
- If the string doesn't contain a number, `null` is now returned instead of an
exception being thrown.
- Objects can be compared with `null` on the LHS without having to implement
`KotoObject::equal` and/or `not_equal`.

#### REPL

Expand Down
6 changes: 4 additions & 2 deletions crates/runtime/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1732,11 +1732,12 @@ impl Vm {
let lhs_value = self.get_register(lhs);
let rhs_value = self.get_register(rhs);
let result_value = match (lhs_value, rhs_value) {
(Null, Null) => true,
(Null, _) | (_, Null) => false,
(Number(a), Number(b)) => a == b,
(Bool(a), Bool(b)) => a == b,
(Str(a), Str(b)) => a == b,
(Range(a), Range(b)) => a == b,
(Null, Null) => true,
(List(a), List(b)) => {
let a = a.clone();
let b = b.clone();
Expand Down Expand Up @@ -1791,11 +1792,12 @@ impl Vm {
let lhs_value = self.get_register(lhs);
let rhs_value = self.get_register(rhs);
let result_value = match (lhs_value, rhs_value) {
(Null, Null) => false,
(Null, _) | (_, Null) => true,
(Number(a), Number(b)) => a != b,
(Bool(a), Bool(b)) => a != b,
(Str(a), Str(b)) => a != b,
(Range(a), Range(b)) => a != b,
(Null, Null) => false,
(List(a), List(b)) => {
let a = a.clone();
let b = b.clone();
Expand Down
33 changes: 28 additions & 5 deletions crates/runtime/tests/object_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ make_object(10)

mod binary_op {
use super::*;
use Value::Bool;

#[test]
fn add() {
Expand Down Expand Up @@ -523,25 +522,49 @@ x.to_number()
#[test]
fn less() {
let script = "(make_object 1) < (make_object 2)";
test_object_script(script, Bool(true));
test_object_script(script, true);
}

#[test]
fn less_or_equal() {
let script = "(make_object 2) <= (make_object 2)";
test_object_script(script, Bool(true));
test_object_script(script, true);
}

#[test]
fn equal() {
let script = "(make_object 2) == (make_object 3)";
test_object_script(script, Bool(false));
test_object_script(script, false);
}

#[test]
fn not_equal() {
let script = "(make_object 2) != (make_object 3)";
test_object_script(script, Bool(true));
test_object_script(script, true);
}

#[test]
fn equal_null_lhs() {
let script = "(make_object 2) == null";
test_object_script(script, false);
}

#[test]
fn equal_null_rhs() {
let script = "null == (make_object 2)";
test_object_script(script, false);
}

#[test]
fn not_equal_null_lhs() {
let script = "(make_object 2) != null";
test_object_script(script, true);
}

#[test]
fn not_equal_null_rhs() {
let script = "null != (make_object 2)";
test_object_script(script, true);
}

#[test]
Expand Down

0 comments on commit f25e624

Please sign in to comment.