Skip to content

Commit f579121

Browse files
author
linzhijun
committed
修改 bug, 依据 C# 语言规范(ECMA-334)第 12.15 节指出:
The conditional operator is right-associative, meaning that an expression of the form a ? b : c ? d : e is evaluated as a ? b : (c ? d : e).
1 parent 5e41f4c commit f579121

4 files changed

Lines changed: 79 additions & 76 deletions

File tree

csharp/ToolGood.Algorithm.Test/AlgorithmEngine/OperatorTest.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using PetaTest;
1+
using PetaTest;
22

33
namespace ToolGood.Algorithm.Test.Operator
44
{
@@ -58,7 +58,10 @@ public void conditional_test()
5858
t1 = engine.TryEvaluate("-7 < -2 ?1 : 2", 0);
5959
Assert.AreEqual(t1, 1);
6060

61-
t1 = engine.TryEvaluate("-7 < -2 ?1 :2", 0);
61+
t1 = engine.TryEvaluate("-7 < -2 ?1 : 7>1?3:2", 0);
62+
Assert.AreEqual(t1, 1);
63+
64+
t1 = engine.TryEvaluate("-7 < -2 ?1 :2", 0);
6265
Assert.AreEqual(t1, 1);
6366
t1 = engine.TryEvaluate("-7 < -2 ? 1 : 2", 0);
6467
Assert.AreEqual(t1, 1);

csharp/ToolGood.Algorithm/ToolGood.Algorithm.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<Product>ToolGood.Algorithm</Product>
2020
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
2121
<SignAssembly>true</SignAssembly>
22-
<Version>6.2.5.29</Version>
22+
<Version>6.2.5.30</Version>
2323
<AssemblyOriginatorKeyFile>ToolGood.Algorithm.snk</AssemblyOriginatorKeyFile>
2424
<DelaySign>false</DelaySign>
2525
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\ToolGood.Algorithm.xml</DocumentationFile>

csharp/ToolGood.Algorithm/math/mathParser.cs

Lines changed: 72 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -674,23 +674,11 @@ private ExprContext expr(int _p) {
674674
}
675675
break;
676676
case 7:
677-
{
678-
_localctx = new IF_funContext(new ExprContext(_parentctx, _parentState));
679-
PushNewRecursionContext(_localctx, _startState, 1);
680-
Match(8);
681-
State = 111;
682-
expr(0);
683-
Match(9);
684-
State = 113;
685-
expr(10);
686-
}
687-
break;
688-
case 8:
689677
{
690678
_localctx = new Function_funContext(new ExprContext(_parentctx, _parentState));
691679
PushNewRecursionContext(_localctx, _startState, 1);
692680
Match(3);
693-
State = 117;
681+
State = 111;
694682
((Function_funContext)_localctx).f = TokenStream.LT(1);
695683
_la = TokenStream.LA(1);
696684
if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & 18295598608285696L) != 0) || ((((_la - 95)) & ~0x3f) == 0 && ((1L << (_la - 95)) & 6642824844070105211L) != 0) || ((((_la - 168)) & ~0x3f) == 0 && ((1L << (_la - 168)) & 360287970189642823L) != 0) || ((((_la - 238)) & ~0x3f) == 0 && ((1L << (_la - 238)) & 504403244164972099L) != 0)) ) {
@@ -705,15 +693,15 @@ private ExprContext expr(int _p) {
705693
_la = TokenStream.LA(1);
706694
if ((((_la) & ~0x3f) == 0 && ((1L << _la) & -335534942L) != 0) || ((((_la - 64)) & ~0x3f) == 0 && ((1L << (_la - 64)) & -1L) != 0) || ((((_la - 128)) & ~0x3f) == 0 && ((1L << (_la - 128)) & -1L) != 0) || ((((_la - 192)) & ~0x3f) == 0 && ((1L << (_la - 192)) & -1L) != 0) || ((((_la - 256)) & ~0x3f) == 0 && ((1L << (_la - 256)) & 70368744177663L) != 0)) {
707695
{
708-
State = 119;
696+
State = 113;
709697
expr(0);
710698
ErrorHandler.Sync(this);
711699
_la = TokenStream.LA(1);
712700
while (_la==4) {
713701
{
714702
{
715703
Match(4);
716-
State = 121;
704+
State = 115;
717705
expr(0);
718706
}
719707
}
@@ -725,7 +713,7 @@ private ExprContext expr(int _p) {
725713
Match(2);
726714
}
727715
break;
728-
case 9:
716+
case 8:
729717
{
730718
_localctx = new DiyFunction_funContext(new ExprContext(_parentctx, _parentState));
731719
PushNewRecursionContext(_localctx, _startState, 1);
@@ -736,15 +724,15 @@ private ExprContext expr(int _p) {
736724
_la = TokenStream.LA(1);
737725
if ((((_la) & ~0x3f) == 0 && ((1L << _la) & -335534942L) != 0) || ((((_la - 64)) & ~0x3f) == 0 && ((1L << (_la - 64)) & -1L) != 0) || ((((_la - 128)) & ~0x3f) == 0 && ((1L << (_la - 128)) & -1L) != 0) || ((((_la - 192)) & ~0x3f) == 0 && ((1L << (_la - 192)) & -1L) != 0) || ((((_la - 256)) & ~0x3f) == 0 && ((1L << (_la - 256)) & 70368744177663L) != 0)) {
738726
{
739-
State = 134;
727+
State = 128;
740728
expr(0);
741729
ErrorHandler.Sync(this);
742730
_la = TokenStream.LA(1);
743731
while (_la==4) {
744732
{
745733
{
746734
Match(4);
747-
State = 136;
735+
State = 130;
748736
expr(0);
749737
}
750738
}
@@ -756,7 +744,7 @@ private ExprContext expr(int _p) {
756744
Match(2);
757745
}
758746
break;
759-
case 10:
747+
case 9:
760748
{
761749
_localctx = new GetJsonValue_funContext(new ExprContext(_parentctx, _parentState));
762750
PushNewRecursionContext(_localctx, _startState, 1);
@@ -774,7 +762,7 @@ private ExprContext expr(int _p) {
774762
break;
775763
case 2:
776764
{
777-
State = 148;
765+
State = 142;
778766
expr(0);
779767
}
780768
break;
@@ -785,7 +773,7 @@ private ExprContext expr(int _p) {
785773
case 3:
786774
{
787775
Match(3);
788-
State = 153;
776+
State = 147;
789777
parameter2();
790778
}
791779
break;
@@ -794,13 +782,25 @@ private ExprContext expr(int _p) {
794782
}
795783
}
796784
break;
797-
case 11:
785+
case 10:
798786
{
799787
_localctx = new Percentage_funContext(new ExprContext(_parentctx, _parentState));
800788
PushNewRecursionContext(_localctx, _startState, 1);
801789
Match(16);
802790
}
803791
break;
792+
case 11:
793+
{
794+
_localctx = new IF_funContext(new ExprContext(_parentctx, _parentState));
795+
PushNewRecursionContext(_localctx, _startState, 1);
796+
Match(8);
797+
State = 154;
798+
expr(0);
799+
Match(9);
800+
State = 156;
801+
expr(0);
802+
}
803+
break;
804804
}
805805
}
806806
}
@@ -1253,62 +1253,62 @@ public Parameter2Context parameter2() {
12531253
1,1,1,1,5,1,64,8,1,10,1,12,1,67,9,1,1,1,3,1,70,8,1,1,1,1,1,1,1,1,1,1,1,
12541254
1,1,5,1,78,8,1,10,1,12,1,81,9,1,1,1,3,1,84,8,1,1,1,1,1,1,1,1,1,3,1,90,
12551255
8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1256-
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,1,123,8,1,
1257-
10,1,12,1,126,9,1,3,1,128,8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,1,138,
1258-
8,1,10,1,12,1,141,9,1,3,1,143,8,1,1,1,1,1,1,1,1,1,1,1,3,1,150,8,1,1,1,
1259-
1,1,1,1,3,1,155,8,1,1,1,1,1,5,1,159,8,1,10,1,12,1,162,9,1,1,2,3,2,165,
1260-
8,2,1,2,1,2,1,3,1,3,1,3,1,3,1,3,1,3,1,3,3,3,176,8,3,1,4,1,4,1,4,0,1,2,
1261-
5,0,2,4,6,8,0,11,2,0,28,28,118,118,1,0,29,30,1,0,31,36,1,0,37,298,1,0,
1262-
14,16,2,0,12,13,17,17,1,0,18,21,1,0,22,23,21,0,38,47,54,54,95,96,98,101,
1263-
107,108,115,123,136,138,147,148,153,155,157,157,168,170,174,174,178,179,
1264-
224,224,226,226,238,239,244,244,247,254,272,272,274,274,294,296,1,0,26,
1265-
27,1,0,28,301,214,0,10,1,0,0,0,2,89,1,0,0,0,4,164,1,0,0,0,6,175,1,0,0,
1266-
0,8,177,1,0,0,0,10,11,3,2,1,0,11,12,5,0,0,1,12,1,1,0,0,0,13,14,6,1,-1,
1267-
0,14,15,5,1,0,0,15,16,3,2,1,0,16,17,5,2,0,0,17,90,1,0,0,0,18,20,3,4,2,
1268-
0,19,21,7,0,0,0,20,19,1,0,0,0,20,21,1,0,0,0,21,90,1,0,0,0,22,90,5,27,0,
1269-
0,23,90,5,301,0,0,24,25,5,7,0,0,25,90,3,2,1,17,26,29,7,1,0,0,27,28,5,1,
1270-
0,0,28,30,5,2,0,0,29,27,1,0,0,0,29,30,1,0,0,0,30,90,1,0,0,0,31,32,7,2,
1271-
0,0,32,33,5,1,0,0,33,90,5,2,0,0,34,35,7,3,0,0,35,36,5,1,0,0,36,41,3,2,
1272-
1,0,37,38,5,4,0,0,38,40,3,2,1,0,39,37,1,0,0,0,40,43,1,0,0,0,41,39,1,0,
1273-
0,0,41,42,1,0,0,0,42,44,1,0,0,0,43,41,1,0,0,0,44,45,5,2,0,0,45,90,1,0,
1274-
0,0,46,47,5,301,0,0,47,56,5,1,0,0,48,53,3,2,1,0,49,50,5,4,0,0,50,52,3,
1275-
2,1,0,51,49,1,0,0,0,52,55,1,0,0,0,53,51,1,0,0,0,53,54,1,0,0,0,54,57,1,
1276-
0,0,0,55,53,1,0,0,0,56,48,1,0,0,0,56,57,1,0,0,0,57,58,1,0,0,0,58,90,5,
1277-
2,0,0,59,60,5,10,0,0,60,65,3,6,3,0,61,62,5,4,0,0,62,64,3,6,3,0,63,61,1,
1278-
0,0,0,64,67,1,0,0,0,65,63,1,0,0,0,65,66,1,0,0,0,66,69,1,0,0,0,67,65,1,
1279-
0,0,0,68,70,5,4,0,0,69,68,1,0,0,0,69,70,1,0,0,0,70,71,1,0,0,0,71,72,5,
1280-
11,0,0,72,90,1,0,0,0,73,74,5,5,0,0,74,79,3,2,1,0,75,76,5,4,0,0,76,78,3,
1281-
2,1,0,77,75,1,0,0,0,78,81,1,0,0,0,79,77,1,0,0,0,79,80,1,0,0,0,80,83,1,
1282-
0,0,0,81,79,1,0,0,0,82,84,5,4,0,0,83,82,1,0,0,0,83,84,1,0,0,0,84,85,1,
1283-
0,0,0,85,86,5,6,0,0,86,90,1,0,0,0,87,90,5,299,0,0,88,90,5,300,0,0,89,13,
1284-
1,0,0,0,89,18,1,0,0,0,89,22,1,0,0,0,89,23,1,0,0,0,89,24,1,0,0,0,89,26,
1285-
1,0,0,0,89,31,1,0,0,0,89,34,1,0,0,0,89,46,1,0,0,0,89,59,1,0,0,0,89,73,
1286-
1,0,0,0,89,87,1,0,0,0,89,88,1,0,0,0,90,160,1,0,0,0,91,92,10,15,0,0,92,
1287-
93,7,4,0,0,93,159,3,2,1,16,94,95,10,14,0,0,95,96,7,5,0,0,96,159,3,2,1,
1288-
15,97,98,10,13,0,0,98,99,7,6,0,0,99,159,3,2,1,14,100,101,10,12,0,0,101,
1289-
102,7,7,0,0,102,159,3,2,1,13,103,104,10,11,0,0,104,105,5,24,0,0,105,159,
1290-
3,2,1,12,106,107,10,10,0,0,107,108,5,25,0,0,108,159,3,2,1,11,109,110,10,
1291-
9,0,0,110,111,5,8,0,0,111,112,3,2,1,0,112,113,5,9,0,0,113,114,3,2,1,10,
1292-
114,159,1,0,0,0,115,116,10,20,0,0,116,117,5,3,0,0,117,118,7,8,0,0,118,
1293-
127,5,1,0,0,119,124,3,2,1,0,120,121,5,4,0,0,121,123,3,2,1,0,122,120,1,
1294-
0,0,0,123,126,1,0,0,0,124,122,1,0,0,0,124,125,1,0,0,0,125,128,1,0,0,0,
1295-
126,124,1,0,0,0,127,119,1,0,0,0,127,128,1,0,0,0,128,129,1,0,0,0,129,159,
1296-
5,2,0,0,130,131,10,19,0,0,131,132,5,3,0,0,132,133,5,301,0,0,133,142,5,
1297-
1,0,0,134,139,3,2,1,0,135,136,5,4,0,0,136,138,3,2,1,0,137,135,1,0,0,0,
1298-
138,141,1,0,0,0,139,137,1,0,0,0,139,140,1,0,0,0,140,143,1,0,0,0,141,139,
1299-
1,0,0,0,142,134,1,0,0,0,142,143,1,0,0,0,143,144,1,0,0,0,144,159,5,2,0,
1300-
0,145,154,10,18,0,0,146,149,5,5,0,0,147,150,5,301,0,0,148,150,3,2,1,0,
1301-
149,147,1,0,0,0,149,148,1,0,0,0,150,151,1,0,0,0,151,155,5,6,0,0,152,153,
1302-
5,3,0,0,153,155,3,8,4,0,154,146,1,0,0,0,154,152,1,0,0,0,155,159,1,0,0,
1303-
0,156,157,10,16,0,0,157,159,5,16,0,0,158,91,1,0,0,0,158,94,1,0,0,0,158,
1256+
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,1,117,8,1,10,1,12,1,120,9,1,3,1,122,
1257+
8,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,1,132,8,1,10,1,12,1,135,9,1,3,1,
1258+
137,8,1,1,1,1,1,1,1,1,1,1,1,3,1,144,8,1,1,1,1,1,1,1,3,1,149,8,1,1,1,1,
1259+
1,1,1,1,1,1,1,1,1,1,1,1,1,5,1,159,8,1,10,1,12,1,162,9,1,1,2,3,2,165,8,
1260+
2,1,2,1,2,1,3,1,3,1,3,1,3,1,3,1,3,1,3,3,3,176,8,3,1,4,1,4,1,4,0,1,2,5,
1261+
0,2,4,6,8,0,11,2,0,28,28,118,118,1,0,29,30,1,0,31,36,1,0,37,298,1,0,14,
1262+
16,2,0,12,13,17,17,1,0,18,21,1,0,22,23,21,0,38,47,54,54,95,96,98,101,107,
1263+
108,115,123,136,138,147,148,153,155,157,157,168,170,174,174,178,179,224,
1264+
224,226,226,238,239,244,244,247,254,272,272,274,274,294,296,1,0,26,27,
1265+
1,0,28,301,214,0,10,1,0,0,0,2,89,1,0,0,0,4,164,1,0,0,0,6,175,1,0,0,0,8,
1266+
177,1,0,0,0,10,11,3,2,1,0,11,12,5,0,0,1,12,1,1,0,0,0,13,14,6,1,-1,0,14,
1267+
15,5,1,0,0,15,16,3,2,1,0,16,17,5,2,0,0,17,90,1,0,0,0,18,20,3,4,2,0,19,
1268+
21,7,0,0,0,20,19,1,0,0,0,20,21,1,0,0,0,21,90,1,0,0,0,22,90,5,27,0,0,23,
1269+
90,5,301,0,0,24,25,5,7,0,0,25,90,3,2,1,17,26,29,7,1,0,0,27,28,5,1,0,0,
1270+
28,30,5,2,0,0,29,27,1,0,0,0,29,30,1,0,0,0,30,90,1,0,0,0,31,32,7,2,0,0,
1271+
32,33,5,1,0,0,33,90,5,2,0,0,34,35,7,3,0,0,35,36,5,1,0,0,36,41,3,2,1,0,
1272+
37,38,5,4,0,0,38,40,3,2,1,0,39,37,1,0,0,0,40,43,1,0,0,0,41,39,1,0,0,0,
1273+
41,42,1,0,0,0,42,44,1,0,0,0,43,41,1,0,0,0,44,45,5,2,0,0,45,90,1,0,0,0,
1274+
46,47,5,301,0,0,47,56,5,1,0,0,48,53,3,2,1,0,49,50,5,4,0,0,50,52,3,2,1,
1275+
0,51,49,1,0,0,0,52,55,1,0,0,0,53,51,1,0,0,0,53,54,1,0,0,0,54,57,1,0,0,
1276+
0,55,53,1,0,0,0,56,48,1,0,0,0,56,57,1,0,0,0,57,58,1,0,0,0,58,90,5,2,0,
1277+
0,59,60,5,10,0,0,60,65,3,6,3,0,61,62,5,4,0,0,62,64,3,6,3,0,63,61,1,0,0,
1278+
0,64,67,1,0,0,0,65,63,1,0,0,0,65,66,1,0,0,0,66,69,1,0,0,0,67,65,1,0,0,
1279+
0,68,70,5,4,0,0,69,68,1,0,0,0,69,70,1,0,0,0,70,71,1,0,0,0,71,72,5,11,0,
1280+
0,72,90,1,0,0,0,73,74,5,5,0,0,74,79,3,2,1,0,75,76,5,4,0,0,76,78,3,2,1,
1281+
0,77,75,1,0,0,0,78,81,1,0,0,0,79,77,1,0,0,0,79,80,1,0,0,0,80,83,1,0,0,
1282+
0,81,79,1,0,0,0,82,84,5,4,0,0,83,82,1,0,0,0,83,84,1,0,0,0,84,85,1,0,0,
1283+
0,85,86,5,6,0,0,86,90,1,0,0,0,87,90,5,299,0,0,88,90,5,300,0,0,89,13,1,
1284+
0,0,0,89,18,1,0,0,0,89,22,1,0,0,0,89,23,1,0,0,0,89,24,1,0,0,0,89,26,1,
1285+
0,0,0,89,31,1,0,0,0,89,34,1,0,0,0,89,46,1,0,0,0,89,59,1,0,0,0,89,73,1,
1286+
0,0,0,89,87,1,0,0,0,89,88,1,0,0,0,90,160,1,0,0,0,91,92,10,15,0,0,92,93,
1287+
7,4,0,0,93,159,3,2,1,16,94,95,10,14,0,0,95,96,7,5,0,0,96,159,3,2,1,15,
1288+
97,98,10,13,0,0,98,99,7,6,0,0,99,159,3,2,1,14,100,101,10,12,0,0,101,102,
1289+
7,7,0,0,102,159,3,2,1,13,103,104,10,11,0,0,104,105,5,24,0,0,105,159,3,
1290+
2,1,12,106,107,10,10,0,0,107,108,5,25,0,0,108,159,3,2,1,11,109,110,10,
1291+
20,0,0,110,111,5,3,0,0,111,112,7,8,0,0,112,121,5,1,0,0,113,118,3,2,1,0,
1292+
114,115,5,4,0,0,115,117,3,2,1,0,116,114,1,0,0,0,117,120,1,0,0,0,118,116,
1293+
1,0,0,0,118,119,1,0,0,0,119,122,1,0,0,0,120,118,1,0,0,0,121,113,1,0,0,
1294+
0,121,122,1,0,0,0,122,123,1,0,0,0,123,159,5,2,0,0,124,125,10,19,0,0,125,
1295+
126,5,3,0,0,126,127,5,301,0,0,127,136,5,1,0,0,128,133,3,2,1,0,129,130,
1296+
5,4,0,0,130,132,3,2,1,0,131,129,1,0,0,0,132,135,1,0,0,0,133,131,1,0,0,
1297+
0,133,134,1,0,0,0,134,137,1,0,0,0,135,133,1,0,0,0,136,128,1,0,0,0,136,
1298+
137,1,0,0,0,137,138,1,0,0,0,138,159,5,2,0,0,139,148,10,18,0,0,140,143,
1299+
5,5,0,0,141,144,5,301,0,0,142,144,3,2,1,0,143,141,1,0,0,0,143,142,1,0,
1300+
0,0,144,145,1,0,0,0,145,149,5,6,0,0,146,147,5,3,0,0,147,149,3,8,4,0,148,
1301+
140,1,0,0,0,148,146,1,0,0,0,149,159,1,0,0,0,150,151,10,16,0,0,151,159,
1302+
5,16,0,0,152,153,10,9,0,0,153,154,5,8,0,0,154,155,3,2,1,0,155,156,5,9,
1303+
0,0,156,157,3,2,1,0,157,159,1,0,0,0,158,91,1,0,0,0,158,94,1,0,0,0,158,
13041304
97,1,0,0,0,158,100,1,0,0,0,158,103,1,0,0,0,158,106,1,0,0,0,158,109,1,0,
1305-
0,0,158,115,1,0,0,0,158,130,1,0,0,0,158,145,1,0,0,0,158,156,1,0,0,0,159,
1305+
0,0,158,124,1,0,0,0,158,139,1,0,0,0,158,150,1,0,0,0,158,152,1,0,0,0,159,
13061306
162,1,0,0,0,160,158,1,0,0,0,160,161,1,0,0,0,161,3,1,0,0,0,162,160,1,0,
13071307
0,0,163,165,5,13,0,0,164,163,1,0,0,0,164,165,1,0,0,0,165,166,1,0,0,0,166,
13081308
167,5,26,0,0,167,5,1,0,0,0,168,169,7,9,0,0,169,170,5,9,0,0,170,176,3,2,
13091309
1,0,171,172,3,8,4,0,172,173,5,9,0,0,173,174,3,2,1,0,174,176,1,0,0,0,175,
13101310
168,1,0,0,0,175,171,1,0,0,0,176,7,1,0,0,0,177,178,7,10,0,0,178,9,1,0,0,
1311-
0,20,20,29,41,53,56,65,69,79,83,89,124,127,139,142,149,154,158,160,164,
1311+
0,20,20,29,41,53,56,65,69,79,83,89,118,121,133,136,143,148,158,160,164,
13121312
175
13131313
};
13141314
public static readonly ATN _ATN =

g4/math.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ expr:
3232
| expr op = (OPNE | OPEQ ) expr # Judge_fun
3333
| expr op = OPAND expr # AndOr_fun
3434
| expr op = OPOR expr # AndOr_fun
35-
| expr '?' expr ':' expr # IF_fun
35+
| expr '?' expr ':' expr <assoc=right> # IF_fun
3636
// 运算符优先级 结束
3737

3838
| f=(TRUE | FALSE) ('(' ')')? # BOOL_fun

0 commit comments

Comments
 (0)