1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using Attributes;
|
---|
5 | using Interpreter;
|
---|
6 | using Stack;
|
---|
7 |
|
---|
8 | /// <summary>
|
---|
9 | /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item.
|
---|
10 | /// The index is taken from the INTEGER stack, and the indexing is done after the index is removed.
|
---|
11 | /// </summary>
|
---|
12 | /// <typeparam name="T">Stacktype</typeparam>
|
---|
13 | public abstract class YankDuplicateExpression<T> : StatelessExpression {
|
---|
14 | public bool Eval(IPushStack<T> stack, IPushStack<long> integerStack) {
|
---|
15 | if ((stack == integerStack && integerStack.Count < 2) ||
|
---|
16 | (stack != integerStack && (integerStack.IsEmpty || stack.IsEmpty)))
|
---|
17 | return false;
|
---|
18 |
|
---|
19 | var index = (int)Math.Abs(integerStack.Pop() % stack.Count);
|
---|
20 | stack.Push(stack[index]);
|
---|
21 | return true;
|
---|
22 | }
|
---|
23 | }
|
---|
24 |
|
---|
25 | [PushExpression(StackTypes.Integer, "INTEGER.YANKDUP")]
|
---|
26 | public class IntegerYankDuplicateExpression : YankDuplicateExpression<long> {
|
---|
27 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
28 | return this.Eval(interpreter.IntegerStack, interpreter.IntegerStack);
|
---|
29 | }
|
---|
30 | }
|
---|
31 |
|
---|
32 | [PushExpression(StackTypes.Float, "FLOAT.YANKDUP", StackTypes.Integer)]
|
---|
33 | public class FloatYankDuplicateExpression : YankDuplicateExpression<double> {
|
---|
34 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
35 | return this.Eval(interpreter.FloatStack, interpreter.IntegerStack);
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | [PushExpression(StackTypes.Boolean, "BOOLEAN.YANKDUP", StackTypes.Integer)]
|
---|
40 | public class BooleanYankDuplicateExpression : YankDuplicateExpression<bool> {
|
---|
41 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
42 | return this.Eval(interpreter.BooleanStack, interpreter.IntegerStack);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [PushExpression(StackTypes.Name, "NAME.YANKDUP", StackTypes.Integer)]
|
---|
47 | public class NameYankDuplicateExpression : YankDuplicateExpression<string> {
|
---|
48 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
49 | return this.Eval(interpreter.NameStack, interpreter.IntegerStack);
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [PushExpression(StackTypes.Exec, "EXEC.YANKDUP", StackTypes.Integer)]
|
---|
54 | public class ExecYankDuplicateExpression : YankDuplicateExpression<Expression> {
|
---|
55 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
56 | return this.Eval(interpreter.ExecStack, interpreter.IntegerStack);
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | [PushExpression(StackTypes.Code, "CODE.YANKDUP", StackTypes.Integer)]
|
---|
61 | public class CodeYankDuplicateExpression : YankDuplicateExpression<Expression> {
|
---|
62 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
63 | return this.Eval(interpreter.CodeStack, interpreter.IntegerStack);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | [PushExpression(StackTypes.Char, "CHAR.YANKDUP", StackTypes.Integer)]
|
---|
68 | public class CharYankDuplicateExpression : YankDuplicateExpression<char> {
|
---|
69 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
70 | return Eval(interpreter.CharStack, interpreter.IntegerStack);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | [PushExpression(StackTypes.String, "STRING.YANKDUP", StackTypes.Integer)]
|
---|
75 | public class StringYankDuplicateExpression : YankDuplicateExpression<string> {
|
---|
76 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
77 | return Eval(interpreter.StringStack, interpreter.IntegerStack);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | [PushExpression(StackTypes.IntegerVector, "INTEGER[].YANKDUP", StackTypes.Integer)]
|
---|
82 | public class IntegerVectorYankDuplicateExpression : YankDuplicateExpression<List<long>> {
|
---|
83 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
84 | return Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | [PushExpression(StackTypes.FloatVector, "FLOAT[].YANKDUP", StackTypes.Integer)]
|
---|
89 | public class FloatVectorYankDuplicateExpression : YankDuplicateExpression<List<double>> {
|
---|
90 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
91 | return Eval(interpreter.FloatVectorStack, interpreter.IntegerStack);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].YANKDUP", StackTypes.Integer)]
|
---|
96 | public class BooleanVectorYankDuplicateExpression : YankDuplicateExpression<List<bool>> {
|
---|
97 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
98 | return Eval(interpreter.BooleanVectorStack, interpreter.IntegerStack);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | [PushExpression(StackTypes.StringVector, "STRING[].YANKDUP", StackTypes.Integer)]
|
---|
103 | public class StringVectorYankDuplicateExpression : YankDuplicateExpression<List<string>> {
|
---|
104 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
105 | return Eval(interpreter.StringVectorStack, interpreter.IntegerStack);
|
---|
106 | }
|
---|
107 | }
|
---|
108 | } |
---|