using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.ProgramSynthesis { [Serializable] [StorableClass] public abstract class PushExpression : StatefulExpression { [StorableConstructor] protected PushExpression(bool deserializing) : base(deserializing) { } protected PushExpression(T state, object other) : base(state) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return false; } protected void Eval(IPushStack stack) { stack.Push(State); } } [StorableClass] [Serializable] public abstract class VectorPushExpression : PushExpression> { [StorableConstructor] protected VectorPushExpression(bool deserializing) : base(deserializing) { } protected VectorPushExpression(IReadOnlyList state) : base(state, null) { } protected override string GetStringRepresentation() { return PushEnvironment.VectorStartSymbolStr + string.Join(PushEnvironment.VectorSeparatorSymbolStr, State) + PushEnvironment.VectorEndSymbolStr; } protected override int CalcHashCode() { return State.HashCode(); } } [StorableClass] [Serializable] [PushExpression(StackTypes.Integer, "INTEGER.PUSH", "Pushes a specific integer onto the INTEGER stack", isHidden: true)] public class IntegerPushExpression : PushExpression { [StorableConstructor] protected IntegerPushExpression(bool deserializing) : base(deserializing) { } public static IntegerPushExpression Create(IManagedPool statefulExpressionPool, long state) { var expression = (IntegerPushExpression)statefulExpressionPool.Get(); expression.State = state; return expression; } /// /// Pushes default(long) /// public IntegerPushExpression() : base(default(long), null) { } public IntegerPushExpression(long state) : base(state, null) { } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.IntegerStack); } } [StorableClass] [Serializable] [PushExpression(StackTypes.Float, "FLOAT.PUSH", "Pushes a specific float onto the FLOAT stack", isHidden: true)] public class FloatPushExpression : PushExpression { [StorableConstructor] protected FloatPushExpression(bool deserializing) : base(deserializing) { } public static FloatPushExpression Create(IManagedPool statefulExpressionPool, double state) { var expression = (FloatPushExpression)statefulExpressionPool.Get(); expression.State = state; return expression; } /// /// Pushes default(double) /// public FloatPushExpression() : base(default(double), null) { } public FloatPushExpression(double state) : base(state, null) { } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.FloatStack); } } [StorableClass] [Serializable] [PushExpression(StackTypes.Boolean, "BOOLEAN.PUSH", "Pushes a specific boolean onto the BOOLEAN stack", isHidden: true)] public class BooleanPushExpression : PushExpression { [StorableConstructor] protected BooleanPushExpression(bool deserializing, object other) : base(deserializing) { } public static BooleanPushExpression Create(IManagedPool statefulExpressionPool, bool state) { var expression = (BooleanPushExpression)statefulExpressionPool.Get(); expression.State = state; return expression; } /// /// Pushes default(bool) /// public BooleanPushExpression() : base(default(bool), null) { } public BooleanPushExpression(bool state) : base(state, null) { } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.BooleanStack); } } [StorableClass] [Serializable] [PushExpression(StackTypes.Name, "NAME.PUSH", "Pushes a specific name onto the NAME stack", isHidden: true)] public class NamePushExpression : PushExpression { [StorableConstructor] protected NamePushExpression(bool deserializing) : base(deserializing) { } public static NamePushExpression Create(IManagedPool statefulExpressionPool, string state) { var expression = (NamePushExpression)statefulExpressionPool.Get(); expression.State = state; return expression; } public NamePushExpression() : base(string.Empty, null) { } public NamePushExpression(string state) : base(state, null) { } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.NameStack); } } [StorableClass] [Serializable] [PushExpression(StackTypes.Exec, "EXEC.PUSH", "Pushes a specific expression onto the EXEC stack", isHidden: true)] public class ExecPushExpression : PushExpression { [StorableConstructor] protected ExecPushExpression(bool deserializing) : base(deserializing) { } public static ExecPushExpression Create(IManagedPool statefulExpressionPool, Expression state) { var expression = (ExecPushExpression)statefulExpressionPool.Get(); expression.State = state; return expression; } private static readonly Expression Noop = ExpressionTable.GetStatelessExpression(); /// /// Pushes ExecNoop /// public ExecPushExpression() : base(Noop, null) { } public ExecPushExpression(Expression state) : base(state, null) { } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.ExecStack); } } [StorableClass] [Serializable] [PushExpression(StackTypes.Char, "CHAR.PUSH", "Pushes a specific char onto the CHAR stack", isHidden: true)] public class CharPushExpression : PushExpression { [StorableConstructor] protected CharPushExpression(bool deserializing) : base(deserializing) { } // not supported so far public static CharPushExpression Create(IManagedPool statefulExpressionPool, char state) { var expression = (CharPushExpression)statefulExpressionPool.Get(); expression.State = state; return expression; } /// /// Pushes default(char) /// public CharPushExpression() : base(default(char), null) { } public CharPushExpression(char state) : base(state, null) { } protected override string GetStringRepresentation() { return PushEnvironment.CharSymbol + State.Printify() + PushEnvironment.CharSymbol; } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.CharStack); } } [StorableClass] [Serializable] [PushExpression(StackTypes.String, "STRING.PUSH", "Pushes a specific string onto the STRING stack", isHidden: true)] public class StringPushExpression : PushExpression { [StorableConstructor] protected StringPushExpression(bool deserializing) : base(deserializing) { } public static StringPushExpression Create(IManagedPool statefulExpressionPool, string state) { var expression = (StringPushExpression)statefulExpressionPool.Get(); expression.State = state; return expression; } /// /// Pushes String.Empty /// public StringPushExpression() : base(string.Empty, null) { } protected override string GetStringRepresentation() { return PushEnvironment.StringSymbol + State + PushEnvironment.StringSymbol; } public StringPushExpression(string state) : base(state, null) { } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.StringStack); } } [StorableClass] [Serializable] [PushExpression(StackTypes.IntegerVector, "INTEGER[].PUSH", "Pushes a specific integer vector onto the INTEGER[] stack", isHidden: true)] public class IntegerVectorPushExpression : VectorPushExpression { [StorableConstructor] protected IntegerVectorPushExpression(bool deserializing) : base(deserializing) { } public static IntegerVectorPushExpression Create(IManagedPool statefulExpressionPool, IReadOnlyList state) { var expression = (IntegerVectorPushExpression)statefulExpressionPool.Get(); expression.State = state; return expression; } [NonSerialized] private static readonly long[] EmptyState = new long[0]; /// /// Pushes long[0] /// public IntegerVectorPushExpression() : base(EmptyState) { } public IntegerVectorPushExpression(IReadOnlyList state) : base(state) { } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.IntegerVectorStack); } } [StorableClass] [Serializable] [PushExpression(StackTypes.FloatVector, "FLOAT[].PUSH", "Pushes a specific float vector onto the FLOAT[] stack", isHidden: true)] public class FloatVectorPushExpression : VectorPushExpression { [StorableConstructor] protected FloatVectorPushExpression(bool deserializing) : base(deserializing) { } public static FloatVectorPushExpression Create(IManagedPool statefulExpressionPool, IReadOnlyList state) { var expression = (FloatVectorPushExpression)statefulExpressionPool.Get(); expression.State = state; return expression; } [NonSerialized] private static readonly double[] EmptyState = new double[0]; /// /// Pushes double[0] /// public FloatVectorPushExpression() : base(EmptyState) { } public FloatVectorPushExpression(IReadOnlyList state) : base(state) { } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.FloatVectorStack); } } [StorableClass] [Serializable] [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].PUSH", "Pushes a specific boolean vector onto the BOOLEAN[] stack", isHidden: true)] public class BooleanVectorPushExpression : VectorPushExpression { [StorableConstructor] protected BooleanVectorPushExpression(bool deserializing) : base(deserializing) { } public static BooleanVectorPushExpression Create(IManagedPool statefulExpressionPool, IReadOnlyList state) { var expression = (BooleanVectorPushExpression)statefulExpressionPool.Get(); expression.State = state; return expression; } [NonSerialized] private static readonly bool[] EmptyState = new bool[0]; /// /// Pushes bool[0] /// public BooleanVectorPushExpression() : base(EmptyState) { } public BooleanVectorPushExpression(IReadOnlyList state) : base(state) { } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.BooleanVectorStack); } } [StorableClass] [Serializable] [PushExpression(StackTypes.StringVector, "STRING[].PUSH", "Pushes a specific string vector onto the STRING[] stack", isHidden: true)] public class StringVectorPushExpression : VectorPushExpression { [StorableConstructor] protected StringVectorPushExpression(bool deserializing) : base(deserializing) { } public static StringVectorPushExpression Create(IManagedPool statefulExpressionPool, IReadOnlyList state) { var expression = (StringVectorPushExpression)statefulExpressionPool.Get(); expression.State = state; return expression; } [NonSerialized] private static readonly string[] EmptyState = new string[0]; /// /// Pushes string[0] /// public StringVectorPushExpression() : base(EmptyState) { } protected override string GetStringRepresentation() { return PushEnvironment.VectorStartSymbolStr + string.Join(PushEnvironment.VectorSeparatorSymbolStr, State.Select(str => PushEnvironment.StringSymbol + str + PushEnvironment.StringSymbol)) + PushEnvironment.VectorEndSymbolStr; } public StringVectorPushExpression(IReadOnlyList state) : base(state) { } public override void Eval(IInternalPushInterpreter interpreter) { Eval(interpreter.StringVectorStack); } } }