Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 9.2 KB
RevLine 
[15771]1using System.Collections.Generic;
2using System.Globalization;
3using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
4
5namespace HeuristicLab.Problems.ProgramSynthesis {
[14952]6  [StorableClass]
[15032]7  [PushExpression(StackTypes.Print, "PRINT.NEWLINE", "Pushes an empty string onto the PRINT STACK.")]
[14897]8  public class PrintNewLineExpression : StatelessExpression {
[14952]9    public PrintNewLineExpression() { }
10    [StorableConstructor]
11    protected PrintNewLineExpression(bool deserializing) : base(deserializing) { }
12
13    public override bool IsNoop(IInternalPushInterpreter interpreter) {
14      return false;
[14897]15    }
[14952]16
17    public override void Eval(IInternalPushInterpreter interpreter) {
[15189]18      interpreter.PrintStack.NewLine();
[14952]19    }
[14897]20  }
21
[14952]22  [StorableClass]
[15334]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]
[14897]39  public abstract class PrintExpression<T> : StatelessExpression {
[14952]40    protected PrintExpression() { }
[15189]41
[14952]42    [StorableConstructor]
43    protected PrintExpression(bool deserializing) : base(deserializing) { }
[14897]44
[14952]45    protected void Eval(IInternalPushInterpreter interpreter, IPushStack<T> stack) {
[15189]46      var value = stack.Pop();
[15017]47
[15189]48      interpreter.PrintStack.Push(value);
[14897]49    }
50
[15017]51    protected void EvalVector(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack) {
[14897]52      var vector = vectorStack.Pop();
53
[15189]54      interpreter.PrintStack.Push(PushEnvironment.VectorStartSymbol);
[14897]55
56      if (vector.Count > 0) {
[15189]57        interpreter.PrintStack.Push(vector[0]);
[14897]58
59        for (var i = 1; i < vector.Count; i++) {
[15189]60          interpreter.PrintStack.Push(PushEnvironment.VectorSeparatorSymbol);
61          interpreter.PrintStack.Push(vector[i]);
[14897]62        }
63      }
64
[15189]65      interpreter.PrintStack.Push(PushEnvironment.VectorEndSymbol);
[14897]66    }
67  }
68
[14952]69  [StorableClass]
[15032]70  [PushExpression(
71    StackTypes.Print,
72    "PRINT.PRINTBOOLEAN",
73    "Pushes the top BOOLEAN onto the PRINT stack.",
74    StackTypes.Boolean)]
[14897]75  public class BooleanPrintExpression : PrintExpression<bool> {
[14952]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;
[14897]82    }
[14952]83
84    public override void Eval(IInternalPushInterpreter interpreter) {
[15289]85      var value = interpreter.BooleanStack.Pop();
86      interpreter.PrintStack.Push(value);
[14952]87    }
[14897]88  }
89
[14952]90  [StorableClass]
[15032]91  [PushExpression(
92    StackTypes.Print,
93    "PRINT.PRINTCHAR",
94    "Pushes the top CHAR onto the PRINT stack.",
95    StackTypes.Char)]
[14897]96  public class CharPrintExpression : PrintExpression<char> {
[14952]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;
[14897]103    }
[14952]104
105    public override void Eval(IInternalPushInterpreter interpreter) {
[15289]106      var value = interpreter.CharStack.Pop();
107      interpreter.PrintStack.Push(value);
[14952]108    }
[14897]109  }
110
[14952]111  [StorableClass]
[15032]112  [PushExpression(
113    StackTypes.Print,
114    "PRINT.PRINTEXEC",
115    "Pushes the top BOOLEAN onto the EXEC stack.",
116    StackTypes.Exec,
[15334]117    requiredBlockCount: 1)]
[14897]118  public class ExecPrintExpression : PrintExpression<Expression> {
[14952]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;
[14897]125    }
[14952]126
127    public override void Eval(IInternalPushInterpreter interpreter) {
[15289]128      var value = interpreter.ExecStack.Pop();
129      interpreter.PrintStack.Push(value);
[14952]130    }
[14897]131  }
132
[14952]133  [StorableClass]
[15032]134  [PushExpression(
135    StackTypes.Print,
136    "PRINT.PRINTFLOAT",
137    "Pushes the top FLOAT onto the PRINT stack.",
138    StackTypes.Float)]
[14897]139  public class FloatPrintExpression : PrintExpression<double> {
[14952]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;
[14897]146    }
[14952]147
148    public override void Eval(IInternalPushInterpreter interpreter) {
[15017]149      var value = interpreter.FloatStack.Pop();
150      var str = value.ToString(interpreter.Configuration.FloatStringFormat, CultureInfo.InvariantCulture);
151
[15289]152      interpreter.PrintStack.Push(str);
[14952]153    }
[14897]154  }
155
[14952]156  [StorableClass]
[15032]157  [PushExpression(
158    StackTypes.Print,
159    "PRINT.PRINTINTEGER",
160    "Pushes the top INTEGER onto the PRINT stack.",
161    StackTypes.Integer)]
[14897]162  public class IntegerPrintExpression : PrintExpression<long> {
[14952]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;
[14897]169    }
[14952]170
171    public override void Eval(IInternalPushInterpreter interpreter) {
[15189]172      var value = interpreter.IntegerStack.Pop();
173      interpreter.PrintStack.Push(value);
[14952]174    }
[14897]175  }
176
[14952]177  [StorableClass]
[15032]178  [PushExpression(
179    StackTypes.Print,
180    "PRINT.PRINTSTRING",
181    "Pushes the top STRING onto the PRINT stack.",
182    StackTypes.String)]
[14897]183  public class StringPrintExpression : PrintExpression<string> {
[14952]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;
[14897]190    }
[14952]191
192    public override void Eval(IInternalPushInterpreter interpreter) {
[15289]193      var value = interpreter.StringStack.Pop();
194      interpreter.PrintStack.Push(value);
[14952]195    }
[14897]196  }
197
[14952]198  [StorableClass]
[15032]199  [PushExpression(
200    StackTypes.Print,
201    "PRINT.PRINTINTEGERVECTOR",
202    "Pushes the top INTEGER[] onto the PRINT stack.",
203    StackTypes.IntegerVector)]
[14897]204  public class IntegerVectorPrintExpression : PrintExpression<long> {
[14952]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;
[14897]211    }
[14952]212
213    public override void Eval(IInternalPushInterpreter interpreter) {
214      EvalVector(interpreter, interpreter.IntegerVectorStack);
215    }
[14897]216  }
217
[14952]218  [StorableClass]
[15032]219  [PushExpression(
220    StackTypes.Print,
221    "PRINT.PRINTFLOATVECTOR",
222    "Pushes the top FLOAT[] onto the PRINT stack.",
223    StackTypes.FloatVector)]
[14897]224  public class FloatVectorPrintExpression : PrintExpression<double> {
[14952]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;
[14897]231    }
[14952]232
233    public override void Eval(IInternalPushInterpreter interpreter) {
234      EvalVector(interpreter, interpreter.FloatVectorStack);
235    }
[14897]236  }
237
[14952]238  [StorableClass]
[15032]239  [PushExpression(
240    StackTypes.Print,
241    "PRINT.PRINTSTRINGVECTOR",
242    "Pushes the top STRING[] onto the PRINT stack.",
243    StackTypes.StringVector)]
[14897]244  public class StringVectorPrintExpression : PrintExpression<string> {
[14952]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;
[14897]251    }
[14952]252
253    public override void Eval(IInternalPushInterpreter interpreter) {
254      EvalVector(interpreter, interpreter.StringVectorStack);
255    }
[14897]256  }
257
[14952]258  [StorableClass]
[15032]259  [PushExpression(
260    StackTypes.Print,
261    "PRINT.PRINTBOOLEANVECTOR",
262    "Pushes the top BOOLEAN[] onto the PRINT stack.",
263    StackTypes.BooleanVector)]
[14897]264  public class BooleanVectorPrintExpression : PrintExpression<bool> {
[14952]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;
[14897]271    }
[14952]272
273    public override void Eval(IInternalPushInterpreter interpreter) {
274      EvalVector(interpreter, interpreter.BooleanVectorStack);
275    }
[14897]276  }
277}
Note: See TracBrowser for help on using the repository browser.