Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PrintExpressions.cs @ 17432

Last change on this file since 17432 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

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