Skip to content

Commit

Permalink
lang: make 0 and 0.0 falsey
Browse files Browse the repository at this point in the history
  • Loading branch information
wkhere committed Apr 30, 2024
1 parent 0353967 commit ff6fe83
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,8 @@ but not between mixed types.
There are boolean operators `and`, `or`, `not` behaving like in Python and Ruby:
they are short-cirtuit and retain the type of an operand
(`1==1 and 42` will return 42). Non-boolean types can be a boolean operand;
for this, there is a definition what is considered "falsey": `false`,
empty string, and `nil`. This may at resemble Python, but note that the zero number
is not considered falsey -- a behavior that may change in the future.
for this, there is a definition what is considered "falsey": `false`, `nil`,
empty string, and zero, like in Python.

Boolean constants are `true` and `false`.
Another constant is `nil`, whose meaning is that
Expand Down
16 changes: 10 additions & 6 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@
[13.2, 'print not 1>3.0', 'true'],
[13.3, 'print not 1.0>3.0', 'true'],
[13.4, 'print not 3<=1', 'true'],
[14, 'print 1<2 and 0 or "whatever"', '0'],
[14.1, 'print 1.0<2 and 0 or "whatever"', '0'],
[14.2, 'print 1<2.0 and 0 or "whatever"', '0'],
[14.3, 'print 1.0<2.0 and 0 or "whatever"', '0'],
[14, 'print 1<2 and 0 or "whatever"', 'whatever'],
[14.1, 'print 1.0<2 and 0 or "whatever"', 'whatever'],
[14.2, 'print 1<2.0 and 0 or "whatever"', 'whatever'],
[14.3, 'print 1.0<2.0 and 0 or "whatever"', 'whatever'],
[14.4, 'print 1<2 and 0.0 or "whatever"', 'whatever'],
[14.5, 'print 1.0<2 and 0.0 or "whatever"', 'whatever'],
[14.6, 'print 1<2.0 and 0.0 or "whatever"', 'whatever'],
[14.7, 'print 1.0<2.0 and 0.0 or "whatever"', 'whatever'],
[15, 'print 1>2 or true and 42', '42'],
[16, 'print 1<10/5 and 127*-1+154', '27'],
[17, 'print "q"*2', 'qq'],
Expand Down Expand Up @@ -320,8 +324,8 @@
[105.4, 'print nil>"ab"', '', "err: GT: invalid types: nil, string "],
[105.5, 'print nil>nil', '', "err: GT: invalid types: nil, nil"],

[106.1, 'print not 0', 'false'],
[106.2, 'print not 0.0', 'false'],
[106.1, 'print not 0', 'true'],
[106.2, 'print not 0.0', 'true'],
[106.3, 'print not false', 'true'],
[106.4, 'print not ""', 'true'],
[106.5, 'print not nil', 'true'],
Expand Down
16 changes: 10 additions & 6 deletions testapi_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func isFalsey(v value) bool {
switch x := v.(type) {
case bool:
return !x
case int:
return x == 0
case float64:
return x == 0.0
case string:
return x == ""
default:
Expand Down

0 comments on commit ff6fe83

Please sign in to comment.