Skip to content

Commit 9f3403e

Browse files
stephenquanpictosTheCodeTraveler
authored
Migrate to PEG parser. Introduce boolean operators and constants. (#2182)
* Migrate to PEG parser. Introduce boolean operators and constants. * Update UnitTest. Added conditional sample. * Improve null handling and added extra boolean/null unit tests * Additional boolean tests * Allow nulls if operator is ? : && || == != * __bool and MathToken record refactor * First attempt to block null reference return * Second attempt to block null reference return * Addessed CS8603 possible null reference return issues * Use correct C# property name pattern. Log invalid math expressions instead of raising exceptions. * Null Forgiving Operator removed. Adjust null checks in MultiMathExpressionConverter and corresponding unit tests. * Add more logical and/or unit tests. Add XAML friendly comparator operators. * Improve unit test coverage on alternate operators. * Improve null handling for logical operators && || and cover these cases in unit tests. * Miscellaneous fixes for Copilot review * Update to .NET 9 * Use Primary Constructor * Remove redundant CodeAnalysis * Update Regex naming * Use Index Operator * Update namings * Refactor * Revert naming, Use `in` + `IReadOnlyList<T>` to protect `MathExpression` inputs --------- Co-authored-by: Pedro Jesus <pedrojesus.cefet@gmail.com> Co-authored-by: Brandon Minnick <13558917+brminnick@users.noreply.github.com>
1 parent 26a7f45 commit 9f3403e

File tree

6 files changed

+649
-270
lines changed

6 files changed

+649
-270
lines changed

samples/CommunityToolkit.Maui.Sample/Pages/Converters/MultiMathExpressionConverterPage.xaml

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,63 +15,86 @@
1515
</ResourceDictionary>
1616
</pages:BasePage.Resources>
1717

18-
<Grid Padding="20"
19-
RowSpacing="20"
20-
ColumnSpacing="12"
21-
ColumnDefinitions="*,*,*,*,*,*,*"
22-
RowDefinitions="100, *">
23-
<Label Grid.ColumnSpan="7"
24-
Grid.Row="0"
25-
Text="This sample demonstrates the use of the MultiMathExpressionConverter. It utilizes the converter to perform various math operations of multiple variables."/>
18+
<VerticalStackLayout Padding="20" Spacing="40">
2619

27-
<Entry Grid.Column="0"
28-
Grid.Row="1"
29-
VerticalTextAlignment="Center"
30-
VerticalOptions="Center"
31-
Text="{Binding X0}"/>
20+
<Grid RowSpacing="20"
21+
ColumnSpacing="12"
22+
ColumnDefinitions="*,*,*,*,*,*,*"
23+
RowDefinitions="100, *">
24+
<Label Grid.ColumnSpan="7"
25+
Grid.Row="0"
26+
Text="This sample demonstrates the use of the MultiMathExpressionConverter. It utilizes the converter to perform various math operations of multiple variables."/>
3227

33-
<Label Grid.Column="1"
34-
Grid.Row="1"
35-
VerticalTextAlignment="Center"
36-
VerticalOptions="Center"
37-
Text="+" />
28+
<Entry Grid.Column="0"
29+
Grid.Row="1"
30+
VerticalTextAlignment="Center"
31+
VerticalOptions="Center"
32+
Text="{Binding X0}"/>
3833

39-
<Entry Grid.Column="2"
40-
Grid.Row="1"
41-
VerticalTextAlignment="Center"
42-
VerticalOptions="Center"
43-
Text="{Binding X1}"/>
34+
<Label Grid.Column="1"
35+
Grid.Row="1"
36+
VerticalTextAlignment="Center"
37+
VerticalOptions="Center"
38+
Text="+" />
4439

45-
<Label Grid.Column="3"
46-
Grid.Row="1"
47-
VerticalTextAlignment="Center"
48-
VerticalOptions="Center"
49-
Text="+" />
40+
<Entry Grid.Column="2"
41+
Grid.Row="1"
42+
VerticalTextAlignment="Center"
43+
VerticalOptions="Center"
44+
Text="{Binding X1}"/>
5045

51-
<Entry Grid.Column="4"
52-
Grid.Row="1"
53-
VerticalTextAlignment="Center"
54-
VerticalOptions="Center"
55-
Text="{Binding X2}"/>
46+
<Label Grid.Column="3"
47+
Grid.Row="1"
48+
VerticalTextAlignment="Center"
49+
VerticalOptions="Center"
50+
Text="+" />
5651

57-
<Label Grid.Column="5"
58-
Grid.Row="1"
59-
VerticalTextAlignment="Center"
60-
VerticalOptions="Center"
61-
Text="=" />
52+
<Entry Grid.Column="4"
53+
Grid.Row="1"
54+
VerticalTextAlignment="Center"
55+
VerticalOptions="Center"
56+
Text="{Binding X2}"/>
6257

63-
<Label Grid.Column="6"
64-
Grid.Row="1"
65-
VerticalTextAlignment="Center"
66-
VerticalOptions="Center">
67-
<Label.Text>
68-
<MultiBinding Converter="{StaticResource MultiMathExpressionConverter}"
69-
ConverterParameter="x0 + x1 + x2">
70-
<Binding Path="X0" Mode="OneWay"/>
71-
<Binding Path="X1" Mode="OneWay"/>
72-
<Binding Path="X2" Mode="OneWay"/>
73-
</MultiBinding>
74-
</Label.Text>
58+
<Label Grid.Column="5"
59+
Grid.Row="1"
60+
VerticalTextAlignment="Center"
61+
VerticalOptions="Center"
62+
Text="=" />
63+
64+
<Label Grid.Column="6"
65+
Grid.Row="1"
66+
VerticalTextAlignment="Center"
67+
VerticalOptions="Center">
68+
<Label.Text>
69+
<MultiBinding Converter="{StaticResource MultiMathExpressionConverter}"
70+
ConverterParameter="x0 + x1 + x2">
71+
<Binding Path="X0" Mode="OneWay"/>
72+
<Binding Path="X1" Mode="OneWay"/>
73+
<Binding Path="X2" Mode="OneWay"/>
74+
</MultiBinding>
75+
</Label.Text>
76+
</Label>
77+
</Grid>
78+
79+
<Label HorizontalOptions="Center"
80+
Text="Valid: Value greater than or equal to 60"
81+
TextColor="Green">
82+
<Label.Triggers>
83+
<DataTrigger TargetType="Label" Value="False">
84+
<DataTrigger.Binding>
85+
<MultiBinding Converter="{StaticResource MultiMathExpressionConverter}"
86+
ConverterParameter="x0 + x1 + x2 &gt;= 60">
87+
<Binding Path="X0" Mode="OneWay"/>
88+
<Binding Path="X1" Mode="OneWay"/>
89+
<Binding Path="X2" Mode="OneWay"/>
90+
</MultiBinding>
91+
</DataTrigger.Binding>
92+
<Setter Property="Text" Value="Error: Value not greater than or equal to 60"/>
93+
<Setter Property="TextColor" Value="Red"/>
94+
</DataTrigger>
95+
</Label.Triggers>
7596
</Label>
76-
</Grid>
97+
98+
</VerticalStackLayout>
99+
77100
</pages:BasePage>

src/CommunityToolkit.Maui.Core/Primitives/MathOperator.shared.cs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public enum MathOperatorPrecedence
1414
/// <summary>High</summary>
1515
High,
1616
/// <summary>Constant</summary>
17-
Constant,
17+
Constant
1818
}
1919

2020
/// <summary>
@@ -25,31 +25,25 @@ public enum MathOperatorPrecedence
2525
/// </remarks>
2626
/// <param name="name">Name</param>
2727
/// <param name="numericCount">Number of Numerals</param>
28-
/// <param name="precedence">Math Operator Preference</param>
2928
/// <param name="calculateFunc">Calculation Function</param>
3029
public sealed class MathOperator(
31-
string name,
32-
int numericCount,
33-
MathOperatorPrecedence precedence,
34-
Func<double[], double> calculateFunc)
30+
string name,
31+
int numericCount,
32+
Func<object?[], object?> calculateFunc)
3533
{
36-
/// <summary>
37-
/// Name
38-
/// </summary>
39-
public string Name { get; } = name;
4034

41-
/// <summary>
42-
/// Number of Numerals
43-
/// </summary>
44-
public int NumericCount { get; } = numericCount;
35+
/// <summary>
36+
/// Name
37+
/// </summary>
38+
public string Name { get; } = name;
4539

46-
/// <summary>
47-
/// Math Operator Precedence
48-
/// </summary>
49-
public MathOperatorPrecedence Precedence { get; } = precedence;
40+
/// <summary>
41+
/// Number of Numerals
42+
/// </summary>
43+
public int NumericCount { get; } = numericCount;
5044

51-
/// <summary>
52-
/// Calculation Function
53-
/// </summary>
54-
public Func<double[], double> CalculateFunc { get; } = calculateFunc;
45+
/// <summary>
46+
/// Calculation Function
47+
/// </summary>
48+
public Func<object?[], object?> CalculateFunc { get; } = calculateFunc;
5549
}

0 commit comments

Comments
 (0)