1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 |
|
---|
5 | using Attributes;
|
---|
6 |
|
---|
7 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
8 |
|
---|
9 | using Interpreter;
|
---|
10 | using Stack;
|
---|
11 |
|
---|
12 | /// <summary>
|
---|
13 | /// Defines the name on top of the NAME stack as an instruction that will push the top item of the T stack onto the EXEC stack.
|
---|
14 | /// </summary>
|
---|
15 | /// <typeparam name="T">Stack item type</typeparam>
|
---|
16 | [StorableClass]
|
---|
17 | public abstract class DefineExpression<T> : StatelessExpression {
|
---|
18 | protected DefineExpression() { }
|
---|
19 | [StorableConstructor]
|
---|
20 | protected DefineExpression(bool deserializing) : base(deserializing) { }
|
---|
21 |
|
---|
22 | protected void Eval(
|
---|
23 | IPushStack<T> stack,
|
---|
24 | IPushStack<string> nameStack,
|
---|
25 | IDictionary<string, Expression> customExpressions,
|
---|
26 | Func<T, Expression> creator) {
|
---|
27 | var name = nameStack.Pop();
|
---|
28 | var expression = creator(stack.Top);
|
---|
29 |
|
---|
30 | if (customExpressions.ContainsKey(name)) customExpressions[name] = expression;
|
---|
31 | else customExpressions.Add(name, expression);
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | [PushExpression(
|
---|
36 | StackTypes.Code,
|
---|
37 | "CODE.DEFINE",
|
---|
38 | "Defines the name on top of the NAME stack as an instruction that will push the top item of the CODE stack onto the EXEC stack.",
|
---|
39 | StackTypes.Name)]
|
---|
40 | [StorableClass]
|
---|
41 | public class CodeDefineExpression : DefineExpression<Expression> {
|
---|
42 | public CodeDefineExpression() { }
|
---|
43 | [StorableConstructor]
|
---|
44 | protected CodeDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
45 |
|
---|
46 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
47 | return (interpreter.Configuration.TopLevelPushCode && interpreter.CodeStack.Count < 2) ||
|
---|
48 | interpreter.CodeStack.IsEmpty ||
|
---|
49 | interpreter.NameStack.IsEmpty;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
53 | var pool = interpreter.PoolContainer.GetStatefulExpressionPool<ExecPushExpression>();
|
---|
54 |
|
---|
55 | Eval(
|
---|
56 | interpreter.CodeStack,
|
---|
57 | interpreter.NameStack,
|
---|
58 | interpreter.CustomExpressions,
|
---|
59 | v => ExecPushExpression.Create(pool, v));
|
---|
60 |
|
---|
61 | interpreter.CodeStack.Pop();
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | [PushExpression(
|
---|
66 | StackTypes.Exec,
|
---|
67 | "EXEC.DEFINE",
|
---|
68 | "Defines the name on top of the NAME stack as an instruction that will push the top item of the EXEC stack onto the EXEC stack.",
|
---|
69 | StackTypes.Name,
|
---|
70 | requiredBlockCount: 1)]
|
---|
71 | [StorableClass]
|
---|
72 | public class ExecDefineExpression : DefineExpression<Expression> {
|
---|
73 | public ExecDefineExpression() { }
|
---|
74 | [StorableConstructor]
|
---|
75 | protected ExecDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
76 |
|
---|
77 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
78 | return interpreter.ExecStack.Count < 2 ||
|
---|
79 | interpreter.NameStack.IsEmpty;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
83 | var pool = interpreter.PoolContainer.GetStatefulExpressionPool<ExecPushExpression>();
|
---|
84 |
|
---|
85 | Eval(
|
---|
86 | interpreter.ExecStack,
|
---|
87 | interpreter.NameStack,
|
---|
88 | interpreter.CustomExpressions,
|
---|
89 | v => ExecPushExpression.Create(pool, v));
|
---|
90 |
|
---|
91 | interpreter.ExecStack.Pop();
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | [PushExpression(
|
---|
96 | StackTypes.Float,
|
---|
97 | "FLOAT.DEFINE",
|
---|
98 | "Defines the name on top of the NAME stack as an instruction that will push the top item of the FLOAT stack onto the EXEC stack.",
|
---|
99 | StackTypes.Name)]
|
---|
100 | [StorableClass]
|
---|
101 | public class FloatDefineExpression : DefineExpression<double> {
|
---|
102 | public FloatDefineExpression() { }
|
---|
103 | [StorableConstructor]
|
---|
104 | protected FloatDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
105 |
|
---|
106 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
107 | return interpreter.FloatStack.IsEmpty ||
|
---|
108 | interpreter.NameStack.IsEmpty;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
112 | var pool = interpreter.PoolContainer.GetStatefulExpressionPool<FloatPushExpression>();
|
---|
113 |
|
---|
114 | Eval(
|
---|
115 | interpreter.FloatStack,
|
---|
116 | interpreter.NameStack,
|
---|
117 | interpreter.CustomExpressions,
|
---|
118 | v => FloatPushExpression.Create(pool, v));
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | [PushExpression(
|
---|
123 | StackTypes.Integer,
|
---|
124 | "INTEGER.DEFINE",
|
---|
125 | "Defines the name on top of the NAME stack as an instruction that will push the top item of the INTEGER stack onto the EXEC stack.",
|
---|
126 | StackTypes.Name)]
|
---|
127 | [StorableClass]
|
---|
128 | public class IntegerDefineExpression : DefineExpression<long> {
|
---|
129 | public IntegerDefineExpression() { }
|
---|
130 | [StorableConstructor]
|
---|
131 | protected IntegerDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
132 |
|
---|
133 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
134 | return interpreter.IntegerStack.IsEmpty ||
|
---|
135 | interpreter.NameStack.IsEmpty;
|
---|
136 | }
|
---|
137 |
|
---|
138 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
139 | var pool = interpreter.PoolContainer.GetStatefulExpressionPool<IntegerPushExpression>();
|
---|
140 |
|
---|
141 | Eval(
|
---|
142 | interpreter.IntegerStack,
|
---|
143 | interpreter.NameStack,
|
---|
144 | interpreter.CustomExpressions,
|
---|
145 | v => IntegerPushExpression.Create(pool, v));
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | [PushExpression(
|
---|
150 | StackTypes.Boolean,
|
---|
151 | "BOOLEAN.DEFINE",
|
---|
152 | "Defines the name on top of the NAME stack as an instruction that will push the top item of the BOOLEAN stack onto the EXEC stack.",
|
---|
153 | StackTypes.Name)]
|
---|
154 | [StorableClass]
|
---|
155 | public class BooleanDefineExpression : DefineExpression<bool> {
|
---|
156 | public BooleanDefineExpression() { }
|
---|
157 | [StorableConstructor]
|
---|
158 | protected BooleanDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
159 |
|
---|
160 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
161 | return interpreter.BooleanStack.IsEmpty ||
|
---|
162 | interpreter.NameStack.IsEmpty;
|
---|
163 | }
|
---|
164 |
|
---|
165 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
166 | var pool = interpreter.PoolContainer.GetStatefulExpressionPool<BooleanPushExpression>();
|
---|
167 |
|
---|
168 | Eval(
|
---|
169 | interpreter.BooleanStack,
|
---|
170 | interpreter.NameStack,
|
---|
171 | interpreter.CustomExpressions,
|
---|
172 | v => BooleanPushExpression.Create(pool, v));
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | [PushExpression(
|
---|
177 | StackTypes.Char,
|
---|
178 | "CHAR.DEFINE",
|
---|
179 | "Defines the name on top of the NAME stack as an instruction that will push the top item of the CHAR stack onto the EXEC stack.",
|
---|
180 | StackTypes.Name)]
|
---|
181 | [StorableClass]
|
---|
182 | public class CharDefineExpression : DefineExpression<char> {
|
---|
183 | public CharDefineExpression() { }
|
---|
184 | [StorableConstructor]
|
---|
185 | protected CharDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
186 |
|
---|
187 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
188 | return interpreter.CharStack.IsEmpty ||
|
---|
189 | interpreter.NameStack.IsEmpty;
|
---|
190 | }
|
---|
191 |
|
---|
192 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
193 | var pool = interpreter.PoolContainer.GetStatefulExpressionPool<CharPushExpression>();
|
---|
194 |
|
---|
195 | Eval(
|
---|
196 | interpreter.CharStack,
|
---|
197 | interpreter.NameStack,
|
---|
198 | interpreter.CustomExpressions,
|
---|
199 | v => CharPushExpression.Create(pool, v));
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | [PushExpression(
|
---|
204 | StackTypes.String,
|
---|
205 | "STRING.DEFINE",
|
---|
206 | "Defines the name on top of the NAME stack as an instruction that will push the top item of the STRING stack onto the EXEC stack.",
|
---|
207 | StackTypes.Name)]
|
---|
208 | [StorableClass]
|
---|
209 | public class StringDefineExpression : DefineExpression<string> {
|
---|
210 | public StringDefineExpression() { }
|
---|
211 | [StorableConstructor]
|
---|
212 | protected StringDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
213 |
|
---|
214 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
215 | return interpreter.StringStack.IsEmpty ||
|
---|
216 | interpreter.NameStack.IsEmpty;
|
---|
217 | }
|
---|
218 |
|
---|
219 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
220 | var pool = interpreter.PoolContainer.GetStatefulExpressionPool<StringPushExpression>();
|
---|
221 |
|
---|
222 | Eval(
|
---|
223 | interpreter.StringStack,
|
---|
224 | interpreter.NameStack,
|
---|
225 | interpreter.CustomExpressions,
|
---|
226 | v => StringPushExpression.Create(pool, v));
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | [PushExpression(
|
---|
231 | StackTypes.IntegerVector,
|
---|
232 | "INTEGER[].DEFINE",
|
---|
233 | "Defines the name on top of the NAME stack as an instruction that will push the top item of the INTEGER[] stack onto the EXEC stack.",
|
---|
234 | StackTypes.Name)]
|
---|
235 | [StorableClass]
|
---|
236 | public class IntegerVectorDefineExpression : DefineExpression<IReadOnlyList<long>> {
|
---|
237 | public IntegerVectorDefineExpression() { }
|
---|
238 | [StorableConstructor]
|
---|
239 | protected IntegerVectorDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
240 |
|
---|
241 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
242 | return interpreter.IntegerVectorStack.IsEmpty ||
|
---|
243 | interpreter.NameStack.IsEmpty;
|
---|
244 | }
|
---|
245 |
|
---|
246 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
247 | var pool = interpreter.PoolContainer.GetStatefulExpressionPool<IntegerVectorPushExpression>();
|
---|
248 |
|
---|
249 | Eval(
|
---|
250 | interpreter.IntegerVectorStack,
|
---|
251 | interpreter.NameStack,
|
---|
252 | interpreter.CustomExpressions,
|
---|
253 | v => IntegerVectorPushExpression.Create(pool, v));
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | [PushExpression(
|
---|
258 | StackTypes.FloatVector,
|
---|
259 | "FLOAT[].DEFINE",
|
---|
260 | "Defines the name on top of the NAME stack as an instruction that will push the top item of the FLOAT[] stack onto the EXEC stack.",
|
---|
261 | StackTypes.Name)]
|
---|
262 | [StorableClass]
|
---|
263 | public class FloatVectorDefineExpression : DefineExpression<IReadOnlyList<double>> {
|
---|
264 | public FloatVectorDefineExpression() { }
|
---|
265 | [StorableConstructor]
|
---|
266 | protected FloatVectorDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
267 |
|
---|
268 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
269 | return interpreter.FloatVectorStack.IsEmpty ||
|
---|
270 | interpreter.NameStack.IsEmpty;
|
---|
271 | }
|
---|
272 |
|
---|
273 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
274 | var pool = interpreter.PoolContainer.GetStatefulExpressionPool<FloatVectorPushExpression>();
|
---|
275 |
|
---|
276 | Eval(
|
---|
277 | interpreter.FloatVectorStack,
|
---|
278 | interpreter.NameStack,
|
---|
279 | interpreter.CustomExpressions,
|
---|
280 | v => FloatVectorPushExpression.Create(pool, v));
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | [PushExpression(
|
---|
285 | StackTypes.BooleanVector,
|
---|
286 | "BOOLEAN[].DEFINE",
|
---|
287 | "Defines the name on top of the NAME stack as an instruction that will push the top item of the BOOLEAN[] stack onto the EXEC stack.",
|
---|
288 | StackTypes.Name)]
|
---|
289 | [StorableClass]
|
---|
290 | public class BooleanVectorDefineExpression : DefineExpression<IReadOnlyList<bool>> {
|
---|
291 | public BooleanVectorDefineExpression() { }
|
---|
292 | [StorableConstructor]
|
---|
293 | protected BooleanVectorDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
294 |
|
---|
295 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
296 | return interpreter.BooleanVectorStack.IsEmpty ||
|
---|
297 | interpreter.NameStack.IsEmpty;
|
---|
298 | }
|
---|
299 |
|
---|
300 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
301 | var pool = interpreter.PoolContainer.GetStatefulExpressionPool<BooleanVectorPushExpression>();
|
---|
302 |
|
---|
303 | Eval(
|
---|
304 | interpreter.BooleanVectorStack,
|
---|
305 | interpreter.NameStack,
|
---|
306 | interpreter.CustomExpressions,
|
---|
307 | v => BooleanVectorPushExpression.Create(pool, v));
|
---|
308 | }
|
---|
309 | }
|
---|
310 |
|
---|
311 | [PushExpression(
|
---|
312 | StackTypes.StringVector,
|
---|
313 | "STRING[].DEFINE",
|
---|
314 | "Defines the name on top of the NAME stack as an instruction that will push the top item of the STRING[] stack onto the EXEC stack.",
|
---|
315 | StackTypes.Name)]
|
---|
316 | [StorableClass]
|
---|
317 | public class StringVectorDefineExpression : DefineExpression<IReadOnlyList<string>> {
|
---|
318 | public StringVectorDefineExpression() { }
|
---|
319 | [StorableConstructor]
|
---|
320 | protected StringVectorDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
321 |
|
---|
322 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
323 | return interpreter.StringVectorStack.IsEmpty ||
|
---|
324 | interpreter.NameStack.IsEmpty;
|
---|
325 | }
|
---|
326 |
|
---|
327 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
328 | var pool = interpreter.PoolContainer.GetStatefulExpressionPool<BooleanVectorPushExpression>();
|
---|
329 |
|
---|
330 | Eval(
|
---|
331 | interpreter.StringVectorStack,
|
---|
332 | interpreter.NameStack,
|
---|
333 | interpreter.CustomExpressions,
|
---|
334 | v => StringVectorPushExpression.Create(pool, v));
|
---|
335 | }
|
---|
336 | }
|
---|
337 | } |
---|