mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 22:28:45 +01:00
Make null falsey.
This commit is contained in:
@ -1,8 +1,5 @@
|
||||
var fib = fn(n) {
|
||||
if (n < 2) {
|
||||
return n
|
||||
}
|
||||
|
||||
if (n < 2) return n
|
||||
return fib.call(n - 1) + fib.call(n - 2)
|
||||
}
|
||||
|
||||
|
||||
@ -997,9 +997,7 @@ static bool runInterpreter(WrenVM* vm)
|
||||
int offset = READ_SHORT();
|
||||
Value condition = POP();
|
||||
|
||||
// False is the only falsey value.
|
||||
// TODO: Null should be falsey too.
|
||||
if (IS_FALSE(condition)) ip += offset;
|
||||
if (IS_FALSE(condition) || IS_NULL(condition)) ip += offset;
|
||||
DISPATCH();
|
||||
}
|
||||
|
||||
@ -1008,18 +1006,16 @@ static bool runInterpreter(WrenVM* vm)
|
||||
int offset = READ_SHORT();
|
||||
Value condition = PEEK();
|
||||
|
||||
// False is the only falsey value.
|
||||
// TODO: Null should be falsey too.
|
||||
if (!IS_FALSE(condition))
|
||||
{
|
||||
// Discard the condition and evaluate the right hand side.
|
||||
POP();
|
||||
}
|
||||
else
|
||||
if (IS_FALSE(condition) || IS_NULL(condition))
|
||||
{
|
||||
// Short-circuit the right hand side.
|
||||
ip += offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Discard the condition and evaluate the right hand side.
|
||||
POP();
|
||||
}
|
||||
DISPATCH();
|
||||
}
|
||||
|
||||
@ -1028,9 +1024,7 @@ static bool runInterpreter(WrenVM* vm)
|
||||
int offset = READ_SHORT();
|
||||
Value condition = PEEK();
|
||||
|
||||
// False is the only falsey value.
|
||||
// TODO: Null should be falsey too.
|
||||
if (IS_FALSE(condition))
|
||||
if (IS_FALSE(condition) || IS_NULL(condition))
|
||||
{
|
||||
// Discard the condition and evaluate the right hand side.
|
||||
POP();
|
||||
|
||||
8
test/for/truth.wren
Normal file
8
test/for/truth.wren
Normal file
@ -0,0 +1,8 @@
|
||||
// False and null are false.
|
||||
if (false) IO.print("bad") else IO.print("false") // expect: false
|
||||
if (null) IO.print("bad") else IO.print("null") // expect: null
|
||||
|
||||
// Everything else is true.
|
||||
if (true) IO.print(true) // expect: true
|
||||
if (0) IO.print(0) // expect: 0
|
||||
if ("") IO.print("empty") // expect: empty
|
||||
28
test/if.wren
28
test/if.wren
@ -1,28 +0,0 @@
|
||||
// Evaluate the 'then' expression if the condition is true.
|
||||
if (true) IO.print("good") // expect: good
|
||||
if (false) IO.print("bad")
|
||||
|
||||
// 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 blocks for branches.
|
||||
if (true) { IO.print("block") } // expect: block
|
||||
if (false) null else { 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
|
||||
|
||||
// Newline after "else".
|
||||
if (false) IO.print("bad") else
|
||||
IO.print("good") // expect: good
|
||||
|
||||
// Only false is falsy.
|
||||
if (0) IO.print(0) // expect: 0
|
||||
if (null) IO.print(null) // expect: null
|
||||
if ("") IO.print("empty") // expect: empty
|
||||
3
test/if/dangling_else.wren
Normal file
3
test/if/dangling_else.wren
Normal 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
test/if/else.wren
Normal file
10
test/if/else.wren
Normal 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
test/if/if.wren
Normal file
14
test/if/if.wren
Normal 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
test/if/truth.wren
Normal file
32
test/if/truth.wren
Normal 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
|
||||
}
|
||||
@ -18,9 +18,3 @@ IO.print(true) && // expect: true
|
||||
// Swallow a trailing newline.
|
||||
IO.print(true &&
|
||||
true) // expect: true
|
||||
|
||||
// Only false is falsy.
|
||||
IO.print(0 && true) // expect: true
|
||||
IO.print(null && true) // expect: true
|
||||
IO.print("" && true) // expect: true
|
||||
IO.print(false && true) // expect: false
|
||||
8
test/logical_operator/and_truth.wren
Normal file
8
test/logical_operator/and_truth.wren
Normal file
@ -0,0 +1,8 @@
|
||||
// False and null are false.
|
||||
IO.print(false && "bad") // expect: false
|
||||
IO.print(null && "bad") // expect: null
|
||||
|
||||
// Everything else is true.
|
||||
IO.print(true && "ok") // expect: ok
|
||||
IO.print(0 && "ok") // expect: ok
|
||||
IO.print("" && "ok") // expect: ok
|
||||
@ -18,9 +18,3 @@ IO.print(false) || // expect: false
|
||||
// Swallow a trailing newline.
|
||||
IO.print(true ||
|
||||
true) // expect: true
|
||||
|
||||
// Only false is falsy.
|
||||
IO.print(0 || true) // expect: 0
|
||||
IO.print(null || true) // expect: null
|
||||
IO.print(("" || true) == "") // expect: true
|
||||
IO.print(false || true) // expect: true
|
||||
8
test/logical_operator/or_truth.wren
Normal file
8
test/logical_operator/or_truth.wren
Normal file
@ -0,0 +1,8 @@
|
||||
// False and null are false.
|
||||
IO.print(false || "ok") // expect: ok
|
||||
IO.print(null || "ok") // expect: ok
|
||||
|
||||
// Everything else is true.
|
||||
IO.print(true || "ok") // expect: true
|
||||
IO.print(0 || "ok") // expect: 0
|
||||
IO.print("s" || "ok") // expect: s
|
||||
26
test/while/truth.wren
Normal file
26
test/while/truth.wren
Normal file
@ -0,0 +1,26 @@
|
||||
// False and null are false.
|
||||
while (false) {
|
||||
IO.print("bad")
|
||||
break
|
||||
}
|
||||
|
||||
while (null) {
|
||||
IO.print("bad")
|
||||
break
|
||||
}
|
||||
|
||||
// Everything else is true.
|
||||
while (true) {
|
||||
IO.print("true") // expect: true
|
||||
break
|
||||
}
|
||||
|
||||
while (0) {
|
||||
IO.print(0) // expect: 0
|
||||
break
|
||||
}
|
||||
|
||||
while ("") {
|
||||
IO.print("string") // expect: string
|
||||
break
|
||||
}
|
||||
Reference in New Issue
Block a user