feat: treat null as falsey in conditional and logical operator branches

Update the VM interpreter to treat null as falsey alongside false in if, while, for, and logical AND/OR operations. Remove the TODO comment and adjust short-circuit logic in OP_JUMP_IF and OP_JUMP_IF_NOT branches. Refactor existing test files into subdirectories (if/, logical_operator/, while/) and add dedicated truthiness tests for each construct. Remove old test/if.wren and update and/or tests to exclude null/0/"" truthiness checks now that null is falsey.
This commit is contained in:
Bob Nystrom
2014-01-21 02:12:55 +00:00
parent a696c060fc
commit a5c4918a75
13 changed files with 118 additions and 58 deletions
+3
View File
@@ -0,0 +1,3 @@
// A dangling else binds to the right-most if.
if (true) if (false) IO.print("bad") else IO.print("good") // expect: good
if (false) if (true) IO.print("bad") else IO.print("bad")
+10
View File
@@ -0,0 +1,10 @@
// Evaluate the 'else' expression if the condition is false.
if (true) IO.print("good") else IO.print("bad") // expect: good
if (false) IO.print("bad") else IO.print("good") // expect: good
// Allow block body.
if (false) null else { IO.print("block") } // expect: block
// Newline after "else".
if (false) IO.print("bad") else
IO.print("good") // expect: good
+14
View File
@@ -0,0 +1,14 @@
// Evaluate the 'then' expression if the condition is true.
if (true) IO.print("good") // expect: good
if (false) IO.print("bad")
// Allow block body.
if (true) { IO.print("block") } // expect: block
// Assignment in if condition.
var a = false
if (a = true) IO.print(a) // expect: true
// Newline after "if".
if
(true) IO.print("good") // expect: good
+32
View File
@@ -0,0 +1,32 @@
class Iter {
new(value) { _value = value }
iterate(iterator) { return _value }
iteratorValue(iterator) { return "value" }
}
// False and null are false.
for (n in new Iter(false)) {
IO.print("bad")
break
}
for (n in new Iter(null)) {
IO.print("bad")
break
}
// Everything else is true.
for (n in new Iter(true)) {
IO.print("true") // expect: true
break
}
for (n in new Iter(0)) {
IO.print(0) // expect: 0
break
}
for (n in new Iter("")) {
IO.print("string") // expect: string
break
}