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