1 | using HeuristicLab.Algorithms.PushGP.Interpreter;
|
---|
2 | using HeuristicLab.Algorithms.PushGP.Stack;
|
---|
3 |
|
---|
4 | namespace HeuristicLab.Algorithms.PushGP.Expressions
|
---|
5 | {
|
---|
6 | public abstract class ShoveExpression<T> : StatelessExpression
|
---|
7 | {
|
---|
8 | public void Eval(IStack<T> stack, IStack<long> integerStack, int count = 1)
|
---|
9 | {
|
---|
10 | if (integerStack.Count == 0)
|
---|
11 | return;
|
---|
12 |
|
---|
13 | if (integerStack.Top > stack.Count - count ||
|
---|
14 | integerStack.Top < 0 ||
|
---|
15 | stack.Count < 2)
|
---|
16 | return;
|
---|
17 |
|
---|
18 | var index = integerStack.Pop();
|
---|
19 | var item = stack.Pop();
|
---|
20 |
|
---|
21 | // TODO: What if index > int.MaxValue ?
|
---|
22 | stack.Insert((int)index, item);
|
---|
23 | }
|
---|
24 | }
|
---|
25 |
|
---|
26 | public class IntegerShoveExpression : ShoveExpression<long>
|
---|
27 | {
|
---|
28 | protected override string InitStringRepresentation() { return "INTEGER.SHOVE"; }
|
---|
29 |
|
---|
30 | public override void Eval(IInterpreter interpreter)
|
---|
31 | {
|
---|
32 | Eval(interpreter.IntegerStack, interpreter.IntegerStack, 2);
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | public class FloatShoveExpression : ShoveExpression<double>
|
---|
37 | {
|
---|
38 | protected override string InitStringRepresentation() { return "FLOAT.SHOVE"; }
|
---|
39 |
|
---|
40 | public override void Eval(IInterpreter interpreter)
|
---|
41 | {
|
---|
42 | Eval(interpreter.FloatStack, interpreter.IntegerStack);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public class BooleanShoveExpression : ShoveExpression<bool>
|
---|
47 | {
|
---|
48 | protected override string InitStringRepresentation() { return "BOOLEAN.SHOVE"; }
|
---|
49 |
|
---|
50 | public override void Eval(IInterpreter interpreter)
|
---|
51 | {
|
---|
52 | Eval(interpreter.BooleanStack, interpreter.IntegerStack);
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | public class NameShoveExpression : ShoveExpression<string>
|
---|
57 | {
|
---|
58 | protected override string InitStringRepresentation() { return "NAME.SHOVE"; }
|
---|
59 |
|
---|
60 | public override void Eval(IInterpreter interpreter)
|
---|
61 | {
|
---|
62 | Eval(interpreter.NameStack, interpreter.IntegerStack);
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public class ExecShoveExpression : ShoveExpression<Expression>
|
---|
67 | {
|
---|
68 | protected override string InitStringRepresentation() { return "EXEC.SHOVE"; }
|
---|
69 |
|
---|
70 | public override void Eval(IInterpreter interpreter)
|
---|
71 | {
|
---|
72 | Eval(interpreter.ExecStack, interpreter.IntegerStack);
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | public class CodeShoveExpression : ShoveExpression<Expression>
|
---|
77 | {
|
---|
78 | protected override string InitStringRepresentation() { return "CODE.SHOVE"; }
|
---|
79 |
|
---|
80 | public override void Eval(IInterpreter interpreter)
|
---|
81 | {
|
---|
82 | Eval(interpreter.CodeStack, interpreter.IntegerStack);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|