Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/DuplicateExpressions.cs @ 14744

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

#2665 Renamings due to typos, ManagedPool tests, Skip Noops in Debugger

File size: 2.4 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
3  using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
4  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
5
6  /// <summary>
7  ///     Duplicates the top item on the INTEGER stack. Does not pop its argument
8  ///     (which, if it did, would negate the effect of the duplication!).
9  /// </summary>
10  /// <typeparam name="T">Stacktype</typeparam>
11  public abstract class DuplicateExpression<T> : StatelessExpression {
12    protected bool Eval(IStack<T> stack) {
13      if (stack.Count == 0) return false;
14
15      stack.Push(stack.Top);
16
17      return true;
18    }
19
20    protected bool Eval(IStack<Expression> stack) {
21      if (stack.Count == 0) return false;
22
23      var top = stack.Top;
24
25      stack.Push(top);
26      return true;
27    }
28  }
29
30  [PushExpression(StackType.Integer, "INTEGER.DUP")]
31  public class IntegerDuplicateExpression : DuplicateExpression<long> {
32    public override bool Eval(IPushInterpreter interpreter) {
33      return this.Eval(interpreter.IntegerStack);
34    }
35  }
36
37  [PushExpression(StackType.Float, "FLOAT.DUP")]
38  public class FloatDuplicateExpression : DuplicateExpression<double> {
39    public override bool Eval(IPushInterpreter interpreter) {
40      return this.Eval(interpreter.FloatStack);
41    }
42  }
43
44  [PushExpression(StackType.Boolean, "BOOLEAN.DUP")]
45  public class BooleanDuplicateExpression : DuplicateExpression<bool> {
46    public override bool Eval(IPushInterpreter interpreter) {
47      return this.Eval(interpreter.BooleanStack);
48    }
49  }
50
51  [PushExpression(StackType.Name, "NAME.DUP")]
52  public class NameDuplicateExpression : DuplicateExpression<string> {
53    public override bool Eval(IPushInterpreter interpreter) {
54      return this.Eval(interpreter.NameStack);
55    }
56  }
57
58  [PushExpression(StackType.Exec, "EXEC.DUP")]
59  public class ExecDuplicateExpression : DuplicateExpression<Expression> {
60    public override bool Eval(IPushInterpreter interpreter) {
61      return this.Eval(interpreter.ExecStack);
62    }
63  }
64
65  [PushExpression(StackType.Code, "CODE.DUP")]
66  public class CodeDuplicateExpression : DuplicateExpression<Expression> {
67    public override bool Eval(IPushInterpreter interpreter) {
68      return this.Eval(interpreter.CodeStack);
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.