1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
2 | using System;
|
---|
3 | using Common;
|
---|
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>
|
---|
11 | [PushExpression(StackTypes.Float, "FLOAT.+")]
|
---|
12 | public class FloatAddExpression : PushResultExpression<double> {
|
---|
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 | });
|
---|
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>
|
---|
29 | [PushExpression(StackTypes.Float, "FLOAT.-")]
|
---|
30 | public class FloatSubtractExpression : PushResultExpression<double> {
|
---|
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 | });
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | /// <summary>
|
---|
45 | /// Pushes the product of the top two items.
|
---|
46 | /// </summary>
|
---|
47 | [PushExpression(StackTypes.Float, "FLOAT.*")]
|
---|
48 | public class FloatMultiplyExpression : PushResultExpression<double> {
|
---|
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 | });
|
---|
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>
|
---|
66 | [PushExpression(StackTypes.Float, "FLOAT./")]
|
---|
67 | public class FloatDivideExpression : PushResultExpression<double> {
|
---|
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);
|
---|
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>
|
---|
87 | [PushExpression(StackTypes.Float, "FLOAT.%")]
|
---|
88 | public class FloatModuloExpression : PushResultExpression<double> {
|
---|
89 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
90 | return Eval(interpreter.FloatStack, 2, values => values[0] % values[1], 0);
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | /// <summary>
|
---|
95 | /// Pushes the minimum of the top two items.
|
---|
96 | /// </summary>
|
---|
97 | [PushExpression(StackTypes.Float, "FLOAT.MIN")]
|
---|
98 | public class FloatMinExpression : PushResultExpression<double> {
|
---|
99 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
100 | return Eval(interpreter.FloatStack, 2, values => Math.Min(values[0], values[1]));
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | /// <summary>
|
---|
105 | /// Pushes the maximum of the top two items.
|
---|
106 | /// </summary>
|
---|
107 | [PushExpression(StackTypes.Float, "FLOAT.MAX")]
|
---|
108 | public class FloatMaxExpression : PushResultExpression<double> {
|
---|
109 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
110 | return Eval(interpreter.FloatStack, 2, values => Math.Max(values[0], values[1]));
|
---|
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>
|
---|
117 | [PushExpression(StackTypes.Float, "FLOAT.<", StackTypes.Boolean)]
|
---|
118 | public class FloatSmallerThanExpression : PushResultExpression<double> {
|
---|
119 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
120 | return Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] < values[1]);
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | /// <summary>
|
---|
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> {
|
---|
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]));
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | /// <summary>
|
---|
135 | /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
|
---|
136 | /// </summary>
|
---|
137 | [PushExpression(StackTypes.Float, "FLOAT.>", StackTypes.Boolean)]
|
---|
138 | public class FloatGreaterThanExpression : PushResultExpression<double> {
|
---|
139 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
140 | return Eval(interpreter.FloatStack, interpreter.BooleanStack, 2, values => values[0] > values[1]);
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | /// <summary>
|
---|
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> {
|
---|
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]));
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | /// <summary>
|
---|
155 | /// Pushes the sine of the top item.
|
---|
156 | /// </summary>
|
---|
157 | [PushExpression(StackTypes.Float, "FLOAT.SIN")]
|
---|
158 | public class FloatSineExpression : PushResultExpression<double> {
|
---|
159 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
160 | return Eval(interpreter.FloatStack, 1, values => Math.Sin(values[0]));
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | /// <summary>
|
---|
165 | /// Pushes the cosine of the top item.
|
---|
166 | /// </summary>
|
---|
167 | [PushExpression(StackTypes.Float, "FLOAT.COS")]
|
---|
168 | public class FloatCosineExpression : PushResultExpression<double> {
|
---|
169 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
170 | return Eval(interpreter.FloatStack, 1, values => Math.Cos(values[0]));
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | /// <summary>
|
---|
175 | /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
|
---|
176 | /// </summary>
|
---|
177 | [PushExpression(StackTypes.Float, "FLOAT.FROMBOOLEAN", StackTypes.Boolean)]
|
---|
178 | public class FloatFromBooleanExpression : StatelessExpression {
|
---|
179 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
180 | if (interpreter.BooleanStack.Count == 0) return false;
|
---|
181 |
|
---|
182 | var condition = interpreter.BooleanStack.Pop();
|
---|
183 | var value = condition ? 1 : 0;
|
---|
184 |
|
---|
185 | interpreter.FloatStack.Push(value);
|
---|
186 |
|
---|
187 | return true;
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | /// <summary>
|
---|
192 | /// Pushes a floating point version of the top INTEGER.
|
---|
193 | /// </summary>
|
---|
194 | [PushExpression(StackTypes.Float, "FLOAT.FROMINTEGER", StackTypes.Integer)]
|
---|
195 | public class FloatFromIntegerExpression : StatelessExpression {
|
---|
196 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
197 | if (interpreter.IntegerStack.Count == 0) return false;
|
---|
198 |
|
---|
199 | var value = (double)interpreter.IntegerStack.Pop();
|
---|
200 |
|
---|
201 | interpreter.FloatStack.Push(value);
|
---|
202 |
|
---|
203 | return true;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | } |
---|