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
|
---|
14 | /// EXEC stack.
|
---|
15 | /// </summary>
|
---|
16 | /// <typeparam name="T">Stacktype</typeparam>
|
---|
17 | [StorableClass]
|
---|
18 | public abstract class DefineExpression<T> : StatelessExpression {
|
---|
19 | protected DefineExpression() { }
|
---|
20 | [StorableConstructor]
|
---|
21 | protected DefineExpression(bool deserializing) : base(deserializing) { }
|
---|
22 |
|
---|
23 | protected void Eval(
|
---|
24 | IPushStack<T> stack,
|
---|
25 | IPushStack<string> nameStack,
|
---|
26 | IDictionary<string, Expression> customExpressions,
|
---|
27 | Func<T, Expression> creator) {
|
---|
28 | var name = nameStack.Pop();
|
---|
29 | var expression = creator(stack.Top);
|
---|
30 |
|
---|
31 | if (customExpressions.ContainsKey(name)) customExpressions[name] = expression;
|
---|
32 | else customExpressions.Add(name, expression);
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | [PushExpression(StackTypes.Code, "CODE.DEFINE", StackTypes.Name)]
|
---|
37 | [StorableClass]
|
---|
38 | public class CodeDefineExpression : DefineExpression<Expression> {
|
---|
39 | public CodeDefineExpression() { }
|
---|
40 | [StorableConstructor]
|
---|
41 | protected CodeDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
42 |
|
---|
43 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
44 | return (interpreter.Configuration.TopLevelPushCode && interpreter.CodeStack.Count < 2) ||
|
---|
45 | interpreter.CodeStack.IsEmpty ||
|
---|
46 | interpreter.NameStack.IsEmpty;
|
---|
47 | }
|
---|
48 |
|
---|
49 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
50 | Eval(
|
---|
51 | interpreter.CodeStack,
|
---|
52 | interpreter.NameStack,
|
---|
53 | interpreter.CustomExpressions,
|
---|
54 | v => new ExecPushExpression(v));
|
---|
55 |
|
---|
56 | interpreter.CodeStack.Pop();
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | [PushExpression(StackTypes.Exec, "EXEC.DEFINE", StackTypes.Name)]
|
---|
61 | [StorableClass]
|
---|
62 | public class ExecDefineExpression : DefineExpression<Expression> {
|
---|
63 | public ExecDefineExpression() { }
|
---|
64 | [StorableConstructor]
|
---|
65 | protected ExecDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
66 |
|
---|
67 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
68 | return interpreter.ExecStack.Count < 2 ||
|
---|
69 | interpreter.NameStack.IsEmpty;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
73 | Eval(
|
---|
74 | interpreter.ExecStack,
|
---|
75 | interpreter.NameStack,
|
---|
76 | interpreter.CustomExpressions,
|
---|
77 | v => new ExecPushExpression(v));
|
---|
78 |
|
---|
79 | interpreter.ExecStack.Pop();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | [PushExpression(StackTypes.Float, "FLOAT.DEFINE", StackTypes.Name)]
|
---|
84 | [StorableClass]
|
---|
85 | public class FloatDefineExpression : DefineExpression<double> {
|
---|
86 | public FloatDefineExpression() { }
|
---|
87 | [StorableConstructor]
|
---|
88 | protected FloatDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
89 |
|
---|
90 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
91 | return interpreter.FloatStack.IsEmpty ||
|
---|
92 | interpreter.NameStack.IsEmpty;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
96 | Eval(
|
---|
97 | interpreter.FloatStack,
|
---|
98 | interpreter.NameStack,
|
---|
99 | interpreter.CustomExpressions,
|
---|
100 | v => new FloatPushExpression(v));
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | [PushExpression(StackTypes.Integer, "INTEGER.DEFINE", StackTypes.Name)]
|
---|
105 | [StorableClass]
|
---|
106 | public class IntegerDefineExpression : DefineExpression<long> {
|
---|
107 | public IntegerDefineExpression() { }
|
---|
108 | [StorableConstructor]
|
---|
109 | protected IntegerDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
110 |
|
---|
111 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
112 | return interpreter.IntegerStack.IsEmpty ||
|
---|
113 | interpreter.NameStack.IsEmpty;
|
---|
114 | }
|
---|
115 |
|
---|
116 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
117 | Eval(
|
---|
118 | interpreter.IntegerStack,
|
---|
119 | interpreter.NameStack,
|
---|
120 | interpreter.CustomExpressions,
|
---|
121 | v => new IntegerPushExpression(v));
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | [PushExpression(StackTypes.Boolean, "BOOLEAN.DEFINE", StackTypes.Name)]
|
---|
126 | [StorableClass]
|
---|
127 | public class BooleanDefineExpression : DefineExpression<bool> {
|
---|
128 | public BooleanDefineExpression() { }
|
---|
129 | [StorableConstructor]
|
---|
130 | protected BooleanDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
131 |
|
---|
132 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
133 | return interpreter.BooleanStack.IsEmpty ||
|
---|
134 | interpreter.NameStack.IsEmpty;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
138 | Eval(
|
---|
139 | interpreter.BooleanStack,
|
---|
140 | interpreter.NameStack,
|
---|
141 | interpreter.CustomExpressions,
|
---|
142 | v => new BooleanPushExpression(v));
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | [PushExpression(StackTypes.Char, "CHAR.DEFINE", StackTypes.Name)]
|
---|
147 | [StorableClass]
|
---|
148 | public class CharDefineExpression : DefineExpression<char> {
|
---|
149 | public CharDefineExpression() { }
|
---|
150 | [StorableConstructor]
|
---|
151 | protected CharDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
152 |
|
---|
153 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
154 | return interpreter.CharStack.IsEmpty ||
|
---|
155 | interpreter.NameStack.IsEmpty;
|
---|
156 | }
|
---|
157 |
|
---|
158 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
159 | Eval(
|
---|
160 | interpreter.CharStack,
|
---|
161 | interpreter.NameStack,
|
---|
162 | interpreter.CustomExpressions,
|
---|
163 | v => new CharPushExpression(v));
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | [PushExpression(StackTypes.String, "STRING.DEFINE", StackTypes.Name)]
|
---|
168 | [StorableClass]
|
---|
169 | public class StringDefineExpression : DefineExpression<string> {
|
---|
170 | public StringDefineExpression() { }
|
---|
171 | [StorableConstructor]
|
---|
172 | protected StringDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
173 |
|
---|
174 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
175 | return interpreter.StringStack.IsEmpty ||
|
---|
176 | interpreter.NameStack.IsEmpty;
|
---|
177 | }
|
---|
178 |
|
---|
179 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
180 | Eval(
|
---|
181 | interpreter.StringStack,
|
---|
182 | interpreter.NameStack,
|
---|
183 | interpreter.CustomExpressions,
|
---|
184 | v => new StringPushExpression(v));
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | [PushExpression(StackTypes.IntegerVector, "INTEGER[].DEFINE", StackTypes.Name)]
|
---|
189 | [StorableClass]
|
---|
190 | public class IntegerVectorDefineExpression : DefineExpression<List<long>> {
|
---|
191 | public IntegerVectorDefineExpression() { }
|
---|
192 | [StorableConstructor]
|
---|
193 | protected IntegerVectorDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
194 |
|
---|
195 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
196 | return interpreter.IntegerVectorStack.IsEmpty ||
|
---|
197 | interpreter.NameStack.IsEmpty;
|
---|
198 | }
|
---|
199 |
|
---|
200 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
201 | Eval(
|
---|
202 | interpreter.IntegerVectorStack,
|
---|
203 | interpreter.NameStack,
|
---|
204 | interpreter.CustomExpressions,
|
---|
205 | v => new IntegerVectorPushExpression(v));
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | [PushExpression(StackTypes.FloatVector, "FLOAT[].DEFINE", StackTypes.Name)]
|
---|
210 | [StorableClass]
|
---|
211 | public class FloatVectorDefineExpression : DefineExpression<List<double>> {
|
---|
212 | public FloatVectorDefineExpression() { }
|
---|
213 | [StorableConstructor]
|
---|
214 | protected FloatVectorDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
215 |
|
---|
216 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
217 | return interpreter.FloatVectorStack.IsEmpty ||
|
---|
218 | interpreter.NameStack.IsEmpty;
|
---|
219 | }
|
---|
220 |
|
---|
221 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
222 | Eval(
|
---|
223 | interpreter.FloatVectorStack,
|
---|
224 | interpreter.NameStack,
|
---|
225 | interpreter.CustomExpressions,
|
---|
226 | v => new FloatVectorPushExpression(v));
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].DEFINE", StackTypes.Name)]
|
---|
231 | [StorableClass]
|
---|
232 | public class BooleanVectorDefineExpression : DefineExpression<List<bool>> {
|
---|
233 | public BooleanVectorDefineExpression() { }
|
---|
234 | [StorableConstructor]
|
---|
235 | protected BooleanVectorDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
236 |
|
---|
237 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
238 | return interpreter.BooleanVectorStack.IsEmpty ||
|
---|
239 | interpreter.NameStack.IsEmpty;
|
---|
240 | }
|
---|
241 |
|
---|
242 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
243 | Eval(
|
---|
244 | interpreter.BooleanVectorStack,
|
---|
245 | interpreter.NameStack,
|
---|
246 | interpreter.CustomExpressions,
|
---|
247 | v => new BooleanVectorPushExpression(v));
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | [PushExpression(StackTypes.StringVector, "STRING[].DEFINE", StackTypes.Name)]
|
---|
252 | [StorableClass]
|
---|
253 | public class StringVectorDefineExpression : DefineExpression<List<string>> {
|
---|
254 | public StringVectorDefineExpression() { }
|
---|
255 | [StorableConstructor]
|
---|
256 | protected StringVectorDefineExpression(bool deserializing) : base(deserializing) { }
|
---|
257 |
|
---|
258 | public override bool IsNoop(IInternalPushInterpreter interpreter) {
|
---|
259 | return interpreter.StringVectorStack.IsEmpty ||
|
---|
260 | interpreter.NameStack.IsEmpty;
|
---|
261 | }
|
---|
262 |
|
---|
263 | public override void Eval(IInternalPushInterpreter interpreter) {
|
---|
264 | Eval(
|
---|
265 | interpreter.StringVectorStack,
|
---|
266 | interpreter.NameStack,
|
---|
267 | interpreter.CustomExpressions,
|
---|
268 | v => new StringVectorPushExpression(v));
|
---|
269 | }
|
---|
270 | }
|
---|
271 | } |
---|