using System.Collections.Generic; using System.Globalization; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.ProgramSynthesis { [StorableClass] [PushExpression(StackTypes.Print, "PRINT.NEWLINE", "Pushes an empty string onto the PRINT STACK.")] public class PrintNewLineExpression : StatelessExpression { public PrintNewLineExpression() { } [StorableConstructor] protected PrintNewLineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return false; } public override void Eval(IInternalPushInterpreter interpreter) { interpreter.PrintStack.NewLine(); } } [StorableClass] [PushExpression(StackTypes.Print, "PRINT.ENSURE_NEWLINE", "Pushes an empty string onto the PRINT STACK if current line isn't empty already.")] public class PrintEnsureNewLineExpression : StatelessExpression { public PrintEnsureNewLineExpression() { } [StorableConstructor] protected PrintEnsureNewLineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.PrintStack.IsCurrentLineEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { interpreter.PrintStack.NewLine(); } } [StorableClass] public abstract class PrintExpression : StatelessExpression { protected PrintExpression() { } [StorableConstructor] protected PrintExpression(bool deserializing) : base(deserializing) { } protected void Eval(IInternalPushInterpreter interpreter, IPushStack stack) { var value = stack.Pop(); interpreter.PrintStack.Push(value); } protected void EvalVector(IInternalPushInterpreter interpreter, IPushStack> vectorStack) { var vector = vectorStack.Pop(); interpreter.PrintStack.Push(PushEnvironment.VectorStartSymbol); if (vector.Count > 0) { interpreter.PrintStack.Push(vector[0]); for (var i = 1; i < vector.Count; i++) { interpreter.PrintStack.Push(PushEnvironment.VectorSeparatorSymbol); interpreter.PrintStack.Push(vector[i]); } } interpreter.PrintStack.Push(PushEnvironment.VectorEndSymbol); } } [StorableClass] [PushExpression( StackTypes.Print, "PRINT.PRINTBOOLEAN", "Pushes the top BOOLEAN onto the PRINT stack.", StackTypes.Boolean)] public class BooleanPrintExpression : PrintExpression { public BooleanPrintExpression() { } [StorableConstructor] protected BooleanPrintExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.BooleanStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var value = interpreter.BooleanStack.Pop(); interpreter.PrintStack.Push(value); } } [StorableClass] [PushExpression( StackTypes.Print, "PRINT.PRINTCHAR", "Pushes the top CHAR onto the PRINT stack.", StackTypes.Char)] public class CharPrintExpression : PrintExpression { public CharPrintExpression() { } [StorableConstructor] protected CharPrintExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.CharStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var value = interpreter.CharStack.Pop(); interpreter.PrintStack.Push(value); } } [StorableClass] [PushExpression( StackTypes.Print, "PRINT.PRINTEXEC", "Pushes the top BOOLEAN onto the EXEC stack.", StackTypes.Exec, requiredBlockCount: 1)] public class ExecPrintExpression : PrintExpression { public ExecPrintExpression() { } [StorableConstructor] protected ExecPrintExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.ExecStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var value = interpreter.ExecStack.Pop(); interpreter.PrintStack.Push(value); } } [StorableClass] [PushExpression( StackTypes.Print, "PRINT.PRINTFLOAT", "Pushes the top FLOAT onto the PRINT stack.", StackTypes.Float)] public class FloatPrintExpression : PrintExpression { public FloatPrintExpression() { } [StorableConstructor] protected FloatPrintExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var value = interpreter.FloatStack.Pop(); var str = value.ToString(interpreter.Configuration.FloatStringFormat, CultureInfo.InvariantCulture); interpreter.PrintStack.Push(str); } } [StorableClass] [PushExpression( StackTypes.Print, "PRINT.PRINTINTEGER", "Pushes the top INTEGER onto the PRINT stack.", StackTypes.Integer)] public class IntegerPrintExpression : PrintExpression { public IntegerPrintExpression() { } [StorableConstructor] protected IntegerPrintExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var value = interpreter.IntegerStack.Pop(); interpreter.PrintStack.Push(value); } } [StorableClass] [PushExpression( StackTypes.Print, "PRINT.PRINTSTRING", "Pushes the top STRING onto the PRINT stack.", StackTypes.String)] public class StringPrintExpression : PrintExpression { public StringPrintExpression() { } [StorableConstructor] protected StringPrintExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.StringStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var value = interpreter.StringStack.Pop(); interpreter.PrintStack.Push(value); } } [StorableClass] [PushExpression( StackTypes.Print, "PRINT.PRINTINTEGERVECTOR", "Pushes the top INTEGER[] onto the PRINT stack.", StackTypes.IntegerVector)] public class IntegerVectorPrintExpression : PrintExpression { public IntegerVectorPrintExpression() { } [StorableConstructor] protected IntegerVectorPrintExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerVectorStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { EvalVector(interpreter, interpreter.IntegerVectorStack); } } [StorableClass] [PushExpression( StackTypes.Print, "PRINT.PRINTFLOATVECTOR", "Pushes the top FLOAT[] onto the PRINT stack.", StackTypes.FloatVector)] public class FloatVectorPrintExpression : PrintExpression { public FloatVectorPrintExpression() { } [StorableConstructor] protected FloatVectorPrintExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatVectorStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { EvalVector(interpreter, interpreter.FloatVectorStack); } } [StorableClass] [PushExpression( StackTypes.Print, "PRINT.PRINTSTRINGVECTOR", "Pushes the top STRING[] onto the PRINT stack.", StackTypes.StringVector)] public class StringVectorPrintExpression : PrintExpression { public StringVectorPrintExpression() { } [StorableConstructor] protected StringVectorPrintExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.StringVectorStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { EvalVector(interpreter, interpreter.StringVectorStack); } } [StorableClass] [PushExpression( StackTypes.Print, "PRINT.PRINTBOOLEANVECTOR", "Pushes the top BOOLEAN[] onto the PRINT stack.", StackTypes.BooleanVector)] public class BooleanVectorPrintExpression : PrintExpression { public BooleanVectorPrintExpression() { } [StorableConstructor] protected BooleanVectorPrintExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.BooleanVectorStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { EvalVector(interpreter, interpreter.BooleanVectorStack); } } }