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