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 | /// Replaces, in the top type vector, item at index(from integer stack) with the first lit-type item.
|
---|
10 | /// </summary>
|
---|
11 | /// <typeparam name="T"></typeparam>
|
---|
12 | public abstract class VectorSetExpression<T> : StatelessExpression {
|
---|
13 | protected bool Eval(
|
---|
14 | IInternalPushInterpreter interpreter,
|
---|
15 | IPushStack<List<T>> vectorStack,
|
---|
16 | IPushStack<T> literalStack,
|
---|
17 | bool isLiteralTypeInteger = false) {
|
---|
18 | if (vectorStack.IsEmpty ||
|
---|
19 | literalStack.IsEmpty ||
|
---|
20 | (isLiteralTypeInteger && interpreter.IntegerStack.Count < 2) ||
|
---|
21 | (!isLiteralTypeInteger && interpreter.IntegerStack.IsEmpty))
|
---|
22 | return false;
|
---|
23 |
|
---|
24 | T literal;
|
---|
25 | int index;
|
---|
26 |
|
---|
27 | if (isLiteralTypeInteger) {
|
---|
28 | literal = literalStack.Top;
|
---|
29 | index = (int)interpreter.IntegerStack[1];
|
---|
30 | interpreter.IntegerStack.Remove(2);
|
---|
31 | } else {
|
---|
32 | literal = literalStack.Pop();
|
---|
33 | index = (int)interpreter.IntegerStack.Pop();
|
---|
34 | }
|
---|
35 |
|
---|
36 | var vector = vectorStack.Pop();
|
---|
37 | index = index % vector.Count;
|
---|
38 |
|
---|
39 | if (index < 0)
|
---|
40 | index = vector.Count - index;
|
---|
41 |
|
---|
42 | vector[index] = literal;
|
---|
43 | return true;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | [PushExpression(StackTypes.IntegerVector, "INTEGER[].SET", StackTypes.Integer)]
|
---|
48 | public class IntegerVectorSetExpression : VectorSetExpression<long> {
|
---|
49 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
50 | return Eval(interpreter, interpreter.IntegerVectorStack, interpreter.IntegerStack, true);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | [PushExpression(StackTypes.FloatVector, "FLOAT[].SET", StackTypes.Float | StackTypes.Integer)]
|
---|
55 | public class FloatVectorSetExpression : VectorSetExpression<double> {
|
---|
56 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
57 | return Eval(interpreter, interpreter.FloatVectorStack, interpreter.FloatStack);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].SET", StackTypes.Boolean | StackTypes.Integer)]
|
---|
62 | public class BooleanVectorSetExpression : VectorSetExpression<bool> {
|
---|
63 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
64 | return Eval(interpreter, interpreter.BooleanVectorStack, interpreter.BooleanStack);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | [PushExpression(StackTypes.StringVector, "STRING[].SET", StackTypes.String | StackTypes.Integer)]
|
---|
69 | public class StringVectorSetExpression : VectorSetExpression<string> {
|
---|
70 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
71 | return Eval(interpreter, interpreter.StringVectorStack, interpreter.StringStack);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|