Changeset 14875 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/BooleanExpressions.cs
- Timestamp:
- 04/18/17 01:15:25 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/BooleanExpressions.cs
r14834 r14875 10 10 /// </summary> 11 11 [PushExpression(StackTypes.Boolean, "BOOLEAN.AND")] 12 public class BooleanAndExpression : PushResultExpression<bool>{12 public class BooleanAndExpression : StatelessExpression { 13 13 public override bool Eval(IInternalPushInterpreter interpreter) { 14 return Eval(interpreter.BooleanStack, 2, values => values[0] && values[1]); 14 if (interpreter.BooleanStack.Count < 2) 15 return false; 16 17 var first = interpreter.BooleanStack.Pop(); 18 var second = interpreter.BooleanStack.Top; 19 var result = second && first; 20 21 interpreter.BooleanStack.SetTop(result); 22 return true; 15 23 } 16 24 } … … 20 28 /// </summary> 21 29 [PushExpression(StackTypes.Boolean, "BOOLEAN.OR")] 22 public class BooleanOrExpression : PushResultExpression<bool>{30 public class BooleanOrExpression : StatelessExpression { 23 31 public override bool Eval(IInternalPushInterpreter interpreter) { 24 return Eval(interpreter.BooleanStack, 2, values => values[0] || values[1]); 32 if (interpreter.BooleanStack.Count < 2) 33 return false; 34 35 var first = interpreter.BooleanStack.Pop(); 36 var second = interpreter.BooleanStack.Top; 37 var result = second || first; 38 39 interpreter.BooleanStack.SetTop(result); 40 return true; 25 41 } 26 42 } … … 30 46 /// </summary> 31 47 [PushExpression(StackTypes.Boolean, "BOOLEAN.NOT")] 32 public class BooleanNotExpression : PushResultExpression<bool>{48 public class BooleanNotExpression : StatelessExpression { 33 49 public override bool Eval(IInternalPushInterpreter interpreter) { 34 return Eval(interpreter.BooleanStack, 1, values => !values[0]); 50 if (interpreter.BooleanStack.IsEmpty) 51 return false; 52 53 interpreter.BooleanStack.SetTop(!interpreter.BooleanStack.Top); 54 return true; 35 55 } 36 56 } … … 70 90 /// </summary> 71 91 [PushExpression(StackTypes.Boolean, "BOOLEAN.INVERT_FIRST_THEN_AND")] 72 public class BooleanInvertFirstThenAnd : PushResultExpression<bool>{92 public class BooleanInvertFirstThenAnd : StatelessExpression { 73 93 public override bool Eval(IInternalPushInterpreter interpreter) { 74 return Eval(interpreter.BooleanStack, 2, values => !values[0] && values[1]); 94 if (interpreter.BooleanStack.Count < 2) 95 return false; 96 97 var first = interpreter.BooleanStack.Pop(); 98 var second = interpreter.BooleanStack.Top; 99 var result = second && !first; 100 101 interpreter.BooleanStack.SetTop(result); 102 return true; 75 103 } 76 104 } … … 80 108 /// </summary> 81 109 [PushExpression(StackTypes.Boolean, "BOOLEAN.INVERT_SECOND_THEN_AND")] 82 public class BooleanInvertSecondThenAnd : PushResultExpression<bool>{110 public class BooleanInvertSecondThenAnd : StatelessExpression { 83 111 public override bool Eval(IInternalPushInterpreter interpreter) { 84 return Eval(interpreter.BooleanStack, 2, values => values[0] && !values[1]); 112 if (interpreter.BooleanStack.Count < 2) 113 return false; 114 115 var first = interpreter.BooleanStack.Pop(); 116 var second = interpreter.BooleanStack.Top; 117 var result = !second && first; 118 119 interpreter.BooleanStack.SetTop(result); 120 return true; 85 121 } 86 122 } … … 88 124 89 125 /// <summary> 90 /// Pushes the logical AND of the top two BOOLEANs, whereby the second value is inverted first.126 /// Pushes the xor of the top tow BOOLEANs 91 127 /// </summary> 92 128 [PushExpression(StackTypes.Boolean, "BOOLEAN.XOR")] 93 public class BooleanXorExpression : PushResultExpression<bool>{129 public class BooleanXorExpression : StatelessExpression { 94 130 public override bool Eval(IInternalPushInterpreter interpreter) { 95 return Eval(interpreter.BooleanStack, 2, values => values[0] ^ values[1]); 131 if (interpreter.BooleanStack.Count < 2) 132 return false; 133 134 var first = interpreter.BooleanStack.Pop(); 135 var second = interpreter.BooleanStack.Top; 136 var result = second ^ first; 137 138 interpreter.BooleanStack.SetTop(result); 139 return true; 96 140 } 97 141 }
Note: See TracChangeset
for help on using the changeset viewer.