Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.Algorithms.PushGP/HeuristicLab.Algorithms.PushGP/Expressions/ShoveExpressions.cs @ 14398

Last change on this file since 14398 was 14398, checked in by pkimmesw, 8 years ago

#2665 Expressions are splitted into StatefullExpressions and StatelessExpressions, Added traits for tests

File size: 2.6 KB
Line 
1using HeuristicLab.Algorithms.PushGP.Interpreter;
2using HeuristicLab.Algorithms.PushGP.Stack;
3
4namespace 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}
Note: See TracBrowser for help on using the repository browser.