Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PrintExpressions.cs @ 16958

Last change on this file since 16958 was 15334, checked in by pkimmesw, 7 years ago

#2665 Testet Problems, Testet error functions, Small fixes, Created HL files

File size: 9.4 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System.Collections.Generic;
3  using System.Globalization;
4  using Attributes;
5  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Constants;
7
8  using Interpreter;
9  using Stack;
10
11  [StorableClass]
12  [PushExpression(StackTypes.Print, "PRINT.NEWLINE", "Pushes an empty string onto the PRINT STACK.")]
13  public class PrintNewLineExpression : StatelessExpression {
14    public PrintNewLineExpression() { }
15    [StorableConstructor]
16    protected PrintNewLineExpression(bool deserializing) : base(deserializing) { }
17
18    public override bool IsNoop(IInternalPushInterpreter interpreter) {
19      return false;
20    }
21
22    public override void Eval(IInternalPushInterpreter interpreter) {
23      interpreter.PrintStack.NewLine();
24    }
25  }
26
27  [StorableClass]
28  [PushExpression(StackTypes.Print, "PRINT.ENSURE_NEWLINE", "Pushes an empty string onto the PRINT STACK if current line isn't empty already.")]
29  public class PrintEnsureNewLineExpression : StatelessExpression {
30    public PrintEnsureNewLineExpression() { }
31    [StorableConstructor]
32    protected PrintEnsureNewLineExpression(bool deserializing) : base(deserializing) { }
33
34    public override bool IsNoop(IInternalPushInterpreter interpreter) {
35      return interpreter.PrintStack.IsCurrentLineEmpty;
36    }
37
38    public override void Eval(IInternalPushInterpreter interpreter) {
39      interpreter.PrintStack.NewLine();
40    }
41  }
42
43  [StorableClass]
44  public abstract class PrintExpression<T> : StatelessExpression {
45    protected PrintExpression() { }
46
47    [StorableConstructor]
48    protected PrintExpression(bool deserializing) : base(deserializing) { }
49
50    protected void Eval(IInternalPushInterpreter interpreter, IPushStack<T> stack) {
51      var value = stack.Pop();
52
53      interpreter.PrintStack.Push(value);
54    }
55
56    protected void EvalVector(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack) {
57      var vector = vectorStack.Pop();
58
59      interpreter.PrintStack.Push(PushEnvironment.VectorStartSymbol);
60
61      if (vector.Count > 0) {
62        interpreter.PrintStack.Push(vector[0]);
63
64        for (var i = 1; i < vector.Count; i++) {
65          interpreter.PrintStack.Push(PushEnvironment.VectorSeparatorSymbol);
66          interpreter.PrintStack.Push(vector[i]);
67        }
68      }
69
70      interpreter.PrintStack.Push(PushEnvironment.VectorEndSymbol);
71    }
72  }
73
74  [StorableClass]
75  [PushExpression(
76    StackTypes.Print,
77    "PRINT.PRINTBOOLEAN",
78    "Pushes the top BOOLEAN onto the PRINT stack.",
79    StackTypes.Boolean)]
80  public class BooleanPrintExpression : PrintExpression<bool> {
81    public BooleanPrintExpression() { }
82    [StorableConstructor]
83    protected BooleanPrintExpression(bool deserializing) : base(deserializing) { }
84
85    public override bool IsNoop(IInternalPushInterpreter interpreter) {
86      return interpreter.BooleanStack.IsEmpty;
87    }
88
89    public override void Eval(IInternalPushInterpreter interpreter) {
90      var value = interpreter.BooleanStack.Pop();
91      interpreter.PrintStack.Push(value);
92    }
93  }
94
95  [StorableClass]
96  [PushExpression(
97    StackTypes.Print,
98    "PRINT.PRINTCHAR",
99    "Pushes the top CHAR onto the PRINT stack.",
100    StackTypes.Char)]
101  public class CharPrintExpression : PrintExpression<char> {
102    public CharPrintExpression() { }
103    [StorableConstructor]
104    protected CharPrintExpression(bool deserializing) : base(deserializing) { }
105
106    public override bool IsNoop(IInternalPushInterpreter interpreter) {
107      return interpreter.CharStack.IsEmpty;
108    }
109
110    public override void Eval(IInternalPushInterpreter interpreter) {
111      var value = interpreter.CharStack.Pop();
112      interpreter.PrintStack.Push(value);
113    }
114  }
115
116  [StorableClass]
117  [PushExpression(
118    StackTypes.Print,
119    "PRINT.PRINTEXEC",
120    "Pushes the top BOOLEAN onto the EXEC stack.",
121    StackTypes.Exec,
122    requiredBlockCount: 1)]
123  public class ExecPrintExpression : PrintExpression<Expression> {
124    public ExecPrintExpression() { }
125    [StorableConstructor]
126    protected ExecPrintExpression(bool deserializing) : base(deserializing) { }
127
128    public override bool IsNoop(IInternalPushInterpreter interpreter) {
129      return interpreter.ExecStack.IsEmpty;
130    }
131
132    public override void Eval(IInternalPushInterpreter interpreter) {
133      var value = interpreter.ExecStack.Pop();
134      interpreter.PrintStack.Push(value);
135    }
136  }
137
138  [StorableClass]
139  [PushExpression(
140    StackTypes.Print,
141    "PRINT.PRINTFLOAT",
142    "Pushes the top FLOAT onto the PRINT stack.",
143    StackTypes.Float)]
144  public class FloatPrintExpression : PrintExpression<double> {
145    public FloatPrintExpression() { }
146    [StorableConstructor]
147    protected FloatPrintExpression(bool deserializing) : base(deserializing) { }
148
149    public override bool IsNoop(IInternalPushInterpreter interpreter) {
150      return interpreter.FloatStack.IsEmpty;
151    }
152
153    public override void Eval(IInternalPushInterpreter interpreter) {
154      var value = interpreter.FloatStack.Pop();
155      var str = value.ToString(interpreter.Configuration.FloatStringFormat, CultureInfo.InvariantCulture);
156
157      interpreter.PrintStack.Push(str);
158    }
159  }
160
161  [StorableClass]
162  [PushExpression(
163    StackTypes.Print,
164    "PRINT.PRINTINTEGER",
165    "Pushes the top INTEGER onto the PRINT stack.",
166    StackTypes.Integer)]
167  public class IntegerPrintExpression : PrintExpression<long> {
168    public IntegerPrintExpression() { }
169    [StorableConstructor]
170    protected IntegerPrintExpression(bool deserializing) : base(deserializing) { }
171
172    public override bool IsNoop(IInternalPushInterpreter interpreter) {
173      return interpreter.IntegerStack.IsEmpty;
174    }
175
176    public override void Eval(IInternalPushInterpreter interpreter) {
177      var value = interpreter.IntegerStack.Pop();
178      interpreter.PrintStack.Push(value);
179    }
180  }
181
182  [StorableClass]
183  [PushExpression(
184    StackTypes.Print,
185    "PRINT.PRINTSTRING",
186    "Pushes the top STRING onto the PRINT stack.",
187    StackTypes.String)]
188  public class StringPrintExpression : PrintExpression<string> {
189    public StringPrintExpression() { }
190    [StorableConstructor]
191    protected StringPrintExpression(bool deserializing) : base(deserializing) { }
192
193    public override bool IsNoop(IInternalPushInterpreter interpreter) {
194      return interpreter.StringStack.IsEmpty;
195    }
196
197    public override void Eval(IInternalPushInterpreter interpreter) {
198      var value = interpreter.StringStack.Pop();
199      interpreter.PrintStack.Push(value);
200    }
201  }
202
203  [StorableClass]
204  [PushExpression(
205    StackTypes.Print,
206    "PRINT.PRINTINTEGERVECTOR",
207    "Pushes the top INTEGER[] onto the PRINT stack.",
208    StackTypes.IntegerVector)]
209  public class IntegerVectorPrintExpression : PrintExpression<long> {
210    public IntegerVectorPrintExpression() { }
211    [StorableConstructor]
212    protected IntegerVectorPrintExpression(bool deserializing) : base(deserializing) { }
213
214    public override bool IsNoop(IInternalPushInterpreter interpreter) {
215      return interpreter.IntegerVectorStack.IsEmpty;
216    }
217
218    public override void Eval(IInternalPushInterpreter interpreter) {
219      EvalVector(interpreter, interpreter.IntegerVectorStack);
220    }
221  }
222
223  [StorableClass]
224  [PushExpression(
225    StackTypes.Print,
226    "PRINT.PRINTFLOATVECTOR",
227    "Pushes the top FLOAT[] onto the PRINT stack.",
228    StackTypes.FloatVector)]
229  public class FloatVectorPrintExpression : PrintExpression<double> {
230    public FloatVectorPrintExpression() { }
231    [StorableConstructor]
232    protected FloatVectorPrintExpression(bool deserializing) : base(deserializing) { }
233
234    public override bool IsNoop(IInternalPushInterpreter interpreter) {
235      return interpreter.FloatVectorStack.IsEmpty;
236    }
237
238    public override void Eval(IInternalPushInterpreter interpreter) {
239      EvalVector(interpreter, interpreter.FloatVectorStack);
240    }
241  }
242
243  [StorableClass]
244  [PushExpression(
245    StackTypes.Print,
246    "PRINT.PRINTSTRINGVECTOR",
247    "Pushes the top STRING[] onto the PRINT stack.",
248    StackTypes.StringVector)]
249  public class StringVectorPrintExpression : PrintExpression<string> {
250    public StringVectorPrintExpression() { }
251    [StorableConstructor]
252    protected StringVectorPrintExpression(bool deserializing) : base(deserializing) { }
253
254    public override bool IsNoop(IInternalPushInterpreter interpreter) {
255      return interpreter.StringVectorStack.IsEmpty;
256    }
257
258    public override void Eval(IInternalPushInterpreter interpreter) {
259      EvalVector(interpreter, interpreter.StringVectorStack);
260    }
261  }
262
263  [StorableClass]
264  [PushExpression(
265    StackTypes.Print,
266    "PRINT.PRINTBOOLEANVECTOR",
267    "Pushes the top BOOLEAN[] onto the PRINT stack.",
268    StackTypes.BooleanVector)]
269  public class BooleanVectorPrintExpression : PrintExpression<bool> {
270    public BooleanVectorPrintExpression() { }
271    [StorableConstructor]
272    protected BooleanVectorPrintExpression(bool deserializing) : base(deserializing) { }
273
274    public override bool IsNoop(IInternalPushInterpreter interpreter) {
275      return interpreter.BooleanVectorStack.IsEmpty;
276    }
277
278    public override void Eval(IInternalPushInterpreter interpreter) {
279      EvalVector(interpreter, interpreter.BooleanVectorStack);
280    }
281  }
282}
Note: See TracBrowser for help on using the repository browser.