using System;
using HeuristicLab.Common;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.ProgramSynthesis {
///
/// Pushes the logical AND of the top two BOOLEANs.
///
[PushExpression(StackTypes.Boolean, "BOOLEAN.AND", "Pushes the logical AND of the top two BOOLEANs.")]
[StorableClass]
public class BooleanAndExpression : StatelessExpression {
public BooleanAndExpression() { }
[StorableConstructor]
public BooleanAndExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.BooleanStack.Count < 2;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var first = interpreter.BooleanStack.Pop();
var second = interpreter.BooleanStack.Top;
var result = second && first;
interpreter.BooleanStack.Top = result;
}
}
///
/// Pushes the logical OR of the top two BOOLEANs.
///
[PushExpression(StackTypes.Boolean, "BOOLEAN.OR", "Pushes the logical OR of the top two BOOLEANs.")]
[StorableClass]
public class BooleanOrExpression : StatelessExpression {
public BooleanOrExpression() { }
[StorableConstructor]
public BooleanOrExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.BooleanStack.Count < 2;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var first = interpreter.BooleanStack.Pop();
var second = interpreter.BooleanStack.Top;
var result = second || first;
interpreter.BooleanStack.Top = result;
}
}
///
/// Pushes the logical NOT of the top BOOLEAN.
///
[PushExpression(StackTypes.Boolean, "BOOLEAN.NOT", "Pushes the logical NOT of the top BOOLEAN.")]
[StorableClass]
public class BooleanNotExpression : StatelessExpression {
public BooleanNotExpression() { }
[StorableConstructor]
public BooleanNotExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.BooleanStack.IsEmpty;
}
public override void Eval(IInternalPushInterpreter interpreter) {
interpreter.BooleanStack.Top = !interpreter.BooleanStack.Top;
}
}
///
/// Pushes FALSE if the top FLOAT is 0.0, or TRUE otherwise.
///
[PushExpression(StackTypes.Boolean, "BOOLEAN.FROMFLOAT", "Pushes FALSE if the top FLOAT is 0.0, or TRUE otherwise.", StackTypes.Float)]
[StorableClass]
public class BooleanFromFloatExpression : StatelessExpression {
public BooleanFromFloatExpression() { }
[StorableConstructor]
public BooleanFromFloatExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.FloatStack.Count == 0;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var value = !Math.Abs(interpreter.FloatStack.Pop()).IsAlmost(0);
interpreter.BooleanStack.Push(value);
}
}
///
/// Pushes FALSE if the top INTEGER is 0, or TRUE otherwise.
///
[PushExpression(StackTypes.Boolean, "BOOLEAN.FROMINTEGER", "Pushes FALSE if the top INTEGER is 0, or TRUE otherwise.", StackTypes.Integer)]
[StorableClass]
public class BooleanFromIntegerExpression : StatelessExpression {
public BooleanFromIntegerExpression() { }
[StorableConstructor]
public BooleanFromIntegerExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.IntegerStack.Count == 0;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var value = interpreter.IntegerStack.Pop() != 0;
interpreter.BooleanStack.Push(value);
}
}
///
/// Pushes the logical AND of the top two BOOLEANs, whereby the first value is inverted first.
///
[PushExpression(StackTypes.Boolean, "BOOLEAN.INVERT_FIRST_THEN_AND", "Pushes the logical AND of the top two BOOLEANs, whereby the first value is inverted first.")]
[StorableClass]
public class BooleanInvertFirstThenAnd : StatelessExpression {
public BooleanInvertFirstThenAnd() { }
[StorableConstructor]
public BooleanInvertFirstThenAnd(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.BooleanStack.Count < 2;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var first = interpreter.BooleanStack.Pop();
var second = interpreter.BooleanStack.Top;
var result = second && !first;
interpreter.BooleanStack.Top = result;
}
}
///
/// Pushes the logical AND of the top two BOOLEANs, whereby the second value is inverted first.
///
[PushExpression(StackTypes.Boolean, "BOOLEAN.INVERT_SECOND_THEN_AND", "Pushes the logical AND of the top two BOOLEANs, whereby the second value is inverted first.")]
[StorableClass]
public class BooleanInvertSecondThenAnd : StatelessExpression {
public BooleanInvertSecondThenAnd() { }
[StorableConstructor]
public BooleanInvertSecondThenAnd(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.BooleanStack.Count < 2;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var first = interpreter.BooleanStack.Pop();
var second = interpreter.BooleanStack.Top;
var result = !second && first;
interpreter.BooleanStack.Top = result;
}
}
///
/// Pushes the xor of the top tow BOOLEANs
///
[PushExpression(StackTypes.Boolean, "BOOLEAN.XOR", "Pushes the xor of the top tow BOOLEANs.")]
[StorableClass]
public class BooleanXorExpression : StatelessExpression {
public BooleanXorExpression() { }
[StorableConstructor]
public BooleanXorExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.BooleanStack.Count < 2;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var first = interpreter.BooleanStack.Pop();
var second = interpreter.BooleanStack.Top;
var result = second ^ first;
interpreter.BooleanStack.Top = result;
}
}
}