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.Interpreter; using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; /// /// Gets the last item from the type stack /// /// [StorableClass] public abstract class VectorLastExpression : StatelessExpression { protected VectorLastExpression() { } [StorableConstructor] protected VectorLastExpression(bool deserializing) : base(deserializing) { } protected bool IsNoop(IPushStack> vectorStack) { return vectorStack.IsEmpty || vectorStack.Top.Count == 0; } protected void Eval(IPushStack> vectorStack, IPushStack literalStack) { var vector = vectorStack.Pop(); literalStack.Push(vector[vector.Count - 1]); } } [StorableClass] [PushExpression( StackTypes.IntegerVector, "INTEGER[].LAST", "Gets the last item from the top INTEGER[].", StackTypes.Integer)] public class IntegerVectorLastExpression : VectorLastExpression { public IntegerVectorLastExpression() { } [StorableConstructor] protected IntegerVectorLastExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.IntegerVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack); } } [StorableClass] [PushExpression( StackTypes.FloatVector, "FLOAT[].LAST", "Gets the last item from the top FLOAT[].", StackTypes.Float)] public class FloatVectorLastExpression : VectorLastExpression { public FloatVectorLastExpression() { } [StorableConstructor] protected FloatVectorLastExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.FloatVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.FloatVectorStack, interpreter.FloatStack); } } [StorableClass] [PushExpression( StackTypes.BooleanVector, "BOOLEAN[].LAST", "Gets the last item from the top BOOLEAN[].", StackTypes.Boolean)] public class BooleanVectorLastExpression : VectorLastExpression { public BooleanVectorLastExpression() { } [StorableConstructor] protected BooleanVectorLastExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.BooleanVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack); } } [StorableClass] [PushExpression( StackTypes.StringVector, "STRING[].LAST", "Gets the last item from the top STRING[].", StackTypes.String)] public class StringVectorLastExpression : VectorLastExpression { public StringVectorLastExpression() { } [StorableConstructor] protected StringVectorLastExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return IsNoop(interpreter.StringVectorStack); } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.StringVectorStack, interpreter.StringStack); } } }