Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/DuplicateExpressions.cs @ 17104

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

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

File size: 8.2 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System.Collections.Generic;
3
4  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
5  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
8
9  /// <summary>
10  /// Duplicates the top item on the T stack. Does not pop its argument
11  /// (which, if it did, would negate the effect of the duplication!).
12  /// </summary>
13  /// <typeparam name="T">Stacktype</typeparam>
14  [StorableClass]
15  public abstract class DuplicateExpression<T> : StatelessExpression {
16    protected DuplicateExpression() { }
17    [StorableConstructor]
18    protected DuplicateExpression(bool deserializing) : base(deserializing) { }
19
20    protected void Eval(IPushStack<T> stack) {
21      stack.Push(stack.Top);
22    }
23  }
24
25  [StorableClass]
26  [PushExpression(StackTypes.Integer, "INTEGER.DUP", "Duplicates the top item on the INTEGER stack.")]
27  public class IntegerDuplicateExpression : DuplicateExpression<long> {
28    public IntegerDuplicateExpression() { }
29    [StorableConstructor]
30    protected IntegerDuplicateExpression(bool deserializing) : base(deserializing) { }
31
32    public override bool IsNoop(IInternalPushInterpreter interpreter) {
33      return interpreter.IntegerStack.IsEmpty;
34    }
35
36    public override void Eval(IInternalPushInterpreter interpreter) {
37      Eval(interpreter.IntegerStack);
38    }
39  }
40
41  [StorableClass]
42  [PushExpression(StackTypes.Float, "FLOAT.DUP", "Duplicates the top item on the FLOAT stack.")]
43  public class FloatDuplicateExpression : DuplicateExpression<double> {
44    public FloatDuplicateExpression() { }
45    [StorableConstructor]
46    protected FloatDuplicateExpression(bool deserializing) : base(deserializing) { }
47
48    public override bool IsNoop(IInternalPushInterpreter interpreter) {
49      return interpreter.FloatStack.IsEmpty;
50    }
51
52    public override void Eval(IInternalPushInterpreter interpreter) {
53      Eval(interpreter.FloatStack);
54    }
55  }
56
57  [StorableClass]
58  [PushExpression(StackTypes.Boolean, "BOOLEAN.DUP", "Duplicates the top item on the BOOLEAN stack.")]
59  public class BooleanDuplicateExpression : DuplicateExpression<bool> {
60    public BooleanDuplicateExpression() { }
61    [StorableConstructor]
62    protected BooleanDuplicateExpression(bool deserializing) : base(deserializing) { }
63
64    public override bool IsNoop(IInternalPushInterpreter interpreter) {
65      return interpreter.BooleanStack.IsEmpty;
66    }
67
68    public override void Eval(IInternalPushInterpreter interpreter) {
69      Eval(interpreter.BooleanStack);
70    }
71  }
72
73  [StorableClass]
74  [PushExpression(StackTypes.Name, "NAME.DUP", "Duplicates the top item on the NAME stack.")]
75  public class NameDuplicateExpression : DuplicateExpression<string> {
76    public NameDuplicateExpression() { }
77    [StorableConstructor]
78    protected NameDuplicateExpression(bool deserializing) : base(deserializing) { }
79
80    public override bool IsNoop(IInternalPushInterpreter interpreter) {
81      return interpreter.NameStack.IsEmpty;
82    }
83
84    public override void Eval(IInternalPushInterpreter interpreter) {
85      Eval(interpreter.NameStack);
86    }
87  }
88
89  [StorableClass]
90  [PushExpression(StackTypes.Exec, "EXEC.DUP", "Duplicates the top item on the EXEC stack." , requiredBlockCount: 1)]
91  public class ExecDuplicateExpression : DuplicateExpression<Expression> {
92    public ExecDuplicateExpression() { }
93    [StorableConstructor]
94    protected ExecDuplicateExpression(bool deserializing) : base(deserializing) { }
95
96    public override bool IsNoop(IInternalPushInterpreter interpreter) {
97      return interpreter.ExecStack.IsEmpty;
98    }
99
100    public override void Eval(IInternalPushInterpreter interpreter) {
101      Eval(interpreter.ExecStack);
102    }
103  }
104
105  [StorableClass]
106  [PushExpression(StackTypes.Code, "CODE.DUP", "Duplicates the top item on the CODE stack.")]
107  public class CodeDuplicateExpression : DuplicateExpression<Expression> {
108    public CodeDuplicateExpression() { }
109    [StorableConstructor]
110    protected CodeDuplicateExpression(bool deserializing) : base(deserializing) { }
111
112    public override bool IsNoop(IInternalPushInterpreter interpreter) {
113      return interpreter.CodeStack.IsEmpty;
114    }
115
116    public override void Eval(IInternalPushInterpreter interpreter) {
117      Eval(interpreter.CodeStack);
118    }
119  }
120
121  [StorableClass]
122  [PushExpression(StackTypes.Char, "CHAR.DUP", "Duplicates the top item on the CHAR stack.")]
123  public class CharDuplicateExpression : DuplicateExpression<char> {
124    public CharDuplicateExpression() { }
125    [StorableConstructor]
126    protected CharDuplicateExpression(bool deserializing) : base(deserializing) { }
127
128    public override bool IsNoop(IInternalPushInterpreter interpreter) {
129      return interpreter.CharStack.IsEmpty;
130    }
131
132    public override void Eval(IInternalPushInterpreter interpreter) {
133      Eval(interpreter.CharStack);
134    }
135  }
136
137  [StorableClass]
138  [PushExpression(StackTypes.String, "STRING.DUP", "Duplicates the top item on the STRING stack.")]
139  public class StringDuplicateExpression : DuplicateExpression<string> {
140    public StringDuplicateExpression() { }
141    [StorableConstructor]
142    protected StringDuplicateExpression(bool deserializing) : base(deserializing) { }
143
144    public override bool IsNoop(IInternalPushInterpreter interpreter) {
145      return interpreter.StringStack.IsEmpty;
146    }
147
148    public override void Eval(IInternalPushInterpreter interpreter) {
149      Eval(interpreter.StringStack);
150    }
151  }
152
153  [StorableClass]
154  [PushExpression(StackTypes.IntegerVector, "INTEGER[].DUP", "Duplicates the top item on the INTEGER[] stack.")]
155  public class IntegerVectorDuplicateExpression : DuplicateExpression<IReadOnlyList<long>> {
156    public IntegerVectorDuplicateExpression() { }
157    [StorableConstructor]
158    protected IntegerVectorDuplicateExpression(bool deserializing) : base(deserializing) { }
159
160    public override bool IsNoop(IInternalPushInterpreter interpreter) {
161      return interpreter.IntegerVectorStack.IsEmpty;
162    }
163
164    public override void Eval(IInternalPushInterpreter interpreter) {
165      Eval(interpreter.IntegerVectorStack);
166    }
167  }
168
169  [StorableClass]
170  [PushExpression(StackTypes.FloatVector, "FLOAT[].DUP", "Duplicates the top item on the FLOAT[] stack.")]
171  public class FloatVectorDuplicateExpression : DuplicateExpression<IReadOnlyList<double>> {
172    public FloatVectorDuplicateExpression() { }
173    [StorableConstructor]
174    protected FloatVectorDuplicateExpression(bool deserializing) : base(deserializing) { }
175
176    public override bool IsNoop(IInternalPushInterpreter interpreter) {
177      return interpreter.FloatVectorStack.IsEmpty;
178    }
179
180    public override void Eval(IInternalPushInterpreter interpreter) {
181      Eval(interpreter.FloatVectorStack);
182    }
183  }
184
185  [StorableClass]
186  [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].DUP", "Duplicates the top item on the BOOLEAN[] stack.")]
187  public class BooleanVectorDuplicateExpression : DuplicateExpression<IReadOnlyList<bool>> {
188    public BooleanVectorDuplicateExpression() { }
189    [StorableConstructor]
190    protected BooleanVectorDuplicateExpression(bool deserializing) : base(deserializing) { }
191
192    public override bool IsNoop(IInternalPushInterpreter interpreter) {
193      return interpreter.BooleanVectorStack.IsEmpty;
194    }
195
196    public override void Eval(IInternalPushInterpreter interpreter) {
197      Eval(interpreter.BooleanVectorStack);
198    }
199  }
200
201  [StorableClass]
202  [PushExpression(StackTypes.StringVector, "STRING[].DUP", "Duplicates the top item on the STRING[] stack.")]
203  public class StringVectorDuplicateExpression : DuplicateExpression<IReadOnlyList<string>> {
204    public StringVectorDuplicateExpression() { }
205    [StorableConstructor]
206    protected StringVectorDuplicateExpression(bool deserializing) : base(deserializing) { }
207
208    public override bool IsNoop(IInternalPushInterpreter interpreter) {
209      return interpreter.StringVectorStack.IsEmpty;
210    }
211
212    public override void Eval(IInternalPushInterpreter interpreter) {
213      Eval(interpreter.StringVectorStack);
214    }
215  }
216}
Note: See TracBrowser for help on using the repository browser.