1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
2 | using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
|
---|
3 |
|
---|
4 | using Interpreter;
|
---|
5 | using Stack;
|
---|
6 |
|
---|
7 | public abstract class ShoveExpression<T> : StatelessExpression {
|
---|
8 | protected bool Eval(IStack<T> stack, IStack<long> integerStack, int count = 1) {
|
---|
9 | if (integerStack.Count == 0 ||
|
---|
10 | integerStack.Top > stack.Count - count ||
|
---|
11 | integerStack.Top < 0 ||
|
---|
12 | stack.Count < 2)
|
---|
13 | return false;
|
---|
14 |
|
---|
15 | var index = (int)integerStack.Pop();
|
---|
16 | var item = stack.Pop();
|
---|
17 |
|
---|
18 | stack.Insert(index, item);
|
---|
19 | return true;
|
---|
20 | }
|
---|
21 | }
|
---|
22 |
|
---|
23 | [PushExpression(StackType.Integer, "INTEGER.SHOVE")]
|
---|
24 | public class IntegerShoveExpression : ShoveExpression<long> {
|
---|
25 | public override bool Eval(IPushInterpreter interpreter) {
|
---|
26 | return Eval(interpreter.IntegerStack, interpreter.IntegerStack, 2);
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | [PushExpression(StackType.Float, "FLOAT.SHOVE")]
|
---|
31 | public class FloatShoveExpression : ShoveExpression<double> {
|
---|
32 | public override bool Eval(IPushInterpreter interpreter) {
|
---|
33 | return Eval(interpreter.FloatStack, interpreter.IntegerStack);
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | [PushExpression(StackType.Boolean, "BOOLEAN.SHOVE")]
|
---|
38 | public class BooleanShoveExpression : ShoveExpression<bool> {
|
---|
39 | public override bool Eval(IPushInterpreter interpreter) {
|
---|
40 | return Eval(interpreter.BooleanStack, interpreter.IntegerStack);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | [PushExpression(StackType.Name, "NAME.SHOVE")]
|
---|
45 | public class NameShoveExpression : ShoveExpression<string> {
|
---|
46 | public override bool Eval(IPushInterpreter interpreter) {
|
---|
47 | return Eval(interpreter.NameStack, interpreter.IntegerStack);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | [PushExpression(StackType.Exec, "EXEC.SHOVE")]
|
---|
52 | public class ExecShoveExpression : ShoveExpression<Expression> {
|
---|
53 | public override bool Eval(IPushInterpreter interpreter) {
|
---|
54 | return Eval(interpreter.ExecStack, interpreter.IntegerStack);
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | [PushExpression(StackType.Code, "CODE.SHOVE")]
|
---|
59 | public class CodeShoveExpression : ShoveExpression<Expression> {
|
---|
60 | public override bool Eval(IPushInterpreter interpreter) {
|
---|
61 | return Eval(interpreter.CodeStack, interpreter.IntegerStack);
|
---|
62 | }
|
---|
63 | }
|
---|
64 | } |
---|