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