using System.Collections.Generic; namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes; using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions; using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter; using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; /// /// Gets the nth item from the type stack /// /// [StorableClass] public abstract class VectorNthExpression : StatelessExpression { protected VectorNthExpression() { } [StorableConstructor] protected VectorNthExpression(bool deserializing) : base(deserializing) { } protected bool IsNoop(IInternalPushInterpreter interpreter, IPushStack> vectorStack) { return vectorStack.IsEmpty || vectorStack.Top.Count == 0 || interpreter.IntegerStack.IsEmpty; } protected void Eval( IInternalPushInterpreter interpreter, IPushStack> vectorStack, IPushStack literalStack) { var vector = vectorStack.Pop(); if (literalStack == interpreter.IntegerStack) { var n = interpreter.IntegerStack.Top.AsInt(vector.Count); literalStack.Top = vector[n]; } else { var n = interpreter.IntegerStack.Pop().AsInt(vector.Count); literalStack.Push(vector[n]); } } } [StorableClass] [PushExpression( StackTypes.IntegerVector, "INTEGER[].NTH", "Pushes the nth item from the top INTEGER[] onto the INTEGER stack, whereby n is taken from the INTEGER stack.", StackTypes.Integer)] public class IntegerVectorNthExpression : VectorNthExpression { public IntegerVectorNthExpression() { } [StorableConstructor] protected IntegerVectorNthExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter, interpreter.IntegerVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack); } } [StorableClass] [PushExpression( StackTypes.FloatVector, "FLOAT[].NTH", "Pushes the nth item from the top FLOAT[] onto the FLOAT stack, whereby n is taken from the INTEGER stack.", StackTypes.Float | StackTypes.Integer)] public class FloatVectorNthExpression : VectorNthExpression { public FloatVectorNthExpression() { } [StorableConstructor] protected FloatVectorNthExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter, interpreter.FloatVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack); } } [StorableClass] [PushExpression( StackTypes.BooleanVector, "BOOLEAN[].NTH", "Pushes the nth item from the top BOOLEAN[] onto the BOOLEAN stack, whereby n is taken from the INTEGER stack.", StackTypes.Boolean | StackTypes.Integer)] public class BooleanVectorNthExpression : VectorNthExpression { public BooleanVectorNthExpression() { } [StorableConstructor] protected BooleanVectorNthExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter, interpreter.BooleanVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack); } } [StorableClass] [PushExpression( StackTypes.StringVector, "STRING[].NTH", "Pushes the nth item from the top STRING[] onto the BOOLEAN stack, whereby n is taken from the INTEGER stack.", StackTypes.String | StackTypes.Integer)] public class StringVectorNthExpression : VectorNthExpression { public StringVectorNthExpression() { } [StorableConstructor] protected StringVectorNthExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter, interpreter.StringVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack); } } }