-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a new function 'toBool' to convert values into boolean type. The changes include adding a new class for this function, adding it to the application scope, and explaining its functionality in the Wiki page. Also added the test cases for the new function. The function is designed to interpret numbers, strings, lists, and other data types in a way that makes sense in a boolean context. This provides a more intuitive way for users to perform boolean operations with different data types.
- Loading branch information
1 parent
19ff972
commit 28d57d3
Showing
5 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,14 @@ null | |
170 | ||
10 | ||
255 | ||
False | ||
False | ||
True | ||
False | ||
True | ||
True | ||
False | ||
False | ||
True | ||
True | ||
False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Description | ||
|
||
The `toBool` function converts a value to a boolean value. | ||
|
||
# Syntax | ||
|
||
``` | ||
toBool(any value) | ||
``` | ||
|
||
- `value` is the value to convert to a boolean value. | ||
|
||
# Remarks | ||
|
||
- The function returns `true` if the value is truthy (true-like), `false` otherwise. | ||
Whether or not a value is truthy can be determined using the following table: | ||
|
||
| Value Type | Truthy | Falsy | | ||
|------------|--------------------------------------|-----------| | ||
| `bool` | `true` | `false` | | ||
| `number` | `> 0` | `<= 0` | | ||
| `string` | `"true"` (case insensitive) or `"1"` | otherwise | | ||
| `list` | if not empty | otherwise | | ||
| `map` | if not empty | otherwise | | ||
| `null` | never | always | | ||
| `void` | never | always | | ||
| `function` | always | never | | ||
|
||
# Examples | ||
|
||
```step | ||
println(toBool("")) // False | ||
println(toBool("this is falsy")) // False | ||
println(toBool("true")) // True | ||
println(toBool("false")) // False | ||
println(toBool("True")) // True | ||
println(toBool("1")) // True | ||
println(toBool("0")) // False | ||
println(toBool(0)) // False | ||
println(toBool(1)) // True | ||
println(toBool(2)) // True | ||
println(toBool(-1)) // False | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using StepLang.Interpreting; | ||
using StepLang.Parsing.Expressions; | ||
|
||
namespace StepLang.Framework.Conversion; | ||
|
||
public class ToBoolFunction : NativeFunction | ||
{ | ||
public const string Identifier = "toBool"; | ||
|
||
public override async Task<ExpressionResult> EvaluateAsync(Interpreter interpreter, IReadOnlyList<Expression> arguments, CancellationToken cancellationToken = default) | ||
{ | ||
CheckArgumentCount(arguments, 1); | ||
|
||
var value = await arguments.Single().EvaluateAsync(interpreter, cancellationToken); | ||
|
||
var result = value switch | ||
{ | ||
StringResult stringResult when string.IsNullOrWhiteSpace(stringResult.Value) => false, | ||
StringResult stringResult => stringResult.Value.ToUpperInvariant() switch | ||
{ | ||
"TRUE" => true, | ||
"1" => true, | ||
_ => false, | ||
}, | ||
NumberResult numberResult => numberResult.Value > 0, | ||
BoolResult boolResult => boolResult.Value, | ||
ListResult listResult => listResult.Value.Count > 0, | ||
MapResult mapResult => mapResult.Value.Count > 0, | ||
FunctionResult => true, | ||
NullResult => false, | ||
VoidResult => false, | ||
_ => throw new NotImplementedException(), | ||
}; | ||
|
||
return new BoolResult(result); | ||
} | ||
|
||
protected override string DebugParamsString => "any value"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters