1 | namespace 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 | /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item.
|
---|
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 YankDuplicateExpression<T> : StatelessExpression {
|
---|
12 | public bool Eval(IStack<T> stack, IStack<long> integerStack) {
|
---|
13 | T item;
|
---|
14 | if (!this.TryGetItem(stack, integerStack, out item))
|
---|
15 | return false;
|
---|
16 |
|
---|
17 | stack.Push(item);
|
---|
18 | return true;
|
---|
19 | }
|
---|
20 |
|
---|
21 | public bool Eval(IStack<Expression> stack, IStack<long> integerStack) {
|
---|
22 | Expression item;
|
---|
23 | if (!this.TryGetItem(stack, integerStack, out item))
|
---|
24 | return false;
|
---|
25 |
|
---|
26 | stack.Push(item);
|
---|
27 | return true;
|
---|
28 | }
|
---|
29 |
|
---|
30 | public bool Eval(IStack<long> integerStack) {
|
---|
31 | if ((integerStack.Count < 2) || (integerStack.Top > integerStack.Count - 2) ||
|
---|
32 | (integerStack.Top < 0)) return false;
|
---|
33 |
|
---|
34 | var index = (int)integerStack.Pop();
|
---|
35 | var item = integerStack.ReverseElementAt(index);
|
---|
36 |
|
---|
37 | integerStack.Push(item);
|
---|
38 | return true;
|
---|
39 | }
|
---|
40 |
|
---|
41 | private bool TryGetItem<E>(IStack<E> stack, IStack<long> integerStack, out E item) {
|
---|
42 | if ((integerStack.Count == 0) ||
|
---|
43 | (integerStack.Top >= stack.Count) ||
|
---|
44 | (integerStack.Top < 0) ||
|
---|
45 | (stack.Count < 2)) {
|
---|
46 | item = default(E);
|
---|
47 | return false;
|
---|
48 | }
|
---|
49 |
|
---|
50 | var index = (int)integerStack.Pop();
|
---|
51 | item = stack.ReverseElementAt(index);
|
---|
52 |
|
---|
53 | return true;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | [PushExpression(StackType.Integer, "INTEGER.YANKDUP")]
|
---|
58 | public class IntegerYankDuplicateExpression : YankDuplicateExpression<long> {
|
---|
59 | public override bool Eval(IPushInterpreter interpreter) {
|
---|
60 | return this.Eval(interpreter.IntegerStack);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | [PushExpression(StackType.Float, "FLOAT.YANKDUP")]
|
---|
65 | public class FloatYankDuplicateExpression : YankDuplicateExpression<double> {
|
---|
66 | public override bool Eval(IPushInterpreter interpreter) {
|
---|
67 | return this.Eval(interpreter.FloatStack, interpreter.IntegerStack);
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | [PushExpression(StackType.Boolean, "BOOLEAN.YANKDUP")]
|
---|
72 | public class BooleanYankDuplicateExpression : YankDuplicateExpression<bool> {
|
---|
73 | public override bool Eval(IPushInterpreter interpreter) {
|
---|
74 | return this.Eval(interpreter.BooleanStack, interpreter.IntegerStack);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | [PushExpression(StackType.Name, "NAME.YANKDUP")]
|
---|
79 | public class NameYankDuplicateExpression : YankDuplicateExpression<string> {
|
---|
80 | public override bool Eval(IPushInterpreter interpreter) {
|
---|
81 | return this.Eval(interpreter.NameStack, interpreter.IntegerStack);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | [PushExpression(StackType.Exec, "EXEC.YANKDUP")]
|
---|
86 | public class ExecYankDuplicateExpression : YankDuplicateExpression<Expression> {
|
---|
87 | public override bool Eval(IPushInterpreter interpreter) {
|
---|
88 | return this.Eval(interpreter.ExecStack, interpreter.IntegerStack);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | [PushExpression(StackType.Code, "CODE.YANKDUP")]
|
---|
93 | public class CodeYankDuplicateExpression : YankDuplicateExpression<Expression> {
|
---|
94 | public override bool Eval(IPushInterpreter interpreter) {
|
---|
95 | return this.Eval(interpreter.CodeStack, interpreter.IntegerStack);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | } |
---|