namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { using System; using System.Collections.Generic; using Attributes; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions; using Interpreter; using Stack; /// /// Pushes a copy of an indexed item "deep" in the stack onto the top of the stack, without removing the deep item. /// The index is taken from the INTEGER stack, and the indexing is done after the index is removed. /// /// Stacktype [StorableClass] public abstract class YankDuplicateExpression : StatelessExpression { protected YankDuplicateExpression() { } [StorableConstructor] protected YankDuplicateExpression(bool deserializing) : base(deserializing) { } protected bool IsNoop(IPushStack stack, IPushStack integerStack) { return (stack == integerStack && integerStack.Count < 3) || (stack != integerStack && (integerStack.IsEmpty || stack.Count < 2)); } protected void Eval(IPushStack stack, IPushStack integerStack) { var index = integerStack.Pop().AsInt(stack.Count); stack.Push(stack[index]); } } [StorableClass] [PushExpression( StackTypes.Integer, "INTEGER.YANKDUP", "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.")] public class IntegerYankDuplicateExpression : YankDuplicateExpression { public IntegerYankDuplicateExpression() { } [StorableConstructor] protected IntegerYankDuplicateExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.IntegerStack, interpreter.IntegerStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.IntegerStack, interpreter.IntegerStack); } } [StorableClass] [PushExpression( StackTypes.Float, "FLOAT.YANKDUP", "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.", StackTypes.Integer)] public class FloatYankDuplicateExpression : YankDuplicateExpression { public FloatYankDuplicateExpression() { } [StorableConstructor] protected FloatYankDuplicateExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.FloatStack, interpreter.IntegerStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.FloatStack, interpreter.IntegerStack); } } [StorableClass] [PushExpression( StackTypes.Boolean, "BOOLEAN.YANKDUP", "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.", StackTypes.Integer)] public class BooleanYankDuplicateExpression : YankDuplicateExpression { public BooleanYankDuplicateExpression() { } [StorableConstructor] protected BooleanYankDuplicateExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.BooleanStack, interpreter.IntegerStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.BooleanStack, interpreter.IntegerStack); } } [StorableClass] [PushExpression( StackTypes.Name, "NAME.YANKDUP", "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.", StackTypes.Integer)] public class NameYankDuplicateExpression : YankDuplicateExpression { public NameYankDuplicateExpression() { } [StorableConstructor] protected NameYankDuplicateExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.NameStack, interpreter.IntegerStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.NameStack, interpreter.IntegerStack); } } [StorableClass] [PushExpression( StackTypes.Exec, "EXEC.YANKDUP", "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.", StackTypes.Integer, requiredBlockCount: 0)] public class ExecYankDuplicateExpression : YankDuplicateExpression { public ExecYankDuplicateExpression() { } [StorableConstructor] protected ExecYankDuplicateExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.ExecStack, interpreter.IntegerStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.ExecStack, interpreter.IntegerStack); } } [StorableClass] [PushExpression( StackTypes.Code, "CODE.YANKDUP", "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.", StackTypes.Integer)] public class CodeYankDuplicateExpression : YankDuplicateExpression { public CodeYankDuplicateExpression() { } [StorableConstructor] protected CodeYankDuplicateExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.CodeStack, interpreter.IntegerStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.CodeStack, interpreter.IntegerStack); } } [StorableClass] [PushExpression( StackTypes.Char, "CHAR.YANKDUP", "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.", StackTypes.Integer)] public class CharYankDuplicateExpression : YankDuplicateExpression { public CharYankDuplicateExpression() { } [StorableConstructor] protected CharYankDuplicateExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.CharStack, interpreter.IntegerStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.CharStack, interpreter.IntegerStack); } } [StorableClass] [PushExpression( StackTypes.String, "STRING.YANKDUP", "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.", StackTypes.Integer)] public class StringYankDuplicateExpression : YankDuplicateExpression { public StringYankDuplicateExpression() { } [StorableConstructor] protected StringYankDuplicateExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.StringStack, interpreter.IntegerStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.StringStack, interpreter.IntegerStack); } } [StorableClass] [PushExpression( StackTypes.IntegerVector, "INTEGER[].YANKDUP", "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.", StackTypes.Integer)] public class IntegerVectorYankDuplicateExpression : YankDuplicateExpression> { public IntegerVectorYankDuplicateExpression() { } [StorableConstructor] protected IntegerVectorYankDuplicateExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.IntegerVectorStack, interpreter.IntegerStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack); } } [StorableClass] [PushExpression( StackTypes.FloatVector, "FLOAT[].YANKDUP", "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.", StackTypes.Integer)] public class FloatVectorYankDuplicateExpression : YankDuplicateExpression> { public FloatVectorYankDuplicateExpression() { } [StorableConstructor] protected FloatVectorYankDuplicateExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.FloatVectorStack, interpreter.IntegerStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.FloatVectorStack, interpreter.IntegerStack); } } [StorableClass] [PushExpression( StackTypes.BooleanVector, "BOOLEAN[].YANKDUP", "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.", StackTypes.Integer)] public class BooleanVectorYankDuplicateExpression : YankDuplicateExpression> { public BooleanVectorYankDuplicateExpression() { } [StorableConstructor] protected BooleanVectorYankDuplicateExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.BooleanVectorStack, interpreter.IntegerStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.BooleanVectorStack, interpreter.IntegerStack); } } [StorableClass] [PushExpression( StackTypes.StringVector, "STRING[].YANKDUP", "Pushes a copy of an indexed item \"deep\" in the stack onto the top of the stack, without removing the deep item.", StackTypes.Integer)] public class StringVectorYankDuplicateExpression : YankDuplicateExpression> { public StringVectorYankDuplicateExpression() { } [StorableConstructor] protected StringVectorYankDuplicateExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.StringVectorStack, interpreter.IntegerStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.StringVectorStack, interpreter.IntegerStack); } } }