1 | using System.Collections.Generic;
|
---|
2 |
|
---|
3 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
4 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
5 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
6 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
7 |
|
---|
8 | /// <summary>
|
---|
9 | /// Gets the first item from the type stack
|
---|
10 | /// </summary>
|
---|
11 | /// <typeparam name="T"></typeparam>
|
---|
12 | public abstract class VectorFirstExpression<T> : StatelessExpression {
|
---|
13 | protected bool Eval(IPushStack<List<T>> vectorStack, IPushStack<T> literalStack) {
|
---|
14 | if (vectorStack.IsEmpty || vectorStack.Top.Count == 0) return false;
|
---|
15 |
|
---|
16 | var vector = vectorStack.Pop();
|
---|
17 | literalStack.Push(vector[0]);
|
---|
18 | return true;
|
---|
19 | }
|
---|
20 | }
|
---|
21 |
|
---|
22 | [PushExpression(StackTypes.IntegerVector, "INTEGER[].FIRST", StackTypes.Integer)]
|
---|
23 | public class IntegerVectorFirstExpression : VectorFirstExpression<long> {
|
---|
24 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
25 | return Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | [PushExpression(StackTypes.FloatVector, "FLOAT[].FIRST", StackTypes.Float)]
|
---|
30 | public class FloatVectorFirstExpression : VectorFirstExpression<double> {
|
---|
31 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
32 | return Eval(interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].FIRST", StackTypes.Boolean)]
|
---|
37 | public class BooleanVectorFirstExpression : VectorFirstExpression<bool> {
|
---|
38 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
39 | return Eval(interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | [PushExpression(StackTypes.StringVector, "STRING[].FIRST", StackTypes.String)]
|
---|
44 | public class StringVectorFirstExpression : VectorFirstExpression<string> {
|
---|
45 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
46 | return Eval(interpreter.StringVectorStack, interpreter.StringStack);
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|