[14727] | 1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
[14875] | 2 | using System;
|
---|
[14834] | 3 | using System.Collections.Generic;
|
---|
[14875] | 4 | using Attributes;
|
---|
| 5 | using Interpreter;
|
---|
| 6 | using Stack;
|
---|
[14834] | 7 |
|
---|
[14727] | 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 {
|
---|
[14834] | 14 | public bool Eval(IPushStack<T> stack, IPushStack<long> integerStack) {
|
---|
[14875] | 15 | if ((stack == integerStack && integerStack.Count < 2) ||
|
---|
| 16 | (stack != integerStack && (integerStack.IsEmpty || stack.IsEmpty)))
|
---|
[14744] | 17 | return false;
|
---|
| 18 |
|
---|
[14875] | 19 | var index = (int)Math.Abs(integerStack.Pop() % stack.Count);
|
---|
| 20 | stack.Push(stack[index]);
|
---|
[14744] | 21 | return true;
|
---|
[14727] | 22 | }
|
---|
| 23 | }
|
---|
| 24 |
|
---|
[14777] | 25 | [PushExpression(StackTypes.Integer, "INTEGER.YANKDUP")]
|
---|
[14727] | 26 | public class IntegerYankDuplicateExpression : YankDuplicateExpression<long> {
|
---|
[14834] | 27 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14875] | 28 | return this.Eval(interpreter.IntegerStack, interpreter.IntegerStack);
|
---|
[14727] | 29 | }
|
---|
| 30 | }
|
---|
| 31 |
|
---|
[14777] | 32 | [PushExpression(StackTypes.Float, "FLOAT.YANKDUP", StackTypes.Integer)]
|
---|
[14727] | 33 | public class FloatYankDuplicateExpression : YankDuplicateExpression<double> {
|
---|
[14834] | 34 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14744] | 35 | return this.Eval(interpreter.FloatStack, interpreter.IntegerStack);
|
---|
[14727] | 36 | }
|
---|
| 37 | }
|
---|
| 38 |
|
---|
[14777] | 39 | [PushExpression(StackTypes.Boolean, "BOOLEAN.YANKDUP", StackTypes.Integer)]
|
---|
[14727] | 40 | public class BooleanYankDuplicateExpression : YankDuplicateExpression<bool> {
|
---|
[14834] | 41 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14744] | 42 | return this.Eval(interpreter.BooleanStack, interpreter.IntegerStack);
|
---|
[14727] | 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[14777] | 46 | [PushExpression(StackTypes.Name, "NAME.YANKDUP", StackTypes.Integer)]
|
---|
[14727] | 47 | public class NameYankDuplicateExpression : YankDuplicateExpression<string> {
|
---|
[14834] | 48 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14744] | 49 | return this.Eval(interpreter.NameStack, interpreter.IntegerStack);
|
---|
[14727] | 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[14777] | 53 | [PushExpression(StackTypes.Exec, "EXEC.YANKDUP", StackTypes.Integer)]
|
---|
[14727] | 54 | public class ExecYankDuplicateExpression : YankDuplicateExpression<Expression> {
|
---|
[14834] | 55 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14744] | 56 | return this.Eval(interpreter.ExecStack, interpreter.IntegerStack);
|
---|
[14727] | 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[14777] | 60 | [PushExpression(StackTypes.Code, "CODE.YANKDUP", StackTypes.Integer)]
|
---|
[14727] | 61 | public class CodeYankDuplicateExpression : YankDuplicateExpression<Expression> {
|
---|
[14834] | 62 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14744] | 63 | return this.Eval(interpreter.CodeStack, interpreter.IntegerStack);
|
---|
[14727] | 64 | }
|
---|
| 65 | }
|
---|
[14777] | 66 |
|
---|
| 67 | [PushExpression(StackTypes.Char, "CHAR.YANKDUP", StackTypes.Integer)]
|
---|
| 68 | public class CharYankDuplicateExpression : YankDuplicateExpression<char> {
|
---|
[14834] | 69 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14777] | 70 | return Eval(interpreter.CharStack, interpreter.IntegerStack);
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | [PushExpression(StackTypes.String, "STRING.YANKDUP", StackTypes.Integer)]
|
---|
| 75 | public class StringYankDuplicateExpression : YankDuplicateExpression<string> {
|
---|
[14834] | 76 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
[14777] | 77 | return Eval(interpreter.StringStack, interpreter.IntegerStack);
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
[14834] | 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 | }
|
---|
[14875] | 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 | }
|
---|
[14727] | 108 | } |
---|