1 | namespace HeuristicLab.Algorithms.PushGP.Expressions
|
---|
2 | {
|
---|
3 | using System;
|
---|
4 | using System.Collections.Generic;
|
---|
5 |
|
---|
6 | using HeuristicLab.Algorithms.PushGP.Interpreter;
|
---|
7 | using HeuristicLab.Algorithms.PushGP.Stack;
|
---|
8 |
|
---|
9 | /// <summary>
|
---|
10 | /// Defines the name on top of the NAME stack as an instruction that will push the top item of the T stack onto the
|
---|
11 | /// EXEC stack.
|
---|
12 | /// </summary>
|
---|
13 | /// <typeparam name="T">Stacktype</typeparam>
|
---|
14 | public abstract class DefineExpression<T> : StatelessExpression
|
---|
15 | {
|
---|
16 | protected void Eval(
|
---|
17 | IStack<T> stack,
|
---|
18 | IStack<string> nameStack,
|
---|
19 | IDictionary<string, Expression> customExpressions,
|
---|
20 | Func<T, Expression> creator)
|
---|
21 | {
|
---|
22 | // not enough arguments on stack
|
---|
23 | if ((nameStack.Count == 0) || (stack.Count < 1)) return;
|
---|
24 |
|
---|
25 | var name = nameStack.Pop();
|
---|
26 | var expression = creator(stack.Top);
|
---|
27 |
|
---|
28 | if (customExpressions.ContainsKey(name)) customExpressions[name] = expression;
|
---|
29 | else customExpressions.Add(name, expression);
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | public class CodeDefineExpression : DefineExpression<Expression>
|
---|
34 | {
|
---|
35 | protected override string InitStringRepresentation()
|
---|
36 | {
|
---|
37 | return "CODE.DEFINE";
|
---|
38 | }
|
---|
39 |
|
---|
40 | public override void Eval(IPushGpInterpreter interpreter)
|
---|
41 | {
|
---|
42 | if (interpreter.Configuration.TopLevelPushCode && (interpreter.CodeStack.Count < 2)) return;
|
---|
43 |
|
---|
44 | this.Eval(
|
---|
45 | interpreter.CodeStack,
|
---|
46 | interpreter.NameStack,
|
---|
47 | interpreter.CustomExpressions,
|
---|
48 | v => new ExecPushExpression(v));
|
---|
49 |
|
---|
50 | interpreter.CodeStack.Pop();
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public class ExecDefineExpression : DefineExpression<Expression>
|
---|
55 | {
|
---|
56 | protected override string InitStringRepresentation()
|
---|
57 | {
|
---|
58 | return "EXEC.DEFINE";
|
---|
59 | }
|
---|
60 |
|
---|
61 | public override void Eval(IPushGpInterpreter interpreter)
|
---|
62 | {
|
---|
63 | if (interpreter.ExecStack.Count < 2) return;
|
---|
64 |
|
---|
65 | this.Eval(
|
---|
66 | interpreter.ExecStack,
|
---|
67 | interpreter.NameStack,
|
---|
68 | interpreter.CustomExpressions,
|
---|
69 | v => new ExecPushExpression(v));
|
---|
70 |
|
---|
71 | interpreter.ExecStack.Pop();
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | public class FloatDefineExpression : DefineExpression<double>
|
---|
76 | {
|
---|
77 | protected override string InitStringRepresentation()
|
---|
78 | {
|
---|
79 | return "FLOAT.DEFINE";
|
---|
80 | }
|
---|
81 |
|
---|
82 | public override void Eval(IPushGpInterpreter interpreter)
|
---|
83 | {
|
---|
84 | this.Eval(
|
---|
85 | interpreter.FloatStack,
|
---|
86 | interpreter.NameStack,
|
---|
87 | interpreter.CustomExpressions,
|
---|
88 | v => new FloatPushExpression(v));
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | public class IntegerDefineExpression : DefineExpression<long>
|
---|
93 | {
|
---|
94 | protected override string InitStringRepresentation()
|
---|
95 | {
|
---|
96 | return "INTEGER.DEFINE";
|
---|
97 | }
|
---|
98 |
|
---|
99 | public override void Eval(IPushGpInterpreter interpreter)
|
---|
100 | {
|
---|
101 | this.Eval(
|
---|
102 | interpreter.IntegerStack,
|
---|
103 | interpreter.NameStack,
|
---|
104 | interpreter.CustomExpressions,
|
---|
105 | v => new IntegerPushExpression(v));
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | public class BooleanDefineExpression : DefineExpression<bool>
|
---|
110 | {
|
---|
111 | protected override string InitStringRepresentation()
|
---|
112 | {
|
---|
113 | return "BOOLEAN.DEFINE";
|
---|
114 | }
|
---|
115 |
|
---|
116 | public override void Eval(IPushGpInterpreter interpreter)
|
---|
117 | {
|
---|
118 | this.Eval(
|
---|
119 | interpreter.BooleanStack,
|
---|
120 | interpreter.NameStack,
|
---|
121 | interpreter.CustomExpressions,
|
---|
122 | v => new BooleanPushExpression(v));
|
---|
123 | }
|
---|
124 | }
|
---|
125 | } |
---|