@@ -533,16 +533,15 @@ public void setUp(final ISymbol newSymbol) {
533
533
* f(-3)
534
534
* </pre>
535
535
*/
536
- private static final class Condition extends AbstractCoreFunctionEvaluator {
536
+ private static final class Condition extends AbstractCoreFunctionEvaluator
537
+ implements IFastFunctionEvaluator {
537
538
538
539
@ Override
539
540
public final IExpr evaluate (final IAST ast , EvalEngine engine ) {
540
541
if (engine .isEvalRHSMode ()) {
542
+ IExpr arg1 = ast .arg1 ();
541
543
IExpr arg2 = ast .arg2 ();
542
- if (engine .evalTrue (arg2 )) {
543
- return ast .arg1 ();
544
- }
545
- throw ConditionException .CONDITION_NIL ;
544
+ return condition (arg1 , arg2 , engine );
546
545
}
547
546
return F .NIL ;
548
547
}
@@ -763,7 +762,8 @@ public void setUp(ISymbol newSymbol) {
763
762
* | hi
764
763
* </pre>
765
764
*/
766
- private static final class Do extends AbstractCoreFunctionEvaluator {
765
+ private static final class Do extends AbstractCoreFunctionEvaluator
766
+ implements IFastFunctionEvaluator {
767
767
768
768
@ Override
769
769
public IExpr evaluate (final IAST ast , EvalEngine engine ) {
@@ -1141,7 +1141,8 @@ public void setUp(final ISymbol newSymbol) {
1141
1141
* 120
1142
1142
* </pre>
1143
1143
*/
1144
- private static final class For extends AbstractCoreFunctionEvaluator {
1144
+ private static final class For extends AbstractCoreFunctionEvaluator
1145
+ implements IFastFunctionEvaluator {
1145
1146
1146
1147
@ Override
1147
1148
public IExpr evaluate (final IAST ast , EvalEngine engine ) {
@@ -1262,26 +1263,27 @@ public void setUp(final ISymbol newSymbol) {
1262
1263
* >> If(a, (*then*) b, (*else*) c);
1263
1264
* </pre>
1264
1265
*/
1265
- private static final class If extends AbstractCoreFunctionEvaluator {
1266
+ private static final class If extends AbstractCoreFunctionEvaluator
1267
+ implements IFastFunctionEvaluator {
1266
1268
1267
1269
@ Override
1268
1270
public IExpr evaluate (final IAST ast , EvalEngine engine ) {
1269
1271
final IExpr temp = engine .evaluate (ast .arg1 ());
1270
1272
1271
1273
if (temp .isFalse ()) {
1272
1274
if (ast .size () >= 4 ) {
1273
- return ast .arg3 ();
1275
+ return engine . evaluate ( ast .arg3 () );
1274
1276
}
1275
1277
1276
1278
return S .Null ;
1277
1279
}
1278
1280
1279
- if (temp .equals ( S . True )) {
1280
- return ast .arg2 ();
1281
+ if (temp .isTrue ( )) {
1282
+ return engine . evaluate ( ast .arg2 () );
1281
1283
}
1282
1284
1283
1285
if (ast .size () == 5 ) {
1284
- return ast .arg4 ();
1286
+ return engine . evaluate ( ast .arg4 () );
1285
1287
}
1286
1288
1287
1289
return F .NIL ;
@@ -1525,7 +1527,8 @@ public int status() {
1525
1527
*
1526
1528
* </blockquote>
1527
1529
*/
1528
- private static final class Module extends AbstractCoreFunctionEvaluator {
1530
+ private static final class Module extends AbstractCoreFunctionEvaluator
1531
+ implements IFastFunctionEvaluator {
1529
1532
/** */
1530
1533
@ Override
1531
1534
public IExpr evaluate (final IAST ast , EvalEngine engine ) {
@@ -3064,7 +3067,8 @@ public void setUp(final ISymbol newSymbol) {
3064
3067
* Switch(b, b)
3065
3068
* </pre>
3066
3069
*/
3067
- private static final class Switch extends AbstractCoreFunctionEvaluator {
3070
+ private static final class Switch extends AbstractCoreFunctionEvaluator
3071
+ implements IFastFunctionEvaluator {
3068
3072
3069
3073
@ Override
3070
3074
public IExpr evaluate (final IAST ast , EvalEngine engine ) {
@@ -3543,7 +3547,8 @@ public void setUp(ISymbol newSymbol) {
3543
3547
* Which(a, b, c)
3544
3548
* </pre>
3545
3549
*/
3546
- private static final class Which extends AbstractCoreFunctionEvaluator {
3550
+ private static final class Which extends AbstractCoreFunctionEvaluator
3551
+ implements IFastFunctionEvaluator {
3547
3552
3548
3553
@ Override
3549
3554
public IExpr evaluate (final IAST ast , EvalEngine engine ) {
@@ -3617,7 +3622,8 @@ public void setUp(final ISymbol newSymbol) {
3617
3622
* 12
3618
3623
* </pre>
3619
3624
*/
3620
- private static final class While extends AbstractCoreFunctionEvaluator {
3625
+ private static final class While extends AbstractCoreFunctionEvaluator
3626
+ implements IFastFunctionEvaluator {
3621
3627
3622
3628
@ Override
3623
3629
public IExpr evaluate (final IAST ast , EvalEngine engine ) {
@@ -3668,7 +3674,8 @@ public void setUp(final ISymbol newSymbol) {
3668
3674
*
3669
3675
* </blockquote>
3670
3676
*/
3671
- private static final class With extends AbstractCoreFunctionEvaluator {
3677
+ private static final class With extends AbstractCoreFunctionEvaluator
3678
+ implements IFastFunctionEvaluator {
3672
3679
@ Override
3673
3680
public IExpr evaluate (final IAST ast , EvalEngine engine ) {
3674
3681
final IAST moduleVariablesList = Validate .checkLocalVariableList (ast , 1 , engine );
@@ -3804,8 +3811,7 @@ public static boolean rememberModuleVariables(IAST variablesList, final String v
3804
3811
} else {
3805
3812
// Local variable specification `1` contains `2` which is not a symbol or an assignment to
3806
3813
// a symbol.
3807
- Errors .printMessage (S .Module , "lvsym" , F .List (variablesList , varExpr ),
3808
- engine );
3814
+ Errors .printMessage (S .Module , "lvsym" , F .List (variablesList , varExpr ), engine );
3809
3815
return false ;
3810
3816
}
3811
3817
}
@@ -4465,6 +4471,23 @@ private static IExpr assignPartValue(IAST lhs, int partPosition, IExpr value) {
4465
4471
return lhs .setAtCopy (partPosition , value );
4466
4472
}
4467
4473
4474
+ /**
4475
+ * If the second argument is true, evaluate the first argument and return the result. Otherwise
4476
+ * throw a condition {@link ConditionException#CONDITION_NIL}.
4477
+ *
4478
+ * @param arg1
4479
+ * @param arg2
4480
+ * @param engine
4481
+ * @throws ConditionException
4482
+ */
4483
+ public static IExpr condition (IExpr arg1 , IExpr arg2 , EvalEngine engine )
4484
+ throws ConditionException {
4485
+ if (engine .evalTrue (arg2 )) {
4486
+ return engine .evaluate (arg1 );
4487
+ }
4488
+ throw ConditionException .CONDITION_NIL ;
4489
+ }
4490
+
4468
4491
/**
4469
4492
* Call <code>assignPart(element, ast, pos, value, engine)</code> recursively and assign the
4470
4493
* result to the given position in the result. <code>result[[position]] = resultValue</code>
0 commit comments