1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
2 | using System;
|
---|
3 |
|
---|
4 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
5 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
6 |
|
---|
7 | using Interpreter;
|
---|
8 |
|
---|
9 | /// <summary>
|
---|
10 | /// Pushes the sum of the top two items.
|
---|
11 | /// </summary>
|
---|
12 | [PushExpression(StackTypes.Integer, "INTEGER.+")]
|
---|
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;
|
---|
24 | }
|
---|
25 | }
|
---|
26 |
|
---|
27 | /// <summary>
|
---|
28 | /// Pushes the difference of the top two items; that is, the second item minus the top item.
|
---|
29 | /// </summary>
|
---|
30 | [PushExpression(StackTypes.Integer, "INTEGER.-")]
|
---|
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;
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | /// <summary>
|
---|
46 | /// Pushes the product of the top two items.
|
---|
47 | /// </summary>
|
---|
48 | [PushExpression(StackTypes.Integer, "INTEGER.*")]
|
---|
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;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | /// <summary>
|
---|
64 | /// Pushes the quotient of the top two items; that is, the second item divided by the top item.
|
---|
65 | /// If the top item is zero this acts as a NOOP.
|
---|
66 | /// </summary>
|
---|
67 | [PushExpression(StackTypes.Integer, "INTEGER./")]
|
---|
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;
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | /// <summary>
|
---|
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
|
---|
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
|
---|
87 | /// http://www.lispworks.com/reference/HyperSpec/Body/f_mod_r.htm.)
|
---|
88 | /// </summary>
|
---|
89 | [PushExpression(StackTypes.Integer, "INTEGER.%")]
|
---|
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;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /// <summary>
|
---|
106 | /// Pushes the minimum of the top two items.
|
---|
107 | /// </summary>
|
---|
108 | [PushExpression(StackTypes.Integer, "INTEGER.MIN")]
|
---|
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;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | /// <summary>
|
---|
124 | /// Pushes the maximum of the top two items.
|
---|
125 | /// </summary>
|
---|
126 | [PushExpression(StackTypes.Integer, "INTEGER.MAX")]
|
---|
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;
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | /// <summary>
|
---|
142 | /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.
|
---|
143 | /// </summary>
|
---|
144 | [PushExpression(StackTypes.Integer, "INTEGER.<", StackTypes.Boolean)]
|
---|
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;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /// <summary>
|
---|
161 | /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.
|
---|
162 | /// </summary>
|
---|
163 | [PushExpression(StackTypes.Integer, "INTEGER.>", StackTypes.Boolean)]
|
---|
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;
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | /// <summary>
|
---|
180 | /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.
|
---|
181 | /// </summary>
|
---|
182 | [PushExpression(StackTypes.Integer, "INTEGER.FROMBOOLEAN", StackTypes.Boolean)]
|
---|
183 | public class IntegerFromBooleanExpression : StatelessExpression {
|
---|
184 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
185 | if (interpreter.BooleanStack.Count == 0)
|
---|
186 | return false;
|
---|
187 |
|
---|
188 | var condition = interpreter.BooleanStack.Pop();
|
---|
189 | var value = condition ? 1 : 0;
|
---|
190 | interpreter.IntegerStack.Push(value);
|
---|
191 | return true;
|
---|
192 | }
|
---|
193 | }
|
---|
194 |
|
---|
195 | /// <summary>
|
---|
196 | /// Pushes the result of truncating the top FLOAT.
|
---|
197 | /// </summary>
|
---|
198 | [PushExpression(StackTypes.Integer, "INTEGER.FROMFLOAT", StackTypes.Float)]
|
---|
199 | public class IntegerFromFloatExpression : StatelessExpression {
|
---|
200 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
201 | if (interpreter.FloatStack.Count == 0)
|
---|
202 | return false;
|
---|
203 |
|
---|
204 | var value = (int)interpreter.FloatStack.Pop();
|
---|
205 | interpreter.IntegerStack.Push(value);
|
---|
206 | return true;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | } |
---|