1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
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 | /// Removes an indexed item from "deep" in the stack and pushes it on top of the stack.
|
---|
11 | /// The index is taken from the INTEGER stack, and the indexing is done after the index is removed.
|
---|
12 | /// </summary>
|
---|
13 | /// <typeparam name="T">Stacktype</typeparam>
|
---|
14 | public abstract class YankExpression<T> : StatelessExpression {
|
---|
15 | public bool Eval(IPushStack<T> stack, IPushStack<long> integerStack) {
|
---|
16 | if ((stack == integerStack && integerStack.Count < 2) ||
|
---|
17 | (stack != integerStack && (integerStack.IsEmpty || stack.IsEmpty)))
|
---|
18 | return false;
|
---|
19 |
|
---|
20 | var index = (int)Math.Abs(integerStack.Pop() % stack.Count);
|
---|
21 | stack.Yank(index);
|
---|
22 | return true;
|
---|
23 | }
|
---|
24 | }
|
---|
25 |
|
---|
26 | [PushExpression(StackTypes.Integer, "INTEGER.YANK")]
|
---|
27 | public class IntegerYankExpression : YankExpression<long> {
|
---|
28 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
29 | return this.Eval(interpreter.IntegerStack, interpreter.IntegerStack);
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | [PushExpression(StackTypes.Float, "FLOAT.YANK", StackTypes.Integer)]
|
---|
34 | public class FloatYankExpression : YankExpression<double> {
|
---|
35 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
36 | return this.Eval(interpreter.FloatStack, interpreter.IntegerStack);
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | [PushExpression(StackTypes.Boolean, "BOOLEAN.YANK", StackTypes.Integer)]
|
---|
41 | public class BooleanYankExpression : YankExpression<bool> {
|
---|
42 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
43 | return this.Eval(interpreter.BooleanStack, interpreter.IntegerStack);
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | [PushExpression(StackTypes.Name, "NAME.YANK", StackTypes.Integer)]
|
---|
48 | public class NameYankExpression : YankExpression<string> {
|
---|
49 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
50 | return this.Eval(interpreter.NameStack, interpreter.IntegerStack);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | [PushExpression(StackTypes.Exec, "EXEC.YANK", StackTypes.Integer)]
|
---|
55 | public class ExecYankExpression : YankExpression<Expression> {
|
---|
56 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
57 | return this.Eval(interpreter.ExecStack, interpreter.IntegerStack);
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | [PushExpression(StackTypes.Code, "CODE.YANK", StackTypes.Integer)]
|
---|
62 | public class CodeYankExpression : YankExpression<Expression> {
|
---|
63 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
64 | return this.Eval(interpreter.CodeStack, interpreter.IntegerStack);
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | [PushExpression(StackTypes.Char, "CHAR.YANK", StackTypes.Integer)]
|
---|
69 | public class CharYankExpression : YankExpression<char> {
|
---|
70 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
71 | return Eval(interpreter.CharStack, interpreter.IntegerStack);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | [PushExpression(StackTypes.String, "STRING.YANK", StackTypes.Integer)]
|
---|
76 | public class StringYankExpression : YankExpression<string> {
|
---|
77 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
78 | return Eval(interpreter.StringStack, interpreter.IntegerStack);
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | [PushExpression(StackTypes.IntegerVector, "INTEGER[].YANK", StackTypes.Integer)]
|
---|
83 | public class IntegerVectorYankExpression : YankExpression<List<long>> {
|
---|
84 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
85 | return Eval(interpreter.IntegerVectorStack, interpreter.IntegerStack);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | [PushExpression(StackTypes.FloatVector, "FLOAT[].YANK", StackTypes.Integer)]
|
---|
90 | public class FloatVectorYankExpression : YankExpression<List<double>> {
|
---|
91 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
92 | return Eval(interpreter.FloatVectorStack, interpreter.IntegerStack);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | [PushExpression(StackTypes.BooleanVector, "BOOLEAN[].YANK", StackTypes.Integer)]
|
---|
97 | public class BooleanVectorYankExpression : YankExpression<List<bool>> {
|
---|
98 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
99 | return Eval(interpreter.BooleanVectorStack, interpreter.IntegerStack);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | [PushExpression(StackTypes.StringVector, "STRING[].YANK", StackTypes.Integer)]
|
---|
104 | public class StringVectorYankExpression : YankExpression<List<string>> {
|
---|
105 | public override bool Eval(IInternalPushInterpreter interpreter) {
|
---|
106 | return Eval(interpreter.StringVectorStack, interpreter.IntegerStack);
|
---|
107 | }
|
---|
108 | }
|
---|
109 | } |
---|