namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions { using System; using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes; using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; using Interpreter; /// /// Pushes the sum of the top two items. /// [PushExpression(StackType.Integer, "INTEGER.+")] public class IntegerAddExpression : PushResultExpression { public override void Eval(IPushGpInterpreter interpreter) { this.Eval(interpreter.IntegerStack, 2, values => values[0] + values[1]); } } /// /// Pushes the difference of the top two items; that is, the second item minus the top item. /// [PushExpression(StackType.Integer, "INTEGER.-")] public class IntegerSubtractExpression : PushResultExpression { public override void Eval(IPushGpInterpreter interpreter) { this.Eval(interpreter.IntegerStack, 2, values => values[0] - values[1]); } } /// /// Pushes the product of the top two items. /// [PushExpression(StackType.Integer, "INTEGER.*")] public class IntegerMultiplyExpression : PushResultExpression { public override void Eval(IPushGpInterpreter interpreter) { this.Eval(interpreter.IntegerStack, 2, values => values[0] * values[1]); } } /// /// 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(StackType.Integer, "INTEGER./")] public class IntegerDivideExpression : PushResultExpression { public override void Eval(IPushGpInterpreter interpreter) { this.Eval(interpreter.IntegerStack, 2, values => values[0] / values[1], 0); } } /// /// Pushes the second stack item modulo the top stack item. If the top item is zero this acts as a NOOP. The modulus is /// computed as the /// remainder of the quotient, where the quotient has first been truncated toward negative infinity. (This is taken /// from the definition /// for the generic MOD function in Common Lisp, which is described for example at /// http://www.lispworks.com/reference/HyperSpec/Body/f_mod_r.htm.) /// [PushExpression(StackType.Integer, "INTEGER.%")] public class IntegerModuloExpression : PushResultExpression { public override void Eval(IPushGpInterpreter interpreter) { this.Eval(interpreter.IntegerStack, 2, values => values[0] % values[1], 0); } } /// /// Pushes the minimum of the top two items. /// [PushExpression(StackType.Integer, "INTEGER.MIN")] public class IntegerMinExpression : PushResultExpression { public override void Eval(IPushGpInterpreter interpreter) { this.Eval(interpreter.IntegerStack, 2, values => Math.Min(values[0], values[1])); } } /// /// Pushes the maximum of the top two items. /// [PushExpression(StackType.Integer, "INTEGER.MAX")] public class IntegerMaxExpression : PushResultExpression { public override void Eval(IPushGpInterpreter interpreter) { this.Eval(interpreter.IntegerStack, 2, values => Math.Max(values[0], values[1])); } } /// /// Pushes TRUE onto the BOOLEAN stack if the second item is less than the top item, or FALSE otherwise. /// [PushExpression(StackType.Integer, "INTEGER.<")] public class IntegerSmallerThanExpression : PushResultExpression { public override void Eval(IPushGpInterpreter interpreter) { this.Eval(interpreter.IntegerStack, interpreter.BooleanStack, 2, values => values[0] < values[1]); } } /// /// Pushes TRUE onto the BOOLEAN stack if the second item is greater than the top item, or FALSE otherwise. /// [PushExpression(StackType.Integer, "INTEGER.>")] public class IntegerGreaterThanExpression : PushResultExpression { public override void Eval(IPushGpInterpreter interpreter) { this.Eval(interpreter.IntegerStack, interpreter.BooleanStack, 2, values => values[0] > values[1]); } } /// /// Pushes 1 if the top BOOLEAN is TRUE, or 0 if the top BOOLEAN is FALSE. /// [PushExpression(StackType.Integer, "INTEGER.FROMBOOLEAN")] public class IntegerFromBooleanExpression : StatelessExpression { public override void Eval(IPushGpInterpreter interpreter) { if (interpreter.BooleanStack.Count == 0) return; var condition = interpreter.BooleanStack.Pop(); var value = condition ? 1 : 0; interpreter.IntegerStack.Push(value); } } /// /// Pushes the result of truncating the top FLOAT. /// [PushExpression(StackType.Integer, "INTEGER.FROMFLOAT")] public class IntegerFromFloatExpression : StatelessExpression { public override void Eval(IPushGpInterpreter interpreter) { if (interpreter.FloatStack.Count == 0) return; var value = (int)interpreter.FloatStack.Pop(); interpreter.IntegerStack.Push(value); } } }