1 | using System.Collections.Generic;
|
---|
2 |
|
---|
3 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
4 | using System;
|
---|
5 |
|
---|
6 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
7 | using HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter;
|
---|
8 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
9 |
|
---|
10 | /// <summary>
|
---|
11 | /// Takes the subvec of the top item on the type stack.
|
---|
12 | /// </summary>
|
---|
13 | /// <typeparam name="T"></typeparam>
|
---|
14 | public abstract class VectorSubExpression<T> : StatelessExpression {
|
---|
15 | protected bool Eval(IInternalPushInterpreter interpreter, IPushStack<List<T>> vectorStack) {
|
---|
16 | if (vectorStack.IsEmpty || interpreter.IntegerStack.Count < 2) return false;
|
---|
17 |
|
---|
18 | var first = (int)Math.Min(vectorStack.Top.Count, Math.Max(0, interpreter.IntegerStack[1]));
|
---|
19 | var second = (int)Math.Min(vectorStack.Top.Count, Math.Max(first, interpreter.IntegerStack.Top));
|
---|
20 | interpreter.IntegerStack.Remove(2);
|
---|
21 |
|
---|
22 | if (vectorStack.Top.Count == 0)
|
---|
23 | return true;
|
---|
24 |
|
---|
25 | var result = vectorStack.Top.GetRange(first, second - first);
|
---|
26 | vectorStack.SetTop(result);
|
---|
27 | return true;
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | [PushExpression(StackTypes.IntegerVector, "INTEGER[].SUB", StackTypes.Integer)]
|
---|
32 | public class IntegerVectorSubExpression : VectorSubExpression<long> {
|
---|
33 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
34 | return Eval(interpreter, interpreter.IntegerVectorStack);
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | [PushExpression(StackTypes.FloatVector, "FLOAT[].SUB", StackTypes.Integer)]
|
---|
39 | public class FloatVectorSubExpression : VectorSubExpression<double> {
|
---|
40 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
41 | return Eval(interpreter, interpreter.FloatVectorStack);
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].SUB", StackTypes.Integer)]
|
---|
46 | public class BooleanVectorSubExpression : VectorSubExpression<bool> {
|
---|
47 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
48 | return Eval(interpreter, interpreter.BooleanVectorStack);
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | [PushExpression(StackTypes.StringVector, "STRING[].SUB", StackTypes.Integer)]
|
---|
53 | public class StringVectorSubExpression : VectorSubExpression<string> {
|
---|
54 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
55 | return Eval(interpreter, interpreter.StringVectorStack);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | } |
---|