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 first item from the type stack /// /// [StorableClass] public abstract class VectorFirstExpression : StatelessExpression { protected VectorFirstExpression() { } [StorableConstructor] protected VectorFirstExpression(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[0]); } } [StorableClass] [PushExpression( StackTypes.IntegerVector, "INTEGER[].FIRST", "Pushes the first INTEGER of the top INTEGER[].", StackTypes.Integer)] public class IntegerVectorFirstExpression : VectorFirstExpression { public IntegerVectorFirstExpression() { } [StorableConstructor] protected IntegerVectorFirstExpression(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[].FIRST", "Pushes the first FLOAT of the top FLOAT[].", StackTypes.Float)] public class FloatVectorFirstExpression : VectorFirstExpression { public FloatVectorFirstExpression() { } [StorableConstructor] protected FloatVectorFirstExpression(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[].FIRST", "Pushes the first BOOLEAN of the top BOOLEAN[].", StackTypes.Boolean)] public class BooleanVectorFirstExpression : VectorFirstExpression { public BooleanVectorFirstExpression() { } [StorableConstructor] protected BooleanVectorFirstExpression(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[].FIRST", "Pushes the first BOOLEAN of the top BOOLEAN[].", StackTypes.String)] public class StringVectorFirstExpression : VectorFirstExpression { public StringVectorFirstExpression() { } [StorableConstructor] protected StringVectorFirstExpression(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); } } }