Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/PrintExpressions.cs @ 15017

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

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

File size: 8.1 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System.Collections.Generic;
3  using System.Globalization;
4  using System.Text;
5  using Attributes;
6  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Constants;
8  using Interpreter;
9  using Stack;
10
11  [StorableClass]
12  [PushExpression(StackTypes.Print, "PRINT.NEWLINE")]
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.Push(string.Empty);
24    }
25  }
26
27  [StorableClass]
28  public abstract class PrintExpression<T> : StatelessExpression {
29    protected PrintExpression() { }
30    [StorableConstructor]
31    protected PrintExpression(bool deserializing) : base(deserializing) { }
32
33    protected void Eval(IInternalPushInterpreter interpreter, IPushStack<T> stack) {
34      var value = stack.Pop().ToString();
35
36      if (interpreter.PrintStack.IsEmpty)
37        interpreter.PrintStack.Push(value);
38      else
39        interpreter.PrintStack.Top += value;
40    }
41
42    protected void EvalVector(IInternalPushInterpreter interpreter, IPushStack<IReadOnlyList<T>> vectorStack) {
43      var sb = new StringBuilder();
44      var vector = vectorStack.Pop();
45
46      sb.Append(PushEnvironment.VectorStartSymbol);
47
48      if (vector.Count > 0) {
49        sb.Append(vector[0]);
50
51        for (var i = 1; i < vector.Count; i++) {
52          sb.Append(PushEnvironment.VectorSeparatorSymbol);
53          sb.Append(vector[i]);
54        }
55      }
56
57      sb.Append(PushEnvironment.VectorEndSymbol);
58
59      var value = sb.ToString();
60      interpreter.PrintStack.Push(value);
61    }
62  }
63
64  [StorableClass]
65  [PushExpression(StackTypes.Print, "PRINT.PRINTBOOLEAN", StackTypes.Boolean)]
66  public class BooleanPrintExpression : PrintExpression<bool> {
67    public BooleanPrintExpression() { }
68    [StorableConstructor]
69    protected BooleanPrintExpression(bool deserializing) : base(deserializing) { }
70
71    public override bool IsNoop(IInternalPushInterpreter interpreter) {
72      return interpreter.BooleanStack.IsEmpty;
73    }
74
75    public override void Eval(IInternalPushInterpreter interpreter) {
76      Eval(interpreter, interpreter.BooleanStack);
77    }
78  }
79
80  [StorableClass]
81  [PushExpression(StackTypes.Print, "PRINT.PRINTCHAR", StackTypes.Char)]
82  public class CharPrintExpression : PrintExpression<char> {
83    public CharPrintExpression() { }
84    [StorableConstructor]
85    protected CharPrintExpression(bool deserializing) : base(deserializing) { }
86
87    public override bool IsNoop(IInternalPushInterpreter interpreter) {
88      return interpreter.CharStack.IsEmpty;
89    }
90
91    public override void Eval(IInternalPushInterpreter interpreter) {
92      Eval(interpreter, interpreter.CharStack);
93    }
94  }
95
96  [StorableClass]
97  [PushExpression(StackTypes.Print, "PRINT.PRINTEXEC", StackTypes.Exec, execIn: 1)]
98  public class ExecPrintExpression : PrintExpression<Expression> {
99    public ExecPrintExpression() { }
100    [StorableConstructor]
101    protected ExecPrintExpression(bool deserializing) : base(deserializing) { }
102
103    public override bool IsNoop(IInternalPushInterpreter interpreter) {
104      return interpreter.ExecStack.IsEmpty;
105    }
106
107    public override void Eval(IInternalPushInterpreter interpreter) {
108      Eval(interpreter, interpreter.ExecStack);
109    }
110  }
111
112  [StorableClass]
113  [PushExpression(StackTypes.Print, "PRINT.PRINTFLOAT", StackTypes.Float)]
114  public class FloatPrintExpression : PrintExpression<double> {
115    public FloatPrintExpression() { }
116    [StorableConstructor]
117    protected FloatPrintExpression(bool deserializing) : base(deserializing) { }
118
119    public override bool IsNoop(IInternalPushInterpreter interpreter) {
120      return interpreter.FloatStack.IsEmpty;
121    }
122
123    public override void Eval(IInternalPushInterpreter interpreter) {
124      var value = interpreter.FloatStack.Pop();
125      var str = value.ToString(interpreter.Configuration.FloatStringFormat, CultureInfo.InvariantCulture);
126
127      if (interpreter.PrintStack.IsEmpty)
128        interpreter.PrintStack.Push(str);
129      else
130        interpreter.PrintStack.Top += str;
131    }
132  }
133
134  [StorableClass]
135  [PushExpression(StackTypes.Print, "PRINT.PRINTINTEGER", StackTypes.Integer)]
136  public class IntegerPrintExpression : PrintExpression<long> {
137    public IntegerPrintExpression() { }
138    [StorableConstructor]
139    protected IntegerPrintExpression(bool deserializing) : base(deserializing) { }
140
141    public override bool IsNoop(IInternalPushInterpreter interpreter) {
142      return interpreter.IntegerStack.IsEmpty;
143    }
144
145    public override void Eval(IInternalPushInterpreter interpreter) {
146      Eval(interpreter, interpreter.IntegerStack);
147    }
148  }
149
150  [StorableClass]
151  [PushExpression(StackTypes.Print, "PRINT.PRINTSTRING", StackTypes.String)]
152  public class StringPrintExpression : PrintExpression<string> {
153    public StringPrintExpression() { }
154    [StorableConstructor]
155    protected StringPrintExpression(bool deserializing) : base(deserializing) { }
156
157    public override bool IsNoop(IInternalPushInterpreter interpreter) {
158      return interpreter.StringStack.IsEmpty;
159    }
160
161    public override void Eval(IInternalPushInterpreter interpreter) {
162      Eval(interpreter, interpreter.StringStack);
163    }
164  }
165
166  [StorableClass]
167  [PushExpression(StackTypes.Print, "PRINT.PRINTINTEGERVECTOR", StackTypes.IntegerVector)]
168  public class IntegerVectorPrintExpression : PrintExpression<long> {
169    public IntegerVectorPrintExpression() { }
170    [StorableConstructor]
171    protected IntegerVectorPrintExpression(bool deserializing) : base(deserializing) { }
172
173    public override bool IsNoop(IInternalPushInterpreter interpreter) {
174      return interpreter.IntegerVectorStack.IsEmpty;
175    }
176
177    public override void Eval(IInternalPushInterpreter interpreter) {
178      EvalVector(interpreter, interpreter.IntegerVectorStack);
179    }
180  }
181
182  [StorableClass]
183  [PushExpression(StackTypes.Print, "PRINT.PRINTFLOATVECTOR", StackTypes.FloatVector)]
184  public class FloatVectorPrintExpression : PrintExpression<double> {
185    public FloatVectorPrintExpression() { }
186    [StorableConstructor]
187    protected FloatVectorPrintExpression(bool deserializing) : base(deserializing) { }
188
189    public override bool IsNoop(IInternalPushInterpreter interpreter) {
190      return interpreter.FloatVectorStack.IsEmpty;
191    }
192
193    public override void Eval(IInternalPushInterpreter interpreter) {
194      EvalVector(interpreter, interpreter.FloatVectorStack);
195    }
196  }
197
198  [StorableClass]
199  [PushExpression(StackTypes.Print, "PRINT.PRINTSTRINGVECTOR", StackTypes.StringVector)]
200  public class StringVectorPrintExpression : PrintExpression<string> {
201    public StringVectorPrintExpression() { }
202    [StorableConstructor]
203    protected StringVectorPrintExpression(bool deserializing) : base(deserializing) { }
204
205    public override bool IsNoop(IInternalPushInterpreter interpreter) {
206      return interpreter.StringVectorStack.IsEmpty;
207    }
208
209    public override void Eval(IInternalPushInterpreter interpreter) {
210      EvalVector(interpreter, interpreter.StringVectorStack);
211    }
212  }
213
214  [StorableClass]
215  [PushExpression(StackTypes.Print, "PRINT.PRINTBOOLEANVECTOR", StackTypes.BooleanVector)]
216  public class BooleanVectorPrintExpression : PrintExpression<bool> {
217    public BooleanVectorPrintExpression() { }
218    [StorableConstructor]
219    protected BooleanVectorPrintExpression(bool deserializing) : base(deserializing) { }
220
221    public override bool IsNoop(IInternalPushInterpreter interpreter) {
222      return interpreter.BooleanVectorStack.IsEmpty;
223    }
224
225    public override void Eval(IInternalPushInterpreter interpreter) {
226      EvalVector(interpreter, interpreter.BooleanVectorStack);
227    }
228  }
229}
Note: See TracBrowser for help on using the repository browser.