Changeset 14875 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/IntegerExpressions.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/IntegerExpressions.cs
r14834 r14875 11 11 /// </summary> 12 12 [PushExpression(StackTypes.Integer, "INTEGER.+")] 13 public class IntegerAddExpression : PushResultExpression<long> { 14 public override bool Eval(IInternalPushInterpreter interpreter) { 15 return Eval(interpreter.IntegerStack, 2, values => values[0] + values[1]); 13 public class IntegerAddExpression : StatelessExpression { 14 public override bool Eval(IInternalPushInterpreter interpreter) { 15 if (interpreter.IntegerStack.Count < 2) 16 return false; 17 18 var first = interpreter.IntegerStack.Pop(); 19 var second = interpreter.IntegerStack.Top; 20 var result = second + first; 21 22 interpreter.IntegerStack.SetTop(result); 23 return true; 16 24 } 17 25 } … … 21 29 /// </summary> 22 30 [PushExpression(StackTypes.Integer, "INTEGER.-")] 23 public class IntegerSubtractExpression : PushResultExpression<long> { 24 public override bool Eval(IInternalPushInterpreter interpreter) { 25 return Eval(interpreter.IntegerStack, 2, values => values[0] - values[1]); 31 public class IntegerSubtractExpression : StatelessExpression { 32 public override bool Eval(IInternalPushInterpreter interpreter) { 33 if (interpreter.IntegerStack.Count < 2) 34 return false; 35 36 var first = interpreter.IntegerStack.Pop(); 37 var second = interpreter.IntegerStack.Top; 38 var result = second - first; 39 40 interpreter.IntegerStack.SetTop(result); 41 return true; 26 42 } 27 43 } … … 31 47 /// </summary> 32 48 [PushExpression(StackTypes.Integer, "INTEGER.*")] 33 public class IntegerMultiplyExpression : PushResultExpression<long> { 34 public override bool Eval(IInternalPushInterpreter interpreter) { 35 return Eval(interpreter.IntegerStack, 2, values => values[0] * values[1]); 49 public class IntegerMultiplyExpression : StatelessExpression { 50 public override bool Eval(IInternalPushInterpreter interpreter) { 51 if (interpreter.IntegerStack.Count < 2) 52 return false; 53 54 var first = interpreter.IntegerStack.Pop(); 55 var second = interpreter.IntegerStack.Top; 56 var result = second * first; 57 58 interpreter.IntegerStack.SetTop(result); 59 return true; 36 60 } 37 61 } … … 42 66 /// </summary> 43 67 [PushExpression(StackTypes.Integer, "INTEGER./")] 44 public class IntegerDivideExpression : PushResultExpression<long> { 45 public override bool Eval(IInternalPushInterpreter interpreter) { 46 return Eval(interpreter.IntegerStack, 2, values => values[0] / values[1], 0); 68 public class IntegerDivideExpression : StatelessExpression { 69 public override bool Eval(IInternalPushInterpreter interpreter) { 70 if (interpreter.IntegerStack.Count < 2 || 71 interpreter.IntegerStack.Top == 0) 72 return false; 73 74 var first = interpreter.IntegerStack.Pop(); 75 var second = interpreter.IntegerStack.Top; 76 var result = second / first; 77 78 interpreter.IntegerStack.SetTop(result); 79 return true; 47 80 } 48 81 } … … 50 83 /// <summary> 51 84 /// Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP. The modulus is 52 /// computed as the 53 /// remainder of the quotient, where the quotient has first been truncated toward negative infinity. (This is taken 54 /// from the definition 55 /// for the generic MOD function in Common Lisp, which is described for example at 85 /// computed as the remainder of the quotient, where the quotient has first been truncated toward negative infinity. (This is taken 86 /// from the definition for the generic MOD function in Common Lisp, which is described for example at 56 87 /// http://www.lispworks.com/reference/HyperSpec/Body/f_mod_r.htm.) 57 88 /// </summary> 58 89 [PushExpression(StackTypes.Integer, "INTEGER.%")] 59 public class IntegerModuloExpression : PushResultExpression<long> { 60 public override bool Eval(IInternalPushInterpreter interpreter) { 61 return Eval(interpreter.IntegerStack, 2, values => values[0] % values[1], 0); 90 public class IntegerModuloExpression : StatelessExpression { 91 public override bool Eval(IInternalPushInterpreter interpreter) { 92 if (interpreter.IntegerStack.Count < 2 || 93 interpreter.IntegerStack.Top == 0) 94 return false; 95 96 var first = interpreter.IntegerStack.Pop(); 97 var second = interpreter.IntegerStack.Top; 98 var result = second % first; 99 100 interpreter.IntegerStack.SetTop(result); 101 return true; 62 102 } 63 103 } … … 67 107 /// </summary> 68 108 [PushExpression(StackTypes.Integer, "INTEGER.MIN")] 69 public class IntegerMinExpression : PushResultExpression<long> { 70 public override bool Eval(IInternalPushInterpreter interpreter) { 71 return Eval(interpreter.IntegerStack, 2, values => Math.Min(values[0], values[1])); 109 public class IntegerMinExpression : StatelessExpression { 110 public override bool Eval(IInternalPushInterpreter interpreter) { 111 if (interpreter.IntegerStack.Count < 2) 112 return false; 113 114 var first = interpreter.IntegerStack.Pop(); 115 var second = interpreter.IntegerStack.Top; 116 var result = Math.Min(second, first); 117 118 interpreter.IntegerStack.SetTop(result); 119 return true; 72 120 } 73 121 } … … 77 125 /// </summary> 78 126 [PushExpression(StackTypes.Integer, "INTEGER.MAX")] 79 public class IntegerMaxExpression : PushResultExpression<long> { 80 public override bool Eval(IInternalPushInterpreter interpreter) { 81 return Eval(interpreter.IntegerStack, 2, values => Math.Max(values[0], values[1])); 127 public class IntegerMaxExpression : StatelessExpression { 128 public override bool Eval(IInternalPushInterpreter interpreter) { 129 if (interpreter.IntegerStack.Count < 2) 130 return false; 131 132 var first = interpreter.IntegerStack.Pop(); 133 var second = interpreter.IntegerStack.Top; 134 var result = Math.Max(second, first); 135 136 interpreter.IntegerStack.SetTop(result); 137 return true; 82 138 } 83 139 } … … 87 143 /// </summary> 88 144 [PushExpression(StackTypes.Integer, "INTEGER.<", StackTypes.Boolean)] 89 public class IntegerSmallerThanExpression : PushResultExpression<long> { 90 public override bool Eval(IInternalPushInterpreter interpreter) { 91 return Eval(interpreter.IntegerStack, interpreter.BooleanStack, 2, values => values[0] < values[1]); 145 public class IntegerSmallerThanExpression : StatelessExpression { 146 public override bool Eval(IInternalPushInterpreter interpreter) { 147 if (interpreter.IntegerStack.Count < 2) 148 return false; 149 150 var first = interpreter.IntegerStack.Top; 151 var second = interpreter.IntegerStack[1]; 152 interpreter.IntegerStack.Remove(2); 153 154 var result = second < first; 155 interpreter.BooleanStack.Push(result); 156 return true; 92 157 } 93 158 } … … 97 162 /// </summary> 98 163 [PushExpression(StackTypes.Integer, "INTEGER.>", StackTypes.Boolean)] 99 public class IntegerGreaterThanExpression : PushResultExpression<long> { 100 public override bool Eval(IInternalPushInterpreter interpreter) { 101 return Eval(interpreter.IntegerStack, interpreter.BooleanStack, 2, values => values[0] > values[1]); 164 public class IntegerGreaterThanExpression : StatelessExpression { 165 public override bool Eval(IInternalPushInterpreter interpreter) { 166 if (interpreter.IntegerStack.Count < 2) 167 return false; 168 169 var first = interpreter.IntegerStack.Top; 170 var second = interpreter.IntegerStack[1]; 171 interpreter.IntegerStack.Remove(2); 172 173 var result = second > first; 174 interpreter.BooleanStack.Push(result); 175 return true; 102 176 } 103 177 } … … 109 183 public class IntegerFromBooleanExpression : StatelessExpression { 110 184 public override bool Eval(IInternalPushInterpreter interpreter) { 111 if (interpreter.BooleanStack.Count == 0) return false; 185 if (interpreter.BooleanStack.Count == 0) 186 return false; 112 187 113 188 var condition = interpreter.BooleanStack.Pop(); 114 189 var value = condition ? 1 : 0; 115 116 190 interpreter.IntegerStack.Push(value); 117 118 191 return true; 119 192 } … … 126 199 public class IntegerFromFloatExpression : StatelessExpression { 127 200 public override bool Eval(IInternalPushInterpreter interpreter) { 128 if (interpreter.FloatStack.Count == 0) return false; 201 if (interpreter.FloatStack.Count == 0) 202 return false; 129 203 130 204 var value = (int)interpreter.FloatStack.Pop(); 131 132 205 interpreter.IntegerStack.Push(value); 133 134 206 return true; 135 207 }
Note: See TracChangeset
for help on using the changeset viewer.