namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
using System;
using System.Linq;
using Attributes;
using Interpreter;
using Persistence.Default.CompositeSerializers.Storable;
using Stack;
///
/// Pushes TRUE onto the BOOLEAN stack if the top char represents a whitespace char, otherwise FALSE.
///
[PushExpression(
StackTypes.Char,
"CHAR.ISWHITESPACE",
"Pushes TRUE onto the BOOLEAN stack if the top char represents a whitespace char, otherwise FALSE.",
StackTypes.Boolean)]
[StorableClass]
public class CharIsWhitespaceExpression : StatelessExpression {
public CharIsWhitespaceExpression() { }
[StorableConstructor]
public CharIsWhitespaceExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.CharStack.IsEmpty;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var c = interpreter.CharStack.Pop();
interpreter.BooleanStack.Push(char.IsWhiteSpace(c));
}
}
///
/// Pushes TRUE onto the CHAR stack if the top char represents a letter, otherwise FALSE.
///
[PushExpression(
StackTypes.Char,
"CHAR.ISLETTER",
"Pushes TRUE onto the CHAR stack if the top char represents a letter, otherwise FALSE.",
StackTypes.Boolean)]
[StorableClass]
public class CharIsLetterExpression : StatelessExpression {
public CharIsLetterExpression() { }
[StorableConstructor]
public CharIsLetterExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.CharStack.IsEmpty;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var c = interpreter.CharStack.Pop();
interpreter.BooleanStack.Push(char.IsLetter(c));
}
}
///
/// Pushes TRUE onto the CHAR stack if the top char represents a digit, otherwise FALSE.
///
[PushExpression(
StackTypes.Char,
"CHAR.ISDIGIT",
"Pushes TRUE onto the CHAR stack if the top char represents a digit, otherwise FALSE.",
StackTypes.Boolean)]
[StorableClass]
public class CharIsDigitExpression : StatelessExpression {
public CharIsDigitExpression() { }
[StorableConstructor]
public CharIsDigitExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.CharStack.IsEmpty;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var c = interpreter.CharStack.Pop();
interpreter.BooleanStack.Push(char.IsDigit(c));
}
}
///
/// Takes the top integer, converts it to an char using ASCII encoding and pushes this char onto the CHAR stack.
///
[PushExpression(
StackTypes.Char,
"CHAR.FROMINTEGER",
"Takes the top integer, converts it to an char using ASCII encoding and pushes this char onto the CHAR stack.",
StackTypes.Integer)]
[StorableClass]
public class CharFromIntegerExpression : StatelessExpression {
public CharFromIntegerExpression() { }
[StorableConstructor]
public CharFromIntegerExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.IntegerStack.IsEmpty;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var value = (int)Math.Abs(interpreter.IntegerStack.Pop() % 128);
var c = Convert.ToChar(value);
interpreter.CharStack.Push(c);
}
}
///
/// Takes the top float, casts it to an integer, converts it to an char using ASCII encoding and pushes this char onto the CHAR stack.
///
[PushExpression(
StackTypes.Char,
"CHAR.FROMFLOAT",
"Takes the top float, casts it to an integer, converts it to an char using ASCII encoding and pushes this char onto the CHAR stack.",
StackTypes.Float)]
[StorableClass]
public class CharFromFloatExpression : StatelessExpression {
public CharFromFloatExpression() { }
[StorableConstructor]
public CharFromFloatExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.FloatStack.IsEmpty;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var value = (int)Math.Abs(interpreter.FloatStack.Pop() % 128);
var c = Convert.ToChar(value);
interpreter.CharStack.Push(c);
}
}
///
/// Takes the top string and pushes the chars of this string onto the CHAR stack in reversed order.
///
[PushExpression(
StackTypes.Char,
"CHAR.ALLFROMSTRING",
"Takes the top string and pushes the chars of this string onto the CHAR stack in reversed order.",
StackTypes.String)]
[StorableClass]
public class CharAllFromStringExpression : StatelessExpression {
public CharAllFromStringExpression() { }
[StorableConstructor]
public CharAllFromStringExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return interpreter.StringStack.IsEmpty;
}
public override void Eval(IInternalPushInterpreter interpreter) {
var chars = interpreter.StringStack.Pop().Reverse().ToList();
interpreter.CharStack.Push(chars);
}
}
}