[14727] | 1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
| 2 | using System;
|
---|
[14834] | 3 | using Common;
|
---|
[14727] | 4 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
| 5 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
| 6 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
| 7 |
|
---|
| 8 | /// <summary>
|
---|
| 9 | /// Pushes the sum of the top two items.
|
---|
| 10 | /// </summary>
|
---|
[14777] | 11 | [PushExpression(StackTypes.Float, "FLOAT.+")]
|
---|
[14727] | 12 | public class FloatAddExpression : PushResultExpression<double> {
|
---|
[14834] | 13 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
| 14 | return Eval(interpreter.FloatStack, 2,
|
---|
| 15 | values => {
|
---|
| 16 | var result = values[0] + values[1];
|
---|
| 17 |
|
---|
| 18 | if (double.IsPositiveInfinity(result)) return double.MaxValue;
|
---|
| 19 | if (double.IsNegativeInfinity(result)) return double.MinValue;
|
---|
| 20 |
|
---|
| 21 | return result;
|
---|
| 22 | });
|
---|
[14727] | 23 | }
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | /// <summary>
|
---|
| 27 | /// Pushes the difference of the top two items; that is, the second item minus the top item.
|
---|
| 28 | /// </summary>
|
---|
[14777] | 29 | [PushExpression(StackTypes.Float, "FLOAT.-")]
|
---|
[14727] | 30 | public class FloatSubtractExpression : PushResultExpression<double> {
|
---|
[14834] | 31 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
| 32 | return Eval(interpreter.FloatStack, 2,
|
---|
| 33 | values => {
|
---|
| 34 | var result = values[0] - values[1];
|
---|
| 35 |
|
---|
| 36 | if (double.IsPositiveInfinity(result)) return double.MaxValue;
|
---|
| 37 | if (double.IsNegativeInfinity(result)) return double.MinValue;
|
---|
| 38 |
|
---|
| 39 | return result;
|
---|
| 40 | });
|
---|
[14727] | 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | /// <summary>
|
---|
| 45 | /// Pushes the product of the top two items.
|
---|
| 46 | /// </summary>
|
---|
[14777] | 47 | [PushExpression(StackTypes.Float, "FLOAT.*")]
|
---|
[14727] | 48 | public class FloatMultiplyExpression : PushResultExpression<double> {
|
---|
[14834] | 49 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
| 50 | return Eval(interpreter.FloatStack, 2,
|
---|
| 51 | values => {
|
---|
| 52 | var result = values[0] * values[1];
|
---|
| 53 |
|
---|
| 54 | if (double.IsPositiveInfinity(result)) return double.MaxValue;
|
---|
| 55 | if (double.IsNegativeInfinity(result)) return double.MinValue;
|
---|
| 56 |
|
---|
| 57 | return result;
|
---|
| 58 | });
|
---|
[14727] | 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /// <summary>
|
---|
| 63 | /// Pushes the quotient of the top two items; that is, the second item divided by the top item.
|
---|
| 64 | /// If the top item is zero this acts as a NOOP.
|
---|
| 65 | /// </summary>
|
---|
[14777] | 66 | [PushExpression(StackTypes.Float, "FLOAT./")]
|
---|
[14727] | 67 | public class FloatDivideExpression : PushResultExpression<double> {
|
---|
[14834] | 68 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
| 69 | return Eval(interpreter.FloatStack, 2, values => {
|
---|
| 70 | var result = values[0] / values[1];
|
---|
| 71 |
|
---|
| 72 | if (double.IsPositiveInfinity(result)) return double.MaxValue;
|
---|
| 73 | if (double.IsNegativeInfinity(result)) return double.MinValue;
|
---|
| 74 |
|
---|
| 75 | return result;
|
---|
| 76 | }, 0);
|
---|
[14727] | 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /// <summary>
|
---|
| 81 | /// Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP.
|
---|
| 82 | /// The modulus is computed as the remainder of the quotient, where the quotient has first been truncated toward
|
---|
| 83 | /// negative infinity.
|
---|
| 84 | /// (This is taken from the definition for the generic MOD function in Common Lisp, which is described
|
---|
| 85 | /// for example at http://www.lispworks.com/reference/HyperSpec/Body/f_mod_r.htm.)
|
---|
| 86 | /// </summary>
|
---|
[14777] | 87 | [PushExpression(StackTypes.Float, "FLOAT.%")]
|
---|
[14727] | 88 | public class FloatModuloExpression : PushResultExpression<double> {
|
---|
[14834] | 89 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14744] | 90 | return Eval(interpreter.FloatStack, 2, values => values[0] % values[1], 0);
|
---|
[14727] | 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /// <summary>
|
---|
| 95 | /// Pushes the minimum of the top two items.
|
---|
| 96 | /// </summary>
|
---|
[14777] | 97 | [PushExpression(StackTypes.Float, "FLOAT.MIN")]
|
---|
[14727] | 98 | public class FloatMinExpression : PushResultExpression<double> {
|
---|
[14834] | 99 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14744] | 100 | return Eval(interpreter.FloatStack, 2, values => Math.Min(values[0], values[1]));
|
---|
[14727] | 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /// <summary>
|
---|
| 105 | /// Pushes the maximum of the top two items.
|
---|
| 106 | /// </summary>
|
---|
[14777] | 107 | [PushExpression(StackTypes.Float, "FLOAT.MAX")]
|
---|
[14727] | 108 | public class FloatMaxExpression : PushResultExpression<double> {
|
---|
[14834] | 109 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14744] | 110 | return Eval(interpreter.FloatStack, 2, values => Math.Max(values[0], values[1]));
|
---|
[14727] | 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /// <summary>
|
---|
| 115 | /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
|
---|
| 116 | /// </summary>
|
---|
[14777] | 117 | [PushExpression(StackTypes.Float, "FLOAT.<", StackTypes.Boolean)]
|
---|
[14727] | 118 | public class FloatSmallerThanExpression : PushResultExpression<double> {
|
---|
[14834] | 119 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14744] | 120 | return Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] < values[1]);
|
---|
[14727] | 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | /// <summary>
|
---|
[14777] | 125 | /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
|
---|
| 126 | /// </summary>
|
---|
| 127 | [PushExpression(StackTypes.Float, "FLOAT.<=", StackTypes.Boolean)]
|
---|
| 128 | public class FloatSmallerThanOrEqualExpression : PushResultExpression<double> {
|
---|
[14834] | 129 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
| 130 | return Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] < values[1] || values[0].IsAlmost(values[1]));
|
---|
[14777] | 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /// <summary>
|
---|
[14727] | 135 | /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
|
---|
| 136 | /// </summary>
|
---|
[14777] | 137 | [PushExpression(StackTypes.Float, "FLOAT.>", StackTypes.Boolean)]
|
---|
[14727] | 138 | public class FloatGreaterThanExpression : PushResultExpression<double> {
|
---|
[14834] | 139 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14744] | 140 | return Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] > values[1]);
|
---|
[14727] | 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /// <summary>
|
---|
[14777] | 145 | /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than or equal to the top item, or FALSE otherwise.
|
---|
| 146 | /// </summary>
|
---|
| 147 | [PushExpression(StackTypes.Float, "FLOAT.>=", StackTypes.Boolean)]
|
---|
| 148 | public class FloatGreaterThanOrEqualExpression : PushResultExpression<double> {
|
---|
[14834] | 149 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
| 150 | return Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] > values[1] || values[0].IsAlmost(values[1]));
|
---|
[14777] | 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /// <summary>
|
---|
[14727] | 155 | /// Pushes the sine of the top item.
|
---|
| 156 | /// </summary>
|
---|
[14777] | 157 | [PushExpression(StackTypes.Float, "FLOAT.SIN")]
|
---|
[14727] | 158 | public class FloatSineExpression : PushResultExpression<double> {
|
---|
[14834] | 159 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14744] | 160 | return Eval(interpreter.FloatStack, 1, values => Math.Sin(values[0]));
|
---|
[14727] | 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /// <summary>
|
---|
| 165 | /// Pushes the cosine of the top item.
|
---|
| 166 | /// </summary>
|
---|
[14777] | 167 | [PushExpression(StackTypes.Float, "FLOAT.COS")]
|
---|
[14727] | 168 | public class FloatCosineExpression : PushResultExpression<double> {
|
---|
[14834] | 169 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14744] | 170 | return Eval(interpreter.FloatStack, 1, values => Math.Cos(values[0]));
|
---|
[14727] | 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /// <summary>
|
---|
| 175 | /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
|
---|
| 176 | /// </summary>
|
---|
[14777] | 177 | [PushExpression(StackTypes.Float, "FLOAT.FROMBOOLEAN", StackTypes.Boolean)]
|
---|
[14727] | 178 | public class FloatFromBooleanExpression : StatelessExpression {
|
---|
[14834] | 179 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14744] | 180 | if (interpreter.BooleanStack.Count == 0) return false;
|
---|
[14727] | 181 |
|
---|
| 182 | var condition = interpreter.BooleanStack.Pop();
|
---|
| 183 | var value = condition ? 1 : 0;
|
---|
| 184 |
|
---|
| 185 | interpreter.FloatStack.Push(value);
|
---|
[14744] | 186 |
|
---|
| 187 | return true;
|
---|
[14727] | 188 | }
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | /// <summary>
|
---|
| 192 | /// Pushes a floating point version of the top INTEGER.
|
---|
| 193 | /// </summary>
|
---|
[14777] | 194 | [PushExpression(StackTypes.Float, "FLOAT.FROMINTEGER", StackTypes.Integer)]
|
---|
[14727] | 195 | public class FloatFromIntegerExpression : StatelessExpression {
|
---|
[14834] | 196 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14744] | 197 | if (interpreter.IntegerStack.Count == 0) return false;
|
---|
[14727] | 198 |
|
---|
| 199 | var value = (double)interpreter.IntegerStack.Pop();
|
---|
| 200 |
|
---|
| 201 | interpreter.FloatStack.Push(value);
|
---|
[14744] | 202 |
|
---|
| 203 | return true;
|
---|
[14727] | 204 | }
|
---|
| 205 | }
|
---|
| 206 | } |
---|