using System; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.ProgramSynthesis { /// /// Pushes the sum of the top two items. /// [PushExpression( StackTypes.Integer, "INTEGER.+", "Pushes the sum of the top two items.")] [StorableClass] public class IntegerAddExpression : StatelessExpression { public IntegerAddExpression() { } [StorableConstructor] protected IntegerAddExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.IntegerStack.Pop(); var second = interpreter.IntegerStack.Top; var result = unchecked(second + first); interpreter.IntegerStack.Top = result; } } /// /// Pushes the difference of the top two items; that is, the second item minus the top item. /// [PushExpression( StackTypes.Integer, "INTEGER.-", "Pushes the difference of the top two items; that is, the second item minus the top item.")] [StorableClass] public class IntegerSubtractExpression : StatelessExpression { public IntegerSubtractExpression() { } [StorableConstructor] protected IntegerSubtractExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.IntegerStack.Pop(); var second = interpreter.IntegerStack.Top; var result = unchecked(second - first); interpreter.IntegerStack.Top = result; } } /// /// Pushes the product of the top two items. /// [PushExpression( StackTypes.Integer, "INTEGER.*", "Pushes the product of the top two items.")] [StorableClass] public class IntegerMultiplyExpression : StatelessExpression { public IntegerMultiplyExpression() { } [StorableConstructor] protected IntegerMultiplyExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.IntegerStack.Pop(); var second = interpreter.IntegerStack.Top; var result = unchecked(second * first); interpreter.IntegerStack.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.Integer, "INTEGER./", "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 IntegerDivideExpression : StatelessExpression { public IntegerDivideExpression() { } [StorableConstructor] protected IntegerDivideExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.Count < 2 || interpreter.IntegerStack.Top == 0 || (interpreter.IntegerStack.Top == -1 && interpreter.IntegerStack[1] == long.MinValue); } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.IntegerStack.Pop(); var second = interpreter.IntegerStack.Top; var result = second / first; interpreter.IntegerStack.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.Integer, "INTEGER.%", "Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP.")] [StorableClass] public class IntegerModuloExpression : StatelessExpression { public IntegerModuloExpression() { } [StorableConstructor] protected IntegerModuloExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.Count < 2 || interpreter.IntegerStack.Top == 0 || (interpreter.IntegerStack.Top == -1 && interpreter.IntegerStack[1] == long.MinValue); } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.IntegerStack.Pop(); var second = interpreter.IntegerStack.Top; var result = second % first; interpreter.IntegerStack.Top = result; } } /// /// Pushes the minimum of the top two items. /// [PushExpression( StackTypes.Integer, "INTEGER.MIN", "Pushes the minimum of the top two items.")] [StorableClass] public class IntegerMinExpression : StatelessExpression { public IntegerMinExpression() { } [StorableConstructor] protected IntegerMinExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.IntegerStack.Pop(); var second = interpreter.IntegerStack.Top; var result = Math.Min(second, first); interpreter.IntegerStack.Top = result; } } /// /// Pushes the maximum of the top two items. /// [PushExpression( StackTypes.Integer, "INTEGER.MAX", "Pushes the maximum of the top two items.")] [StorableClass] public class IntegerMaxExpression : StatelessExpression { public IntegerMaxExpression() { } [StorableConstructor] protected IntegerMaxExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.IntegerStack.Pop(); var second = interpreter.IntegerStack.Top; var result = Math.Max(second, first); interpreter.IntegerStack.Top = result; } } /// /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise. /// [PushExpression( StackTypes.Integer, "INTEGER.<", "Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise.", StackTypes.Boolean)] [StorableClass] public class IntegerSmallerThanExpression : StatelessExpression { public IntegerSmallerThanExpression() { } [StorableConstructor] protected IntegerSmallerThanExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.IntegerStack.Top; var second = interpreter.IntegerStack[1]; interpreter.IntegerStack.Remove(2); var result = second < first; interpreter.BooleanStack.Push(result); } } /// /// Pushes TRUE onto the BOOLEAN stack if the second item is less than or equal to the top item, or FALSE otherwise. /// [PushExpression( StackTypes.Integer, "INTEGER.<=", "Pushes TRUE onto the BOOLEAN stack if the second item is less than or equal to the top item, or FALSE otherwise.", StackTypes.Boolean)] [StorableClass] public class IntegerSmallerOrEqualToExpression : StatelessExpression { public IntegerSmallerOrEqualToExpression() { } [StorableConstructor] protected IntegerSmallerOrEqualToExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.IntegerStack.Top; var second = interpreter.IntegerStack[1]; interpreter.IntegerStack.Remove(2); var result = second <= 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.Integer, "INTEGER.>", "Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise.", StackTypes.Boolean)] [StorableClass] public class IntegerGreaterThanExpression : StatelessExpression { public IntegerGreaterThanExpression() { } [StorableConstructor] protected IntegerGreaterThanExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.IntegerStack.Top; var second = interpreter.IntegerStack[1]; interpreter.IntegerStack.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.Integer, "INTEGER.>=", "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 IntegerGreaterOrEqualToExpression : StatelessExpression { public IntegerGreaterOrEqualToExpression() { } [StorableConstructor] protected IntegerGreaterOrEqualToExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.Count < 2; } public override void Eval(IInternalPushInterpreter interpreter) { var first = interpreter.IntegerStack.Top; var second = interpreter.IntegerStack[1]; interpreter.IntegerStack.Remove(2); var result = second >= first; interpreter.BooleanStack.Push(result); } } /// /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE. /// [PushExpression( StackTypes.Integer, "INTEGER.FROMBOOLEAN", "Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE.", StackTypes.Boolean)] [StorableClass] public class IntegerFromBooleanExpression : StatelessExpression { public IntegerFromBooleanExpression() { } [StorableConstructor] protected IntegerFromBooleanExpression(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.IntegerStack.Push(value); } } /// /// Pushes the result of truncating the top FLOAT. /// [PushExpression( StackTypes.Integer, "INTEGER.FROMFLOAT", "Pushes the result of truncating the top FLOAT.", StackTypes.Float)] [StorableClass] public class IntegerFromFloatExpression : StatelessExpression { public IntegerFromFloatExpression() { } [StorableConstructor] protected IntegerFromFloatExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.FloatStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var value = unchecked((long)interpreter.FloatStack.Pop()); interpreter.IntegerStack.Push(value); } } /// /// Pushes the result of truncating the top CHAR. /// [PushExpression( StackTypes.Integer, "INTEGER.FROMCHAR", "Pushes the result of truncating the top CHAR.", StackTypes.Char)] [StorableClass] public class IntegerFromCharExpression : StatelessExpression { public IntegerFromCharExpression() { } [StorableConstructor] protected IntegerFromCharExpression(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.IntegerStack.Push(value); } } /// /// Pushes the result of parsing the top STRING. /// [PushExpression( StackTypes.Integer, "INTEGER.FROMSTRING", "Pushes the result of parsing the top STRING.", StackTypes.String)] [StorableClass] public class IntegerFromStringExpression : StatelessExpression { public IntegerFromStringExpression() { } [StorableConstructor] protected IntegerFromStringExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { long tmp; return interpreter.StringStack.IsEmpty || interpreter.StringStack.Top.Length == 0 || !long.TryParse(interpreter.StringStack.Top, out tmp); } public override void Eval(IInternalPushInterpreter interpreter) { var str = interpreter.StringStack.Pop(); var value = long.Parse(str); interpreter.IntegerStack.Push(value); } } /// /// Pushes the result of increasing the top INTEGER by 1. /// [PushExpression( StackTypes.Integer, "INTEGER.INC", "Pushes the result of increasing the top INTEGER by 1.")] [StorableClass] public class IntegerIncExpression : StatelessExpression { public IntegerIncExpression() { } [StorableConstructor] protected IntegerIncExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var result = unchecked(interpreter.IntegerStack.Top + 1); interpreter.IntegerStack.Top = result; } } /// /// Pushes the result of decreasing the top INTEGER by 1. /// [PushExpression( StackTypes.Integer, "INTEGER.DEC", "Pushes the result of decreasing the top INTEGER by 1.")] [StorableClass] public class IntegerDecExpression : StatelessExpression { public IntegerDecExpression() { } [StorableConstructor] protected IntegerDecExpression(bool deserializing) : base(deserializing) { } public override bool IsNoop(IInternalPushInterpreter interpreter) { return interpreter.IntegerStack.IsEmpty; } public override void Eval(IInternalPushInterpreter interpreter) { var result = unchecked(interpreter.IntegerStack.Top - 1); interpreter.IntegerStack.Top = result; } } }