namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
using System.Collections.Generic;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
///
/// Takes the length of the top item on the type stack
///
///
[StorableClass]
public abstract class VectorLengthExpression : StatelessExpression {
protected VectorLengthExpression() { }
[StorableConstructor]
protected VectorLengthExpression(bool deserializing) : base(deserializing) { }
protected bool IsNoop(IPushStack> vectorStack) {
return vectorStack.IsEmpty;
}
protected void Eval(IInternalPushInterpreter interpreter, IPushStack> vectorStack) {
var length = vectorStack.Pop().Count;
interpreter.IntegerStack.Push(length);
}
}
[StorableClass]
[PushExpression(
StackTypes.IntegerVector,
"INTEGER[].LENGTH",
"Pushes the length of the top INTEGER[] on the INTEGER stack",
StackTypes.Integer)]
public class IntegerVectorLengthExpression : VectorLengthExpression {
public IntegerVectorLengthExpression() { }
[StorableConstructor]
protected IntegerVectorLengthExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return IsNoop(interpreter.IntegerVectorStack);
}
public override void Eval(IInternalPushInterpreter interpreter) {
Eval(interpreter, interpreter.IntegerVectorStack);
}
}
[StorableClass]
[PushExpression(
StackTypes.FloatVector,
"FLOAT[].LENGTH",
"Pushes the length of the top FLOAT[] on the INTEGER stack",
StackTypes.Integer)]
public class FloatVectorLengthExpression : VectorLengthExpression {
public FloatVectorLengthExpression() { }
[StorableConstructor]
protected FloatVectorLengthExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return IsNoop(interpreter.FloatVectorStack);
}
public override void Eval(IInternalPushInterpreter interpreter) {
Eval(interpreter, interpreter.FloatVectorStack);
}
}
[StorableClass]
[PushExpression(
StackTypes.BooleanVector,
"BOOLEAN[].LENGTH",
"Pushes the length of the top BOOLEAN[] on the INTEGER stack",
StackTypes.Integer)]
public class BooleanVectorLengthExpression : VectorLengthExpression {
public BooleanVectorLengthExpression() { }
[StorableConstructor]
protected BooleanVectorLengthExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return IsNoop(interpreter.BooleanVectorStack);
}
public override void Eval(IInternalPushInterpreter interpreter) {
Eval(interpreter, interpreter.BooleanVectorStack);
}
}
[StorableClass]
[PushExpression(
StackTypes.StringVector,
"STRING[].LENGTH",
"Pushes the length of the top STRING[] on the INTEGER stack",
StackTypes.Integer)]
public class StringVectorLengthExpression : VectorLengthExpression {
public StringVectorLengthExpression() { }
[StorableConstructor]
protected StringVectorLengthExpression(bool deserializing) : base(deserializing) { }
public override bool IsNoop(IInternalPushInterpreter interpreter) {
return IsNoop(interpreter.StringVectorStack);
}
public override void Eval(IInternalPushInterpreter interpreter) {
Eval(interpreter, interpreter.StringVectorStack);
}
}
}