using System; using System.Collections.Generic; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.ProgramSynthesis { /// /// Defines the name on top of the NAME stack as an instruction that will push the top item of the T stack onto the EXEC stack. /// /// Stack item type [StorableClass] public abstract class DefineExpression : StatelessExpression { protected DefineExpression() { } [StorableConstructor] protected DefineExpression(bool deserializing) : base(deserializing) { } protected void Eval( IPushStack stack, IPushStack nameStack, IDictionary customExpressions, Func creator) { var name = nameStack.Pop(); var expression = creator(stack.Top); if (customExpressions.ContainsKey(name)) customExpressions[name] = expression; else customExpressions.Add(name, expression); } } [PushExpression( StackTypes.Code, "CODE.DEFINE", "Defines the name on top of the NAME stack as an instruction that will push the top item of the CODE stack onto the EXEC stack.", StackTypes.Name)] [StorableClass] public class CodeDefineExpression : DefineExpression { public CodeDefineExpression() { } [StorableConstructor] protected CodeDefineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return (interpreter.Configuration.TopLevelPushCode && interpreter.CodeStack.Count < 2) || interpreter.CodeStack.IsEmpty || interpreter.NameStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var pool = interpreter.PoolContainer.GetStatefulExpressionPool(); Eval( interpreter.CodeStack, interpreter.NameStack, interpreter.CustomExpressions, v => ExecPushExpression.Create(pool, v)); interpreter.CodeStack.Pop(); } } [PushExpression( StackTypes.Exec, "EXEC.DEFINE", "Defines the name on top of the NAME stack as an instruction that will push the top item of the EXEC stack onto the EXEC stack.", StackTypes.Name, requiredBlockCount: 1)] [StorableClass] public class ExecDefineExpression : DefineExpression { public ExecDefineExpression() { } [StorableConstructor] protected ExecDefineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.ExecStack.Count < 2 || interpreter.NameStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var pool = interpreter.PoolContainer.GetStatefulExpressionPool(); Eval( interpreter.ExecStack, interpreter.NameStack, interpreter.CustomExpressions, v => ExecPushExpression.Create(pool, v)); interpreter.ExecStack.Pop(); } } [PushExpression( StackTypes.Float, "FLOAT.DEFINE", "Defines the name on top of the NAME stack as an instruction that will push the top item of the FLOAT stack onto the EXEC stack.", StackTypes.Name)] [StorableClass] public class FloatDefineExpression : DefineExpression { public FloatDefineExpression() { } [StorableConstructor] protected FloatDefineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.IsEmpty || interpreter.NameStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var pool = interpreter.PoolContainer.GetStatefulExpressionPool(); Eval( interpreter.FloatStack, interpreter.NameStack, interpreter.CustomExpressions, v => FloatPushExpression.Create(pool, v)); } } [PushExpression( StackTypes.Integer, "INTEGER.DEFINE", "Defines the name on top of the NAME stack as an instruction that will push the top item of the INTEGER stack onto the EXEC stack.", StackTypes.Name)] [StorableClass] public class IntegerDefineExpression : DefineExpression { public IntegerDefineExpression() { } [StorableConstructor] protected IntegerDefineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.IsEmpty || interpreter.NameStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var pool = interpreter.PoolContainer.GetStatefulExpressionPool(); Eval( interpreter.IntegerStack, interpreter.NameStack, interpreter.CustomExpressions, v => IntegerPushExpression.Create(pool, v)); } } [PushExpression( StackTypes.Boolean, "BOOLEAN.DEFINE", "Defines the name on top of the NAME stack as an instruction that will push the top item of the BOOLEAN stack onto the EXEC stack.", StackTypes.Name)] [StorableClass] public class BooleanDefineExpression : DefineExpression { public BooleanDefineExpression() { } [StorableConstructor] protected BooleanDefineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.BooleanStack.IsEmpty || interpreter.NameStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var pool = interpreter.PoolContainer.GetStatefulExpressionPool(); Eval( interpreter.BooleanStack, interpreter.NameStack, interpreter.CustomExpressions, v => BooleanPushExpression.Create(pool, v)); } } [PushExpression( StackTypes.Char, "CHAR.DEFINE", "Defines the name on top of the NAME stack as an instruction that will push the top item of the CHAR stack onto the EXEC stack.", StackTypes.Name)] [StorableClass] public class CharDefineExpression : DefineExpression { public CharDefineExpression() { } [StorableConstructor] protected CharDefineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.CharStack.IsEmpty || interpreter.NameStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var pool = interpreter.PoolContainer.GetStatefulExpressionPool(); Eval( interpreter.CharStack, interpreter.NameStack, interpreter.CustomExpressions, v => CharPushExpression.Create(pool, v)); } } [PushExpression( StackTypes.String, "STRING.DEFINE", "Defines the name on top of the NAME stack as an instruction that will push the top item of the STRING stack onto the EXEC stack.", StackTypes.Name)] [StorableClass] public class StringDefineExpression : DefineExpression { public StringDefineExpression() { } [StorableConstructor] protected StringDefineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.StringStack.IsEmpty || interpreter.NameStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var pool = interpreter.PoolContainer.GetStatefulExpressionPool(); Eval( interpreter.StringStack, interpreter.NameStack, interpreter.CustomExpressions, v => StringPushExpression.Create(pool, v)); } } [PushExpression( StackTypes.IntegerVector, "INTEGER[].DEFINE", "Defines the name on top of the NAME stack as an instruction that will push the top item of the INTEGER[] stack onto the EXEC stack.", StackTypes.Name)] [StorableClass] public class IntegerVectorDefineExpression : DefineExpression> { public IntegerVectorDefineExpression() { } [StorableConstructor] protected IntegerVectorDefineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerVectorStack.IsEmpty || interpreter.NameStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var pool = interpreter.PoolContainer.GetStatefulExpressionPool(); Eval( interpreter.IntegerVectorStack, interpreter.NameStack, interpreter.CustomExpressions, v => IntegerVectorPushExpression.Create(pool, v)); } } [PushExpression( StackTypes.FloatVector, "FLOAT[].DEFINE", "Defines the name on top of the NAME stack as an instruction that will push the top item of the FLOAT[] stack onto the EXEC stack.", StackTypes.Name)] [StorableClass] public class FloatVectorDefineExpression : DefineExpression> { public FloatVectorDefineExpression() { } [StorableConstructor] protected FloatVectorDefineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatVectorStack.IsEmpty || interpreter.NameStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var pool = interpreter.PoolContainer.GetStatefulExpressionPool(); Eval( interpreter.FloatVectorStack, interpreter.NameStack, interpreter.CustomExpressions, v => FloatVectorPushExpression.Create(pool, v)); } } [PushExpression( StackTypes.BooleanVector, "BOOLEAN[].DEFINE", "Defines the name on top of the NAME stack as an instruction that will push the top item of the BOOLEAN[] stack onto the EXEC stack.", StackTypes.Name)] [StorableClass] public class BooleanVectorDefineExpression : DefineExpression> { public BooleanVectorDefineExpression() { } [StorableConstructor] protected BooleanVectorDefineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.BooleanVectorStack.IsEmpty || interpreter.NameStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var pool = interpreter.PoolContainer.GetStatefulExpressionPool(); Eval( interpreter.BooleanVectorStack, interpreter.NameStack, interpreter.CustomExpressions, v => BooleanVectorPushExpression.Create(pool, v)); } } [PushExpression( StackTypes.StringVector, "STRING[].DEFINE", "Defines the name on top of the NAME stack as an instruction that will push the top item of the STRING[] stack onto the EXEC stack.", StackTypes.Name)] [StorableClass] public class StringVectorDefineExpression : DefineExpression> { public StringVectorDefineExpression() { } [StorableConstructor] protected StringVectorDefineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.StringVectorStack.IsEmpty || interpreter.NameStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var pool = interpreter.PoolContainer.GetStatefulExpressionPool(); Eval( interpreter.StringVectorStack, interpreter.NameStack, interpreter.CustomExpressions, v => StringVectorPushExpression.Create(pool, v)); } } }