1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
2 | using System;
|
---|
3 | using System.Globalization;
|
---|
4 |
|
---|
5 | using Common;
|
---|
6 |
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
9 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
10 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
11 |
|
---|
12 | /// <summary>
|
---|
13 | /// Pushes the sum of the top two items.
|
---|
14 | /// </summary>
|
---|
15 | [PushExpression(StackTypes.Float, "FLOAT.+")]
|
---|
16 | [StorableClass]
|
---|
17 | public class FloatAddExpression : StatelessExpression {
|
---|
18 | public FloatAddExpression() { }
|
---|
19 | [StorableConstructor]
|
---|
20 | protected FloatAddExpression(bool deserializing) : base(deserializing) { }
|
---|
21 |
|
---|
22 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
23 | return interpreter.FloatStack.Count < 2;
|
---|
24 | }
|
---|
25 |
|
---|
26 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
27 | var first = interpreter.FloatStack.Pop();
|
---|
28 | var second = interpreter.FloatStack.Top;
|
---|
29 | var result = second + first;
|
---|
30 |
|
---|
31 | if (double.IsPositiveInfinity(result)) result = double.MaxValue;
|
---|
32 | if (double.IsNegativeInfinity(result)) result = double.MinValue;
|
---|
33 |
|
---|
34 | interpreter.FloatStack.Top = result;
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | /// <summary>
|
---|
39 | /// Pushes the difference of the top two items; that is, the second item minus the top item.
|
---|
40 | /// </summary>
|
---|
41 | [PushExpression(StackTypes.Float, "FLOAT.-")]
|
---|
42 | [StorableClass]
|
---|
43 | public class FloatSubtractExpression : StatelessExpression {
|
---|
44 | public FloatSubtractExpression() { }
|
---|
45 | [StorableConstructor]
|
---|
46 | protected FloatSubtractExpression(bool deserializing) : base(deserializing) { }
|
---|
47 |
|
---|
48 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
49 | return interpreter.FloatStack.Count < 2;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
53 | var first = interpreter.FloatStack.Pop();
|
---|
54 | var second = interpreter.FloatStack.Top;
|
---|
55 | var result = second - first;
|
---|
56 |
|
---|
57 | if (double.IsPositiveInfinity(result)) result = double.MaxValue;
|
---|
58 | if (double.IsNegativeInfinity(result)) result = double.MinValue;
|
---|
59 |
|
---|
60 | interpreter.FloatStack.Top = result;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | /// <summary>
|
---|
65 | /// Pushes the product of the top two items.
|
---|
66 | /// </summary>
|
---|
67 | [PushExpression(StackTypes.Float, "FLOAT.*")]
|
---|
68 | [StorableClass]
|
---|
69 | public class FloatMultiplyExpression : StatelessExpression {
|
---|
70 | public FloatMultiplyExpression() { }
|
---|
71 | [StorableConstructor]
|
---|
72 | protected FloatMultiplyExpression(bool deserializing) : base(deserializing) { }
|
---|
73 |
|
---|
74 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
75 | return interpreter.FloatStack.Count < 2;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
79 | var first = interpreter.FloatStack.Pop();
|
---|
80 | var second = interpreter.FloatStack.Top;
|
---|
81 | var result = second * first;
|
---|
82 |
|
---|
83 | if (double.IsPositiveInfinity(result)) result = double.MaxValue;
|
---|
84 | if (double.IsNegativeInfinity(result)) result = double.MinValue;
|
---|
85 |
|
---|
86 | interpreter.FloatStack.Top = result;
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | /// <summary>
|
---|
91 | /// Pushes the quotient of the top two items; that is, the second item divided by the top item.
|
---|
92 | /// If the top item is zero this acts as a NOOP.
|
---|
93 | /// </summary>
|
---|
94 | [PushExpression(StackTypes.Float, "FLOAT./")]
|
---|
95 | [StorableClass]
|
---|
96 | public class FloatDivideExpression : StatelessExpression {
|
---|
97 | public FloatDivideExpression() { }
|
---|
98 | [StorableConstructor]
|
---|
99 | protected FloatDivideExpression(bool deserializing) : base(deserializing) { }
|
---|
100 |
|
---|
101 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
102 | return interpreter.FloatStack.Count < 2 || interpreter.FloatStack.Top.IsAlmost(0);
|
---|
103 | }
|
---|
104 |
|
---|
105 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
106 | var first = interpreter.FloatStack.Pop();
|
---|
107 | var second = interpreter.FloatStack.Top;
|
---|
108 | var result = second / first;
|
---|
109 |
|
---|
110 | if (double.IsPositiveInfinity(result)) result = double.MaxValue;
|
---|
111 | if (double.IsNegativeInfinity(result)) result = double.MinValue;
|
---|
112 |
|
---|
113 | interpreter.FloatStack.Top = result;
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | /// <summary>
|
---|
118 | /// Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP.
|
---|
119 | /// The modulus is computed as the remainder of the quotient, where the quotient has first been truncated toward
|
---|
120 | /// negative infinity.
|
---|
121 | /// (This is taken from the definition for the generic MOD function in Common Lisp, which is described
|
---|
122 | /// for example at http://www.lispworks.com/reference/HyperSpec/Body/f_mod_r.htm.)
|
---|
123 | /// </summary>
|
---|
124 | [PushExpression(StackTypes.Float, "FLOAT.%")]
|
---|
125 | [StorableClass]
|
---|
126 | public class FloatModuloExpression : StatelessExpression {
|
---|
127 | public FloatModuloExpression() { }
|
---|
128 | [StorableConstructor]
|
---|
129 | protected FloatModuloExpression(bool deserializing) : base(deserializing) { }
|
---|
130 |
|
---|
131 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
132 | return interpreter.FloatStack.Count < 2 || interpreter.FloatStack.Top.IsAlmost(0);
|
---|
133 | }
|
---|
134 |
|
---|
135 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
136 | var first = interpreter.FloatStack.Pop();
|
---|
137 | var second = interpreter.FloatStack.Top;
|
---|
138 | var result = second % first;
|
---|
139 |
|
---|
140 | interpreter.FloatStack.Top = result;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | /// <summary>
|
---|
145 | /// Pushes the minimum of the top two items.
|
---|
146 | /// </summary>
|
---|
147 | [PushExpression(StackTypes.Float, "FLOAT.MIN")]
|
---|
148 | [StorableClass]
|
---|
149 | public class FloatMinExpression : StatelessExpression {
|
---|
150 | public FloatMinExpression() { }
|
---|
151 | [StorableConstructor]
|
---|
152 | protected FloatMinExpression(bool deserializing) : base(deserializing) { }
|
---|
153 |
|
---|
154 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
155 | return interpreter.FloatStack.Count < 2;
|
---|
156 | }
|
---|
157 |
|
---|
158 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
159 | var first = interpreter.FloatStack.Pop();
|
---|
160 | var second = interpreter.FloatStack.Top;
|
---|
161 | var result = Math.Min(second, first);
|
---|
162 |
|
---|
163 | interpreter.FloatStack.Top = result;
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | /// <summary>
|
---|
168 | /// Pushes the maximum of the top two items.
|
---|
169 | /// </summary>
|
---|
170 | [PushExpression(StackTypes.Float, "FLOAT.MAX")]
|
---|
171 | [StorableClass]
|
---|
172 | public class FloatMaxExpression : StatelessExpression {
|
---|
173 | public FloatMaxExpression() { }
|
---|
174 | [StorableConstructor]
|
---|
175 | protected FloatMaxExpression(bool deserializing) : base(deserializing) { }
|
---|
176 |
|
---|
177 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
178 | return interpreter.FloatStack.Count < 2;
|
---|
179 | }
|
---|
180 |
|
---|
181 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
182 | var first = interpreter.FloatStack.Pop();
|
---|
183 | var second = interpreter.FloatStack.Top;
|
---|
184 | var result = Math.Max(second, first);
|
---|
185 |
|
---|
186 | interpreter.FloatStack.Top = result;
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | /// <summary>
|
---|
191 | /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
|
---|
192 | /// </summary>
|
---|
193 | [PushExpression(StackTypes.Float, "FLOAT.<", StackTypes.Boolean)]
|
---|
194 | [StorableClass]
|
---|
195 | public class FloatSmallerThanExpression : StatelessExpression {
|
---|
196 | public FloatSmallerThanExpression() { }
|
---|
197 | [StorableConstructor]
|
---|
198 | protected FloatSmallerThanExpression(bool deserializing) : base(deserializing) { }
|
---|
199 |
|
---|
200 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
201 | return interpreter.FloatStack.Count < 2;
|
---|
202 | }
|
---|
203 |
|
---|
204 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
205 | var first = interpreter.FloatStack.Top;
|
---|
206 | var second = interpreter.FloatStack[1];
|
---|
207 | interpreter.FloatStack.Remove(2);
|
---|
208 |
|
---|
209 | var result = second < first;
|
---|
210 | interpreter.BooleanStack.Push(result);
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | /// <summary>
|
---|
215 | /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
|
---|
216 | /// </summary>
|
---|
217 | [PushExpression(StackTypes.Float, "FLOAT.<=", StackTypes.Boolean)]
|
---|
218 | [StorableClass]
|
---|
219 | public class FloatSmallerThanOrEqualExpression : StatelessExpression {
|
---|
220 | public FloatSmallerThanOrEqualExpression() { }
|
---|
221 | [StorableConstructor]
|
---|
222 | protected FloatSmallerThanOrEqualExpression(bool deserializing) : base(deserializing) { }
|
---|
223 |
|
---|
224 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
225 | return interpreter.FloatStack.Count < 2;
|
---|
226 | }
|
---|
227 |
|
---|
228 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
229 | var first = interpreter.FloatStack.Top;
|
---|
230 | var second = interpreter.FloatStack[1];
|
---|
231 | interpreter.FloatStack.Remove(2);
|
---|
232 |
|
---|
233 | var result = second < first || second.IsAlmost(first);
|
---|
234 | interpreter.BooleanStack.Push(result);
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | /// <summary>
|
---|
239 | /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
|
---|
240 | /// </summary>
|
---|
241 | [PushExpression(StackTypes.Float, "FLOAT.>", StackTypes.Boolean)]
|
---|
242 | [StorableClass]
|
---|
243 | public class FloatGreaterThanExpression : StatelessExpression {
|
---|
244 | public FloatGreaterThanExpression() { }
|
---|
245 | [StorableConstructor]
|
---|
246 | protected FloatGreaterThanExpression(bool deserializing) : base(deserializing) { }
|
---|
247 |
|
---|
248 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
249 | return interpreter.FloatStack.Count < 2;
|
---|
250 | }
|
---|
251 |
|
---|
252 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
253 | var first = interpreter.FloatStack.Top;
|
---|
254 | var second = interpreter.FloatStack[1];
|
---|
255 | interpreter.FloatStack.Remove(2);
|
---|
256 |
|
---|
257 | var result = second > first;
|
---|
258 | interpreter.BooleanStack.Push(result);
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | /// <summary>
|
---|
263 | /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than or equal to the top item, or FALSE otherwise.
|
---|
264 | /// </summary>
|
---|
265 | [PushExpression(StackTypes.Float, "FLOAT.>=", StackTypes.Boolean)]
|
---|
266 | [StorableClass]
|
---|
267 | public class FloatGreaterThanOrEqualExpression : StatelessExpression {
|
---|
268 | public FloatGreaterThanOrEqualExpression() { }
|
---|
269 | [StorableConstructor]
|
---|
270 | protected FloatGreaterThanOrEqualExpression(bool deserializing) : base(deserializing) { }
|
---|
271 |
|
---|
272 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
273 | return interpreter.FloatStack.Count < 2;
|
---|
274 | }
|
---|
275 |
|
---|
276 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
277 | var first = interpreter.FloatStack.Top;
|
---|
278 | var second = interpreter.FloatStack[1];
|
---|
279 | interpreter.FloatStack.Remove(2);
|
---|
280 |
|
---|
281 | var result = second > first || second.IsAlmost(first);
|
---|
282 | interpreter.BooleanStack.Push(result);
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | /// <summary>
|
---|
287 | /// Pushes the sine of the top item.
|
---|
288 | /// </summary>
|
---|
289 | [PushExpression(StackTypes.Float, "FLOAT.SIN")]
|
---|
290 | [StorableClass]
|
---|
291 | public class FloatSineExpression : StatelessExpression {
|
---|
292 | public FloatSineExpression() { }
|
---|
293 | [StorableConstructor]
|
---|
294 | protected FloatSineExpression(bool deserializing) : base(deserializing) { }
|
---|
295 |
|
---|
296 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
297 | return interpreter.FloatStack.IsEmpty;
|
---|
298 | }
|
---|
299 |
|
---|
300 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
301 | var result = Math.Sin(interpreter.FloatStack.Top);
|
---|
302 | interpreter.FloatStack.Top = result;
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | /// <summary>
|
---|
307 | /// Pushes the cosine of the top item.
|
---|
308 | /// </summary>
|
---|
309 | [PushExpression(StackTypes.Float, "FLOAT.COS")]
|
---|
310 | public class FloatCosineExpression : StatelessExpression {
|
---|
311 | public FloatCosineExpression() { }
|
---|
312 | [StorableConstructor]
|
---|
313 | protected FloatCosineExpression(bool deserializing) : base(deserializing) { }
|
---|
314 |
|
---|
315 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
316 | return interpreter.FloatStack.IsEmpty;
|
---|
317 | }
|
---|
318 |
|
---|
319 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
320 | var result = Math.Cos(interpreter.FloatStack.Top);
|
---|
321 | interpreter.FloatStack.Top = result;
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | /// <summary>
|
---|
326 | /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
|
---|
327 | /// </summary>
|
---|
328 | [PushExpression(StackTypes.Float, "FLOAT.FROMBOOLEAN", StackTypes.Boolean)]
|
---|
329 | [StorableClass]
|
---|
330 | public class FloatFromBooleanExpression : StatelessExpression {
|
---|
331 | public FloatFromBooleanExpression() { }
|
---|
332 | [StorableConstructor]
|
---|
333 | protected FloatFromBooleanExpression(bool deserializing) : base(deserializing) { }
|
---|
334 |
|
---|
335 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
336 | return interpreter.BooleanStack.IsEmpty;
|
---|
337 | }
|
---|
338 |
|
---|
339 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
340 | var condition = interpreter.BooleanStack.Pop();
|
---|
341 | var value = condition ? 1 : 0;
|
---|
342 |
|
---|
343 | interpreter.FloatStack.Push(value);
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 | /// <summary>
|
---|
348 | /// Pushes a floating point version of the top INTEGER.
|
---|
349 | /// </summary>
|
---|
350 | [PushExpression(StackTypes.Float, "FLOAT.FROMINTEGER", StackTypes.Integer)]
|
---|
351 | [StorableClass]
|
---|
352 | public class FloatFromIntegerExpression : StatelessExpression {
|
---|
353 | public FloatFromIntegerExpression() { }
|
---|
354 | [StorableConstructor]
|
---|
355 | protected FloatFromIntegerExpression(bool deserializing) : base(deserializing) { }
|
---|
356 |
|
---|
357 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
358 | return interpreter.IntegerStack.IsEmpty;
|
---|
359 | }
|
---|
360 |
|
---|
361 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
362 | var value = (double)interpreter.IntegerStack.Pop();
|
---|
363 | interpreter.FloatStack.Push(value);
|
---|
364 | }
|
---|
365 | }
|
---|
366 |
|
---|
367 | /// <summary>
|
---|
368 | /// Pushes the result of truncating the top CHAR.
|
---|
369 | /// </summary>
|
---|
370 | [PushExpression(StackTypes.Float, "FLOAT.FROMCHAR", StackTypes.Char)]
|
---|
371 | [StorableClass]
|
---|
372 | public class FloatFromCharExpression : StatelessExpression {
|
---|
373 | public FloatFromCharExpression() { }
|
---|
374 | [StorableConstructor]
|
---|
375 | protected FloatFromCharExpression(bool deserializing) : base(deserializing) { }
|
---|
376 |
|
---|
377 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
378 | return interpreter.CharStack.IsEmpty;
|
---|
379 | }
|
---|
380 |
|
---|
381 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
382 | var value = interpreter.CharStack.Pop();
|
---|
383 | interpreter.FloatStack.Push(value);
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | /// <summary>
|
---|
388 | /// Pushes the result of parsing the top STRING.
|
---|
389 | /// </summary>
|
---|
390 | [PushExpression(StackTypes.Float, "FLOAT.FROMSTRING", StackTypes.String)]
|
---|
391 | [StorableClass]
|
---|
392 | public class FloatFromStringExpression : StatelessExpression {
|
---|
393 | public FloatFromStringExpression() { }
|
---|
394 | [StorableConstructor]
|
---|
395 | protected FloatFromStringExpression(bool deserializing) : base(deserializing) { }
|
---|
396 |
|
---|
397 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
398 | double tmp;
|
---|
399 | return interpreter.StringStack.IsEmpty ||
|
---|
400 | interpreter.StringStack.Top.Length == 0 ||
|
---|
401 | !double.TryParse(interpreter.StringStack.Top, out tmp);
|
---|
402 | }
|
---|
403 |
|
---|
404 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
405 | var str = interpreter.StringStack.Pop();
|
---|
406 | var value = double.Parse(str, CultureInfo.InvariantCulture);
|
---|
407 |
|
---|
408 | interpreter.FloatStack.Push(value);
|
---|
409 | }
|
---|
410 | }
|
---|
411 |
|
---|
412 | /// <summary>
|
---|
413 | /// Pushes the result of increasing the top INTEGER by 1.
|
---|
414 | /// </summary>
|
---|
415 | [PushExpression(StackTypes.Float, "FLOAT.INC")]
|
---|
416 | [StorableClass]
|
---|
417 | public class FloatIncExpression : StatelessExpression {
|
---|
418 | public FloatIncExpression() { }
|
---|
419 | [StorableConstructor]
|
---|
420 | protected FloatIncExpression(bool deserializing) : base(deserializing) { }
|
---|
421 |
|
---|
422 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
423 | return interpreter.FloatStack.IsEmpty;
|
---|
424 | }
|
---|
425 |
|
---|
426 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
427 | interpreter.FloatStack.Top += 1;
|
---|
428 | }
|
---|
429 | }
|
---|
430 |
|
---|
431 | /// <summary>
|
---|
432 | /// Pushes the result of decreasing the top INTEGER by 1.
|
---|
433 | /// </summary>
|
---|
434 | [PushExpression(StackTypes.Float, "FLOAT.DEC")]
|
---|
435 | [StorableClass]
|
---|
436 | public class FloatDecExpression : StatelessExpression {
|
---|
437 | public FloatDecExpression() { }
|
---|
438 | [StorableConstructor]
|
---|
439 | protected FloatDecExpression(bool deserializing) : base(deserializing) { }
|
---|
440 |
|
---|
441 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
442 | return interpreter.FloatStack.IsEmpty;
|
---|
443 | }
|
---|
444 |
|
---|
445 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
446 | interpreter.FloatStack.Top -= 1;
|
---|
447 | }
|
---|
448 | }
|
---|
449 | } |
---|