namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { using System; using System.Globalization; using Common; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes; using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter; using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; /// /// Pushes the sum of the top two items. /// [PushExpression( StackTypes.Float, "FLOAT.+", "Pushes the sum of the top two items.")] [StorableClass] public class FloatAddExpression : StatelessExpression { public FloatAddExpression() { } [StorableConstructor] protected FloatAddExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.FloatStack.Pop(); var second = interpreter.FloatStack.Top; var result = second + first; if (double.IsPositiveInfinity(result)) result = double.MaxValue; if (double.IsNegativeInfinity(result)) result = double.MinValue; interpreter.FloatStack.Top = result; } } /// /// Pushes the difference of the top two items; that is, the second item minus the top item. /// [PushExpression( StackTypes.Float, "FLOAT.-", "Pushes the difference of the top two items; that is, the second item minus the top item.")] [StorableClass] public class FloatSubtractExpression : StatelessExpression { public FloatSubtractExpression() { } [StorableConstructor] protected FloatSubtractExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.FloatStack.Pop(); var second = interpreter.FloatStack.Top; var result = second - first; if (double.IsPositiveInfinity(result)) result = double.MaxValue; if (double.IsNegativeInfinity(result)) result = double.MinValue; interpreter.FloatStack.Top = result; } } /// /// Pushes the product of the top two items. /// [PushExpression( StackTypes.Float, "FLOAT.*", "Pushes the product of the top two items.")] [StorableClass] public class FloatMultiplyExpression : StatelessExpression { public FloatMultiplyExpression() { } [StorableConstructor] protected FloatMultiplyExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.FloatStack.Pop(); var second = interpreter.FloatStack.Top; var result = second * first; if (double.IsPositiveInfinity(result)) result = double.MaxValue; if (double.IsNegativeInfinity(result)) result = double.MinValue; interpreter.FloatStack.Top = result; } } /// /// Pushes the quotient of the top two items; that is, the second item divided by the top item. /// If the top item is zero this acts as a NOOP. /// [PushExpression( StackTypes.Float, "FLOAT./", "Pushes the quotient of the top two items; that is, the second item divided by the top item. If the top item is zero this acts as a NOOP.")] [StorableClass] public class FloatDivideExpression : StatelessExpression { public FloatDivideExpression() { } [StorableConstructor] protected FloatDivideExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.Count < 2 || interpreter.FloatStack.Top.IsAlmost(0); } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.FloatStack.Pop(); var second = interpreter.FloatStack.Top; var result = second / first; if (double.IsPositiveInfinity(result)) result = double.MaxValue; if (double.IsNegativeInfinity(result)) result = double.MinValue; interpreter.FloatStack.Top = result; } } /// /// Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP. /// [PushExpression( StackTypes.Float, "FLOAT.%", "Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP.")] [StorableClass] public class FloatModuloExpression : StatelessExpression { public FloatModuloExpression() { } [StorableConstructor] protected FloatModuloExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.Count < 2 || interpreter.FloatStack.Top.IsAlmost(0); } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.FloatStack.Pop(); var second = interpreter.FloatStack.Top; var result = second % first; interpreter.FloatStack.Top = result; } } /// /// Pushes the minimum of the top two items. /// [PushExpression( StackTypes.Float, "FLOAT.MIN", "Pushes the minimum of the top two items.")] [StorableClass] public class FloatMinExpression : StatelessExpression { public FloatMinExpression() { } [StorableConstructor] protected FloatMinExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.FloatStack.Pop(); var second = interpreter.FloatStack.Top; var result = Math.Min(second, first); interpreter.FloatStack.Top = result; } } /// /// Pushes the maximum of the top two items. /// [PushExpression( StackTypes.Float, "FLOAT.MAX", "Pushes the maximum of the top two items.")] [StorableClass] public class FloatMaxExpression : StatelessExpression { public FloatMaxExpression() { } [StorableConstructor] protected FloatMaxExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.FloatStack.Pop(); var second = interpreter.FloatStack.Top; var result = Math.Max(second, first); interpreter.FloatStack.Top = result; } } /// /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise. /// [PushExpression( StackTypes.Float, "FLOAT.<", "Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.", StackTypes.Boolean)] [StorableClass] public class FloatSmallerThanExpression : StatelessExpression { public FloatSmallerThanExpression() { } [StorableConstructor] protected FloatSmallerThanExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.FloatStack.Top; var second = interpreter.FloatStack[1]; interpreter.FloatStack.Remove(2); var result = second < first; interpreter.BooleanStack.Push(result); } } /// /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise. /// [PushExpression( StackTypes.Float, "FLOAT.<=", "Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.", StackTypes.Boolean)] [StorableClass] public class FloatSmallerThanOrEqualExpression : StatelessExpression { public FloatSmallerThanOrEqualExpression() { } [StorableConstructor] protected FloatSmallerThanOrEqualExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.FloatStack.Top; var second = interpreter.FloatStack[1]; interpreter.FloatStack.Remove(2); var result = second < first || second.IsAlmost(first); interpreter.BooleanStack.Push(result); } } /// /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise. /// [PushExpression( StackTypes.Float, "FLOAT.>", "Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.", StackTypes.Boolean)] [StorableClass] public class FloatGreaterThanExpression : StatelessExpression { public FloatGreaterThanExpression() { } [StorableConstructor] protected FloatGreaterThanExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.FloatStack.Top; var second = interpreter.FloatStack[1]; interpreter.FloatStack.Remove(2); var result = second > first; interpreter.BooleanStack.Push(result); } } /// /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than or equal to the top item, or FALSE otherwise. /// [PushExpression( StackTypes.Float, "FLOAT.>=", "Pushes TRUE onto the BOOLEAN stack if the second item is greater than or equal to the top item, or FALSE otherwise.", StackTypes.Boolean)] [StorableClass] public class FloatGreaterThanOrEqualExpression : StatelessExpression { public FloatGreaterThanOrEqualExpression() { } [StorableConstructor] protected FloatGreaterThanOrEqualExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.FloatStack.Top; var second = interpreter.FloatStack[1]; interpreter.FloatStack.Remove(2); var result = second > first || second.IsAlmost(first); interpreter.BooleanStack.Push(result); } } /// /// Pushes the sine of the top item. /// [PushExpression(StackTypes.Float, "FLOAT.SIN", "Pushes the sine of the top item.")] [StorableClass] public class FloatSineExpression : StatelessExpression { public FloatSineExpression() { } [StorableConstructor] protected FloatSineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var result = Math.Sin(interpreter.FloatStack.Top); interpreter.FloatStack.Top = result; } } /// /// Pushes the cosine of the top item. /// [PushExpression(StackTypes.Float, "FLOAT.COS", "Pushes the cosine of the top item.")] [StorableClass] public class FloatCosineExpression : StatelessExpression { public FloatCosineExpression() { } [StorableConstructor] protected FloatCosineExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var result = Math.Cos(interpreter.FloatStack.Top); interpreter.FloatStack.Top = result; } } /// /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE. /// [PushExpression( StackTypes.Float, "FLOAT.FROMBOOLEAN", "Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.", StackTypes.Boolean)] [StorableClass] public class FloatFromBooleanExpression : StatelessExpression { public FloatFromBooleanExpression() { } [StorableConstructor] protected FloatFromBooleanExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.BooleanStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var condition = interpreter.BooleanStack.Pop(); var value = condition ? 1 : 0; interpreter.FloatStack.Push(value); } } /// /// Pushes a floating point version of the top INTEGER. /// [PushExpression( StackTypes.Float, "FLOAT.FROMINTEGER", "Pushes a floating point version of the top INTEGER.", StackTypes.Integer)] [StorableClass] public class FloatFromIntegerExpression : StatelessExpression { public FloatFromIntegerExpression() { } [StorableConstructor] protected FloatFromIntegerExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var value = (double)interpreter.IntegerStack.Pop(); interpreter.FloatStack.Push(value); } } /// /// Pushes the result of truncating the top CHAR. /// [PushExpression( StackTypes.Float, "FLOAT.FROMCHAR", "Pushes the result of truncating the top CHAR.", StackTypes.Char)] [StorableClass] public class FloatFromCharExpression : StatelessExpression { public FloatFromCharExpression() { } [StorableConstructor] protected FloatFromCharExpression(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.FloatStack.Push(value); } } /// /// Pushes the result of parsing the top STRING. /// [PushExpression( StackTypes.Float, "FLOAT.FROMSTRING", "Pushes the result of parsing the top STRING.", StackTypes.String)] [StorableClass] public class FloatFromStringExpression : StatelessExpression { public FloatFromStringExpression() { } [StorableConstructor] protected FloatFromStringExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { double tmp; return interpreter.StringStack.IsEmpty || interpreter.StringStack.Top.Length == 0 || !double.TryParse( interpreter.StringStack.Top, NumberStyles.AllowDecimalPoint | NumberStyles.Float, CultureInfo.InvariantCulture, out tmp); } public override void Eval(IInternalPushInterpreter interpreter) { var str = interpreter.StringStack.Pop(); var value = double.Parse(str, NumberStyles.AllowDecimalPoint | NumberStyles.Float, CultureInfo.InvariantCulture); interpreter.FloatStack.Push(value); } } /// /// Pushes the result of increasing the top INTEGER by 1. /// [PushExpression( StackTypes.Float, "FLOAT.INC", "Pushes the result of increasing the top INTEGER by 1.")] [StorableClass] public class FloatIncExpression : StatelessExpression { public FloatIncExpression() { } [StorableConstructor] protected FloatIncExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { interpreter.FloatStack.Top += 1; } } /// /// Pushes the result of decreasing the top INTEGER by 1. /// [PushExpression( StackTypes.Float, "FLOAT.DEC", "Pushes the result of decreasing the top INTEGER by 1.")] [StorableClass] public class FloatDecExpression : StatelessExpression { public FloatDecExpression() { } [StorableConstructor] protected FloatDecExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { interpreter.FloatStack.Top -= 1; } } }