Skip to content

Commit

Permalink
Add 'toBool' conversion function
Browse files Browse the repository at this point in the history
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
ricardoboss committed Sep 8, 2023
1 parent 19ff972 commit 28d57d3
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
11 changes: 11 additions & 0 deletions StepLang.Tests/Examples/conversions.step.out
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@ null
170
10
255
False
False
True
False
True
True
False
False
True
True
False
43 changes: 43 additions & 0 deletions StepLang.Wiki/Functions/ToBool.md
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
```
12 changes: 12 additions & 0 deletions StepLang/Examples/conversions.step
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ println(toNumber("FF", 16))
println(toNumber("aa", 16))
println(toNumber("1010", 2))
println(toNumber("377", 8))

println(toBool(""))
println(toBool("this is falsy"))
println(toBool("true"))
println(toBool("false"))
println(toBool("True"))
println(toBool("1"))
println(toBool("0"))
println(toBool(0))
println(toBool(1))
println(toBool(2))
println(toBool(-1))
39 changes: 39 additions & 0 deletions StepLang/Framework/Conversion/ToBoolFunction.cs
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";
}
1 change: 1 addition & 0 deletions StepLang/Interpreting/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private Scope()
SetVariable(ToStringFunction.Identifier, new FunctionResult(new ToStringFunction()));
SetVariable(ToRadixFunction.Identifier, new FunctionResult(new ToRadixFunction()));
SetVariable(ToNumberFunction.Identifier, new FunctionResult(new ToNumberFunction()));
SetVariable(ToBoolFunction.Identifier, new FunctionResult(new ToBoolFunction()));

// other functions
SetVariable(FileExistsFunction.Identifier, new FunctionResult(new FileExistsFunction()));
Expand Down

0 comments on commit 28d57d3

Please sign in to comment.