Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/YankExpressions.cs @ 14777

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

#2665 simplifier, push solution results view, performance improvements, small bug fixes, ui fixes

File size: 3.2 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  ///     Removes an indexed item from "deep" in the stack and pushes it on top of the stack.
8  ///     The index is taken from the INTEGER stack, and the indexing is done after the index is removed.
9  /// </summary>
10  /// <typeparam name="T">Stacktype</typeparam>
11  public abstract class YankExpression<T> : StatelessExpression {
12    public bool Eval(IStack<T> stack, IStack<long> integerStack, int count = 1) {
13      if (integerStack.Count == 0) return false;
14
15      var index = integerStack.Top;
16
17      if ((index > stack.Count - count) || (index < 0) || (stack.Count < 2)) return false;
18
19      integerStack.Pop();
20      stack.Yank((int)index);
21
22      return true;
23    }
24  }
25
26  [PushExpression(StackTypes.Integer, "INTEGER.YANK")]
27  public class IntegerYankExpression : YankExpression<long> {
28    public override bool Eval(IPushInterpreter interpreter) {
29      return this.Eval(interpreter.IntegerStack, interpreter.IntegerStack, 2);
30    }
31  }
32
33  [PushExpression(StackTypes.Float, "FLOAT.YANK", StackTypes.Integer)]
34  public class FloatYankExpression : YankExpression<double> {
35    public override bool Eval(IPushInterpreter interpreter) {
36      return this.Eval(interpreter.FloatStack, interpreter.IntegerStack);
37    }
38  }
39
40  [PushExpression(StackTypes.Boolean, "BOOLEAN.YANK", StackTypes.Integer)]
41  public class BooleanYankExpression : YankExpression<bool> {
42    public override bool Eval(IPushInterpreter interpreter) {
43      return this.Eval(interpreter.BooleanStack, interpreter.IntegerStack);
44    }
45  }
46
47  [PushExpression(StackTypes.Name, "NAME.YANK", StackTypes.Integer)]
48  public class NameYankExpression : YankExpression<string> {
49    public override bool Eval(IPushInterpreter interpreter) {
50      return this.Eval(interpreter.NameStack, interpreter.IntegerStack);
51    }
52  }
53
54  [PushExpression(StackTypes.Exec, "EXEC.YANK", StackTypes.Integer)]
55  public class ExecYankExpression : YankExpression<Expression> {
56    public override bool Eval(IPushInterpreter interpreter) {
57      return this.Eval(interpreter.ExecStack, interpreter.IntegerStack);
58    }
59  }
60
61  [PushExpression(StackTypes.Code, "CODE.YANK", StackTypes.Integer)]
62  public class CodeYankExpression : YankExpression<Expression> {
63    public override bool Eval(IPushInterpreter interpreter) {
64      return this.Eval(interpreter.CodeStack, interpreter.IntegerStack);
65    }
66  }
67
68  [PushExpression(StackTypes.Char, "CHAR.YANK", StackTypes.Integer)]
69  public class CharYankExpression : YankExpression<char> {
70    public override bool Eval(IPushInterpreter interpreter) {
71      return Eval(interpreter.CharStack, interpreter.IntegerStack);
72    }
73  }
74
75  [PushExpression(StackTypes.String, "STRING.YANK", StackTypes.Integer)]
76  public class StringYankExpression : YankExpression<string> {
77    public override bool Eval(IPushInterpreter interpreter) {
78      return Eval(interpreter.StringStack, interpreter.IntegerStack);
79    }
80  }
81}
Note: See TracBrowser for help on using the repository browser.